|
16 | 16 | #include <stdio.h>
|
17 | 17 | #include <string.h>
|
18 | 18 | #include <stdlib.h>
|
19 |
| -#include <setjmp.h> |
20 | 19 |
|
21 | 20 | #include "jerryscript.h"
|
| 21 | +#include "jerryscript-ext/handler.h" |
22 | 22 | #include "jerryscript-port.h"
|
23 | 23 | #include "jmem.h"
|
| 24 | +#include "setjmp.h" |
24 | 25 |
|
25 | 26 | /**
|
26 | 27 | * Maximum command line arguments number.
|
@@ -311,61 +312,14 @@ print_unhandled_exception (jerry_value_t error_value, /**< error value */
|
311 | 312 | jerry_release_value (err_str_val);
|
312 | 313 | } /* print_unhandled_exception */
|
313 | 314 |
|
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 |
| - |
353 | 315 | /**
|
354 | 316 | * Register a JavaScript function in the global object.
|
355 | 317 | */
|
356 | 318 | static void
|
357 | 319 | register_js_function (const char *name_p, /**< name of the function */
|
358 | 320 | jerry_external_handler_t handler_p) /**< function callback */
|
359 | 321 | {
|
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); |
369 | 323 |
|
370 | 324 | if (jerry_value_has_error_flag (result_val))
|
371 | 325 | {
|
@@ -447,58 +401,66 @@ int jerry_main (int argc, char *argv[])
|
447 | 401 | }
|
448 | 402 | }
|
449 | 403 |
|
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 |
| - |
459 | 404 | jerry_init (flags);
|
460 | 405 |
|
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); |
463 | 409 |
|
464 | 410 | jerry_value_t ret_value = jerry_create_undefined ();
|
465 | 411 |
|
466 |
| - for (i = 0; i < files_counter; i++) |
| 412 | + if (files_counter == 0) |
467 | 413 | {
|
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); |
476 | 417 |
|
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); |
482 | 419 |
|
483 | 420 | if (!jerry_value_has_error_flag (ret_value))
|
484 | 421 | {
|
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); |
488 | 423 | }
|
489 |
| - |
490 |
| - if (jerry_value_has_error_flag (ret_value)) |
| 424 | + } |
| 425 | + else |
| 426 | + { |
| 427 | + for (i = 0; i < files_counter; i++) |
491 | 428 | {
|
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); |
494 | 431 |
|
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); |
497 | 455 |
|
498 |
| - jmem_heap_free_block ((void*) source_p, source_size); |
| 456 | + break; |
| 457 | + } |
| 458 | + |
| 459 | + jmem_heap_free_block ((void*) source_p, source_size); |
499 | 460 |
|
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 | + } |
502 | 464 | }
|
503 | 465 |
|
504 | 466 | int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
|
@@ -564,3 +526,13 @@ jerry_port_get_current_time (void)
|
564 | 526 | {
|
565 | 527 | return 0;
|
566 | 528 | } /* 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