[njs] QuickJS: moving njs object creation to common code.
noreply at nginx.com
noreply at nginx.com
Wed Sep 4 00:59:02 UTC 2024
details: https://github.com/nginx/njs/commit/b70ab370b8fd8c7e37a6491697dfe357031f4632
branches: master
commit: b70ab370b8fd8c7e37a6491697dfe357031f4632
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon, 8 Jul 2024 23:35:12 -0700
description:
QuickJS: moving njs object creation to common code.
So it will be shared between modules and CLI.
---
external/njs_shell.c | 87 ++--------------------------------------------
src/qjs.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/qjs.h | 1 +
3 files changed, 102 insertions(+), 84 deletions(-)
diff --git a/external/njs_shell.c b/external/njs_shell.c
index ec2a3875..addf5a34 100644
--- a/external/njs_shell.c
+++ b/external/njs_shell.c
@@ -1901,46 +1901,6 @@ njs_qjs_clear_timeout(JSContext *ctx, JSValueConst this_val, int argc,
}
-static njs_int_t
-njs_qjs_set_to_string_tag(JSContext *ctx, JSValueConst val, const char *tag)
-{
- JSAtom atom;
- JSValue global_obj, symbol, toStringTag;
- njs_int_t ret;
-
- global_obj = JS_GetGlobalObject(ctx);
-
- symbol = JS_GetPropertyStr(ctx, global_obj, "Symbol");
- JS_FreeValue(ctx, global_obj);
- if (JS_IsException(symbol)) {
- return -1;
- }
-
- toStringTag = JS_GetPropertyStr(ctx, symbol, "toStringTag");
- if (JS_IsException(toStringTag)) {
- JS_FreeValue(ctx, symbol);
- return -1;
- }
-
- atom = JS_ValueToAtom(ctx, toStringTag);
-
- JS_FreeValue(ctx, symbol);
- JS_FreeValue(ctx, toStringTag);
-
- if (atom == JS_ATOM_NULL) {
- JS_ThrowInternalError(ctx, "failed to get atom");
- return -1;
- }
-
- ret = JS_DefinePropertyValue(ctx, val, atom, JS_NewString(ctx, tag),
- JS_PROP_C_W_E);
-
- JS_FreeAtom(ctx, atom);
-
- return ret;
-}
-
-
static JSValue
njs_qjs_process_getter(JSContext *ctx, JSValueConst this_val)
{
@@ -1963,7 +1923,7 @@ njs_qjs_process_getter(JSContext *ctx, JSValueConst this_val)
return JS_EXCEPTION;
}
- ret = njs_qjs_set_to_string_tag(ctx, obj, "process");
+ ret = qjs_set_to_string_tag(ctx, obj, "process");
if (ret == -1) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
@@ -2066,46 +2026,6 @@ error:
return obj;
}
-static JSValue
-njs_qjs_njs_getter(JSContext *ctx, JSValueConst this_val)
-{
- JSValue obj;
- njs_int_t ret;
-
- obj = JS_NewObject(ctx);
- if (JS_IsException(obj)) {
- return JS_EXCEPTION;
- }
-
- ret = njs_qjs_set_to_string_tag(ctx, obj, "njs");
- if (ret == -1) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
-
- ret = JS_SetPropertyStr(ctx, obj, "version_number",
- JS_NewInt32(ctx, NJS_VERSION_NUMBER));
- if (ret == -1) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
-
- ret = JS_SetPropertyStr(ctx, obj, "version",
- JS_NewString(ctx, NJS_VERSION));
- if (ret == -1) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
-
- ret = JS_SetPropertyStr(ctx, obj, "engine", JS_NewString(ctx, "QuickJS"));
- if (ret == -1) {
- JS_FreeValue(ctx, obj);
- return JS_EXCEPTION;
- }
-
- return obj;
-}
-
static njs_int_t njs_qjs_global_init(JSContext *ctx, JSValue global_obj);
static void njs_qjs_dump_error(JSContext *ctx);
@@ -2656,7 +2576,6 @@ njs_qjs_new_262(JSContext *ctx, JSValueConst this_val)
static const JSCFunctionListEntry njs_qjs_global_proto[] = {
JS_CFUNC_DEF("clearTimeout", 1, njs_qjs_clear_timeout),
JS_CFUNC_MAGIC_DEF("print", 0, njs_qjs_console_log, NJS_LOG_INFO),
- JS_CGETSET_DEF("njs", njs_qjs_njs_getter, NULL),
JS_CGETSET_DEF("process", njs_qjs_process_getter, NULL),
JS_CFUNC_MAGIC_DEF("setImmediate", 0, njs_qjs_set_timer, 1),
JS_CFUNC_MAGIC_DEF("setTimeout", 0, njs_qjs_set_timer, 0),
@@ -2840,9 +2759,9 @@ njs_engine_qjs_init(njs_engine_t *engine, njs_opts_t *opts)
goto done;
}
- ret = njs_qjs_set_to_string_tag(ctx, obj, "Console");
+ ret = qjs_set_to_string_tag(ctx, obj, "Console");
if (ret == -1) {
- njs_stderror("njs_qjs_set_to_string_tag() failed\n");
+ njs_stderror("qjs_set_to_string_tag() failed\n");
ret = NJS_ERROR;
goto done;
}
diff --git a/src/qjs.c b/src/qjs.c
index 0a31b748..de6bf17b 100644
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -5,11 +5,21 @@
*/
#include <qjs.h>
+#include <njs.h> /* NJS_VERSION */
+
+
+static JSValue qjs_njs_getter(JSContext *ctx, JSValueConst this_val);
+
+
+static const JSCFunctionListEntry qjs_global_proto[] = {
+ JS_CGETSET_DEF("njs", qjs_njs_getter, NULL),
+};
JSContext *
qjs_new_context(JSRuntime *rt, _Bool eval)
{
+ JSValue global_obj;
JSContext *ctx;
qjs_module_t **module;
@@ -38,10 +48,98 @@ qjs_new_context(JSRuntime *rt, _Bool eval)
}
}
+ global_obj = JS_GetGlobalObject(ctx);
+
+ JS_SetPropertyFunctionList(ctx, global_obj, qjs_global_proto,
+ njs_nitems(qjs_global_proto));
+
+ JS_FreeValue(ctx, global_obj);
+
return ctx;
}
+static JSValue
+qjs_njs_getter(JSContext *ctx, JSValueConst this_val)
+{
+ int ret;
+ JSValue obj;
+
+ obj = JS_NewObject(ctx);
+ if (JS_IsException(obj)) {
+ return JS_EXCEPTION;
+ }
+
+ ret = qjs_set_to_string_tag(ctx, obj, "njs");
+ if (ret == -1) {
+ JS_FreeValue(ctx, obj);
+ return JS_EXCEPTION;
+ }
+
+ ret = JS_SetPropertyStr(ctx, obj, "version_number",
+ JS_NewInt32(ctx, NJS_VERSION_NUMBER));
+ if (ret == -1) {
+ JS_FreeValue(ctx, obj);
+ return JS_EXCEPTION;
+ }
+
+ ret = JS_SetPropertyStr(ctx, obj, "version",
+ JS_NewString(ctx, NJS_VERSION));
+ if (ret == -1) {
+ JS_FreeValue(ctx, obj);
+ return JS_EXCEPTION;
+ }
+
+ ret = JS_SetPropertyStr(ctx, obj, "engine", JS_NewString(ctx, "QuickJS"));
+ if (ret == -1) {
+ JS_FreeValue(ctx, obj);
+ return JS_EXCEPTION;
+ }
+
+ return obj;
+}
+
+
+int
+qjs_set_to_string_tag(JSContext *ctx, JSValueConst val, const char *tag)
+{
+ int ret;
+ JSAtom atom;
+ JSValue global_obj, symbol, toStringTag;
+
+ global_obj = JS_GetGlobalObject(ctx);
+
+ symbol = JS_GetPropertyStr(ctx, global_obj, "Symbol");
+ JS_FreeValue(ctx, global_obj);
+ if (JS_IsException(symbol)) {
+ return -1;
+ }
+
+ toStringTag = JS_GetPropertyStr(ctx, symbol, "toStringTag");
+ if (JS_IsException(toStringTag)) {
+ JS_FreeValue(ctx, symbol);
+ return -1;
+ }
+
+ atom = JS_ValueToAtom(ctx, toStringTag);
+
+ JS_FreeValue(ctx, symbol);
+ JS_FreeValue(ctx, toStringTag);
+
+ if (atom == JS_ATOM_NULL) {
+ JS_ThrowInternalError(ctx, "failed to get atom");
+ return -1;
+ }
+
+ ret = JS_DefinePropertyValue(ctx, val, atom, JS_NewString(ctx, tag),
+ JS_PROP_C_W_E);
+
+ JS_FreeAtom(ctx, atom);
+
+ return ret;
+}
+
+
int
qjs_to_bytes(JSContext *ctx, qjs_bytes_t *bytes, JSValueConst value)
{
diff --git a/src/qjs.h b/src/qjs.h
index f8eabefa..563a5b15 100644
--- a/src/qjs.h
+++ b/src/qjs.h
@@ -64,6 +64,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);
typedef struct {
int tag;
More information about the nginx-devel
mailing list