Skip to content

Commit

Permalink
Implement console.log and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
janjongboom committed Dec 13, 2017
1 parent 469698c commit 5e8e01a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mbed-js-repl.lib
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://github.com/janjongboom/mbed-js-repl/#5e3ca1bf1f0784e694109689c603a86491c40720
https://github.com/janjongboom/mbed-js-repl/#3bb04531f83765ca6d115858d8a078d6ec1449df
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,90 @@ static int load_javascript() {
return 0;
}

static jerry_value_t console_log_handler(
const jerry_value_t func_value, /**< function object */
const jerry_value_t this_val, /**< this arg */
const jerry_value_t *args_p, /**< function arguments */
const jerry_length_t args_cnt) /**< number of function arguments */
{
for (size_t ix = 0; ix < args_cnt; ix++) {
if (ix != 0) {
printf(", ");
}

const jerry_value_t returned_value = args_p[ix];

jerry_value_t str_value = jerry_value_to_string(returned_value);

jerry_size_t size = jerry_get_string_size(str_value);

jerry_char_t* ret_buffer = (jerry_char_t*)calloc(size + 1, 1);

jerry_string_to_char_buffer(str_value, ret_buffer, size);

if (jerry_value_is_string(returned_value)) {
printf("%s", ret_buffer);
}
else if (jerry_value_is_array(returned_value)) {
printf("[%s]", ret_buffer);
}
else {
printf("%s", ret_buffer);
}

free(ret_buffer);

jerry_release_value(str_value);
}

printf("\r\n");

return jerry_create_undefined();
}


void jsmbed_js_create_console_object() {
// Grab global object
jerry_value_t global_object = jerry_get_global_object();

// Create name 'console', which is an object
jerry_value_t console_prop_name = jerry_create_string((const jerry_char_t *) "console");
jerry_value_t console = jerry_create_object();

// log, warn, error names on console object
jerry_value_t log_name = jerry_create_string((const jerry_char_t *) "log");
jerry_value_t warn_name = jerry_create_string((const jerry_char_t *) "warn");
jerry_value_t error_name = jerry_create_string((const jerry_char_t *) "error");

// pointer to the implementation
jerry_value_t log_func_obj = jerry_create_external_function(&console_log_handler);

// attach the function to log/warn/error
jerry_set_property(console, log_name, log_func_obj);
jerry_set_property(console, warn_name, log_func_obj);
jerry_set_property(console, error_name, log_func_obj);

// attach console to global object
jerry_set_property(global_object, console_prop_name, console);

// and free...
jerry_release_value(log_func_obj);

jerry_release_value(log_name);
jerry_release_value(warn_name);
jerry_release_value(error_name);

jerry_release_value(console_prop_name);
jerry_release_value(console);
jerry_release_value (global_object);
}

int jsmbed_js_init() {
jerry_init_flag_t flags = JERRY_INIT_EMPTY;
jerry_init(flags);

jsmbed_js_load_magic_strings();
jsmbed_js_create_console_object();
mbed::js::LibraryRegistry::getInstance().register_all();

return 0;
Expand Down
8 changes: 8 additions & 0 deletions mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"target_overrides": {
"*": {
"platform.stdio-baud-rate": 115200,
"platform.stdio-convert-newlines": true
}
}
}
2 changes: 1 addition & 1 deletion source/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ var iv = setInterval(function() {
led.write(led.read() === 0 ? 1 : 0);
}, 500);

print("To stop LED1 from blinking, run `clearInterval(" + iv + ")`");
console.log("To stop LED1 from blinking, run `clearInterval(" + iv + ")`");

repl_start();

0 comments on commit 5e8e01a

Please sign in to comment.