Skip to content

Commit 2b152f0

Browse files
robertsipkaakosthekiss
authored andcommitted
Use external print handler in riot-stm32f4 target (jerryscript-project#1814)
Extend example code with registering the `print` function in the global object. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka [email protected]
1 parent 838e74d commit 2b152f0

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

targets/riot-stm32f4/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ CFLAGS += -DDEVELHELP
3737
# Change this to 0 show compiler invocation lines by default:
3838
QUIET ?= 1
3939

40-
INCLUDES += -I$(JERRYDIR)/jerry-core/include
40+
INCLUDES += -I$(JERRYDIR)/jerry-core/include -I$(JERRYDIR)/jerry-ext/include
4141

4242
# Add the shell and some shell commands
4343
USEMODULE += shell
4444
USEMODULE += shell_commands
4545

4646
# Add the jerry libs
47-
USEMODULE += libjerrycore libjerryport-minimal
47+
USEMODULE += libjerrycore libjerryport-minimal libjerryext
4848

4949

5050
include $(RIOTBASE)/Makefile.include

targets/riot-stm32f4/Makefile.riot

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ libjerry:
4242
-DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \
4343
-DMEM_HEAP_SIZE_KB=$(JERRYHEAP)
4444

45-
make -C$(BUILD_DIR) jerry-core jerry-port-default-minimal
45+
make -C$(BUILD_DIR) jerry-core jerry-port-default-minimal jerry-ext
4646
cp $(BUILD_DIR)/lib/libjerry-core.a $(COPYTARGET)/libjerrycore.a
4747
cp $(BUILD_DIR)/lib/libjerry-port-default-minimal.a $(COPYTARGET)/libjerryport-minimal.a
48+
cp $(BUILD_DIR)/lib/libjerry-ext.a $(COPYTARGET)/libjerryext.a
4849

4950

5051
riot-jerry: libjerry

targets/riot-stm32f4/source/main-riotos.c

+58-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@
1717
#include <string.h>
1818
#include "shell.h"
1919
#include "jerryscript.h"
20+
#include "jerryscript-ext/handler.h"
21+
22+
/**
23+
* Standalone Jerry exit codes
24+
*/
25+
#define JERRY_STANDALONE_EXIT_CODE_OK (0)
26+
#define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
27+
28+
/**
29+
* Register a JavaScript function in the global object.
30+
*/
31+
static void
32+
register_js_function (const char *name_p, /**< name of the function */
33+
jerry_external_handler_t handler_p) /**< function callback */
34+
{
35+
jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
36+
37+
if (jerry_value_has_error_flag (result_val))
38+
{
39+
printf ("Warning: failed to register '%s' method.", name_p);
40+
}
41+
42+
jerry_release_value (result_val);
43+
} /* register_js_function */
2044

2145
/**
2246
* Jerryscript simple test
@@ -26,13 +50,44 @@ int test_jerry (int argc, char **argv)
2650
/* Suppress compiler errors */
2751
(void) argc;
2852
(void) argv;
53+
54+
jerry_value_t ret_value = jerry_create_undefined ();
55+
2956
const jerry_char_t script[] = "print ('Hello, World!');";
57+
size_t script_size = strlen ((const char *) script);
3058
printf ("This test run the following script code: [%s]\n\n", script);
3159

32-
size_t script_size = strlen ((const char *) script);
33-
bool ret_value = jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
60+
/* Initialize engine */
61+
jerry_init (JERRY_INIT_EMPTY);
62+
63+
/* Register the print function in the global object. */
64+
register_js_function ("print", jerryx_handler_print);
65+
66+
/* Setup Global scope code */
67+
ret_value = jerry_parse (script, script_size, false);
68+
69+
if (!jerry_value_has_error_flag (ret_value))
70+
{
71+
/* Execute the parsed source code in the Global scope */
72+
ret_value = jerry_run (ret_value);
73+
}
74+
75+
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
76+
77+
if (jerry_value_has_error_flag (ret_value))
78+
{
79+
printf ("Script Error!");
80+
81+
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
82+
}
83+
84+
jerry_release_value (ret_value);
85+
86+
/* Cleanup engine */
87+
jerry_cleanup ();
88+
89+
return ret_code;
3490

35-
return (ret_value ? 1 : 0);
3691
} /* test_jerry */
3792

3893
const shell_command_t shell_commands[] = {

0 commit comments

Comments
 (0)