Skip to content

Commit 7d133e5

Browse files
authored
Use external print, gc and assert handlers in nuttx-stm32f4 target (#1813)
JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 1a7b258 commit 7d133e5

File tree

2 files changed

+64
-87
lines changed

2 files changed

+64
-87
lines changed

targets/nuttx-stm32f4/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ STACKSIZE = $(CONFIG_JERRYSCRIPT_STACKSIZE)
2929
CFLAGS += -std=c99 -DJERRY_NDEBUG -DJERRY_JS_PARSER '-DCONFIG_MEM_HEAP_AREA_SIZE=$(CONFIG_JERRYSCRIPT_HEAPSIZE)' -DCONFIG_DISABLE_ES2015_PROMISE_BUILTIN
3030
CFLAGS += -I$(ROOT_DIR)/ $(shell find $(ROOT_DIR)/jerryscript/jerry-core -type d | sed -r -e 's/^/-I/g')
3131
CFLAGS += -I$(ROOT_DIR)/jerryscript/jerry-libm/include
32+
CFLAGS += -I$(ROOT_DIR)/jerryscript/jerry-ext/include
3233

3334
# Fill error messages for builtin error objects
3435
ifeq ($(CONFIG_JERRYSCRIPT_ERROR_MESSAGES),y)
@@ -57,8 +58,12 @@ jerry_core_allin.c:
5758
jerry_libm_allin.c:
5859
find $(ROOT_DIR)/jerryscript/jerry-libm -name "*.c" | sed -r -e 's/(\.\.\/)*(.+)/#include "\2"/g' > jerry_libm_allin.c
5960

61+
.PHONY: jerry_ext_allin.c
62+
jerry_ext_allin.c:
63+
find $(ROOT_DIR)/jerryscript/jerry-ext -name "*.c" | sed -r -e 's/(\.\.\/)*(.+)/#include "\2"/g' > jerry_ext_allin.c
64+
6065
ASRCS = setjmp.S
61-
CSRCS = jerry_core_allin.c jerry_libm_allin.c
66+
CSRCS = jerry_core_allin.c jerry_libm_allin.c jerry_ext_allin.c
6267
MAINSRC = jerry_main.c
6368

6469
CONFIG_JERRYSCRIPT_PROGNAME ?= jerry$(EXEEXT)

targets/nuttx-stm32f4/jerry_main.c

Lines changed: 58 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
#include <stdio.h>
1717
#include <string.h>
1818
#include <stdlib.h>
19-
#include <setjmp.h>
2019

2120
#include "jerryscript.h"
21+
#include "jerryscript-ext/handler.h"
2222
#include "jerryscript-port.h"
2323
#include "jmem.h"
24+
#include "setjmp.h"
2425

2526
/**
2627
* Maximum command line arguments number.
@@ -311,61 +312,14 @@ print_unhandled_exception (jerry_value_t error_value, /**< error value */
311312
jerry_release_value (err_str_val);
312313
} /* print_unhandled_exception */
313314

314-
/**
315-
* Provide the 'assert' implementation for the engine.
316-
*
317-
* @return true - if only one argument was passed and the argument is a boolean true.
318-
*/
319-
static jerry_value_t
320-
assert_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
321-
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
322-
const jerry_value_t args_p[], /**< function arguments */
323-
const jerry_length_t args_cnt) /**< number of function arguments */
324-
{
325-
if (args_cnt == 1
326-
&& jerry_value_is_boolean (args_p[0])
327-
&& jerry_get_boolean_value (args_p[0]))
328-
{
329-
return jerry_create_boolean (true);
330-
}
331-
else
332-
{
333-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
334-
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
335-
}
336-
} /* assert_handler */
337-
338-
/**
339-
* Provide the 'gc' implementation for the engine.
340-
*
341-
* @return undefined.
342-
*/
343-
static jerry_value_t
344-
gc_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
345-
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
346-
const jerry_value_t args_p[] __attribute__((unused)), /**< function arguments */
347-
const jerry_length_t args_cnt __attribute__((unused))) /**< number of function arguments */
348-
{
349-
jerry_gc ();
350-
return jerry_create_undefined ();
351-
} /* gc_handler */
352-
353315
/**
354316
* Register a JavaScript function in the global object.
355317
*/
356318
static void
357319
register_js_function (const char *name_p, /**< name of the function */
358320
jerry_external_handler_t handler_p) /**< function callback */
359321
{
360-
jerry_value_t global_obj_val = jerry_get_global_object ();
361-
362-
jerry_value_t function_val = jerry_create_external_function (handler_p);
363-
jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) name_p);
364-
jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
365-
366-
jerry_release_value (function_name_val);
367-
jerry_release_value (function_val);
368-
jerry_release_value (global_obj_val);
322+
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
369323

