Rework the public API (#4829)
Related to #4186. Some notable changes: - The term 'Error' now strictly refers to native Error objects defined in the ECMA standard, which are ordinary objects. All other uses of 'error' or 'error reference' where the term refers to a thrown value is now called 'exception'. - Simplified the naming scheme of many String API functions. These functions will now also take an 'encoding' argument to specify the desired encoding in which to operate. - Removed the substring-copy-to-buffer functions. These functions behaved awkwardly, as they use character index to specify the start/end positions, and were mostly used incorrectly with byte offsets instead. The functionality can still be replicated with other functions if necessary. - String-to-buffer functions will no longer fail if the buffer is not sufficiently large, the string will instead be cropped. - Fixed the usage of the '_sz' prefix in many API functions. The term 'sz' means zero-terminated string in hungarian notation, this was used incorrectly in many cases. - Renamed most of the public API functions to have shorter, more on-point names, rather than the often too long descriptive names. Functions are now also grouped by the type of value they operate on, where this makes sense. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
@@ -13,59 +13,58 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Infra */
|
||||
#include "infra/bsp.h"
|
||||
#include "infra/reboot.h"
|
||||
#include "infra/log.h"
|
||||
#include "infra/time.h"
|
||||
#include "infra/system_events.h"
|
||||
#include "infra/tcmd/handler.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "cfw/cfw.h"
|
||||
/* Watchdog helper */
|
||||
#include "infra/bsp.h"
|
||||
#include "infra/log.h"
|
||||
#include "infra/reboot.h"
|
||||
#include "infra/system_events.h"
|
||||
#include "infra/tcmd/handler.h"
|
||||
#include "infra/time.h"
|
||||
#include "infra/wdt_helper.h"
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "zephyr.h"
|
||||
#include "microkernel/task.h"
|
||||
#include "os/os.h"
|
||||
#include "misc/printk.h"
|
||||
#include "os/os.h"
|
||||
#include "string.h"
|
||||
#include "zephyr.h"
|
||||
|
||||
static T_QUEUE queue;
|
||||
|
||||
jerry_value_t print_function;
|
||||
|
||||
void jerry_resolve_error (jerry_value_t ret_value)
|
||||
void
|
||||
jerry_resolve_error (jerry_value_t ret_value)
|
||||
{
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
ret_value = jerry_get_value_from_error (ret_value, true);
|
||||
ret_value = jerry_exception_value (ret_value, true);
|
||||
jerry_value_t err_str_val = jerry_value_to_string (ret_value);
|
||||
jerry_size_t err_str_size = jerry_get_utf8_string_size (err_str_val);
|
||||
jerry_size_t err_str_size = jerry_string_size (err_str_val, JERRY_ENCODING_UTF8);
|
||||
jerry_char_t *err_str_buf = (jerry_char_t *) balloc (err_str_size, NULL);
|
||||
jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size);
|
||||
jerry_size_t sz = jerry_string_to_buffer (err_str_val, JERRY_ENCODING_UTF8, err_str_buf, err_str_size);
|
||||
err_str_buf[sz] = 0;
|
||||
printk ("Script Error: unhandled exception: %s\n", err_str_buf);
|
||||
bfree(err_str_buf);
|
||||
jerry_release_value (err_str_val);
|
||||
bfree (err_str_buf);
|
||||
jerry_value_free (err_str_val);
|
||||
}
|
||||
}
|
||||
|
||||
void help ()
|
||||
void
|
||||
help ()
|
||||
{
|
||||
printk ("Usage:\n");
|
||||
printk ("js e 'JavaScript Command'\n");
|
||||
printk ("eg. js e print ('Hello World');\n");
|
||||
}
|
||||
|
||||
void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
|
||||
void
|
||||
eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
@@ -77,7 +76,7 @@ void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
|
||||
{
|
||||
OS_ERR_TYPE err;
|
||||
size_t str_total_length = 0;
|
||||
size_t *str_lens = (size_t *) balloc ((argc - 2) * sizeof(size_t), &err);
|
||||
size_t *str_lens = (size_t *) balloc ((argc - 2) * sizeof (size_t), &err);
|
||||
if (str_lens == NULL || err != E_OS_OK)
|
||||
{
|
||||
printk ("%s: allocate memory failed!", __func__);
|
||||
@@ -101,7 +100,7 @@ void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
|
||||
char *p = buffer;
|
||||
for (int i = 2; i < argc; ++i)
|
||||
{
|
||||
for (int j =0; j < str_lens[i - 2]; ++j)
|
||||
for (int j = 0; j < str_lens[i - 2]; ++j)
|
||||
{
|
||||
*p = argv[i][j];
|
||||
++p;
|
||||
@@ -113,41 +112,44 @@ void eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
|
||||
|
||||
jerry_value_t eval_ret = jerry_eval (buffer, str_total_length - 1, JERRY_PARSE_NO_OPTS);
|
||||
|
||||
if (jerry_value_is_error (eval_ret))
|
||||
if (jerry_value_is_exception (eval_ret))
|
||||
{
|
||||
jerry_resolve_error (eval_ret);
|
||||
TCMD_RSP_ERROR (ctx, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_value_t args[] = {eval_ret};
|
||||
jerry_value_t ret_val_print = jerry_call_function (print_function,
|
||||
jerry_create_undefined (),
|
||||
args,
|
||||
1);
|
||||
jerry_release_value (ret_val_print);
|
||||
jerry_value_t args[] = { eval_ret };
|
||||
jerry_value_t ret_val_print = jerry_call (print_function, jerry_undefined (), args, 1);
|
||||
jerry_value_free (ret_val_print);
|
||||
TCMD_RSP_FINAL (ctx, NULL);
|
||||
}
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
bfree (buffer);
|
||||
bfree (str_lens);
|
||||
}
|
||||
}
|
||||
|
||||
void jerry_start ()
|
||||
void
|
||||
jerry_start ()
|
||||
{
|
||||
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
srand (now.u);
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
|
||||
print_function = jerry_get_property (global_obj_val, print_func_name_val);
|
||||
jerry_release_value (print_func_name_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_t global_obj_val = jerry_current_realm ();
|
||||
jerry_value_t print_func_name_val = jerry_string_sz ("print");
|
||||
print_function = jerry_object_get (global_obj_val, print_func_name_val);
|
||||
jerry_value_free (print_func_name_val);
|
||||
jerry_value_free (global_obj_val);
|
||||
}
|
||||
|
||||
/* Application main entry point */
|
||||
void main_task (void *param)
|
||||
void
|
||||
main_task (void *param)
|
||||
{
|
||||
/* Init BSP (also init BSP on ARC core) */
|
||||
queue = bsp_init ();
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
/**
|
||||
* Computes the end of the directory part of a path.
|
||||
@@ -102,8 +102,7 @@ typedef struct jerry_port_module_t
|
||||
/**
|
||||
* Native info descriptor for modules.
|
||||
*/
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info =
|
||||
{
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info = {
|
||||
.free_cb = NULL,
|
||||
};
|
||||
|
||||
@@ -136,8 +135,8 @@ jerry_port_module_free (jerry_port_module_manager_t *manager_p, /**< module mana
|
||||
if (release_all || module_p->realm == realm)
|
||||
{
|
||||
free (module_p->path_p);
|
||||
jerry_release_value (module_p->realm);
|
||||
jerry_release_value (module_p->module);
|
||||
jerry_value_free (module_p->realm);
|
||||
jerry_value_free (module_p->module);
|
||||
|
||||
free (module_p);
|
||||
|
||||
@@ -174,20 +173,18 @@ jerry_port_module_manager_init (void *user_data_p)
|
||||
static void
|
||||
jerry_port_module_manager_deinit (void *user_data_p) /**< context pointer to deinitialize */
|
||||
{
|
||||
jerry_value_t undef = jerry_create_undefined ();
|
||||
jerry_value_t undef = jerry_undefined ();
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) user_data_p, undef);
|
||||
jerry_release_value (undef);
|
||||
jerry_value_free (undef);
|
||||
} /* jerry_port_module_manager_deinit */
|
||||
|
||||
/**
|
||||
* Declare the context data manager for modules.
|
||||
*/
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager =
|
||||
{
|
||||
.init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed = sizeof (jerry_port_module_manager_t)
|
||||
};
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager = { .init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed =
|
||||
sizeof (jerry_port_module_manager_t) };
|
||||
|
||||
/**
|
||||
* Default module resolver.
|
||||
@@ -201,44 +198,43 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
(void) user_p;
|
||||
|
||||
jerry_port_module_t *module_p;
|
||||
const jerry_char_t *base_path_p = NULL;
|
||||
size_t base_path_length = 0;
|
||||
jerry_port_module_t *module_p = jerry_object_get_native_ptr (referrer, &jerry_port_module_native_info);
|
||||
|
||||
if (jerry_get_object_native_pointer (referrer, (void **) &module_p, &jerry_port_module_native_info))
|
||||
if (module_p != NULL)
|
||||
{
|
||||
base_path_p = module_p->path_p;
|
||||
base_path_length = module_p->base_path_length;
|
||||
}
|
||||
|
||||
jerry_size_t in_path_length = jerry_get_utf8_string_size (specifier);
|
||||
jerry_size_t in_path_length = jerry_string_size (specifier, JERRY_ENCODING_UTF8);
|
||||
jerry_char_t *in_path_p = (jerry_char_t *) malloc (in_path_length + 1);
|
||||
jerry_string_to_utf8_char_buffer (specifier, in_path_p, in_path_length);
|
||||
jerry_string_to_buffer (specifier, JERRY_ENCODING_UTF8, in_path_p, in_path_length);
|
||||
in_path_p[in_path_length] = '\0';
|
||||
|
||||
jerry_char_t *path_p = jerry_port_normalize_path (in_path_p, in_path_length, base_path_p, base_path_length);
|
||||
|
||||
if (path_p == NULL)
|
||||
{
|
||||
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Out of memory");
|
||||
return jerry_throw_sz (JERRY_ERROR_COMMON, "Out of memory");
|
||||
}
|
||||
|
||||
jerry_value_t realm = jerry_get_global_object ();
|
||||
jerry_value_t realm = jerry_current_realm ();
|
||||
|
||||
jerry_port_module_manager_t *manager_p;
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager);
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager);
|
||||
|
||||
module_p = manager_p->module_head_p;
|
||||
|
||||
while (module_p != NULL)
|
||||
{
|
||||
if (module_p->realm == realm
|
||||
&& strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
if (module_p->realm == realm && strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
return jerry_acquire_value (module_p->module);
|
||||
jerry_value_free (realm);
|
||||
return jerry_value_copy (module_p->module);
|
||||
}
|
||||
|
||||
module_p = module_p->next_p;
|
||||
@@ -251,28 +247,24 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
/* TODO: This is incorrect, but makes test262 module tests pass
|
||||
* (they should throw SyntaxError, but not because the module cannot be found). */
|
||||
return jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "Module file not found");
|
||||
jerry_value_free (realm);
|
||||
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found");
|
||||
}
|
||||
|
||||
jerry_parse_options_t parse_options;
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_RESOURCE;
|
||||
parse_options.resource_name = jerry_create_string_sz ((const jerry_char_t *) in_path_p, in_path_length);
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME;
|
||||
parse_options.source_name = jerry_string ((const jerry_char_t *) in_path_p, in_path_length, JERRY_ENCODING_UTF8);
|
||||
|
||||
jerry_value_t ret_value = jerry_parse (source_p,
|
||||
source_size,
|
||||
&parse_options);
|
||||
jerry_value_t ret_value = jerry_parse (source_p, source_size, &parse_options);
|
||||
|
||||
jerry_release_value (parse_options.resource_name);
|
||||
jerry_value_free (parse_options.source_name);
|
||||
jerry_port_release_source (source_p);
|
||||
free (in_path_p);
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
free (path_p);
|
||||
jerry_release_value (realm);
|
||||
jerry_value_free (realm);
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
@@ -282,9 +274,9 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
module_p->path_p = path_p;
|
||||
module_p->base_path_length = jerry_port_get_directory_end (module_p->path_p);
|
||||
module_p->realm = realm;
|
||||
module_p->module = jerry_acquire_value (ret_value);
|
||||
module_p->module = jerry_value_copy (ret_value);
|
||||
|
||||
jerry_set_object_native_pointer (ret_value, module_p, &jerry_port_module_native_info);
|
||||
jerry_object_set_native_ptr (ret_value, &jerry_port_module_native_info, module_p);
|
||||
manager_p->module_head_p = module_p;
|
||||
|
||||
return ret_value;
|
||||
@@ -297,6 +289,5 @@ void
|
||||
jerry_port_module_release (const jerry_value_t realm) /**< if this argument is object, release only those modules,
|
||||
* which realm value is equal to this argument. */
|
||||
{
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager),
|
||||
realm);
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager), realm);
|
||||
} /* jerry_port_module_release */
|
||||
|
||||
@@ -13,67 +13,64 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "jerry_extapi.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "c_types.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerry_extapi.h"
|
||||
#define __UNUSED__ __attribute__ ((unused))
|
||||
|
||||
#define __UNUSED__ __attribute__((unused))
|
||||
#define DELCARE_HANDLER(NAME) \
|
||||
static jerry_value_t NAME##_handler (const jerry_call_info_t *call_info_p __UNUSED__, \
|
||||
const jerry_value_t args_p[], \
|
||||
const jerry_length_t args_cnt)
|
||||
|
||||
#define DELCARE_HANDLER(NAME) \
|
||||
static jerry_value_t \
|
||||
NAME ## _handler (const jerry_call_info_t *call_info_p __UNUSED__, \
|
||||
const jerry_value_t args_p[], \
|
||||
const jerry_length_t args_cnt)
|
||||
#define REGISTER_HANDLER(NAME) register_native_function (#NAME, NAME##_handler)
|
||||
|
||||
#define REGISTER_HANDLER(NAME) \
|
||||
register_native_function ( # NAME, NAME ## _handler)
|
||||
|
||||
DELCARE_HANDLER(assert) {
|
||||
if (args_cnt == 1
|
||||
&& jerry_value_is_true (args_p[0]))
|
||||
DELCARE_HANDLER (assert)
|
||||
{
|
||||
if (args_cnt == 1 && jerry_value_is_true (args_p[0]))
|
||||
{
|
||||
printf (">> Jerry assert true\r\n");
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
}
|
||||
printf ("Script assertion failed\n");
|
||||
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
} /* assert */
|
||||
|
||||
|
||||
DELCARE_HANDLER(print) {
|
||||
DELCARE_HANDLER (print)
|
||||
{
|
||||
if (args_cnt)
|
||||
{
|
||||
for (jerry_length_t cc = 0; cc < args_cnt; cc++)
|
||||
{
|
||||
if (jerry_value_is_string (args_p[cc]))
|
||||
{
|
||||
jerry_size_t size = jerry_get_utf8_string_size (args_p[0]);
|
||||
jerry_size_t size = jerry_string_size (args_p[0], JERRY_ENCODING_UTF8);
|
||||
char *buffer;
|
||||
buffer = (char *) malloc(size + 1);
|
||||
buffer = (char *) malloc (size + 1);
|
||||
|
||||
if(!buffer)
|
||||
if (!buffer)
|
||||
{
|
||||
// not enough memory for this string.
|
||||
printf("[<too-long-string>]");
|
||||
continue;
|
||||
// not enough memory for this string.
|
||||
printf ("[<too-long-string>]");
|
||||
continue;
|
||||
}
|
||||
|
||||
jerry_string_to_utf8_char_buffer (args_p[cc],
|
||||
(jerry_char_t *) buffer,
|
||||
size);
|
||||
*(buffer + size) = 0;
|
||||
printf("%s ", buffer);
|
||||
jerry_string_to_buffer (args_p[cc], JERRY_ENCODING_UTF8, (jerry_char_t *) buffer, size);
|
||||
buffer[size] = '\0';
|
||||
printf ("%s ", buffer);
|
||||
free (buffer);
|
||||
}
|
||||
else if (jerry_value_is_number (args_p[cc]))
|
||||
{
|
||||
double number = jerry_get_number_value (args_p[cc]);
|
||||
double number = jerry_value_as_number (args_p[cc]);
|
||||
if ((int) number == number)
|
||||
{
|
||||
printf ("%d", (int) number);
|
||||
@@ -81,106 +78,105 @@ DELCARE_HANDLER(print) {
|
||||
else
|
||||
{
|
||||
char buff[50];
|
||||
sprintf(buff, "%.10f", number);
|
||||
printf("%s", buff);
|
||||
sprintf (buff, "%.10f", number);
|
||||
printf ("%s", buff);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
printf ("\r\n");
|
||||
}
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
} /* print */
|
||||
|
||||
DELCARE_HANDLER(gpio_dir) {
|
||||
DELCARE_HANDLER (gpio_dir)
|
||||
{
|
||||
if (args_cnt < 2)
|
||||
{
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
}
|
||||
|
||||
int port = (int) jerry_get_number_value (args_p[0]);
|
||||
int value = (int) jerry_get_number_value (args_p[1]);
|
||||
int port = (int) jerry_value_as_number (args_p[0]);
|
||||
int value = (int) jerry_value_as_number (args_p[1]);
|
||||
|
||||
if (value)
|
||||
{
|
||||
GPIO_AS_OUTPUT(1 << port);
|
||||
GPIO_AS_OUTPUT (1 << port);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_AS_INPUT(1 << port);
|
||||
GPIO_AS_INPUT (1 << port);
|
||||
}
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
} /* gpio_dir */
|
||||
|
||||
DELCARE_HANDLER(gpio_set) {
|
||||
DELCARE_HANDLER (gpio_set)
|
||||
{
|
||||
if (args_cnt < 2)
|
||||
{
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
}
|
||||
|
||||
int port = (int) jerry_get_number_value (args_p[0]);
|
||||
int value = (int) jerry_get_number_value (args_p[1]);
|
||||
int port = (int) jerry_value_as_number (args_p[0]);
|
||||
int value = (int) jerry_value_as_number (args_p[1]);
|
||||
|
||||
GPIO_OUTPUT_SET(port, value);
|
||||
GPIO_OUTPUT_SET (port, value);
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
} /* gpio_set */
|
||||
|
||||
|
||||
DELCARE_HANDLER(gpio_get) {
|
||||
DELCARE_HANDLER (gpio_get)
|
||||
{
|
||||
if (args_cnt < 1)
|
||||
{
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
}
|
||||
|
||||
int port = (int) jerry_get_number_value (args_p[0]);
|
||||
int value = GPIO_INPUT_GET(port) ? 1 : 0;
|
||||
int port = (int) jerry_value_as_number (args_p[0]);
|
||||
int value = GPIO_INPUT_GET (port) ? 1 : 0;
|
||||
|
||||
return jerry_create_number ((double) value);
|
||||
return jerry_number ((double) value);
|
||||
} /* gpio_get */
|
||||
|
||||
static bool
|
||||
register_native_function (const char* name,
|
||||
jerry_external_handler_t handler)
|
||||
register_native_function (const char *name, jerry_external_handler_t handler)
|
||||
{
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
jerry_value_t reg_func_val = jerry_create_external_function (handler);
|
||||
bool bok = true;
|
||||
jerry_value_t global_obj_val = jerry_current_realm ();
|
||||
jerry_value_t reg_func_val = jerry_function_external (handler);
|
||||
|
||||
if (!(jerry_value_is_function (reg_func_val)
|
||||
&& jerry_value_is_constructor (reg_func_val)))
|
||||
if (!(jerry_value_is_function (reg_func_val) && jerry_value_is_constructor (reg_func_val)))
|
||||
{
|
||||
printf ("!!! create_external_function failed !!!\r\n");
|
||||
jerry_release_value (reg_func_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_free (reg_func_val);
|
||||
jerry_value_free (global_obj_val);
|
||||
return false;
|
||||
}
|
||||
|
||||
jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) name);
|
||||
jerry_value_t res = jerry_set_property (global_obj_val, prop_name_val, reg_func_val);
|
||||
jerry_value_t prop_name_val = jerry_string_sz (name);
|
||||
jerry_value_t res = jerry_object_set (global_obj_val, prop_name_val, reg_func_val);
|
||||
|
||||
jerry_release_value (reg_func_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_release_value (prop_name_val);
|
||||
jerry_value_free (reg_func_val);
|
||||
jerry_value_free (global_obj_val);
|
||||
jerry_value_free (prop_name_val);
|
||||
|
||||
if (jerry_value_is_error (res))
|
||||
if (jerry_value_is_exception (res))
|
||||
{
|
||||
printf ("!!! register_native_function failed: [%s]\r\n", name);
|
||||
jerry_release_value (res);
|
||||
jerry_value_free (res);
|
||||
return false;
|
||||
}
|
||||
|
||||
jerry_release_value (res);
|
||||
jerry_value_free (res);
|
||||
|
||||
return true;
|
||||
} /* register_native_function */
|
||||
|
||||
void js_register_functions (void)
|
||||
void
|
||||
js_register_functions (void)
|
||||
{
|
||||
REGISTER_HANDLER(assert);
|
||||
REGISTER_HANDLER(print);
|
||||
REGISTER_HANDLER(gpio_dir);
|
||||
REGISTER_HANDLER(gpio_set);
|
||||
REGISTER_HANDLER(gpio_get);
|
||||
REGISTER_HANDLER (assert);
|
||||
REGISTER_HANDLER (print);
|
||||
REGISTER_HANDLER (gpio_dir);
|
||||
REGISTER_HANDLER (gpio_set);
|
||||
REGISTER_HANDLER (gpio_get);
|
||||
} /* js_register_functions */
|
||||
|
||||
@@ -13,88 +13,97 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "jerry_extapi.h"
|
||||
#include "jerry_run.h"
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
static const char* fn_sys_loop_name = "sysloop";
|
||||
#include "jerry_extapi.h"
|
||||
|
||||
void js_entry ()
|
||||
static const char *fn_sys_loop_name = "sysloop";
|
||||
|
||||
void
|
||||
js_entry ()
|
||||
{
|
||||
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
srand (now.u);
|
||||
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
js_register_functions ();
|
||||
}
|
||||
|
||||
int js_eval (const char *source_p, const size_t source_size)
|
||||
int
|
||||
js_eval (const char *source_p, const size_t source_size)
|
||||
{
|
||||
jerry_value_t res = jerry_eval ((jerry_char_t *) source_p,
|
||||
source_size,
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
if (jerry_value_is_error (res)) {
|
||||
jerry_release_value (res);
|
||||
jerry_value_t res = jerry_eval ((jerry_char_t *) source_p, source_size, JERRY_PARSE_NO_OPTS);
|
||||
if (jerry_value_is_exception (res))
|
||||
{
|
||||
jerry_value_free (res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
jerry_release_value (res);
|
||||
jerry_value_free (res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int js_loop (uint32_t ticknow)
|
||||
int
|
||||
js_loop (uint32_t ticknow)
|
||||
{
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) fn_sys_loop_name);
|
||||
jerry_value_t sysloop_func = jerry_get_property (global_obj_val, prop_name_val);
|
||||
jerry_release_value (prop_name_val);
|
||||
jerry_value_t global_obj_val = jerry_current_realm ();
|
||||
jerry_value_t prop_name_val = jerry_string_sz (fn_sys_loop_name);
|
||||
jerry_value_t sysloop_func = jerry_object_get (global_obj_val, prop_name_val);
|
||||
jerry_value_free (prop_name_val);
|
||||
|
||||
if (jerry_value_is_error (sysloop_func)) {
|
||||
if (jerry_value_is_exception (sysloop_func))
|
||||
{
|
||||
printf ("Error: '%s' not defined!!!\r\n", fn_sys_loop_name);
|
||||
jerry_release_value (sysloop_func);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_free (sysloop_func);
|
||||
jerry_value_free (global_obj_val);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!jerry_value_is_function (sysloop_func)) {
|
||||
if (!jerry_value_is_function (sysloop_func))
|
||||
{
|
||||
printf ("Error: '%s' is not a function!!!\r\n", fn_sys_loop_name);
|
||||
jerry_release_value (sysloop_func);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_free (sysloop_func);
|
||||
jerry_value_free (global_obj_val);
|
||||
return -2;
|
||||
}
|
||||
|
||||
jerry_value_t val_args[] = { jerry_create_number (ticknow) };
|
||||
jerry_value_t val_args[] = { jerry_number (ticknow) };
|
||||
uint16_t val_argv = sizeof (val_args) / sizeof (jerry_value_t);
|
||||
|
||||
jerry_value_t res = jerry_call_function (sysloop_func,
|
||||
global_obj_val,
|
||||
val_args,
|
||||
val_argv);
|
||||
jerry_value_t res = jerry_call (sysloop_func, global_obj_val, val_args, val_argv);
|
||||
|
||||
for (uint16_t i = 0; i < val_argv; i++) {
|
||||
jerry_release_value (val_args[i]);
|
||||
for (uint16_t i = 0; i < val_argv; i++)
|
||||
{
|
||||
jerry_value_free (val_args[i]);
|
||||
}
|
||||
|
||||
jerry_release_value (sysloop_func);
|
||||
jerry_release_value (global_obj_val);
|
||||
jerry_value_free (sysloop_func);
|
||||
jerry_value_free (global_obj_val);
|
||||
|
||||
if (jerry_value_is_error (res)) {
|
||||
jerry_release_value (res);
|
||||
if (jerry_value_is_exception (res))
|
||||
{
|
||||
jerry_value_free (res);
|
||||
return -3;
|
||||
}
|
||||
|
||||
jerry_release_value (res);
|
||||
jerry_value_free (res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void js_exit (void)
|
||||
void
|
||||
js_exit (void)
|
||||
{
|
||||
jerry_cleanup ();
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "mbed.h"
|
||||
|
||||
/**
|
||||
@@ -22,9 +21,11 @@
|
||||
*
|
||||
* Called if/when the AnalogIn is GC'ed.
|
||||
*/
|
||||
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)(void* void_ptr, jerry_object_native_info_t *info_p) {
|
||||
(void) info_p;
|
||||
delete static_cast<AnalogIn*>(void_ptr);
|
||||
void
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (AnalogIn) (void* void_ptr, jerry_object_native_info_t* info_p)
|
||||
{
|
||||
(void) info_p;
|
||||
delete static_cast<AnalogIn*> (void_ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,9 +33,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)(void* void_ptr, jerry_object_nat
|
||||
*
|
||||
* Set AnalogIn#destructor as the free callback.
|
||||
*/
|
||||
static const jerry_object_native_info_t native_obj_type_info = {
|
||||
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(AnalogIn)
|
||||
};
|
||||
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (AnalogIn) };
|
||||
|
||||
/**
|
||||
* AnalogIn#read (native JavaScript method)
|
||||
@@ -43,22 +43,22 @@ static const jerry_object_native_info_t native_obj_type_info = {
|
||||
*
|
||||
* @returns A floating-point value representing the current input voltage, measured as a percentage
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(AnalogIn, read) {
|
||||
CHECK_ARGUMENT_COUNT(AnalogIn, read, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (AnalogIn, read)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (AnalogIn, read, (args_count == 0));
|
||||
|
||||
// Extract native AnalogIn pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native AnalogIn pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native AnalogIn pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native AnalogIn pointer");
|
||||
}
|
||||
|
||||
AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
|
||||
AnalogIn* native_ptr = static_cast<AnalogIn*> (void_ptr);
|
||||
|
||||
float result = native_ptr->read();
|
||||
return jerry_create_number(result);
|
||||
float result = native_ptr->read ();
|
||||
return jerry_number (result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,22 +68,22 @@ DECLARE_CLASS_FUNCTION(AnalogIn, read) {
|
||||
*
|
||||
* @returns 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(AnalogIn, read_u16) {
|
||||
CHECK_ARGUMENT_COUNT(AnalogIn, read_u16, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (AnalogIn, read_u16)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (AnalogIn, read_u16, (args_count == 0));
|
||||
|
||||
// Extract native AnalogIn pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native AnalogIn pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native AnalogIn pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native AnalogIn pointer");
|
||||
}
|
||||
|
||||
AnalogIn* native_ptr = static_cast<AnalogIn*>(void_ptr);
|
||||
AnalogIn* native_ptr = static_cast<AnalogIn*> (void_ptr);
|
||||
|
||||
uint16_t result = native_ptr->read_u16();
|
||||
return jerry_create_number(result);
|
||||
uint16_t result = native_ptr->read_u16 ();
|
||||
return jerry_number (result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,22 +92,23 @@ DECLARE_CLASS_FUNCTION(AnalogIn, read_u16) {
|
||||
* @param pin_name mbed pin to connect the AnalogIn to.
|
||||
* @returns a JavaScript object representing a AnalogIn.
|
||||
*/
|
||||
DECLARE_CLASS_CONSTRUCTOR(AnalogIn) {
|
||||
CHECK_ARGUMENT_COUNT(AnalogIn, __constructor, args_count == 1);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(AnalogIn, __constructor, 0, number);
|
||||
DECLARE_CLASS_CONSTRUCTOR (AnalogIn)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (AnalogIn, __constructor, args_count == 1);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (AnalogIn, __constructor, 0, number);
|
||||
|
||||
PinName pin_name = PinName(jerry_get_number_value(args[0]));
|
||||
PinName pin_name = PinName (jerry_value_as_number (args[0]));
|
||||
|
||||
// create native object
|
||||
AnalogIn* native_ptr = new AnalogIn(pin_name);
|
||||
// create native object
|
||||
AnalogIn* native_ptr = new AnalogIn (pin_name);
|
||||
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_create_object();
|
||||
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_object ();
|
||||
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
|
||||
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read);
|
||||
ATTACH_CLASS_FUNCTION(js_object, AnalogIn, read_u16);
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION (js_object, AnalogIn, read);
|
||||
ATTACH_CLASS_FUNCTION (js_object, AnalogIn, read_u16);
|
||||
|
||||
return js_object;
|
||||
return js_object;
|
||||
}
|
||||
|
||||
+73
-71
@@ -12,9 +12,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "mbed.h"
|
||||
|
||||
/**
|
||||
@@ -22,9 +21,11 @@
|
||||
*
|
||||
* Called if/when the DigitalOut is GC'ed.
|
||||
*/
|
||||
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(void* void_ptr, jerry_object_native_info_t *info_p) {
|
||||
(void) info_p;
|
||||
delete static_cast<DigitalOut*>(void_ptr);
|
||||
void
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (DigitalOut) (void* void_ptr, jerry_object_native_info_t* info_p)
|
||||
{
|
||||
(void) info_p;
|
||||
delete static_cast<DigitalOut*> (void_ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,9 +33,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)(void* void_ptr, jerry_object_n
|
||||
*
|
||||
* Set DigitalOut#destructor as the free callback.
|
||||
*/
|
||||
static const jerry_object_native_info_t native_obj_type_info = {
|
||||
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(DigitalOut)
|
||||
};
|
||||
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (DigitalOut) };
|
||||
|
||||
/**
|
||||
* DigitalOut#write (native JavaScript method)
|
||||
@@ -45,25 +45,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
|
||||
* respectively
|
||||
* @returns undefined, or an error if invalid arguments are provided.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(DigitalOut, write) {
|
||||
CHECK_ARGUMENT_COUNT(DigitalOut, write, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, write, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (DigitalOut, write)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (DigitalOut, write, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (DigitalOut, write, 0, number);
|
||||
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
|
||||
|
||||
int arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->write(arg0);
|
||||
int arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->write (arg0);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,22 +73,22 @@ DECLARE_CLASS_FUNCTION(DigitalOut, write) {
|
||||
*
|
||||
* @returns 1 if the pin is currently high, or 0 if the pin is currently low.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(DigitalOut, read) {
|
||||
CHECK_ARGUMENT_COUNT(DigitalOut, read, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (DigitalOut, read)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (DigitalOut, read, (args_count == 0));
|
||||
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
|
||||
|
||||
int result = native_ptr->read();
|
||||
return jerry_create_number(result);
|
||||
int result = native_ptr->read ();
|
||||
return jerry_number (result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,22 +97,22 @@ DECLARE_CLASS_FUNCTION(DigitalOut, read) {
|
||||
* @returns 0 if the DigitalOut is set to NC, or 1 if it is connected to an
|
||||
* actual pin
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
|
||||
CHECK_ARGUMENT_COUNT(DigitalOut, is_connected, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (DigitalOut, is_connected)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (DigitalOut, is_connected, (args_count == 0));
|
||||
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native DigitalOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native DigitalOut pointer");
|
||||
}
|
||||
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*>(void_ptr);
|
||||
DigitalOut* native_ptr = static_cast<DigitalOut*> (void_ptr);
|
||||
|
||||
int result = native_ptr->is_connected();
|
||||
return jerry_create_number(result);
|
||||
int result = native_ptr->is_connected ();
|
||||
return jerry_number (result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,35 +122,37 @@ DECLARE_CLASS_FUNCTION(DigitalOut, is_connected) {
|
||||
* @param value (optional) Initial value of the DigitalOut.
|
||||
* @returns a JavaScript object representing a DigitalOut.
|
||||
*/
|
||||
DECLARE_CLASS_CONSTRUCTOR(DigitalOut) {
|
||||
CHECK_ARGUMENT_COUNT(DigitalOut, __constructor, (args_count == 1 || args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(DigitalOut, __constructor, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION(DigitalOut, __constructor, 1, number, (args_count == 2));
|
||||
DECLARE_CLASS_CONSTRUCTOR (DigitalOut)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (DigitalOut, __constructor, (args_count == 1 || args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (DigitalOut, __constructor, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION (DigitalOut, __constructor, 1, number, (args_count == 2));
|
||||
|
||||
DigitalOut* native_ptr;
|
||||
DigitalOut* native_ptr;
|
||||
|
||||
// Call correct overload of DigitalOut::DigitalOut depending on the
|
||||
// arguments passed.
|
||||
PinName pin_name = PinName(jerry_get_number_value(args[0]));
|
||||
// Call correct overload of DigitalOut::DigitalOut depending on the
|
||||
// arguments passed.
|
||||
PinName pin_name = PinName (jerry_value_as_number (args[0]));
|
||||
|
||||
switch (args_count) {
|
||||
case 1:
|
||||
native_ptr = new DigitalOut(pin_name);
|
||||
break;
|
||||
case 2:
|
||||
int value = static_cast<int>(jerry_get_number_value(args[1]));
|
||||
native_ptr = new DigitalOut(pin_name, value);
|
||||
break;
|
||||
}
|
||||
switch (args_count)
|
||||
{
|
||||
case 1:
|
||||
native_ptr = new DigitalOut (pin_name);
|
||||
break;
|
||||
case 2:
|
||||
int value = static_cast<int> (jerry_value_as_number (args[1]));
|
||||
native_ptr = new DigitalOut (pin_name, value);
|
||||
break;
|
||||
}
|
||||
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_create_object();
|
||||
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_object ();
|
||||
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
|
||||
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, write);
|
||||
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, read);
|
||||
ATTACH_CLASS_FUNCTION(js_object, DigitalOut, is_connected);
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, write);
|
||||
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, read);
|
||||
ATTACH_CLASS_FUNCTION (js_object, DigitalOut, is_connected);
|
||||
|
||||
return js_object;
|
||||
return js_object;
|
||||
}
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "jerryscript-mbed-drivers/I2C-js.h"
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "mbed.h"
|
||||
|
||||
/**
|
||||
@@ -23,9 +23,11 @@
|
||||
*
|
||||
* Called if/when the I2C object is GC'ed.
|
||||
*/
|
||||
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C) (void *void_ptr, jerry_object_native_info_t *info_p) {
|
||||
(void) info_p;
|
||||
delete static_cast<I2C*>(void_ptr);
|
||||
void
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (I2C) (void *void_ptr, jerry_object_native_info_t *info_p)
|
||||
{
|
||||
(void) info_p;
|
||||
delete static_cast<I2C *> (void_ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,9 +35,7 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C) (void *void_ptr, jerry_object_native_
|
||||
*
|
||||
* Set I2C#destructor as the free callback.
|
||||
*/
|
||||
static const jerry_object_native_info_t native_obj_type_info = {
|
||||
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(I2C)
|
||||
};
|
||||
static const jerry_object_native_info_t native_obj_type_info = { .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR (I2C) };
|
||||
|
||||
/**
|
||||
* I2C#frequency (native JavaScript method)
|
||||
@@ -44,25 +44,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
|
||||
*
|
||||
* @param frequency New I2C Frequency
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(I2C, frequency) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, frequency, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, frequency, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (I2C, frequency)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, frequency, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, frequency, 0, number);
|
||||
|
||||
// Unwrap native I2C object
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Unwrap native I2C object
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
int hz = jerry_get_number_value(args[0]);
|
||||
native_ptr->frequency(hz);
|
||||
int hz = jerry_value_as_number (args[0]);
|
||||
native_ptr->frequency (hz);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,77 +86,84 @@ DECLARE_CLASS_FUNCTION(I2C, frequency) {
|
||||
*
|
||||
* @returns array: Data read from the I2C bus
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(I2C, read) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, read, (args_count == 1 || args_count == 3 || args_count == 4));
|
||||
DECLARE_CLASS_FUNCTION (I2C, read)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, read, (args_count == 1 || args_count == 3 || args_count == 4));
|
||||
|
||||
if (args_count == 1) {
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
if (args_count == 1)
|
||||
{
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 0, number);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
|
||||
int data = jerry_get_number_value(args[0]);
|
||||
int result = native_ptr->read(data);
|
||||
|
||||
return jerry_create_number(result);
|
||||
} else {
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 1, array);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, read, 2, number);
|
||||
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, read, 3, boolean, (args_count == 4));
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
|
||||
const uint32_t data_len = jerry_get_array_length(args[1]);
|
||||
|
||||
int address = jerry_get_number_value(args[0]);
|
||||
int length = jerry_get_number_value(args[2]);
|
||||
|
||||
char *data = new char[data_len];
|
||||
|
||||
bool repeated = false;
|
||||
if (args_count == 4) {
|
||||
repeated = jerry_value_is_true(args[3]);
|
||||
}
|
||||
|
||||
int result = native_ptr->read(address, data, length, repeated);
|
||||
|
||||
jerry_value_t out_array = jerry_create_array(data_len);
|
||||
|
||||
for (uint32_t i = 0; i < data_len; i++) {
|
||||
jerry_value_t val = jerry_create_number(double(data[i]));
|
||||
jerry_release_value(jerry_set_property_by_index(out_array, i, val));
|
||||
jerry_release_value(val);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
if (result == 0) {
|
||||
// ACK
|
||||
return out_array;
|
||||
} else {
|
||||
// NACK
|
||||
const char *error_msg = "NACK received from I2C bus";
|
||||
|
||||
jerry_release_value(out_array);
|
||||
return jerry_create_error(JERRY_ERROR_COMMON, reinterpret_cast<const jerry_char_t *>(error_msg));
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
int data = jerry_value_as_number (args[0]);
|
||||
int result = native_ptr->read (data);
|
||||
|
||||
return jerry_number (result);
|
||||
}
|
||||
else
|
||||
{
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 1, array);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, read, 2, number);
|
||||
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION (I2C, read, 3, boolean, (args_count == 4));
|
||||
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
const uint32_t data_len = jerry_array_length (args[1]);
|
||||
|
||||
int address = jerry_value_as_number (args[0]);
|
||||
int length = jerry_value_as_number (args[2]);
|
||||
|
||||
char *data = new char[data_len];
|
||||
|
||||
bool repeated = false;
|
||||
if (args_count == 4)
|
||||
{
|
||||
repeated = jerry_value_is_true (args[3]);
|
||||
}
|
||||
|
||||
int result = native_ptr->read (address, data, length, repeated);
|
||||
|
||||
jerry_value_t out_array = jerry_array (data_len);
|
||||
|
||||
for (uint32_t i = 0; i < data_len; i++)
|
||||
{
|
||||
jerry_value_t val = jerry_number (double (data[i]));
|
||||
jerry_value_free (jerry_object_set_index (out_array, i, val));
|
||||
jerry_value_free (val);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
// ACK
|
||||
return out_array;
|
||||
}
|
||||
else
|
||||
{
|
||||
// NACK
|
||||
const char *error_msg = "NACK received from I2C bus";
|
||||
|
||||
jerry_value_free (out_array);
|
||||
return jerry_throw_sz (JERRY_ERROR_COMMON, error_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,65 +186,68 @@ DECLARE_CLASS_FUNCTION(I2C, read) {
|
||||
*
|
||||
* @returns 0 on success, non-0 on failure
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(I2C, write) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, write, (args_count == 1 || args_count == 3 || args_count == 4));
|
||||
DECLARE_CLASS_FUNCTION (I2C, write)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, write, (args_count == 1 || args_count == 3 || args_count == 4));
|
||||
|
||||
if (args_count == 1) {
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
|
||||
if (args_count == 1)
|
||||
{
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 0, number);
|
||||
|
||||
// Extract native I2C object
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native I2C object
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
|
||||
// Unwrap arguments
|
||||
int data = jerry_get_number_value(args[0]);
|
||||
|
||||
int result = native_ptr->write(data);
|
||||
return jerry_create_number(result);
|
||||
} else {
|
||||
// 3 or 4
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 1, array);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, write, 2, number);
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION(I2C, write, 3, boolean, (args_count == 4));
|
||||
|
||||
// Extract native I2C object
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
|
||||
// Unwrap arguments
|
||||
int address = jerry_get_number_value(args[0]);
|
||||
const uint32_t data_len = jerry_get_array_length(args[1]);
|
||||
int length = jerry_get_number_value(args[2]);
|
||||
bool repeated = args_count == 4 && jerry_value_is_true(args[3]);
|
||||
|
||||
// Construct data byte array
|
||||
char *data = new char[data_len];
|
||||
for (uint32_t i = 0; i < data_len; i++) {
|
||||
data[i] = jerry_get_number_value(jerry_get_property_by_index(args[1], i));
|
||||
}
|
||||
|
||||
int result = native_ptr->write(address, data, length, repeated);
|
||||
|
||||
// free dynamically allocated resources
|
||||
delete[] data;
|
||||
|
||||
return jerry_create_number(result);
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
// Unwrap arguments
|
||||
int data = jerry_value_as_number (args[0]);
|
||||
|
||||
int result = native_ptr->write (data);
|
||||
return jerry_number (result);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 3 or 4
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 1, array);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, write, 2, number);
|
||||
CHECK_ARGUMENT_TYPE_ON_CONDITION (I2C, write, 3, boolean, (args_count == 4));
|
||||
|
||||
// Extract native I2C object
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (void_ptr != NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
// Unwrap arguments
|
||||
int address = jerry_value_as_number (args[0]);
|
||||
const uint32_t data_len = jerry_array_length (args[1]);
|
||||
int length = jerry_value_as_number (args[2]);
|
||||
bool repeated = args_count == 4 && jerry_value_is_true (args[3]);
|
||||
|
||||
// Construct data byte array
|
||||
char *data = new char[data_len];
|
||||
for (uint32_t i = 0; i < data_len; i++)
|
||||
{
|
||||
data[i] = jerry_value_as_number (jerry_object_get_index (args[1], i));
|
||||
}
|
||||
|
||||
int result = native_ptr->write (address, data, length, repeated);
|
||||
|
||||
// free dynamically allocated resources
|
||||
delete[] data;
|
||||
|
||||
return jerry_number (result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,22 +255,22 @@ DECLARE_CLASS_FUNCTION(I2C, write) {
|
||||
*
|
||||
* Creates a start condition on the I2C bus.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(I2C, start) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, start, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (I2C, start)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, start, (args_count == 0));
|
||||
|
||||
// Extract native I2C object
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native I2C object
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
native_ptr->start();
|
||||
return jerry_create_undefined();
|
||||
native_ptr->start ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,22 +278,22 @@ DECLARE_CLASS_FUNCTION(I2C, start) {
|
||||
*
|
||||
* Creates a stop condition on the I2C bus.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(I2C, stop) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, stop, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (I2C, stop)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, stop, (args_count == 0));
|
||||
|
||||
// Extract native I2C object
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native I2C object
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native I2C pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native I2C pointer");
|
||||
}
|
||||
|
||||
I2C *native_ptr = static_cast<I2C*>(void_ptr);
|
||||
I2C *native_ptr = static_cast<I2C *> (void_ptr);
|
||||
|
||||
native_ptr->stop();
|
||||
return jerry_create_undefined();
|
||||
native_ptr->stop ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,24 +303,25 @@ DECLARE_CLASS_FUNCTION(I2C, stop) {
|
||||
* @param scl mbed pin for I2C clock
|
||||
* @returns a JavaScript object representing the I2C bus.
|
||||
*/
|
||||
DECLARE_CLASS_CONSTRUCTOR(I2C) {
|
||||
CHECK_ARGUMENT_COUNT(I2C, __constructor, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(I2C, __constructor, 1, number);
|
||||
DECLARE_CLASS_CONSTRUCTOR (I2C)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (I2C, __constructor, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, __constructor, 0, number);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (I2C, __constructor, 1, number);
|
||||
|
||||
int sda = jerry_get_number_value(args[0]);
|
||||
int scl = jerry_get_number_value(args[1]);
|
||||
int sda = jerry_value_as_number (args[0]);
|
||||
int scl = jerry_value_as_number (args[1]);
|
||||
|
||||
I2C *native_ptr = new I2C((PinName)sda, (PinName)scl);
|
||||
I2C *native_ptr = new I2C ((PinName) sda, (PinName) scl);
|
||||
|
||||
jerry_value_t js_object = jerry_create_object();
|
||||
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
|
||||
jerry_value_t js_object = jerry_object ();
|
||||
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
|
||||
|
||||
ATTACH_CLASS_FUNCTION(js_object, I2C, frequency);
|
||||
ATTACH_CLASS_FUNCTION(js_object, I2C, read);
|
||||
ATTACH_CLASS_FUNCTION(js_object, I2C, write);
|
||||
ATTACH_CLASS_FUNCTION(js_object, I2C, start);
|
||||
ATTACH_CLASS_FUNCTION(js_object, I2C, stop);
|
||||
ATTACH_CLASS_FUNCTION (js_object, I2C, frequency);
|
||||
ATTACH_CLASS_FUNCTION (js_object, I2C, read);
|
||||
ATTACH_CLASS_FUNCTION (js_object, I2C, write);
|
||||
ATTACH_CLASS_FUNCTION (js_object, I2C, start);
|
||||
ATTACH_CLASS_FUNCTION (js_object, I2C, stop);
|
||||
|
||||
return js_object;
|
||||
return js_object;
|
||||
}
|
||||
|
||||
+150
-147
@@ -12,10 +12,9 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "jerryscript-mbed-event-loop/EventLoop.h"
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "mbed.h"
|
||||
|
||||
/**
|
||||
@@ -23,13 +22,15 @@
|
||||
*
|
||||
* Called if/when the InterruptIn object is GC'ed.
|
||||
*/
|
||||
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p) {
|
||||
(void) info_p;
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
void
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p)
|
||||
{
|
||||
(void) info_p;
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
native_ptr->rise(0);
|
||||
native_ptr->fall(0);
|
||||
delete native_ptr;
|
||||
native_ptr->rise (0);
|
||||
native_ptr->fall (0);
|
||||
delete native_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,9 +38,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object
|
||||
*
|
||||
* Set InterruptIn#destructor as the free callback.
|
||||
*/
|
||||
static const jerry_object_native_info_t native_obj_type_info = {
|
||||
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn)
|
||||
};
|
||||
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) };
|
||||
|
||||
/**
|
||||
* InterruptIn#rise (native JavaScript method)
|
||||
@@ -48,62 +48,63 @@ static const jerry_object_native_info_t native_obj_type_info = {
|
||||
*
|
||||
* @param cb Callback function, or null to detach previously attached callback.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, rise, (args_count == 1));
|
||||
DECLARE_CLASS_FUNCTION (InterruptIn, rise)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, rise, (args_count == 1));
|
||||
|
||||
// Detach the rise callback when InterruptIn::rise(null) is called
|
||||
if (jerry_value_is_null(args[0])) {
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Detach the rise callback when InterruptIn::rise(null) is called
|
||||
if (jerry_value_is_null (args[0]))
|
||||
{
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
|
||||
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
|
||||
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
|
||||
jerry_release_value(property_name);
|
||||
|
||||
// Only drop the callback if it exists
|
||||
if (jerry_value_is_function(cb_func)) {
|
||||
// Ensure that the EventLoop frees memory used by the callback.
|
||||
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
|
||||
}
|
||||
jerry_release_value(cb_func);
|
||||
|
||||
native_ptr->rise(0);
|
||||
|
||||
return jerry_create_undefined();
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
// Assuming we actually have a callback now...
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, rise, 0, function);
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
jerry_value_t property_name = jerry_string_sz ("cb_rise");
|
||||
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
|
||||
jerry_value_free (property_name);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
// Only drop the callback if it exists
|
||||
if (jerry_value_is_function (cb_func))
|
||||
{
|
||||
// Ensure that the EventLoop frees memory used by the callback.
|
||||
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
|
||||
}
|
||||
jerry_value_free (cb_func);
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
native_ptr->rise (0);
|
||||
|
||||
jerry_value_t f = args[0];
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
// Pass the function to EventLoop.
|
||||
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
|
||||
native_ptr->rise(cb);
|
||||
// Assuming we actually have a callback now...
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, rise, 0, function);
|
||||
|
||||
// Keep track of our callback internally.
|
||||
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
|
||||
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
|
||||
jerry_release_value(property_name);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
return jerry_create_undefined();
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
jerry_value_t f = args[0];
|
||||
|
||||
// Pass the function to EventLoop.
|
||||
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
|
||||
native_ptr->rise (cb);
|
||||
|
||||
// Keep track of our callback internally.
|
||||
jerry_value_t property_name = jerry_string_sz ("cb_rise");
|
||||
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
|
||||
jerry_value_free (property_name);
|
||||
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,62 +114,63 @@ DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
|
||||
*
|
||||
* @param cb Callback function, or null to detach previously attached callback.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, fall, (args_count == 1));
|
||||
DECLARE_CLASS_FUNCTION (InterruptIn, fall)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, fall, (args_count == 1));
|
||||
|
||||
// Detach the fall callback when InterruptIn::fall(null) is called
|
||||
if (jerry_value_is_null(args[0])) {
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Detach the fall callback when InterruptIn::fall(null) is called
|
||||
if (jerry_value_is_null (args[0]))
|
||||
{
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
|
||||
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
|
||||
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
|
||||
jerry_release_value(property_name);
|
||||
|
||||
// Only drop the callback if it exists
|
||||
if (jerry_value_is_function(cb_func)) {
|
||||
// Ensure that the EventLoop frees memory used by the callback.
|
||||
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
|
||||
}
|
||||
jerry_release_value(cb_func);
|
||||
|
||||
native_ptr->fall(0);
|
||||
|
||||
return jerry_create_undefined();
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
// Assuming we actually have a callback now...
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, fall, 0, function);
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
jerry_value_t property_name = jerry_string_sz ("cb_fall");
|
||||
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
|
||||
jerry_value_free (property_name);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
// Only drop the callback if it exists
|
||||
if (jerry_value_is_function (cb_func))
|
||||
{
|
||||
// Ensure that the EventLoop frees memory used by the callback.
|
||||
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
|
||||
}
|
||||
jerry_value_free (cb_func);
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
native_ptr->fall (0);
|
||||
|
||||
jerry_value_t f = args[0];
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
// Pass the function to EventLoop.
|
||||
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
|
||||
native_ptr->fall(cb);
|
||||
// Assuming we actually have a callback now...
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, fall, 0, function);
|
||||
|
||||
// Keep track of our callback internally.
|
||||
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
|
||||
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
|
||||
jerry_release_value(property_name);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
return jerry_create_undefined();
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
jerry_value_t f = args[0];
|
||||
|
||||
// Pass the function to EventLoop.
|
||||
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
|
||||
native_ptr->fall (cb);
|
||||
|
||||
// Keep track of our callback internally.
|
||||
jerry_value_t property_name = jerry_string_sz ("cb_fall");
|
||||
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
|
||||
jerry_value_free (property_name);
|
||||
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,24 +180,24 @@ DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
|
||||
*
|
||||
* @param mode PullUp, PullDown, PullNone
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, mode, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, mode, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (InterruptIn, mode)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, mode, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, mode, 0, number);
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
int pull = jerry_get_number_value(args[0]);
|
||||
native_ptr->mode((PinMode)pull);
|
||||
int pull = jerry_value_as_number (args[0]);
|
||||
native_ptr->mode ((PinMode) pull);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,21 +205,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
|
||||
*
|
||||
* Disable IRQ. See InterruptIn.h in mbed-os sources for more details.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, disable_irq, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (InterruptIn, disable_irq)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, disable_irq, (args_count == 0));
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
native_ptr->disable_irq();
|
||||
return jerry_create_undefined();
|
||||
native_ptr->disable_irq ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,21 +227,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
|
||||
*
|
||||
* Enable IRQ. See InterruptIn.h in mbed-os sources for more details.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, enable_irq, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (InterruptIn, enable_irq)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, enable_irq, (args_count == 0));
|
||||
|
||||
void *void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
|
||||
}
|
||||
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
|
||||
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
|
||||
|
||||
native_ptr->enable_irq();
|
||||
return jerry_create_undefined();
|
||||
native_ptr->enable_irq ();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,21 +251,22 @@ DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
|
||||
*
|
||||
* @returns JavaScript object wrapping InterruptIn native object
|
||||
*/
|
||||
DECLARE_CLASS_CONSTRUCTOR(InterruptIn) {
|
||||
CHECK_ARGUMENT_COUNT(InterruptIn, __constructor, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, __constructor, 0, number);
|
||||
int pin = jerry_get_number_value(args[0]);
|
||||
DECLARE_CLASS_CONSTRUCTOR (InterruptIn)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (InterruptIn, __constructor, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, __constructor, 0, number);
|
||||
int pin = jerry_value_as_number (args[0]);
|
||||
|
||||
InterruptIn *native_ptr = new InterruptIn((PinName)pin);
|
||||
jerry_value_t js_object = jerry_create_object();
|
||||
InterruptIn *native_ptr = new InterruptIn ((PinName) pin);
|
||||
jerry_value_t js_object = jerry_object ();
|
||||
|
||||
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
|
||||
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
|
||||
|
||||
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, rise);
|
||||
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, fall);
|
||||
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, mode);
|
||||
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, enable_irq);
|
||||
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, disable_irq);
|
||||
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, rise);
|
||||
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, fall);
|
||||
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, mode);
|
||||
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, enable_irq);
|
||||
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, disable_irq);
|
||||
|
||||
return js_object;
|
||||
return js_object;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
#include "mbed.h"
|
||||
|
||||
/**
|
||||
@@ -22,9 +21,11 @@
|
||||
*
|
||||
* Called if/when the PwmOut is GC'ed.
|
||||
*/
|
||||
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr, jerry_object_native_info_t *info_p) {
|
||||
(void) info_p;
|
||||
delete static_cast<PwmOut*>(void_ptr);
|
||||
void
|
||||
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (PwmOut) (void* void_ptr, jerry_object_native_info_t* info_p)
|
||||
{
|
||||
(void) info_p;
|
||||
delete static_cast<PwmOut*> (void_ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,9 +33,7 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)(void* void_ptr, jerry_object_nativ
|
||||
*
|
||||
* Set PwmOut#destructor as the free callback.
|
||||
*/
|
||||
static const jerry_object_native_info_t native_obj_type_info = {
|
||||
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(PwmOut)
|
||||
};
|
||||
static const jerry_object_native_info_t native_obj_type_info = { .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR (PwmOut) };
|
||||
|
||||
/**
|
||||
* PwmOut#write (native JavaScript method)
|
||||
@@ -47,25 +46,25 @@ static const jerry_object_native_info_t native_obj_type_info = {
|
||||
* Values outside this range will be saturated to 0.0f or 1.0f
|
||||
* @returns undefined
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, write) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, write, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, write, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, write)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, write, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, write, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->write(static_cast<float>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->write (static_cast<float> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,22 +80,22 @@ DECLARE_CLASS_FUNCTION(PwmOut, write) {
|
||||
* @note
|
||||
* This value may not match exactly the value set by a previous <write>.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, read) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, read, (args_count == 0));
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, read)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, read, (args_count == 0));
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
float result = native_ptr->read();
|
||||
return jerry_create_number(result);
|
||||
float result = native_ptr->read ();
|
||||
return jerry_number (result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,25 +107,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, read) {
|
||||
* The resolution is currently in microseconds; periods smaller than this
|
||||
* will be set to zero.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, period) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, period, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, period)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, period, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->period(static_cast<float>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->period (static_cast<float> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,25 +133,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period) {
|
||||
*
|
||||
* Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, period_ms) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, period_ms, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_ms, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, period_ms)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, period_ms, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period_ms, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->period_ms(static_cast<int>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->period_ms (static_cast<int> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,25 +159,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period_ms) {
|
||||
*
|
||||
* Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, period_us) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, period_us, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, period_us, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, period_us)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, period_us, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, period_us, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->period_us(static_cast<int>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->period_us (static_cast<int> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,25 +185,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, period_us) {
|
||||
*
|
||||
* Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->pulsewidth(static_cast<float>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->pulsewidth (static_cast<float> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,25 +211,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth) {
|
||||
*
|
||||
* Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_ms, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_ms, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth_ms)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth_ms, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth_ms, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->pulsewidth_ms(static_cast<int>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->pulsewidth_ms (static_cast<int> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,25 +237,25 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_ms) {
|
||||
*
|
||||
* Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
|
||||
*/
|
||||
DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, pulsewidth_us, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, pulsewidth_us, 0, number);
|
||||
DECLARE_CLASS_FUNCTION (PwmOut, pulsewidth_us)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, pulsewidth_us, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, pulsewidth_us, 0, number);
|
||||
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr;
|
||||
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
|
||||
// Extract native PwmOut pointer
|
||||
void* void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
|
||||
|
||||
if (!has_ptr) {
|
||||
return jerry_create_error(JERRY_ERROR_TYPE,
|
||||
(const jerry_char_t *) "Failed to get native PwmOut pointer");
|
||||
}
|
||||
if (void_ptr == NULL)
|
||||
{
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native PwmOut pointer");
|
||||
}
|
||||
|
||||
PwmOut* native_ptr = static_cast<PwmOut*>(void_ptr);
|
||||
PwmOut* native_ptr = static_cast<PwmOut*> (void_ptr);
|
||||
|
||||
double arg0 = jerry_get_number_value(args[0]);
|
||||
native_ptr->pulsewidth_us(static_cast<int>(arg0));
|
||||
double arg0 = jerry_value_as_number (args[0]);
|
||||
native_ptr->pulsewidth_us (static_cast<int> (arg0));
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,28 +264,29 @@ DECLARE_CLASS_FUNCTION(PwmOut, pulsewidth_us) {
|
||||
* @param pin_name mbed pin to connect the PwmOut to.
|
||||
* @returns a JavaScript object representing a PwmOut.
|
||||
*/
|
||||
DECLARE_CLASS_CONSTRUCTOR(PwmOut) {
|
||||
CHECK_ARGUMENT_COUNT(PwmOut, __constructor, args_count == 1);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(PwmOut, __constructor, 0, number);
|
||||
DECLARE_CLASS_CONSTRUCTOR (PwmOut)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (PwmOut, __constructor, args_count == 1);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (PwmOut, __constructor, 0, number);
|
||||
|
||||
PinName pin_name = PinName(jerry_get_number_value(args[0]));
|
||||
PinName pin_name = PinName (jerry_value_as_number (args[0]));
|
||||
|
||||
// Create the native object
|
||||
PwmOut* native_ptr = new PwmOut(pin_name);
|
||||
// Create the native object
|
||||
PwmOut* native_ptr = new PwmOut (pin_name);
|
||||
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_create_object();
|
||||
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
|
||||
// create the jerryscript object
|
||||
jerry_value_t js_object = jerry_object ();
|
||||
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
|
||||
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, write);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, read);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_ms);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, period_us);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_ms);
|
||||
ATTACH_CLASS_FUNCTION(js_object, PwmOut, pulsewidth_us);
|
||||
// attach methods
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, write);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, read);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period_ms);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, period_us);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth_ms);
|
||||
ATTACH_CLASS_FUNCTION (js_object, PwmOut, pulsewidth_us);
|
||||
|
||||
return js_object;
|
||||
return js_object;
|
||||
}
|
||||
|
||||
+36
-27
@@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-drivers/setInterval-js.h"
|
||||
|
||||
#include "jerryscript-mbed-event-loop/EventLoop.h"
|
||||
|
||||
/**
|
||||
@@ -23,26 +24,33 @@
|
||||
* @param function Function to call
|
||||
* @param interval Time between function calls, in ms.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(setInterval) {
|
||||
CHECK_ARGUMENT_COUNT(global, setInterval, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 1, number);
|
||||
DECLARE_GLOBAL_FUNCTION (setInterval)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, setInterval, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 1, number);
|
||||
|
||||
int interval = int(jerry_get_number_value(args[1]));
|
||||
int interval = int (jerry_value_as_number (args[1]));
|
||||
|
||||
int id = mbed::js::EventLoop::getInstance().getQueue().call_every(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
|
||||
int id = mbed::js::EventLoop::getInstance ().getQueue ().call_every (interval,
|
||||
jerry_call,
|
||||
args[0],
|
||||
jerry_null (),
|
||||
(jerry_value_t*) NULL,
|
||||
0);
|
||||
|
||||
jerry_value_t result = jerry_set_property_by_index(call_info_p->function, id, args[0]);
|
||||
jerry_value_t result = jerry_object_set_index (call_info_p->function, id, args[0]);
|
||||
|
||||
if (jerry_value_is_error(result)) {
|
||||
jerry_release_value(result);
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerry_value_free (result);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setInterval");
|
||||
}
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to run setInterval");
|
||||
}
|
||||
|
||||
jerry_release_value(result);
|
||||
return jerry_create_number(id);
|
||||
jerry_value_free (result);
|
||||
return jerry_number (id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,22 +60,23 @@ DECLARE_GLOBAL_FUNCTION(setInterval) {
|
||||
*
|
||||
* @param id ID of the timeout event, returned by setInterval.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(clearInterval) {
|
||||
CHECK_ARGUMENT_COUNT(global, clearInterval, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearInterval, 0, number);
|
||||
DECLARE_GLOBAL_FUNCTION (clearInterval)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, clearInterval, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, clearInterval, 0, number);
|
||||
|
||||
int id = int(jerry_get_number_value(args[0]));
|
||||
int id = int (jerry_value_as_number (args[0]));
|
||||
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
jerry_value_t global_obj = jerry_get_global_object();
|
||||
jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setInterval");
|
||||
jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
|
||||
jerry_release_value(prop_name);
|
||||
jerry_value_t global_obj = jerry_current_realm ();
|
||||
jerry_value_t prop_name = jerry_string_sz ("setInterval");
|
||||
jerry_value_t func_obj = jerry_object_get (global_obj, prop_name);
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
jerry_delete_property_by_index(func_obj, id);
|
||||
jerry_release_value(func_obj);
|
||||
jerry_release_value(global_obj);
|
||||
jerry_object_delete_index (func_obj, id);
|
||||
jerry_value_free (func_obj);
|
||||
jerry_value_free (global_obj);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
+36
-27
@@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-drivers/setTimeout-js.h"
|
||||
|
||||
#include "jerryscript-mbed-event-loop/EventLoop.h"
|
||||
|
||||
/**
|
||||
@@ -23,26 +24,33 @@
|
||||
* @param function Function to call
|
||||
* @param wait_time Time before function is called, in ms.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(setTimeout) {
|
||||
CHECK_ARGUMENT_COUNT(global, setTimeout, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setTimeout, 1, number);
|
||||
DECLARE_GLOBAL_FUNCTION (setTimeout)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, setTimeout, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setTimeout, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setTimeout, 1, number);
|
||||
|
||||
int interval = int(jerry_get_number_value(args[1]));
|
||||
int interval = int (jerry_value_as_number (args[1]));
|
||||
|
||||
int id = mbed::js::EventLoop::getInstance().getQueue().call_in(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
|
||||
int id = mbed::js::EventLoop::getInstance ().getQueue ().call_in (interval,
|
||||
jerry_call,
|
||||
args[0],
|
||||
jerry_null (),
|
||||
(jerry_value_t*) NULL,
|
||||
0);
|
||||
|
||||
jerry_value_t result = jerry_set_property_by_index(call_info_p->function, id, args[0]);
|
||||
jerry_value_t result = jerry_object_set_index (call_info_p->function, id, args[0]);
|
||||
|
||||
if (jerry_value_is_error(result)) {
|
||||
jerry_release_value(result);
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerry_value_free (result);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setTimeout");
|
||||
}
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to run setTimeout");
|
||||
}
|
||||
|
||||
jerry_release_value(result);
|
||||
return jerry_create_number(id);
|
||||
jerry_value_free (result);
|
||||
return jerry_number (id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,22 +60,23 @@ DECLARE_GLOBAL_FUNCTION(setTimeout) {
|
||||
*
|
||||
* @param id ID of the timeout event, returned by setTimeout.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(clearTimeout) {
|
||||
CHECK_ARGUMENT_COUNT(global, clearTimeout, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearTimeout, 0, number);
|
||||
DECLARE_GLOBAL_FUNCTION (clearTimeout)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, clearTimeout, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, clearTimeout, 0, number);
|
||||
|
||||
int id = int(jerry_get_number_value(args[0]));
|
||||
int id = int (jerry_value_as_number (args[0]));
|
||||
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
jerry_value_t global_obj = jerry_get_global_object();
|
||||
jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setTimeout");
|
||||
jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
|
||||
jerry_release_value(prop_name);
|
||||
jerry_value_t global_obj = jerry_current_realm ();
|
||||
jerry_value_t prop_name = jerry_string_sz ("setTimeout");
|
||||
jerry_value_t func_obj = jerry_object_get (global_obj, prop_name);
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
jerry_delete_property_by_index(func_obj, id);
|
||||
jerry_release_value(func_obj);
|
||||
jerry_release_value(global_obj);
|
||||
jerry_object_delete_index (func_obj, id);
|
||||
jerry_value_free (func_obj);
|
||||
jerry_value_free (global_obj);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ class EventLoop {
|
||||
}
|
||||
|
||||
void callback(jerry_value_t f) {
|
||||
queue.call(jerry_call_function, f, jerry_create_null(), (const jerry_value_t*)NULL, 0);
|
||||
queue.call(jerry_call, f, jerry_null(), (const jerry_value_t*)NULL, 0);
|
||||
}
|
||||
|
||||
void nativeCallback(Callback<void()> cb) {
|
||||
|
||||
@@ -44,24 +44,24 @@ static int load_javascript() {
|
||||
|
||||
jerry_value_t parsed_code = jerry_parse(code, length, NULL);
|
||||
|
||||
if (jerry_value_is_error(parsed_code)) {
|
||||
if (jerry_value_is_exception(parsed_code)) {
|
||||
LOG_PRINT_ALWAYS("jerry_parse failed [%s]\r\n", js_codes[src].name);
|
||||
jerry_release_value(parsed_code);
|
||||
jerry_value_free(parsed_code);
|
||||
jsmbed_js_exit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
jerry_value_t returned_value = jerry_run(parsed_code);
|
||||
jerry_release_value(parsed_code);
|
||||
jerry_value_free(parsed_code);
|
||||
|
||||
if (jerry_value_is_error(returned_value)) {
|
||||
if (jerry_value_is_exception(returned_value)) {
|
||||
LOG_PRINT_ALWAYS("jerry_run failed [%s]\r\n", js_codes[src].name);
|
||||
jerry_release_value(returned_value);
|
||||
jerry_value_free(returned_value);
|
||||
jsmbed_js_exit();
|
||||
return -1;
|
||||
}
|
||||
|
||||
jerry_release_value(returned_value);
|
||||
jerry_value_free(returned_value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -12,38 +12,43 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "jerryscript-mbed-launcher/setup.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jerryscript-mbed-util/logging.h"
|
||||
|
||||
extern uint32_t jsmbed_js_magic_string_count;
|
||||
extern uint32_t jsmbed_js_magic_string_values[];
|
||||
|
||||
extern const jerry_char_t *jsmbed_js_magic_strings[];
|
||||
extern const char *jsmbed_js_magic_strings[];
|
||||
extern const jerry_length_t jsmbed_js_magic_string_lengths[];
|
||||
|
||||
void jsmbed_js_load_magic_strings() {
|
||||
if (jsmbed_js_magic_string_count == 0) {
|
||||
return;
|
||||
}
|
||||
void
|
||||
jsmbed_js_load_magic_strings ()
|
||||
{
|
||||
if (jsmbed_js_magic_string_count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
jerry_register_magic_strings(jsmbed_js_magic_strings,
|
||||
jsmbed_js_magic_string_count,
|
||||
jsmbed_js_magic_string_lengths);
|
||||
jerry_register_magic_strings ((const jerry_char_t **) jsmbed_js_magic_strings,
|
||||
jsmbed_js_magic_string_count,
|
||||
jsmbed_js_magic_string_lengths);
|
||||
|
||||
jerry_value_t global = jerry_get_global_object();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
|
||||
for (unsigned int idx = 0; idx < jsmbed_js_magic_string_count; idx++) {
|
||||
jerry_value_t constant_value = jerry_create_number(jsmbed_js_magic_string_values[idx]);
|
||||
jerry_value_t magic_string = jerry_create_string(jsmbed_js_magic_strings[idx]);
|
||||
for (unsigned int idx = 0; idx < jsmbed_js_magic_string_count; idx++)
|
||||
{
|
||||
jerry_value_t constant_value = jerry_number (jsmbed_js_magic_string_values[idx]);
|
||||
jerry_value_t magic_string = jerry_string_sz (jsmbed_js_magic_strings[idx]);
|
||||
|
||||
jerry_release_value(jerry_set_property(global, magic_string, constant_value));
|
||||
jerry_value_free (jerry_object_set (global, magic_string, constant_value));
|
||||
|
||||
jerry_release_value(constant_value);
|
||||
jerry_release_value(magic_string);
|
||||
}
|
||||
jerry_value_free (constant_value);
|
||||
jerry_value_free (magic_string);
|
||||
}
|
||||
|
||||
jerry_release_value(global);
|
||||
jerry_value_free (global);
|
||||
}
|
||||
|
||||
+56
-49
@@ -12,66 +12,73 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "jerryscript-mbed-library-registry/wrap_tools.h"
|
||||
|
||||
bool jsmbed_wrap_register_global_function(const char* name, jerry_external_handler_t handler) {
|
||||
jerry_value_t global_object_val = jerry_get_global_object();
|
||||
jerry_value_t reg_function = jerry_create_external_function(handler);
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool is_ok = true;
|
||||
bool
|
||||
jsmbed_wrap_register_global_function (const char* name, jerry_external_handler_t handler)
|
||||
{
|
||||
jerry_value_t global_object_val = jerry_current_realm ();
|
||||
jerry_value_t reg_function = jerry_function_external (handler);
|
||||
|
||||
if (!(jerry_value_is_function(reg_function)
|
||||
&& jerry_value_is_constructor(reg_function))) {
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed!\r\n");
|
||||
jerry_release_value(global_object_val);
|
||||
jerry_release_value(reg_function);
|
||||
return is_ok;
|
||||
}
|
||||
|
||||
if (jerry_value_is_error(reg_function)) {
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS("Error: jerry_create_external_function has error flag! \r\n");
|
||||
jerry_release_value(global_object_val);
|
||||
jerry_release_value(reg_function);
|
||||
return is_ok;
|
||||
}
|
||||
|
||||
jerry_value_t jerry_name = jerry_create_string((jerry_char_t *) name);
|
||||
|
||||
jerry_value_t set_result = jerry_set_property(global_object_val, jerry_name, reg_function);
|
||||
|
||||
|
||||
if (jerry_value_is_error(set_result)) {
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed: [%s]\r\n", name);
|
||||
}
|
||||
|
||||
jerry_release_value(jerry_name);
|
||||
jerry_release_value(global_object_val);
|
||||
jerry_release_value(reg_function);
|
||||
jerry_release_value(set_result);
|
||||
bool is_ok = true;
|
||||
|
||||
if (!(jerry_value_is_function (reg_function) && jerry_value_is_constructor (reg_function)))
|
||||
{
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS ("Error: jerry_function_external failed!\r\n");
|
||||
jerry_value_free (global_object_val);
|
||||
jerry_value_free (reg_function);
|
||||
return is_ok;
|
||||
}
|
||||
|
||||
if (jerry_value_is_exception (reg_function))
|
||||
{
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS ("Error: jerry_function_external has error flag! \r\n");
|
||||
jerry_value_free (global_object_val);
|
||||
jerry_value_free (reg_function);
|
||||
return is_ok;
|
||||
}
|
||||
|
||||
jerry_value_t jerry_name = jerry_string_sz (name);
|
||||
|
||||
jerry_value_t set_result = jerry_object_set (global_object_val, jerry_name, reg_function);
|
||||
|
||||
if (jerry_value_is_exception (set_result))
|
||||
{
|
||||
is_ok = false;
|
||||
LOG_PRINT_ALWAYS ("Error: jerry_function_external failed: [%s]\r\n", name);
|
||||
}
|
||||
|
||||
jerry_value_free (jerry_name);
|
||||
jerry_value_free (global_object_val);
|
||||
jerry_value_free (reg_function);
|
||||
jerry_value_free (set_result);
|
||||
|
||||
return is_ok;
|
||||
}
|
||||
|
||||
bool jsmbed_wrap_register_class_constructor(const char* name, jerry_external_handler_t handler) {
|
||||
// Register class constructor as a global function
|
||||
return jsmbed_wrap_register_global_function(name, handler);
|
||||
bool
|
||||
jsmbed_wrap_register_class_constructor (const char* name, jerry_external_handler_t handler)
|
||||
{
|
||||
// Register class constructor as a global function
|
||||
return jsmbed_wrap_register_global_function (name, handler);
|
||||
}
|
||||
|
||||
bool jsmbed_wrap_register_class_function(jerry_value_t this_obj, const char* name, jerry_external_handler_t handler) {
|
||||
jerry_value_t property_name = jerry_create_string(reinterpret_cast<const jerry_char_t *>(name));
|
||||
jerry_value_t handler_obj = jerry_create_external_function(handler);
|
||||
bool
|
||||
jsmbed_wrap_register_class_function (jerry_value_t this_obj, const char* name, jerry_external_handler_t handler)
|
||||
{
|
||||
jerry_value_t property_name = jerry_string_sz (name);
|
||||
jerry_value_t handler_obj = jerry_function_external (handler);
|
||||
|
||||
jerry_release_value(jerry_set_property(this_obj, property_name, handler_obj));
|
||||
jerry_value_free (jerry_object_set (this_obj, property_name, handler_obj));
|
||||
|
||||
jerry_release_value(handler_obj);
|
||||
jerry_release_value(property_name);
|
||||
jerry_value_free (handler_obj);
|
||||
jerry_value_free (property_name);
|
||||
|
||||
// TODO: check for errors, and return false in the case of errors
|
||||
return true;
|
||||
// TODO: check for errors, and return false in the case of errors
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -19,75 +19,73 @@
|
||||
* Used in header/source files for wrappers, to declare the signature of the
|
||||
* registration function.
|
||||
*/
|
||||
#define DECLARE_JS_WRAPPER_REGISTRATION(NAME) \
|
||||
void jsmbed_wrap_registry_entry__ ## NAME (void)
|
||||
#define DECLARE_JS_WRAPPER_REGISTRATION(NAME) void jsmbed_wrap_registry_entry__##NAME (void)
|
||||
|
||||
//
|
||||
// 2. Wrapper function declaration/use macros
|
||||
//
|
||||
|
||||
// Global functions
|
||||
#define DECLARE_GLOBAL_FUNCTION(NAME) \
|
||||
jerry_value_t \
|
||||
NAME_FOR_GLOBAL_FUNCTION(NAME) (const jerry_call_info_t *call_info_p, \
|
||||
const jerry_value_t args[], \
|
||||
const jerry_length_t args_count)
|
||||
#define DECLARE_GLOBAL_FUNCTION(NAME) \
|
||||
jerry_value_t NAME_FOR_GLOBAL_FUNCTION ( \
|
||||
NAME) (const jerry_call_info_t* call_info_p, const jerry_value_t args[], const jerry_length_t args_count)
|
||||
|
||||
#define REGISTER_GLOBAL_FUNCTION(NAME) \
|
||||
jsmbed_wrap_register_global_function ( # NAME, NAME_FOR_GLOBAL_FUNCTION(NAME) )
|
||||
#define REGISTER_GLOBAL_FUNCTION(NAME) jsmbed_wrap_register_global_function (#NAME, NAME_FOR_GLOBAL_FUNCTION (NAME))
|
||||
|
||||
#define REGISTER_GLOBAL_FUNCTION_WITH_HANDLER(NAME, HANDLER) \
|
||||
jsmbed_wrap_register_global_function ( # NAME, HANDLER )
|
||||
#define REGISTER_GLOBAL_FUNCTION_WITH_HANDLER(NAME, HANDLER) jsmbed_wrap_register_global_function (#NAME, HANDLER)
|
||||
|
||||
// Class constructors
|
||||
#define DECLARE_CLASS_CONSTRUCTOR(CLASS) \
|
||||
jerry_value_t \
|
||||
NAME_FOR_CLASS_CONSTRUCTOR(CLASS) (const jerry_call_info_t *call_info_p, \
|
||||
const jerry_value_t args[], \
|
||||
const jerry_length_t args_count)
|
||||
#define DECLARE_CLASS_CONSTRUCTOR(CLASS) \
|
||||
jerry_value_t NAME_FOR_CLASS_CONSTRUCTOR ( \
|
||||
CLASS) (const jerry_call_info_t* call_info_p, const jerry_value_t args[], const jerry_length_t args_count)
|
||||
|
||||
#define REGISTER_CLASS_CONSTRUCTOR(CLASS) \
|
||||
jsmbed_wrap_register_class_constructor ( # CLASS, NAME_FOR_CLASS_CONSTRUCTOR(CLASS) )
|
||||
jsmbed_wrap_register_class_constructor (#CLASS, NAME_FOR_CLASS_CONSTRUCTOR (CLASS))
|
||||
|
||||
// Class functions
|
||||
#define DECLARE_CLASS_FUNCTION(CLASS, NAME) \
|
||||
jerry_value_t \
|
||||
NAME_FOR_CLASS_FUNCTION(CLASS, NAME) (const jerry_call_info_t *call_info_p, \
|
||||
const jerry_value_t args[], \
|
||||
const jerry_length_t args_count)
|
||||
#define DECLARE_CLASS_FUNCTION(CLASS, NAME) \
|
||||
jerry_value_t NAME_FOR_CLASS_FUNCTION (CLASS, NAME) (const jerry_call_info_t* call_info_p, \
|
||||
const jerry_value_t args[], \
|
||||
const jerry_length_t args_count)
|
||||
|
||||
#define ATTACH_CLASS_FUNCTION(OBJECT, CLASS, NAME) \
|
||||
jsmbed_wrap_register_class_function (OBJECT, # NAME, NAME_FOR_CLASS_FUNCTION(CLASS, NAME) )
|
||||
jsmbed_wrap_register_class_function (OBJECT, #NAME, NAME_FOR_CLASS_FUNCTION (CLASS, NAME))
|
||||
|
||||
//
|
||||
// 3. Argument checking macros
|
||||
//
|
||||
#define CHECK_ARGUMENT_COUNT(CLASS, NAME, EXPR) \
|
||||
if (!(EXPR)) { \
|
||||
const char* error_msg = "ERROR: wrong argument count for " # CLASS "." # NAME ", expected " # EXPR "."; \
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
|
||||
}
|
||||
#define CHECK_ARGUMENT_COUNT(CLASS, NAME, EXPR) \
|
||||
if (!(EXPR)) \
|
||||
{ \
|
||||
const char* error_msg = "ERROR: wrong argument count for " #CLASS "." #NAME ", expected " #EXPR "."; \
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, error_msg); \
|
||||
}
|
||||
|
||||
#define CHECK_ARGUMENT_TYPE_ALWAYS(CLASS, NAME, INDEX, TYPE) \
|
||||
if (!jerry_value_is_ ## TYPE (args[INDEX])) { \
|
||||
const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
|
||||
}
|
||||
#define CHECK_ARGUMENT_TYPE_ALWAYS(CLASS, NAME, INDEX, TYPE) \
|
||||
if (!jerry_value_is_##TYPE (args[INDEX])) \
|
||||
{ \
|
||||
const char* error_msg = \
|
||||
"ERROR: wrong argument type for " #CLASS "." #NAME ", expected argument " #INDEX " to be a " #TYPE ".\n"; \
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, error_msg); \
|
||||
}
|
||||
|
||||
#define CHECK_ARGUMENT_TYPE_ON_CONDITION(CLASS, NAME, INDEX, TYPE, EXPR) \
|
||||
if ((EXPR)) { \
|
||||
if (!jerry_value_is_ ## TYPE (args[INDEX])) { \
|
||||
const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
|
||||
} \
|
||||
}
|
||||
#define CHECK_ARGUMENT_TYPE_ON_CONDITION(CLASS, NAME, INDEX, TYPE, EXPR) \
|
||||
if ((EXPR)) \
|
||||
{ \
|
||||
if (!jerry_value_is_##TYPE (args[INDEX])) \
|
||||
{ \
|
||||
const char* error_msg = \
|
||||
"ERROR: wrong argument type for " #CLASS "." #NAME ", expected argument " #INDEX " to be a " #TYPE ".\n"; \
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, error_msg); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define NAME_FOR_GLOBAL_FUNCTION(NAME) __gen_jsmbed_global_func_ ## NAME
|
||||
#define NAME_FOR_CLASS_CONSTRUCTOR(CLASS) __gen_jsmbed_class_constructor_ ## CLASS
|
||||
#define NAME_FOR_CLASS_FUNCTION(CLASS, NAME) __gen_jsmbed_func_c_ ## CLASS ## _f_ ## NAME
|
||||
#define NAME_FOR_GLOBAL_FUNCTION(NAME) __gen_jsmbed_global_func_##NAME
|
||||
#define NAME_FOR_CLASS_CONSTRUCTOR(CLASS) __gen_jsmbed_class_constructor_##CLASS
|
||||
#define NAME_FOR_CLASS_FUNCTION(CLASS, NAME) __gen_jsmbed_func_c_##CLASS##_f_##NAME
|
||||
|
||||
#define NAME_FOR_CLASS_NATIVE_CONSTRUCTOR(CLASS, TYPELIST) __gen_native_jsmbed_ ## CLASS ## __Special_create_ ## TYPELIST
|
||||
#define NAME_FOR_CLASS_NATIVE_DESTRUCTOR(CLASS) __gen_native_jsmbed_ ## CLASS ## __Special_destroy
|
||||
#define NAME_FOR_CLASS_NATIVE_FUNCTION(CLASS, NAME) __gen_native_jsmbed_ ## CLASS ## _ ## NAME
|
||||
#define NAME_FOR_CLASS_NATIVE_CONSTRUCTOR(CLASS, TYPELIST) __gen_native_jsmbed_##CLASS##__Special_create_##TYPELIST
|
||||
#define NAME_FOR_CLASS_NATIVE_DESTRUCTOR(CLASS) __gen_native_jsmbed_##CLASS##__Special_destroy
|
||||
#define NAME_FOR_CLASS_NATIVE_FUNCTION(CLASS, NAME) __gen_native_jsmbed_##CLASS##_##NAME
|
||||
|
||||
#endif // _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H
|
||||
#endif // _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H
|
||||
|
||||
+114
-133
@@ -15,13 +15,14 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "jerryscript-ext/debugger.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "setjmp.h"
|
||||
|
||||
/**
|
||||
@@ -111,7 +112,7 @@ read_file (const char *file_name, /**< source code */
|
||||
if (!bytes_read || bytes_read != script_len)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
|
||||
free ((void*) buffer);
|
||||
free ((void *) buffer);
|
||||
|
||||
fclose (file);
|
||||
return NULL;
|
||||
@@ -132,7 +133,7 @@ static uint32_t
|
||||
str_to_uint (const char *num_str_p, /**< string to convert */
|
||||
char **out_p) /**< [out] end of the number */
|
||||
{
|
||||
assert (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES));
|
||||
assert (jerry_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES));
|
||||
|
||||
uint32_t result = 0;
|
||||
|
||||
@@ -157,124 +158,111 @@ str_to_uint (const char *num_str_p, /**< string to convert */
|
||||
static void
|
||||
print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
{
|
||||
assert (jerry_value_is_error (error_value));
|
||||
assert (jerry_value_is_exception (error_value));
|
||||
|
||||
error_value = jerry_get_value_from_error (error_value, false);
|
||||
error_value = jerry_exception_value (error_value, false);
|
||||
jerry_value_t err_str_val = jerry_value_to_string (error_value);
|
||||
jerry_size_t err_str_size = jerry_get_utf8_string_size (err_str_val);
|
||||
jerry_char_t err_str_buf[256];
|
||||
|
||||
jerry_release_value (error_value);
|
||||
jerry_value_free (error_value);
|
||||
|
||||
if (err_str_size >= 256)
|
||||
{
|
||||
const char msg[] = "[Error message too long]";
|
||||
err_str_size = sizeof (msg) / sizeof (char) - 1;
|
||||
memcpy (err_str_buf, msg, err_str_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size);
|
||||
assert (sz == err_str_size);
|
||||
err_str_buf[err_str_size] = 0;
|
||||
jerry_size_t sz = jerry_string_to_buffer (err_str_val, JERRY_ENCODING_UTF8, err_str_buf, sizeof (err_str_buf) - 1);
|
||||
err_str_buf[sz] = '\0';
|
||||
|
||||
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES)
|
||||
&& jerry_get_error_type (error_value) == JERRY_ERROR_SYNTAX)
|
||||
if (jerry_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES) && jerry_error_type (error_value) == JERRY_ERROR_SYNTAX)
|
||||
{
|
||||
jerry_char_t *string_end_p = err_str_buf + sz;
|
||||
uint32_t err_line = 0;
|
||||
uint32_t err_col = 0;
|
||||
char *path_str_p = NULL;
|
||||
char *path_str_end_p = NULL;
|
||||
|
||||
/* 1. parse column and line information */
|
||||
for (jerry_char_t *current_p = err_str_buf; current_p < string_end_p; current_p++)
|
||||
{
|
||||
jerry_char_t *string_end_p = err_str_buf + sz;
|
||||
uint32_t err_line = 0;
|
||||
uint32_t err_col = 0;
|
||||
char *path_str_p = NULL;
|
||||
char *path_str_end_p = NULL;
|
||||
|
||||
/* 1. parse column and line information */
|
||||
for (jerry_char_t *current_p = err_str_buf; current_p < string_end_p; current_p++)
|
||||
if (*current_p == '[')
|
||||
{
|
||||
if (*current_p == '[')
|
||||
current_p++;
|
||||
|
||||
if (*current_p == '<')
|
||||
{
|
||||
current_p++;
|
||||
|
||||
if (*current_p == '<')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
path_str_p = (char *) current_p;
|
||||
while (current_p < string_end_p && *current_p != ':')
|
||||
{
|
||||
current_p++;
|
||||
}
|
||||
|
||||
path_str_end_p = (char *) current_p++;
|
||||
|
||||
err_line = str_to_uint ((char *) current_p, (char **) ¤t_p);
|
||||
|
||||
current_p++;
|
||||
|
||||
err_col = str_to_uint ((char *) current_p, NULL);
|
||||
break;
|
||||
}
|
||||
} /* for */
|
||||
|
||||
if (err_line != 0 && err_col != 0)
|
||||
{
|
||||
uint32_t curr_line = 1;
|
||||
|
||||
bool is_printing_context = false;
|
||||
uint32_t pos = 0;
|
||||
|
||||
/* Temporarily modify the error message, so we can use the path. */
|
||||
*path_str_end_p = '\0';
|
||||
|
||||
size_t source_size;
|
||||
const jerry_char_t *source_p = read_file (path_str_p, &source_size);
|
||||
|
||||
/* Revert the error message. */
|
||||
*path_str_end_p = ':';
|
||||
|
||||
/* 2. seek and print */
|
||||
while (source_p[pos] != '\0')
|
||||
path_str_p = (char *) current_p;
|
||||
while (current_p < string_end_p && *current_p != ':')
|
||||
{
|
||||
if (source_p[pos] == '\n')
|
||||
{
|
||||
curr_line++;
|
||||
}
|
||||
|
||||
if (err_line < SYNTAX_ERROR_CONTEXT_SIZE
|
||||
|| (err_line >= curr_line
|
||||
&& (err_line - curr_line) <= SYNTAX_ERROR_CONTEXT_SIZE))
|
||||
{
|
||||
/* context must be printed */
|
||||
is_printing_context = true;
|
||||
}
|
||||
|
||||
if (curr_line > err_line)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_printing_context)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%c", source_p[pos]);
|
||||
}
|
||||
|
||||
pos++;
|
||||
current_p++;
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "\n");
|
||||
path_str_end_p = (char *) current_p++;
|
||||
|
||||
while (--err_col)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "~");
|
||||
}
|
||||
err_line = str_to_uint ((char *) current_p, (char **) ¤t_p);
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "^\n");
|
||||
current_p++;
|
||||
|
||||
err_col = str_to_uint ((char *) current_p, NULL);
|
||||
break;
|
||||
}
|
||||
} /* for */
|
||||
|
||||
if (err_line != 0 && err_col != 0)
|
||||
{
|
||||
uint32_t curr_line = 1;
|
||||
|
||||
bool is_printing_context = false;
|
||||
uint32_t pos = 0;
|
||||
|
||||
/* Temporarily modify the error message, so we can use the path. */
|
||||
*path_str_end_p = '\0';
|
||||
|
||||
size_t source_size;
|
||||
const jerry_char_t *source_p = read_file (path_str_p, &source_size);
|
||||
|
||||
/* Revert the error message. */
|
||||
*path_str_end_p = ':';
|
||||
|
||||
/* 2. seek and print */
|
||||
while (source_p[pos] != '\0')
|
||||
{
|
||||
if (source_p[pos] == '\n')
|
||||
{
|
||||
curr_line++;
|
||||
}
|
||||
|
||||
if (err_line < SYNTAX_ERROR_CONTEXT_SIZE
|
||||
|| (err_line >= curr_line && (err_line - curr_line) <= SYNTAX_ERROR_CONTEXT_SIZE))
|
||||
{
|
||||
/* context must be printed */
|
||||
is_printing_context = true;
|
||||
}
|
||||
|
||||
if (curr_line > err_line)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_printing_context)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%c", source_p[pos]);
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "\n");
|
||||
|
||||
while (--err_col)
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "~");
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "^\n");
|
||||
}
|
||||
}
|
||||
|
||||
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: %s\n", err_str_buf);
|
||||
jerry_release_value (err_str_val);
|
||||
jerry_value_free (err_str_val);
|
||||
} /* print_unhandled_exception */
|
||||
|
||||
/**
|
||||
@@ -284,26 +272,27 @@ static void
|
||||
register_js_function (const char *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
|
||||
jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);
|
||||
|
||||
if (jerry_value_is_error (result_val))
|
||||
if (jerry_value_is_exception (result_val))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p);
|
||||
}
|
||||
|
||||
jerry_release_value (result_val);
|
||||
jerry_value_free (result_val);
|
||||
} /* register_js_function */
|
||||
|
||||
|
||||
/**
|
||||
* Main program.
|
||||
*
|
||||
* @return 0 if success, error code otherwise
|
||||
*/
|
||||
#ifdef CONFIG_BUILD_KERNEL
|
||||
int main (int argc, FAR char *argv[])
|
||||
int
|
||||
main (int argc, FAR char *argv[])
|
||||
#else /* !defined(CONFIG_BUILD_KERNEL) */
|
||||
int jerry_main (int argc, char *argv[])
|
||||
int
|
||||
jerry_main (int argc, char *argv[])
|
||||
#endif /* defined(CONFIG_BUILD_KERNEL) */
|
||||
{
|
||||
if (argc > JERRY_MAX_COMMAND_LINE_ARGS)
|
||||
@@ -335,11 +324,6 @@ int jerry_main (int argc, char *argv[])
|
||||
flags |= JERRY_INIT_MEM_STATS;
|
||||
set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
}
|
||||
else if (!strcmp ("--mem-stats-separate", argv[i]))
|
||||
{
|
||||
flags |= JERRY_INIT_MEM_STATS_SEPARATE;
|
||||
set_log_level (JERRY_LOG_LEVEL_DEBUG);
|
||||
}
|
||||
else if (!strcmp ("--show-opcodes", argv[i]))
|
||||
{
|
||||
flags |= JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES;
|
||||
@@ -347,7 +331,7 @@ int jerry_main (int argc, char *argv[])
|
||||
}
|
||||
else if (!strcmp ("--log-level", argv[i]))
|
||||
{
|
||||
if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >='0' && argv[i][0] <= '3')
|
||||
if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >= '0' && argv[i][0] <= '3')
|
||||
{
|
||||
set_log_level (argv[i][0] - '0');
|
||||
}
|
||||
@@ -383,15 +367,14 @@ int jerry_main (int argc, char *argv[])
|
||||
|
||||
if (start_debug_server)
|
||||
{
|
||||
jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port)
|
||||
&& jerryx_debugger_ws_create ());
|
||||
jerryx_debugger_after_connect (jerryx_debugger_tcp_create (debug_port) && jerryx_debugger_ws_create ());
|
||||
}
|
||||
|
||||
register_js_function ("assert", jerryx_handler_assert);
|
||||
register_js_function ("gc", jerryx_handler_gc);
|
||||
register_js_function ("print", jerryx_handler_print);
|
||||
|
||||
jerry_value_t ret_value = jerry_create_undefined ();
|
||||
jerry_value_t ret_value = jerry_undefined ();
|
||||
|
||||
if (files_counter == 0)
|
||||
{
|
||||
@@ -400,7 +383,7 @@ int jerry_main (int argc, char *argv[])
|
||||
|
||||
ret_value = jerry_parse (script, sizeof (script) - 1, NULL);
|
||||
|
||||
if (!jerry_value_is_error (ret_value))
|
||||
if (!jerry_value_is_exception (ret_value))
|
||||
{
|
||||
ret_value = jerry_run (ret_value);
|
||||
}
|
||||
@@ -419,50 +402,48 @@ int jerry_main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
jerry_parse_options_t parse_options;
|
||||
parse_options.options = JERRY_PARSE_HAS_RESOURCE;
|
||||
parse_options.resource_name = jerry_create_string ((const jerry_char_t *) file_names[i]);
|
||||
parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME;
|
||||
parse_options.source_name = jerry_string_sz (file_names[i]);
|
||||
|
||||
ret_value = jerry_parse (source_p,
|
||||
source_size,
|
||||
&parse_options);
|
||||
jerry_release_value (parse_options.resource_name);
|
||||
free ((void*) source_p);
|
||||
ret_value = jerry_parse (source_p, source_size, &parse_options);
|
||||
jerry_value_free (parse_options.source_name);
|
||||
free ((void *) source_p);
|
||||
|
||||
if (!jerry_value_is_error (ret_value))
|
||||
if (!jerry_value_is_exception (ret_value))
|
||||
{
|
||||
jerry_value_t func_val = ret_value;
|
||||
ret_value = jerry_run (func_val);
|
||||
jerry_release_value (func_val);
|
||||
jerry_value_free (func_val);
|
||||
}
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
print_unhandled_exception (ret_value);
|
||||
break;
|
||||
}
|
||||
|
||||
jerry_release_value (ret_value);
|
||||
ret_value = jerry_create_undefined ();
|
||||
jerry_value_free (ret_value);
|
||||
ret_value = jerry_undefined ();
|
||||
}
|
||||
}
|
||||
|
||||
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
|
||||
ret_value = jerry_run_all_enqueued_jobs ();
|
||||
ret_value = jerry_run_jobs ();
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
jerry_cleanup ();
|
||||
|
||||
return ret_code;
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
/**
|
||||
* Computes the end of the directory part of a path.
|
||||
@@ -102,8 +102,7 @@ typedef struct jerry_port_module_t
|
||||
/**
|
||||
* Native info descriptor for modules.
|
||||
*/
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info =
|
||||
{
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info = {
|
||||
.free_cb = NULL,
|
||||
};
|
||||
|
||||
@@ -136,8 +135,8 @@ jerry_port_module_free (jerry_port_module_manager_t *manager_p, /**< module mana
|
||||
if (release_all || module_p->realm == realm)
|
||||
{
|
||||
free (module_p->path_p);
|
||||
jerry_release_value (module_p->realm);
|
||||
jerry_release_value (module_p->module);
|
||||
jerry_value_free (module_p->realm);
|
||||
jerry_value_free (module_p->module);
|
||||
|
||||
free (module_p);
|
||||
|
||||
@@ -174,20 +173,18 @@ jerry_port_module_manager_init (void *user_data_p)
|
||||
static void
|
||||
jerry_port_module_manager_deinit (void *user_data_p) /**< context pointer to deinitialize */
|
||||
{
|
||||
jerry_value_t undef = jerry_create_undefined ();
|
||||
jerry_value_t undef = jerry_undefined ();
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) user_data_p, undef);
|
||||
jerry_release_value (undef);
|
||||
jerry_value_free (undef);
|
||||
} /* jerry_port_module_manager_deinit */
|
||||
|
||||
/**
|
||||
* Declare the context data manager for modules.
|
||||
*/
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager =
|
||||
{
|
||||
.init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed = sizeof (jerry_port_module_manager_t)
|
||||
};
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager = { .init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed =
|
||||
sizeof (jerry_port_module_manager_t) };
|
||||
|
||||
/**
|
||||
* Default module resolver.
|
||||
@@ -201,44 +198,43 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
(void) user_p;
|
||||
|
||||
jerry_port_module_t *module_p;
|
||||
const jerry_char_t *base_path_p = NULL;
|
||||
size_t base_path_length = 0;
|
||||
jerry_port_module_t *module_p = jerry_object_get_native_ptr (referrer, &jerry_port_module_native_info);
|
||||
|
||||
if (jerry_get_object_native_pointer (referrer, (void **) &module_p, &jerry_port_module_native_info))
|
||||
if (module_p != NULL)
|
||||
{
|
||||
base_path_p = module_p->path_p;
|
||||
base_path_length = module_p->base_path_length;
|
||||
}
|
||||
|
||||
jerry_size_t in_path_length = jerry_get_utf8_string_size (specifier);
|
||||
jerry_size_t in_path_length = jerry_string_size (specifier, JERRY_ENCODING_UTF8);
|
||||
jerry_char_t *in_path_p = (jerry_char_t *) malloc (in_path_length + 1);
|
||||
jerry_string_to_utf8_char_buffer (specifier, in_path_p, in_path_length);
|
||||
jerry_string_to_buffer (specifier, JERRY_ENCODING_UTF8, in_path_p, in_path_length);
|
||||
in_path_p[in_path_length] = '\0';
|
||||
|
||||
jerry_char_t *path_p = jerry_port_normalize_path (in_path_p, in_path_length, base_path_p, base_path_length);
|
||||
|
||||
if (path_p == NULL)
|
||||
{
|
||||
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Out of memory");
|
||||
return jerry_throw_sz (JERRY_ERROR_COMMON, "Out of memory");
|
||||
}
|
||||
|
||||
jerry_value_t realm = jerry_get_global_object ();
|
||||
jerry_value_t realm = jerry_current_realm ();
|
||||
|
||||
jerry_port_module_manager_t *manager_p;
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager);
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager);
|
||||
|
||||
module_p = manager_p->module_head_p;
|
||||
|
||||
while (module_p != NULL)
|
||||
{
|
||||
if (module_p->realm == realm
|
||||
&& strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
if (module_p->realm == realm && strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
return jerry_acquire_value (module_p->module);
|
||||
jerry_value_free (realm);
|
||||
return jerry_value_copy (module_p->module);
|
||||
}
|
||||
|
||||
module_p = module_p->next_p;
|
||||
@@ -251,28 +247,25 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
/* TODO: This is incorrect, but makes test262 module tests pass
|
||||
* (they should throw SyntaxError, but not because the module cannot be found). */
|
||||
return jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "Module file not found");
|
||||
jerry_value_free (realm);
|
||||
|
||||
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found");
|
||||
}
|
||||
|
||||
jerry_parse_options_t parse_options;
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_RESOURCE;
|
||||
parse_options.resource_name = jerry_create_string_sz ((const jerry_char_t *) in_path_p, in_path_length);
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME;
|
||||
parse_options.source_name = jerry_string (in_path_p, in_path_length, JERRY_ENCODING_UTF8);
|
||||
|
||||
jerry_value_t ret_value = jerry_parse (source_p,
|
||||
source_size,
|
||||
&parse_options);
|
||||
jerry_value_t ret_value = jerry_parse (source_p, source_size, &parse_options);
|
||||
|
||||
jerry_release_value (parse_options.resource_name);
|
||||
jerry_value_free (parse_options.source_name);
|
||||
jerry_port_release_source (source_p);
|
||||
free (in_path_p);
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
free (path_p);
|
||||
jerry_release_value (realm);
|
||||
jerry_value_free (realm);
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
@@ -282,9 +275,9 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
module_p->path_p = path_p;
|
||||
module_p->base_path_length = jerry_port_get_directory_end (module_p->path_p);
|
||||
module_p->realm = realm;
|
||||
module_p->module = jerry_acquire_value (ret_value);
|
||||
module_p->module = jerry_value_copy (ret_value);
|
||||
|
||||
jerry_set_object_native_pointer (ret_value, module_p, &jerry_port_module_native_info);
|
||||
jerry_object_set_native_ptr (ret_value, &jerry_port_module_native_info, module_p);
|
||||
manager_p->module_head_p = module_p;
|
||||
|
||||
return ret_value;
|
||||
@@ -297,6 +290,5 @@ void
|
||||
jerry_port_module_release (const jerry_value_t realm) /**< if this argument is object, release only those modules,
|
||||
* which realm value is equal to this argument. */
|
||||
{
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager),
|
||||
realm);
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager), realm);
|
||||
} /* jerry_port_module_release */
|
||||
|
||||
@@ -30,16 +30,16 @@ set_led (const jerry_value_t func_value, /**< function object */
|
||||
if (args_cnt != 2)
|
||||
{
|
||||
Serial.println ("Wrong arguments count in 'test.setLed' function.");
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
}
|
||||
|
||||
int ledPin = jerry_get_number_value (args_p[0]);
|
||||
int ledPin = jerry_value_as_number (args_p[0]);
|
||||
bool value = jerry_value_is_true (args_p[1]);
|
||||
|
||||
pinMode (ledPin, OUTPUT);
|
||||
digitalWrite (ledPin, value);
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
} /* set_led */
|
||||
|
||||
/**
|
||||
@@ -54,14 +54,14 @@ js_delay (const jerry_value_t func_value, /**< function object */
|
||||
if (args_cnt != 1)
|
||||
{
|
||||
Serial.println ("Wrong arguments count in 'test.delay' function.");
|
||||
return jerry_create_boolean (false);
|
||||
return jerry_boolean (false);
|
||||
}
|
||||
|
||||
int millisec = jerry_get_number_value (args_p[0]);
|
||||
int millisec = jerry_value_as_number (args_p[0]);
|
||||
|
||||
delay (millisec);
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
} /* js_delay */
|
||||
|
||||
/*
|
||||
@@ -73,32 +73,32 @@ init_jerry ()
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
/* Create an empty JS object */
|
||||
jerry_value_t object = jerry_create_object ();
|
||||
jerry_value_t object = jerry_object ();
|
||||
|
||||
jerry_value_t func_obj;
|
||||
jerry_value_t prop_name;
|
||||
|
||||
func_obj = jerry_create_external_function (set_led);
|
||||
prop_name = jerry_create_string ((const jerry_char_t *) "setLed");
|
||||
jerry_release_value (jerry_set_property (object, prop_name, func_obj));
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (func_obj);
|
||||
func_obj = jerry_function_external (set_led);
|
||||
prop_name = jerry_string_sz ("setLed");
|
||||
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (func_obj);
|
||||
|
||||
func_obj = jerry_create_external_function (js_delay);
|
||||
prop_name = jerry_create_string ((const jerry_char_t *) "delay");
|
||||
jerry_release_value (jerry_set_property (object, prop_name, func_obj));
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (func_obj);
|
||||
func_obj = jerry_function_external (js_delay);
|
||||
prop_name = jerry_string_sz ("delay");
|
||||
jerry_value_free (jerry_object_set (object, prop_name, func_obj));
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (func_obj);
|
||||
|
||||
/* Wrap the JS object (not empty anymore) into a jerry api value */
|
||||
jerry_value_t global_object = jerry_get_global_object ();
|
||||
jerry_value_t global_object = jerry_current_realm ();
|
||||
|
||||
/* Add the JS object to the global context */
|
||||
prop_name = jerry_create_string ((const jerry_char_t *) "test");
|
||||
jerry_release_value (jerry_set_property (global_object, prop_name, object));
|
||||
jerry_release_value (prop_name);
|
||||
jerry_release_value (object);
|
||||
jerry_release_value (global_object);
|
||||
prop_name = jerry_string_sz ("test");
|
||||
jerry_value_free (jerry_object_set (global_object, prop_name, object));
|
||||
jerry_value_free (prop_name);
|
||||
jerry_value_free (object);
|
||||
jerry_value_free (global_object);
|
||||
} /* init_jerry */
|
||||
|
||||
/**
|
||||
@@ -117,7 +117,7 @@ test_jerry ()
|
||||
jerry_value_t eval_ret = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
|
||||
|
||||
/* Free JavaScript value, returned by eval */
|
||||
jerry_release_value (eval_ret);
|
||||
jerry_value_free (eval_ret);
|
||||
} /* test_jerry */
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "shell.h"
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include "shell.h"
|
||||
|
||||
/**
|
||||
* Standalone Jerry exit codes
|
||||
@@ -34,26 +36,27 @@ static void
|
||||
register_js_function (const char *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
|
||||
jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);
|
||||
|
||||
if (jerry_value_is_error (result_val))
|
||||
if (jerry_value_is_exception (result_val))
|
||||
{
|
||||
printf ("Warning: failed to register '%s' method.", name_p);
|
||||
}
|
||||
|
||||
jerry_release_value (result_val);
|
||||
jerry_value_free (result_val);
|
||||
} /* register_js_function */
|
||||
|
||||
/**
|
||||
* Jerryscript simple test
|
||||
*/
|
||||
int test_jerry (int argc, char **argv)
|
||||
int
|
||||
test_jerry (int argc, char **argv)
|
||||
{
|
||||
/* Suppress compiler errors */
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
jerry_value_t ret_value = jerry_create_undefined ();
|
||||
jerry_value_t ret_value = jerry_undefined ();
|
||||
|
||||
const jerry_char_t script[] = "print ('Hello, World!');";
|
||||
printf ("This test run the following script code: [%s]\n\n", script);
|
||||
@@ -67,7 +70,7 @@ int test_jerry (int argc, char **argv)
|
||||
/* Setup Global scope code */
|
||||
ret_value = jerry_parse (script, sizeof (script) - 1, NULL);
|
||||
|
||||
if (!jerry_value_is_error (ret_value))
|
||||
if (!jerry_value_is_exception (ret_value))
|
||||
{
|
||||
/* Execute the parsed source code in the Global scope */
|
||||
ret_value = jerry_run (ret_value);
|
||||
@@ -75,14 +78,14 @@ int test_jerry (int argc, char **argv)
|
||||
|
||||
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
printf ("Script Error!");
|
||||
|
||||
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
|
||||
}
|
||||
|
||||
jerry_release_value (ret_value);
|
||||
jerry_value_free (ret_value);
|
||||
|
||||
/* Cleanup engine */
|
||||
jerry_cleanup ();
|
||||
@@ -91,14 +94,17 @@ int test_jerry (int argc, char **argv)
|
||||
|
||||
} /* test_jerry */
|
||||
|
||||
const shell_command_t shell_commands[] = {
|
||||
{ "test", "Jerryscript Hello World test", test_jerry },
|
||||
{ NULL, NULL, NULL }
|
||||
};
|
||||
const shell_command_t shell_commands[] = { { "test", "Jerryscript Hello World test", test_jerry },
|
||||
{ NULL, NULL, NULL } };
|
||||
|
||||
int main (void)
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
srand (now.u);
|
||||
printf ("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
|
||||
printf ("This board features a(n) %s MCU.\n", RIOT_MCU);
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
/**
|
||||
* Computes the end of the directory part of a path.
|
||||
@@ -102,8 +102,7 @@ typedef struct jerry_port_module_t
|
||||
/**
|
||||
* Native info descriptor for modules.
|
||||
*/
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info =
|
||||
{
|
||||
static const jerry_object_native_info_t jerry_port_module_native_info = {
|
||||
.free_cb = NULL,
|
||||
};
|
||||
|
||||
@@ -136,8 +135,8 @@ jerry_port_module_free (jerry_port_module_manager_t *manager_p, /**< module mana
|
||||
if (release_all || module_p->realm == realm)
|
||||
{
|
||||
free (module_p->path_p);
|
||||
jerry_release_value (module_p->realm);
|
||||
jerry_release_value (module_p->module);
|
||||
jerry_value_free (module_p->realm);
|
||||
jerry_value_free (module_p->module);
|
||||
|
||||
free (module_p);
|
||||
|
||||
@@ -174,20 +173,18 @@ jerry_port_module_manager_init (void *user_data_p)
|
||||
static void
|
||||
jerry_port_module_manager_deinit (void *user_data_p) /**< context pointer to deinitialize */
|
||||
{
|
||||
jerry_value_t undef = jerry_create_undefined ();
|
||||
jerry_value_t undef = jerry_undefined ();
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) user_data_p, undef);
|
||||
jerry_release_value (undef);
|
||||
jerry_value_free (undef);
|
||||
} /* jerry_port_module_manager_deinit */
|
||||
|
||||
/**
|
||||
* Declare the context data manager for modules.
|
||||
*/
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager =
|
||||
{
|
||||
.init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed = sizeof (jerry_port_module_manager_t)
|
||||
};
|
||||
static const jerry_context_data_manager_t jerry_port_module_manager = { .init_cb = jerry_port_module_manager_init,
|
||||
.deinit_cb = jerry_port_module_manager_deinit,
|
||||
.bytes_needed =
|
||||
sizeof (jerry_port_module_manager_t) };
|
||||
|
||||
/**
|
||||
* Default module resolver.
|
||||
@@ -201,44 +198,43 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
(void) user_p;
|
||||
|
||||
jerry_port_module_t *module_p;
|
||||
jerry_port_module_t *module_p = jerry_object_get_native_ptr (referrer, &jerry_port_module_native_info);
|
||||
const jerry_char_t *base_path_p = NULL;
|
||||
size_t base_path_length = 0;
|
||||
|
||||
if (jerry_get_object_native_pointer (referrer, (void **) &module_p, &jerry_port_module_native_info))
|
||||
if (module_p != NULL)
|
||||
{
|
||||
base_path_p = module_p->path_p;
|
||||
base_path_length = module_p->base_path_length;
|
||||
}
|
||||
|
||||
jerry_size_t in_path_length = jerry_get_utf8_string_size (specifier);
|
||||
jerry_size_t in_path_length = jerry_string_size (specifier, JERRY_ENCODING_UTF8);
|
||||
jerry_char_t *in_path_p = (jerry_char_t *) malloc (in_path_length + 1);
|
||||
jerry_string_to_utf8_char_buffer (specifier, in_path_p, in_path_length);
|
||||
jerry_string_to_buffer (specifier, JERRY_ENCODING_UTF8, in_path_p, in_path_length);
|
||||
in_path_p[in_path_length] = '\0';
|
||||
|
||||
jerry_char_t *path_p = jerry_port_normalize_path (in_path_p, in_path_length, base_path_p, base_path_length);
|
||||
|
||||
if (path_p == NULL)
|
||||
{
|
||||
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Out of memory");
|
||||
return jerry_throw_sz (JERRY_ERROR_COMMON, "Out of memory");
|
||||
}
|
||||
|
||||
jerry_value_t realm = jerry_get_global_object ();
|
||||
jerry_value_t realm = jerry_current_realm ();
|
||||
|
||||
jerry_port_module_manager_t *manager_p;
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager);
|
||||
manager_p = (jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager);
|
||||
|
||||
module_p = manager_p->module_head_p;
|
||||
|
||||
while (module_p != NULL)
|
||||
{
|
||||
if (module_p->realm == realm
|
||||
&& strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
if (module_p->realm == realm && strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
return jerry_acquire_value (module_p->module);
|
||||
jerry_value_free (realm);
|
||||
return jerry_value_copy (module_p->module);
|
||||
}
|
||||
|
||||
module_p = module_p->next_p;
|
||||
@@ -251,28 +247,24 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
{
|
||||
free (path_p);
|
||||
free (in_path_p);
|
||||
jerry_release_value (realm);
|
||||
/* TODO: This is incorrect, but makes test262 module tests pass
|
||||
* (they should throw SyntaxError, but not because the module cannot be found). */
|
||||
return jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "Module file not found");
|
||||
jerry_value_free (realm);
|
||||
return jerry_throw_sz (JERRY_ERROR_SYNTAX, "Module file not found");
|
||||
}
|
||||
|
||||
jerry_parse_options_t parse_options;
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_RESOURCE;
|
||||
parse_options.resource_name = jerry_create_string_sz ((const jerry_char_t *) in_path_p, in_path_length);
|
||||
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME;
|
||||
parse_options.source_name = jerry_string ((const jerry_char_t *) in_path_p, in_path_length, JERRY_ENCODING_UTF8);
|
||||
|
||||
jerry_value_t ret_value = jerry_parse (source_p,
|
||||
source_size,
|
||||
&parse_options);
|
||||
jerry_value_t ret_value = jerry_parse (source_p, source_size, &parse_options);
|
||||
|
||||
jerry_release_value (parse_options.resource_name);
|
||||
jerry_value_free (parse_options.source_name);
|
||||
jerry_port_release_source (source_p);
|
||||
free (in_path_p);
|
||||
|
||||
if (jerry_value_is_error (ret_value))
|
||||
if (jerry_value_is_exception (ret_value))
|
||||
{
|
||||
free (path_p);
|
||||
jerry_release_value (realm);
|
||||
jerry_value_free (realm);
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
@@ -282,9 +274,9 @@ jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier
|
||||
module_p->path_p = path_p;
|
||||
module_p->base_path_length = jerry_port_get_directory_end (module_p->path_p);
|
||||
module_p->realm = realm;
|
||||
module_p->module = jerry_acquire_value (ret_value);
|
||||
module_p->module = jerry_value_copy (ret_value);
|
||||
|
||||
jerry_set_object_native_pointer (ret_value, module_p, &jerry_port_module_native_info);
|
||||
jerry_object_set_native_ptr (ret_value, &jerry_port_module_native_info, module_p);
|
||||
manager_p->module_head_p = module_p;
|
||||
|
||||
return ret_value;
|
||||
@@ -297,6 +289,5 @@ void
|
||||
jerry_port_module_release (const jerry_value_t realm) /**< if this argument is object, release only those modules,
|
||||
* which realm value is equal to this argument. */
|
||||
{
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager),
|
||||
realm);
|
||||
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_context_data (&jerry_port_module_manager), realm);
|
||||
} /* jerry_port_module_release */
|
||||
|
||||
@@ -14,16 +14,16 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <zephyr.h>
|
||||
#include <sys/printk.h>
|
||||
#include "getline-zephyr.h"
|
||||
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jerryscript.h"
|
||||
|
||||
#include "getline-zephyr.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
#include <sys/printk.h>
|
||||
|
||||
static jerry_value_t print_function;
|
||||
|
||||
@@ -34,25 +34,24 @@ static void
|
||||
register_js_function (const char *name_p, /**< name of the function */
|
||||
jerry_external_handler_t handler_p) /**< function callback */
|
||||
{
|
||||
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
|
||||
jerry_value_t result_val = jerryx_handler_register_global (name_p, handler_p);
|
||||
|
||||
if (jerry_value_is_error (result_val))
|
||||
if (jerry_value_is_exception (result_val))
|
||||
{
|
||||
jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p);
|
||||
}
|
||||
|
||||
jerry_release_value (result_val);
|
||||
jerry_value_free (result_val);
|
||||
} /* register_js_function */
|
||||
|
||||
static int shell_cmd_handler (char *source_buffer)
|
||||
static int
|
||||
shell_cmd_handler (char *source_buffer)
|
||||
{
|
||||
jerry_value_t ret_val;
|
||||
|
||||
ret_val = jerry_eval ((jerry_char_t *) source_buffer,
|
||||
strlen (source_buffer),
|
||||
JERRY_PARSE_NO_OPTS);
|
||||
ret_val = jerry_eval ((jerry_char_t *) source_buffer, strlen (source_buffer), JERRY_PARSE_NO_OPTS);
|
||||
|
||||
if (jerry_value_is_error (ret_val))
|
||||
if (jerry_value_is_exception (ret_val))
|
||||
{
|
||||
/* User-friendly error messages require at least "cp" JerryScript
|
||||
profile. Include a message prefix in case "cp_minimal" profile
|
||||
@@ -60,44 +59,47 @@ static int shell_cmd_handler (char *source_buffer)
|
||||
printf ("Error executing statement: ");
|
||||
/* Clear error flag, otherwise print call below won't produce any
|
||||
output. */
|
||||
ret_val = jerry_get_value_from_error (ret_val, true);
|
||||
ret_val = jerry_exception_value (ret_val, true);
|
||||
}
|
||||
|
||||
if (!jerry_value_is_error (print_function))
|
||||
if (!jerry_value_is_exception (print_function))
|
||||
{
|
||||
jerry_value_t ret_val_print = jerry_call_function (print_function,
|
||||
jerry_create_undefined (),
|
||||
&ret_val,
|
||||
1);
|
||||
jerry_release_value (ret_val_print);
|
||||
jerry_value_t ret_val_print = jerry_call (print_function, jerry_undefined (), &ret_val, 1);
|
||||
jerry_value_free (ret_val_print);
|
||||
}
|
||||
|
||||
jerry_release_value (ret_val);
|
||||
jerry_value_free (ret_val);
|
||||
|
||||
return 0;
|
||||
} /* shell_cmd_handler */
|
||||
|
||||
void main (void)
|
||||
void
|
||||
main (void)
|
||||
{
|
||||
union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
|
||||
union
|
||||
{
|
||||
double d;
|
||||
unsigned u;
|
||||
} now = { .d = jerry_port_get_current_time () };
|
||||
srand (now.u);
|
||||
uint32_t zephyr_ver = sys_kernel_version_get ();
|
||||
printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
|
||||
printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION);
|
||||
printf ("Zephyr version %d.%d.%d\n", (int)SYS_KERNEL_VER_MAJOR (zephyr_ver),
|
||||
(int)SYS_KERNEL_VER_MINOR (zephyr_ver),
|
||||
(int)SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver));
|
||||
printf ("Zephyr version %d.%d.%d\n",
|
||||
(int) SYS_KERNEL_VER_MAJOR (zephyr_ver),
|
||||
(int) SYS_KERNEL_VER_MINOR (zephyr_ver),
|
||||
(int) SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver));
|
||||
|
||||
zephyr_getline_init ();
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
register_js_function ("print", jerryx_handler_print);
|
||||
jerry_value_t global_obj_val = jerry_get_global_object ();
|
||||
jerry_value_t global_obj_val = jerry_current_realm ();
|
||||
|
||||
jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
|
||||
print_function = jerry_get_property (global_obj_val, print_func_name_val);
|
||||
jerry_release_value (print_func_name_val);
|
||||
jerry_release_value (global_obj_val);
|
||||
if (jerry_value_is_error (print_function))
|
||||
jerry_value_t print_func_name_val = jerry_string_sz ("print");
|
||||
print_function = jerry_object_get (global_obj_val, print_func_name_val);
|
||||
jerry_value_free (print_func_name_val);
|
||||
jerry_value_free (global_obj_val);
|
||||
if (jerry_value_is_exception (print_function))
|
||||
{
|
||||
printf ("Error: could not look up print function, expression results won't be printed\n");
|
||||
}
|
||||
@@ -105,8 +107,8 @@ void main (void)
|
||||
while (1)
|
||||
{
|
||||
char *s;
|
||||
printf("js> ");
|
||||
fflush(stdout);
|
||||
printf ("js> ");
|
||||
fflush (stdout);
|
||||
s = zephyr_getline ();
|
||||
if (*s)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user