[njs] QuickJS: reimplemented process.argv.

noreply at nginx.com noreply at nginx.com
Fri Jan 24 23:13:02 UTC 2025


details:   https://github.com/nginx/njs/commit/70f75ed0cb19ab2348207b48b5b1e7ea7b9cac33
branches:  master
commit:    70f75ed0cb19ab2348207b48b5b1e7ea7b9cac33
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue, 21 Jan 2025 18:07:53 -0800
description:
QuickJS: reimplemented process.argv.

Without using JS_SetOpaque(), because in QuickJS-NG opaque pointer
cannot be set for internal classes, including ordinary objects.

---
 src/qjs.c | 76 ++++++++++++++++++++-------------------------------------------
 1 file changed, 24 insertions(+), 52 deletions(-)

diff --git a/src/qjs.c b/src/qjs.c
index e21e6568..8c5b0b6d 100644
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -45,7 +45,6 @@ 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);
@@ -137,7 +136,6 @@ static const JSCFunctionListEntry qjs_njs_proto[] = {
 
 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),
@@ -262,51 +260,6 @@ qjs_process_to_string_tag(JSContext *ctx, JSValueConst this_val)
 }
 
 
-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;
-    }
-
-    if (JS_ToInt32(ctx, &argc, val) < 0) {
-        return JS_EXCEPTION;
-    }
-
-    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;
-    }
-
-    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)
 {
@@ -451,20 +404,39 @@ qjs_process_ppid(JSContext *ctx, JSValueConst this_val)
 JSValue
 qjs_process_object(JSContext *ctx, int argc, const char **argv)
 {
-    JSValue  obj;
+    int      i;
+    JSValue  obj, str, val;
+
+    val = JS_NewArray(ctx);
+    if (JS_IsException(val)) {
+        return JS_EXCEPTION;
+    }
+
+    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;
+        }
+
+        if (JS_DefinePropertyValueUint32(ctx, val, i, str, JS_PROP_C_W_E) < 0) {
+            JS_FreeValue(ctx, str);
+            JS_FreeValue(ctx, val);
+            return JS_EXCEPTION;
+        }
+    }
 
     obj = JS_NewObject(ctx);
     if (JS_IsException(obj)) {
+        JS_FreeValue(ctx, val);
         return JS_EXCEPTION;
     }
 
     JS_SetPropertyFunctionList(ctx, obj, qjs_process_proto,
                                njs_nitems(qjs_process_proto));
 
-    JS_SetOpaque(obj, argv);
-
-    if (JS_SetPropertyStr(ctx, obj, "argc", JS_NewInt32(ctx, argc)) < 0) {
-        JS_FreeValue(ctx, obj);
+    if (JS_SetPropertyStr(ctx, obj, "argv", val) < 0) {
+        JS_FreeValue(ctx, val);
         return JS_EXCEPTION;
     }
 


More information about the nginx-devel mailing list