Skip to content

Commit

Permalink
QuickJS: reworked process object.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed Nov 6, 2024
1 parent 352c2e5 commit 98fff32
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 155 deletions.
122 changes: 10 additions & 112 deletions external/njs_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions nginx/ngx_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 98fff32

Please sign in to comment.