17
17
#include <string.h>
18
18
#include "shell.h"
19
19
#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 */
20
44
21
45
/**
22
46
* Jerryscript simple test
@@ -26,13 +50,44 @@ int test_jerry (int argc, char **argv)
26
50
/* Suppress compiler errors */
27
51
(void ) argc ;
28
52
(void ) argv ;
53
+
54
+ jerry_value_t ret_value = jerry_create_undefined ();
55
+
29
56
const jerry_char_t script [] = "print ('Hello, World!');" ;
57
+ size_t script_size = strlen ((const char * ) script );
30
58
printf ("This test run the following script code: [%s]\n\n" , script );
31
59
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 ;
34
90
35
- return (ret_value ? 1 : 0 );
36
91
} /* test_jerry */
37
92
38
93
const shell_command_t shell_commands [] = {
0 commit comments