370324
if (jerry_value_has_error_flag (result_val))
371325
{
@@ -447,58 +401,66 @@ int jerry_main (int argc, char *argv[])
447401
}
448402
}
449403

450-
if (files_counter == 0)
451-
{
452-
printf ("No input files, running a hello world demo:\n");
453-
char *source_p = "var a = 3.5; print('Hello world ' + (a + 1.5) + ' times from JerryScript')";
454-
455-
jerry_run_simple ((jerry_char_t *) source_p, strlen (source_p), flags);
456-
return JERRY_STANDALONE_EXIT_CODE_OK;
457-
}
458-
459404
jerry_init (flags);
460405

461-
register_js_function ("assert", assert_handler);
462-
register_js_function ("gc", gc_handler);
406+
register_js_function ("assert", jerryx_handler_assert);
407+
register_js_function ("gc", jerryx_handler_gc);
408+
register_js_function ("print", jerryx_handler_print);
463409

464410
jerry_value_t ret_value = jerry_create_undefined ();
465411

466-
for (i = 0; i < files_counter; i++)
412+
if (files_counter == 0)
467413
{
468-
size_t source_size;
469-
const jerry_char_t *source_p = read_file (file_names[i], &source_size);
470-
471-
if (source_p == NULL)
472-
{
473-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Source file load error\n");
474-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
475-
}
414+
printf ("No input files, running a hello world demo:\n");
415+
const jerry_char_t script[] = "var str = 'Hello World'; print(str + ' from JerryScript')";
416+
size_t script_size = strlen ((const char *) script);
476417

477-
ret_value = jerry_parse_named_resource ((jerry_char_t *) file_names[i],
478-
strlen (file_names[i]),
479-
source_p,
480-
source_size,
481-
false);
418+
ret_value = jerry_parse (script, script_size, false);
482419

483420
if (!jerry_value_has_error_flag (ret_value))
484421
{
485-
jerry_value_t func_val = ret_value;
486-
ret_value = jerry_run (func_val);
487-
jerry_release_value (func_val);
422+
ret_value = jerry_run (ret_value);
488423
}
489-
490-
if (jerry_value_has_error_flag (ret_value))
424+
}
425+
else
426+
{
427+
for (i = 0; i < files_counter; i++)
491428
{
492-
print_unhandled_exception (ret_value, source_p);
493-
jmem_heap_free_block ((void*) source_p, source_size);
429+
size_t source_size;
430+
const jerry_char_t *source_p = read_file (file_names[i], &source_size);
494431

495-
break;
496-
}
432+
if (source_p == NULL)
433+
{
434+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Source file load error\n");
435+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
436+
}
437+
438+
ret_value = jerry_parse_named_resource ((jerry_char_t *) file_names[i],
439+
strlen (file_names[i]),
440+
source_p,
441+
source_size,
442+
false);
443+
444+
if (!jerry_value_has_error_flag (ret_value))
445+
{
446+
jerry_value_t func_val = ret_value;
447+
ret_value = jerry_run (func_val);
448+
jerry_release_value (func_val);
449+
}
450+
451+
if (jerry_value_has_error_flag (ret_value))
452+
{
453+
print_unhandled_exception (ret_value, source_p);
454+
jmem_heap_free_block ((void*) source_p, source_size);
497455

498-
jmem_heap_free_block ((void*) source_p, source_size);
456+
break;
457+
}
458+
459+
jmem_heap_free_block ((void*) source_p, source_size);
499460

500-
jerry_release_value (ret_value);
501-
ret_value = jerry_create_undefined ();
461+
jerry_release_value (ret_value);
462+
ret_value = jerry_create_undefined ();
463+
}
502464
}
503465

504466
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
@@ -564,3 +526,13 @@ jerry_port_get_current_time (void)
564526
{
565527
return 0;
566528
} /* jerry_port_get_current_time */
529+
530+
/**
531+
* Provide the implementation of jerryx_port_handler_print_char.
532+
* Uses 'printf' to print a single character to standard output.
533+
*/
534+
void
535+
jerryx_port_handler_print_char (char c) /**< the character to print */
536+
{
537+
printf ("%c", c);
538+
} /* jerryx_port_handler_print_char */

0 commit comments

Comments
 (0)