Skip to content

Commit

Permalink
[process] Add process.exit() for Linux build (zephyrproject-rtos#1390)
Browse files Browse the repository at this point in the history
Linux instance can now be closed from JS with process.exit()

Fixes zephyrproject-rtos#1387

Signed-off-by: James Prestwood <james.prestwood@intel.com>
  • Loading branch information
James Prestwood authored and grgustaf committed Jul 24, 2017
1 parent 575163a commit ef3830b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/zjs_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ static ZJS_DECL_FUNC(stop_js_handler)
return ZJS_UNDEFINED;
}

#ifdef ZJS_LINUX_BUILD
static ZJS_DECL_FUNC(process_exit)
{
ZJS_VALIDATE_ARGS(Z_OPTIONAL Z_NUMBER);

int status = 0;

if (argc > 0) {
status = jerry_get_number_value(argv[0]);
}
ZJS_PRINT("Exiting with code=%d\n", status);
exit(status);
}
#endif

void zjs_modules_init()
{
// Add module.exports to global namespace
Expand All @@ -166,6 +181,12 @@ void zjs_modules_init()
// create the C handler for require JS call
zjs_obj_add_function(global_obj, native_require_handler, "require");

#ifdef ZJS_LINUX_BUILD
ZVAL process = jerry_create_object();
zjs_obj_add_function(process, process_exit, "exit");
zjs_set_property(global_obj, "process", process);
#endif

// auto-load the events module without waiting for require(); needed so its
// init function will run before it's used by UART, etc.
int modcount = sizeof(zjs_modules_array) / sizeof(module_t);
Expand Down

0 comments on commit ef3830b

Please sign in to comment.