]> git.kaiwu.me - njs.git/commitdiff
QuickJS: qjs_new_context() accepts now additional modules.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 24 Jul 2024 05:55:01 +0000 (22:55 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Wed, 4 Sep 2024 00:58:54 +0000 (17:58 -0700)
external/njs_shell.c
src/qjs.c
src/qjs.h

index addf5a34f7c70064b241470d6c1c727df4ff4481..806936343bf5c8c1f15a17173aaac5e643707c90 100644 (file)
@@ -2731,7 +2731,7 @@ njs_engine_qjs_init(njs_engine_t *engine, njs_opts_t *opts)
         return NJS_ERROR;
     }
 
-    engine->u.qjs.ctx = qjs_new_context(engine->u.qjs.rt, 1);
+    engine->u.qjs.ctx = qjs_new_context(engine->u.qjs.rt, NULL, 1);
     if (engine->u.qjs.ctx == NULL) {
         njs_stderror("JS_NewContext() failed\n");
         return NJS_ERROR;
index de6bf17b83cf059d8f5ed21c34bc90b07f7bcb16..b8c534861535bc62e30040a2d5ffba8c2e93819f 100644 (file)
--- a/src/qjs.c
+++ b/src/qjs.c
@@ -17,7 +17,7 @@ static const JSCFunctionListEntry qjs_global_proto[] = {
 
 
 JSContext *
-qjs_new_context(JSRuntime *rt, _Bool eval)
+qjs_new_context(JSRuntime *rt, qjs_module_t **addons, _Bool eval)
 {
     JSValue       global_obj;
     JSContext     *ctx;
@@ -48,6 +48,14 @@ qjs_new_context(JSRuntime *rt, _Bool eval)
         }
     }
 
+    if (addons != NULL) {
+        for (module = addons; *module != NULL; module++) {
+            if ((*module)->init(ctx, (*module)->name) == NULL) {
+                return NULL;
+            }
+        }
+    }
+
     global_obj = JS_GetGlobalObject(ctx);
 
     JS_SetPropertyFunctionList(ctx, global_obj, qjs_global_proto,
index 563a5b15531c6658711f2933c4f05c8b429bd2d1..531940ea9f725fcb2f2e79d8f4c6ee98c5687ad8 100644 (file)
--- a/src/qjs.h
+++ b/src/qjs.h
@@ -41,7 +41,7 @@ typedef struct {
 } qjs_module_t;
 
 
-JSContext *qjs_new_context(JSRuntime *rt, _Bool eval);
+JSContext *qjs_new_context(JSRuntime *rt, qjs_module_t **addons, _Bool eval);
 
 
 JSValue qjs_buffer_alloc(JSContext *ctx, size_t size);