diff --git a/external/njs_shell.c b/external/njs_shell.c index 30d4d8c60..776c2536b 100644 --- a/external/njs_shell.c +++ b/external/njs_shell.c @@ -1901,15 +1901,17 @@ njs_qjs_clear_timeout(JSContext *ctx, JSValueConst this_val, int argc, } +static JSValue +njs_qjs_console_to_string_tag(JSContext *ctx, JSValueConst this_val) +{ + return JS_NewString(ctx, "Console"); +} + + static JSValue njs_qjs_process_getter(JSContext *ctx, JSValueConst this_val) { - char **ep; - JSAtom atom; - JSValue obj, val, str, name, env; - njs_int_t ret; - njs_uint_t i; - const char *entry, *value; + JSValue obj; njs_console_t *console; console = JS_GetRuntimeOpaque(JS_GetRuntime(ctx)); @@ -1918,106 +1920,8 @@ njs_qjs_process_getter(JSContext *ctx, JSValueConst this_val) return JS_DupValue(ctx, console->process); } - obj = JS_NewObject(ctx); - if (JS_IsException(obj)) { - return JS_EXCEPTION; - } - - ret = qjs_set_to_string_tag(ctx, obj, "process"); - if (ret == -1) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - val = JS_NewArray(ctx); - if (JS_IsException(val)) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - ret = JS_SetPropertyStr(ctx, obj, "argv", val); - if (ret == -1) { - JS_FreeValue(ctx, obj); - JS_FreeValue(ctx, val); - return JS_EXCEPTION; - } - - for (i = 0; i < console->argc; i++) { - str = JS_NewStringLen(ctx, console->argv[i], - njs_strlen(console->argv[i])); - if (JS_IsException(str)) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - ret = JS_DefinePropertyValueUint32(ctx, val, i, str, JS_PROP_C_W_E); - if (ret == -1) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - } - - env = JS_NewObject(ctx); + obj = qjs_process_object(ctx, console->argc, (const char **) console->argv); if (JS_IsException(obj)) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - ret = JS_SetPropertyStr(ctx, obj, "env", env); - if (ret == -1) { - JS_FreeValue(ctx, obj); - JS_FreeValue(ctx, env); - return JS_EXCEPTION; - } - - ep = environ; - - while (*ep != NULL) { - entry = *ep++; - - value = (const char *) njs_strchr(entry, '='); - if (njs_slow_path(value == NULL)) { - continue; - } - - str = JS_UNDEFINED; - name = JS_NewStringLen(ctx, entry, value - entry); - if (JS_IsException(name)) { - goto error; - } - - str = JS_NewStringLen(ctx, value, njs_strlen(value)); - if (JS_IsException(str)) { - goto error; - } - - atom = JS_ValueToAtom(ctx, name); - if (atom == JS_ATOM_NULL) { - goto error; - } - - ret = JS_DefinePropertyValue(ctx, env, atom, str, JS_PROP_C_W_E); - JS_FreeAtom(ctx, atom); - if (ret == -1) { -error: - JS_FreeValue(ctx, name); - JS_FreeValue(ctx, str); - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - JS_FreeValue(ctx, name); - } - - ret = JS_SetPropertyStr(ctx, obj, "pid", JS_NewInt32(ctx, getpid())); - if (ret == -1) { - JS_FreeValue(ctx, obj); - return JS_EXCEPTION; - } - - ret = JS_SetPropertyStr(ctx, obj, "ppid", JS_NewInt32(ctx, getppid())); - if (ret == -1) { - JS_FreeValue(ctx, obj); return JS_EXCEPTION; } @@ -2583,6 +2487,7 @@ static const JSCFunctionListEntry njs_qjs_global_proto[] = { static const JSCFunctionListEntry njs_qjs_console_proto[] = { + JS_CGETSET_DEF("[Symbol.toStringTag]", njs_qjs_console_to_string_tag, NULL), JS_CFUNC_MAGIC_DEF("error", 0, njs_qjs_console_log, NJS_LOG_ERROR), JS_CFUNC_MAGIC_DEF("info", 0, njs_qjs_console_log, NJS_LOG_INFO), JS_CFUNC_MAGIC_DEF("log", 0, njs_qjs_console_log, NJS_LOG_INFO), @@ -2759,13 +2664,6 @@ njs_engine_qjs_init(njs_engine_t *engine, njs_opts_t *opts) goto done; } - ret = qjs_set_to_string_tag(ctx, obj, "Console"); - if (ret == -1) { - njs_stderror("qjs_set_to_string_tag() failed\n"); - ret = NJS_ERROR; - goto done; - } - JS_SetOpaque(obj, &njs_console); JS_SetPropertyFunctionList(ctx, obj, njs_qjs_console_proto, diff --git a/nginx/ngx_js.c b/nginx/ngx_js.c index f70288cf1..20c8b2f3d 100644 --- a/nginx/ngx_js.c +++ b/nginx/ngx_js.c @@ -70,6 +70,7 @@ static ngx_int_t ngx_engine_qjs_pending(ngx_engine_t *engine); static ngx_int_t ngx_engine_qjs_string(ngx_engine_t *e, njs_opaque_value_t *value, ngx_str_t *str); +static JSValue ngx_qjs_process_getter(JSContext *ctx, JSValueConst this_val); static JSValue ngx_qjs_ext_set_timeout(JSContext *cx, JSValueConst this_val, int argc, JSValueConst *argv, int immediate); static JSValue ngx_qjs_ext_clear_timeout(JSContext *cx, JSValueConst this_val, @@ -457,6 +458,7 @@ static const JSCFunctionListEntry ngx_qjs_ext_console[] = { static const JSCFunctionListEntry ngx_qjs_ext_global[] = { + JS_CGETSET_DEF("process", ngx_qjs_process_getter, NULL), JS_CFUNC_MAGIC_DEF("setTimeout", 1, ngx_qjs_ext_set_timeout, 0), JS_CFUNC_MAGIC_DEF("setImmediate", 1, ngx_qjs_ext_set_timeout, 1), JS_CFUNC_DEF("clearTimeout", 1, ngx_qjs_ext_clear_timeout), @@ -1566,6 +1568,13 @@ ngx_qjs_clear_timer(ngx_qjs_event_t *event) } +static JSValue +ngx_qjs_process_getter(JSContext *cx, JSValueConst this_val) +{ + return qjs_process_object(cx, ngx_argc, (const char **) ngx_argv); +} + + static JSValue ngx_qjs_ext_set_timeout(JSContext *cx, JSValueConst this_val, int argc, JSValueConst *argv, int immediate) @@ -3823,6 +3832,10 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf, ngx_pool_cleanup_t *cln; ngx_js_named_path_t *import; + if (ngx_set_environment(cf->cycle, NULL) == NULL) { + return NGX_ERROR; + } + if (conf->preload_objects != NGX_CONF_UNSET_PTR) { if (ngx_js_init_preload_vm(cf, (ngx_js_loc_conf_t *)conf) != NGX_OK) { return NGX_ERROR; diff --git a/nginx/t/js_process.t b/nginx/t/js_process.t new file mode 100644 index 000000000..40c1fc1b9 --- /dev/null +++ b/nginx/t/js_process.t @@ -0,0 +1,84 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) Nginx, Inc. + +# Tests for http njs module, process object. + +############################################################################### + +use warnings; +use strict; + +use Test::More; +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +env FOO=bar; +env BAR=baz; + +http { + %%TEST_GLOBALS_HTTP%% + + js_import test.js; + + server { + listen 127.0.0.1:8080; + server_name localhost; + + location /argv { + js_content test.argv; + } + + location /env { + js_content test.env; + } + } +} + +EOF + +$t->write_file('test.js', <= 0}`); + } + + function env(r) { + var e = process.env[r.args.var]; + r.return(200, e ? e : 'undefined'); + } + + export default { argv, env }; + +EOF + +$t->try_run('no njs process object')->plan(4); + +############################################################################### + +like(http_get('/argv'), qr/true true/, 'argv'); +like(http_get('/env?var=FOO'), qr/bar/, 'env FOO'); +like(http_get('/env?var=BAR'), qr/baz/, 'env BAR'); +like(http_get('/env?var=HOME'), qr/undefined/, 'env HOME'); + +############################################################################### diff --git a/nginx/t/stream_js_process.t b/nginx/t/stream_js_process.t new file mode 100644 index 000000000..85becd317 --- /dev/null +++ b/nginx/t/stream_js_process.t @@ -0,0 +1,108 @@ +#!/usr/bin/perl + +# (C) Dmitry Volyntsev +# (C) Nginx, Inc. + +# Tests for stream njs module, process object. + +############################################################################### + +use warnings; +use strict; + +use Test::More; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; +use Test::Nginx::Stream qw/ stream /; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/stream stream_return/) + ->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +env FOO=bar; +env BAR=baz; + +stream { + %%TEST_GLOBALS_STREAM%% + + js_import test.js; + + js_set $env_foo test.env_foo; + js_set $env_bar test.env_bar; + js_set $env_home test.env_home; + js_set $argv test.argv; + + server { + listen 127.0.0.1:8081; + return $env_foo; + } + + server { + listen 127.0.0.1:8082; + return $env_bar; + } + + server { + listen 127.0.0.1:8083; + return $env_home; + } + + server { + listen 127.0.0.1:8084; + return $argv; + } +} + +EOF + +$t->write_file('test.js', <= 0}`; + } + + export default { env_foo, env_bar, env_home, argv }; + +EOF + +$t->try_run('no njs stream session object')->plan(4); + +############################################################################### + +is(stream('127.0.0.1:' . port(8081))->read(), 'bar', 'env.FOO'); +is(stream('127.0.0.1:' . port(8082))->read(), 'baz', 'env.BAR'); +is(stream('127.0.0.1:' . port(8083))->read(), 'undefined', 'env HOME'); +is(stream('127.0.0.1:' . port(8084))->read(), 'true true', 'argv'); + +############################################################################### diff --git a/src/njs_builtin.c b/src/njs_builtin.c index 3ff6f5475..812f18d24 100644 --- a/src/njs_builtin.c +++ b/src/njs_builtin.c @@ -7,6 +7,7 @@ #include +#include typedef struct { @@ -22,6 +23,12 @@ typedef struct { } njs_builtin_traverse_t; +typedef struct { + njs_str_t name; + int value; +} njs_signal_entry_t; + + static njs_int_t njs_global_this_prop_handler(njs_vm_t *vm, njs_object_prop_t *self, njs_value_t *global, njs_value_t *setval, njs_value_t *retval); @@ -99,6 +106,31 @@ static const njs_object_type_init_t *const }; +/* P1990 signals from `man 7 signal` are supported */ +static njs_signal_entry_t njs_signals_table[] = { + { njs_str("ABRT"), SIGABRT }, + { njs_str("ALRM"), SIGALRM }, + { njs_str("CHLD"), SIGCHLD }, + { njs_str("CONT"), SIGCONT }, + { njs_str("FPE"), SIGFPE }, + { njs_str("HUP"), SIGHUP }, + { njs_str("ILL"), SIGILL }, + { njs_str("INT"), SIGINT }, + { njs_str("KILL"), SIGKILL }, + { njs_str("PIPE"), SIGPIPE }, + { njs_str("QUIT"), SIGQUIT }, + { njs_str("SEGV"), SIGSEGV }, + { njs_str("STOP"), SIGSTOP }, + { njs_str("TSTP"), SIGTSTP }, + { njs_str("TERM"), SIGTERM }, + { njs_str("TTIN"), SIGTTIN }, + { njs_str("TTOU"), SIGTTOU }, + { njs_str("USR1"), SIGUSR1 }, + { njs_str("USR2"), SIGUSR2 }, + { njs_null_str, 0 } +}; + + njs_inline njs_int_t njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, const njs_object_init_t *init) @@ -1380,6 +1412,67 @@ njs_process_object_ppid(njs_vm_t *vm, njs_object_prop_t *prop, } +static njs_int_t +njs_ext_process_kill(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, + njs_index_t magic, njs_value_t *retval) +{ + int signal; + njs_str_t str; + njs_uint_t pid; + njs_value_t *arg; + njs_signal_entry_t *s; + + arg = njs_arg(args, nargs, 1); + if (!njs_value_is_number(arg)) { + njs_vm_type_error(vm, "\"pid\" is not a number"); + return NJS_ERROR; + } + + pid = njs_value_number(arg); + signal = SIGTERM; + + arg = njs_arg(args, nargs, 2); + if (njs_value_is_number(arg)) { + signal = njs_value_number(arg); + + } else if (njs_value_is_string(arg)) { + njs_value_string_get(arg, &str); + + if (str.length < 3 || memcmp(str.start, "SIG", 3) != 0) { + njs_vm_type_error(vm, "\"signal\" unknown value: \"%V\"", &str); + return NJS_ERROR; + } + + str.start += 3; + str.length -= 3; + + for (s = &njs_signals_table[0]; s->name.length != 0; s++) { + if (njs_strstr_eq(&str, &s->name)) { + signal = s->value; + break; + } + } + + if (s->name.length == 0) { + njs_vm_type_error(vm, "\"signal\" unknown value"); + return NJS_ERROR; + } + + } else if (!njs_value_is_undefined(arg)) { + njs_vm_type_error(vm, "\"signal\" invalid type"); + return NJS_ERROR; + } + + if (kill(pid, signal) < 0) { + njs_vm_error(vm, "kill failed with (%d:%s)", errno, strerror(errno)); + return NJS_ERROR; + } + + njs_set_boolean(retval, 1); + return NJS_OK; +} + + static const njs_object_prop_t njs_process_object_properties[] = { { @@ -1396,6 +1489,8 @@ static const njs_object_prop_t njs_process_object_properties[] = NJS_DECLARE_PROP_HANDLER("pid", njs_process_object_pid, 0, 0, 0), NJS_DECLARE_PROP_HANDLER("ppid", njs_process_object_ppid, 0, 0, 0), + + NJS_DECLARE_PROP_NATIVE("kill", njs_ext_process_kill, 2, 0), }; diff --git a/src/qjs.c b/src/qjs.c index 3d378fcc2..44e9fe95a 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -7,14 +7,78 @@ #include #include /* NJS_VERSION */ +#include +#include +#include +#include + + +typedef struct { + njs_str_t name; + int value; +} qjs_signal_entry_t; + + +extern char **environ; + static JSValue qjs_njs_getter(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_njs_to_string_tag(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_process_to_string_tag(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_process_argv(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_process_env(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_process_kill(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv); +static JSValue qjs_process_pid(JSContext *ctx, JSValueConst this_val); +static JSValue qjs_process_ppid(JSContext *ctx, JSValueConst this_val); + + +/* P1990 signals from `man 7 signal` are supported */ +static qjs_signal_entry_t qjs_signals_table[] = { + { njs_str("ABRT"), SIGABRT }, + { njs_str("ALRM"), SIGALRM }, + { njs_str("CHLD"), SIGCHLD }, + { njs_str("CONT"), SIGCONT }, + { njs_str("FPE"), SIGFPE }, + { njs_str("HUP"), SIGHUP }, + { njs_str("ILL"), SIGILL }, + { njs_str("INT"), SIGINT }, + { njs_str("KILL"), SIGKILL }, + { njs_str("PIPE"), SIGPIPE }, + { njs_str("QUIT"), SIGQUIT }, + { njs_str("SEGV"), SIGSEGV }, + { njs_str("STOP"), SIGSTOP }, + { njs_str("TSTP"), SIGTSTP }, + { njs_str("TERM"), SIGTERM }, + { njs_str("TTIN"), SIGTTIN }, + { njs_str("TTOU"), SIGTTOU }, + { njs_str("USR1"), SIGUSR1 }, + { njs_str("USR2"), SIGUSR2 }, + { njs_null_str, 0 } +}; static const JSCFunctionListEntry qjs_global_proto[] = { JS_CGETSET_DEF("njs", qjs_njs_getter, NULL), }; +static const JSCFunctionListEntry qjs_njs_proto[] = { + JS_CGETSET_DEF("[Symbol.toStringTag]", qjs_njs_to_string_tag, NULL), + JS_PROP_STRING_DEF("version", NJS_VERSION, JS_PROP_C_W_E), + JS_PROP_INT32_DEF("version_number", NJS_VERSION_NUMBER, + JS_PROP_C_W_E), + JS_PROP_STRING_DEF("engine", "QuickJS", JS_PROP_C_W_E), +}; + +static const JSCFunctionListEntry qjs_process_proto[] = { + JS_CGETSET_DEF("[Symbol.toStringTag]", qjs_process_to_string_tag, NULL), + JS_CGETSET_DEF("argv", qjs_process_argv, NULL), + JS_CGETSET_DEF("env", qjs_process_env, NULL), + JS_CFUNC_DEF("kill", 2, qjs_process_kill), + JS_CGETSET_DEF("pid", qjs_process_pid, NULL), + JS_CGETSET_DEF("ppid", qjs_process_ppid, NULL), +}; + JSContext * qjs_new_context(JSRuntime *rt, qjs_module_t **addons) @@ -91,7 +155,6 @@ qjs_new_context(JSRuntime *rt, qjs_module_t **addons) static JSValue qjs_njs_getter(JSContext *ctx, JSValueConst this_val) { - int ret; JSValue obj; obj = JS_NewObject(ctx); @@ -99,73 +162,232 @@ qjs_njs_getter(JSContext *ctx, JSValueConst this_val) return JS_EXCEPTION; } - ret = qjs_set_to_string_tag(ctx, obj, "njs"); - if (ret == -1) { - JS_FreeValue(ctx, obj); + JS_SetPropertyFunctionList(ctx, obj, qjs_njs_proto, + njs_nitems(qjs_njs_proto)); + + return obj; +} + + +static JSValue +qjs_njs_to_string_tag(JSContext *ctx, JSValueConst this_val) +{ + return JS_NewString(ctx, "njs"); +} + + +static JSValue +qjs_process_to_string_tag(JSContext *ctx, JSValueConst this_val) +{ + return JS_NewString(ctx, "process"); +} + + +static JSValue +qjs_process_argv(JSContext *ctx, JSValueConst this_val) +{ + int i, ret, argc; + JSValue val, str; + const char **argv; + + val = JS_GetPropertyStr(ctx, this_val, "argc"); + if (JS_IsException(val)) { return JS_EXCEPTION; } - ret = JS_SetPropertyStr(ctx, obj, "version_number", - JS_NewInt32(ctx, NJS_VERSION_NUMBER)); - if (ret == -1) { - JS_FreeValue(ctx, obj); + if (JS_ToInt32(ctx, &argc, val) < 0) { return JS_EXCEPTION; } - ret = JS_SetPropertyStr(ctx, obj, "version", - JS_NewString(ctx, NJS_VERSION)); - if (ret == -1) { - JS_FreeValue(ctx, obj); + argv = JS_GetOpaque(this_val, JS_GetClassID(this_val)); + if (argv == NULL) { + return JS_NewArray(ctx); + } + + val = JS_NewArray(ctx); + if (JS_IsException(val)) { return JS_EXCEPTION; } - ret = JS_SetPropertyStr(ctx, obj, "engine", JS_NewString(ctx, "QuickJS")); - if (ret == -1) { - JS_FreeValue(ctx, obj); + for (i = 0; i < argc; i++) { + str = JS_NewStringLen(ctx, argv[i], njs_strlen(argv[i])); + if (JS_IsException(str)) { + JS_FreeValue(ctx, val); + return JS_EXCEPTION; + } + + ret = JS_DefinePropertyValueUint32(ctx, val, i, str, JS_PROP_C_W_E); + if (ret < 0) { + JS_FreeValue(ctx, str); + JS_FreeValue(ctx, val); + return JS_EXCEPTION; + } + } + + return val; +} + + +static JSValue +qjs_process_env(JSContext *ctx, JSValueConst this_val) +{ + int ret; + char **ep; + JSValue obj; + JSAtom atom; + JSValue str, name; + const char *entry, *value; + + obj = JS_NewObject(ctx); + if (JS_IsException(obj)) { return JS_EXCEPTION; } + ep = environ; + + while (*ep != NULL) { + entry = *ep++; + + value = (const char *) njs_strchr(entry, '='); + if (value == NULL) { + continue; + } + + str = JS_UNDEFINED; + name = JS_NewStringLen(ctx, entry, value - entry); + if (JS_IsException(name)) { + goto error; + } + + value++; + + str = JS_NewStringLen(ctx, value, njs_strlen(value)); + if (JS_IsException(str)) { + goto error; + } + + atom = JS_ValueToAtom(ctx, name); + if (atom == JS_ATOM_NULL) { + goto error; + } + + ret = JS_DefinePropertyValue(ctx, obj, atom, str, JS_PROP_C_W_E); + JS_FreeAtom(ctx, atom); + if (ret < 0) { +error: + JS_FreeValue(ctx, name); + JS_FreeValue(ctx, str); + return JS_EXCEPTION; + } + + JS_FreeValue(ctx, name); + } + return obj; } -int -qjs_set_to_string_tag(JSContext *ctx, JSValueConst val, const char *tag) +static JSValue +qjs_process_kill(JSContext *ctx, JSValueConst this_val, int argc, + JSValueConst *argv) { - int ret; - JSAtom atom; - JSValue global_obj, symbol, toStringTag; + int signo, pid; + JSValue val; + njs_str_t name; + const char *signal; + qjs_signal_entry_t *entry; - global_obj = JS_GetGlobalObject(ctx); + if (JS_ToInt32(ctx, &pid, argv[0]) < 0) { + return JS_EXCEPTION; + } - symbol = JS_GetPropertyStr(ctx, global_obj, "Symbol"); - JS_FreeValue(ctx, global_obj); - if (JS_IsException(symbol)) { - return -1; + if (JS_IsNumber(argv[1])) { + if (JS_ToInt32(ctx, &signo, argv[1]) < 0) { + return JS_EXCEPTION; + } + + if (signo < 0 || signo >= NSIG) { + return JS_ThrowTypeError(ctx, "unknown signal: %d", signo); + } + + } else { + val = JS_ToString(ctx, argv[1]); + if (JS_IsException(val)) { + return JS_EXCEPTION; + } + + signal = JS_ToCString(ctx, val); + if (signal == NULL) { + JS_FreeValue(ctx, val); + return JS_EXCEPTION; + } + + if (njs_strlen(signal) < 3 || memcmp(signal, "SIG", 3) != 0) { + JS_FreeCString(ctx, signal); + return JS_ThrowTypeError(ctx, "unknown signal: %s", signal); + } + + name.start = (u_char *) signal + 3; + name.length = njs_strlen(signal) - 3; + + for (entry = qjs_signals_table; entry->name.length != 0; entry++) { + if (njs_strstr_eq(&entry->name, &name)) { + signo = entry->value; + break; + } + } + + JS_FreeCString(ctx, signal); + + if (entry->name.length == 0) { + return JS_ThrowTypeError(ctx, "unknown signal: %s", signal); + } } - toStringTag = JS_GetPropertyStr(ctx, symbol, "toStringTag"); - if (JS_IsException(toStringTag)) { - JS_FreeValue(ctx, symbol); - return -1; + if (kill(pid, signo) < 0) { + return JS_ThrowTypeError(ctx, "kill failed with (%d:%s)", errno, + strerror(errno)); } - atom = JS_ValueToAtom(ctx, toStringTag); + return JS_TRUE; +} + + +static JSValue +qjs_process_pid(JSContext *ctx, JSValueConst this_val) +{ + return JS_NewInt32(ctx, getpid()); +} + - JS_FreeValue(ctx, symbol); - JS_FreeValue(ctx, toStringTag); +static JSValue +qjs_process_ppid(JSContext *ctx, JSValueConst this_val) +{ + return JS_NewInt32(ctx, getppid()); +} - if (atom == JS_ATOM_NULL) { - JS_ThrowInternalError(ctx, "failed to get atom"); - return -1; + +JSValue +qjs_process_object(JSContext *ctx, int argc, const char **argv) +{ + JSValue obj; + + obj = JS_NewObject(ctx); + if (JS_IsException(obj)) { + return JS_EXCEPTION; } - ret = JS_DefinePropertyValue(ctx, val, atom, JS_NewString(ctx, tag), - JS_PROP_C_W_E); + JS_SetPropertyFunctionList(ctx, obj, qjs_process_proto, + njs_nitems(qjs_process_proto)); + + JS_SetOpaque(obj, argv); - JS_FreeAtom(ctx, atom); + if (JS_SetPropertyStr(ctx, obj, "argc", JS_NewInt32(ctx, argc)) < 0) { + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; + } - return ret; + return obj; } diff --git a/src/qjs.h b/src/qjs.h index 2418e6cd9..81769abb7 100644 --- a/src/qjs.h +++ b/src/qjs.h @@ -70,7 +70,7 @@ typedef struct { const qjs_buffer_encoding_t *qjs_buffer_encoding(JSContext *ctx, JSValueConst value, JS_BOOL thrw); -int qjs_set_to_string_tag(JSContext *ctx, JSValueConst val, const char *tag); +JSValue qjs_process_object(JSContext *ctx, int argc, const char **argv); typedef struct { int tag; diff --git a/test/shell_test.exp b/test/shell_test.exp index 139daa177..b713ed09b 100644 --- a/test/shell_test.exp +++ b/test/shell_test.exp @@ -423,6 +423,9 @@ njs_run {"-c" "console.log(process.pid)"} "\\d+" njs_run {"-c" "console.log(process.ppid)"} "\\d+" +njs_run {"-c" "console.log(process.kill(process.pid, 0))"} "true" +njs_run {"-c" "console.log(process.kill(process.pid, 'SIGCHLD'))"} "true" + # script args diff --git a/test/shell_test_njs.exp b/test/shell_test_njs.exp index fac0fe3a9..9c15b81a2 100644 --- a/test/shell_test_njs.exp +++ b/test/shell_test_njs.exp @@ -124,6 +124,8 @@ njs_run {"-c" "console.log(process.pid)"} "\\d+" njs_run {"-c" "console.log(process.ppid)"} "\\d+" +njs_run {"-c" "console.log(process.kill(process.pid, 0))"} "true" + # script args diff --git a/ts/njs_core.d.ts b/ts/njs_core.d.ts index 0e286f93d..2f1d45d01 100644 --- a/ts/njs_core.d.ts +++ b/ts/njs_core.d.ts @@ -579,6 +579,11 @@ interface NjsProcess { readonly ppid: number; readonly argv: string[]; readonly env: NjsEnv; + + /** + * @since 0.8.8 + */ + kill(pid: number, signal?: string | number): true; } declare const process: NjsProcess;