Skip to content

Commit 283282f

Browse files
committed
Modules: refactored preloading.
1 parent 16f42c8 commit 283282f

File tree

2 files changed

+33
-78
lines changed

2 files changed

+33
-78
lines changed

nginx/ngx_js.c

Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static ngx_int_t ngx_engine_njs_string(ngx_engine_t *e,
5757
njs_opaque_value_t *value, ngx_str_t *str);
5858
static void ngx_engine_njs_destroy(ngx_engine_t *e, ngx_js_ctx_t *ctx,
5959
ngx_js_loc_conf_t *conf);
60+
static ngx_int_t ngx_js_init_preload_vm(njs_vm_t *vm, ngx_js_loc_conf_t *conf);
6061

6162
#if (NJS_HAVE_QUICKJS)
6263
static ngx_int_t ngx_engine_qjs_init(ngx_engine_t *engine,
@@ -547,8 +548,8 @@ static ngx_int_t
547548
ngx_engine_njs_init(ngx_engine_t *engine, ngx_engine_opts_t *opts)
548549
{
549550
njs_vm_t *vm;
550-
ngx_int_t rc;
551-
njs_vm_opt_t vm_options;
551+
ngx_int_t rc;
552+
njs_vm_opt_t vm_options;
552553

553554
njs_vm_opt_init(&vm_options);
554555

@@ -558,6 +559,7 @@ ngx_engine_njs_init(ngx_engine_t *engine, ngx_engine_opts_t *opts)
558559
vm_options.file = opts->file;
559560
vm_options.argv = ngx_argv;
560561
vm_options.argc = ngx_argc;
562+
vm_options.init = 1;
561563

562564
vm = njs_vm_create(&vm_options);
563565
if (vm == NULL) {
@@ -596,6 +598,15 @@ ngx_engine_njs_compile(ngx_js_loc_conf_t *conf, ngx_log_t *log, u_char *start,
596598
static const njs_str_t file_name_key = njs_str("fileName");
597599

598600
vm = conf->engine->u.njs.vm;
601+
602+
if (conf->preload_objects != NGX_CONF_UNSET_PTR) {
603+
if (ngx_js_init_preload_vm(vm, conf) != NGX_OK) {
604+
ngx_log_error(NGX_LOG_EMERG, log, 0,
605+
"failed to initialize preload objects");
606+
return NGX_ERROR;
607+
}
608+
}
609+
599610
end = start + size;
600611

601612
rc = njs_vm_compile(vm, &start, end);
@@ -641,14 +652,10 @@ ngx_engine_njs_compile(ngx_js_loc_conf_t *conf, ngx_log_t *log, u_char *start,
641652
ngx_engine_t *
642653
ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
643654
{
644-
njs_vm_t *vm;
645-
njs_int_t rc;
646-
njs_str_t key;
647-
ngx_str_t exception;
648-
ngx_uint_t i;
649-
ngx_engine_t *engine;
650-
njs_opaque_value_t retval;
651-
ngx_js_named_path_t *preload;
655+
njs_vm_t *vm;
656+
ngx_str_t exception;
657+
ngx_engine_t *engine;
658+
njs_opaque_value_t retval;
652659

653660
vm = njs_vm_clone(cf->engine->u.njs.vm, external);
654661
if (vm == NULL) {
@@ -664,27 +671,6 @@ ngx_njs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
664671
engine->pool = njs_vm_memory_pool(vm);
665672
engine->u.njs.vm = vm;
666673

667-
/* bind objects from preload vm */
668-
669-
if (cf->preload_objects != NGX_CONF_UNSET_PTR) {
670-
preload = cf->preload_objects->elts;
671-
672-
for (i = 0; i < cf->preload_objects->nelts; i++) {
673-
key.start = preload[i].name.data;
674-
key.length = preload[i].name.len;
675-
676-
rc = njs_vm_value(cf->preload_vm, &key, njs_value_arg(&retval));
677-
if (rc != NJS_OK) {
678-
return NULL;
679-
}
680-
681-
rc = njs_vm_bind(vm, &key, njs_value_arg(&retval), 0);
682-
if (rc != NJS_OK) {
683-
return NULL;
684-
}
685-
}
686-
}
687-
688674
if (njs_vm_start(vm, njs_value_arg(&retval)) == NJS_ERROR) {
689675
ngx_js_exception(vm, &exception);
690676

@@ -978,9 +964,6 @@ ngx_qjs_clone(ngx_js_ctx_t *ctx, ngx_js_loc_conf_t *cf, void *external)
978964

979965
JS_SetHostPromiseRejectionTracker(rt, ngx_qjs_rejection_tracker, ctx);
980966

981-
982-
/* TODO: bind objects from preload vm */
983-
984967
rv = JS_UNDEFINED;
985968
pc = engine->precompiled->start;
986969
length = engine->precompiled->items;
@@ -3271,32 +3254,20 @@ ngx_js_preload_object(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
32713254
}
32723255

32733256

3274-
ngx_int_t
3275-
ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf)
3257+
static ngx_int_t
3258+
ngx_js_init_preload_vm(njs_vm_t *vm, ngx_js_loc_conf_t *conf)
32763259
{
32773260
u_char *p, *start;
32783261
size_t size;
3279-
njs_vm_t *vm;
32803262
njs_int_t ret;
32813263
ngx_uint_t i;
3282-
njs_vm_opt_t options;
32833264
njs_opaque_value_t retval;
32843265
ngx_js_named_path_t *preload;
32853266

3286-
njs_vm_opt_init(&options);
3287-
3288-
options.init = 1;
3289-
options.addons = njs_js_addon_modules_shared;
3290-
3291-
vm = njs_vm_create(&options);
3292-
if (vm == NULL) {
3293-
goto error;
3294-
}
3295-
32963267
njs_str_t str = njs_str(
3297-
"import fs from 'fs';"
3268+
"import __fs from 'fs';"
32983269

3299-
"let g = (function (np, no, nf, nsp, r) {"
3270+
"{ let g = (function (np, no, nf, nsp, r) {"
33003271
"return function (n, p) {"
33013272
"p = (p[0] == '/') ? p : ngx.conf_prefix + p;"
33023273
"let o = r(p);"
@@ -3312,7 +3283,7 @@ ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf)
33123283
"return;"
33133284
"}"
33143285
"})(JSON.parse,Object,Object.freeze,"
3315-
"Object.setPrototypeOf,fs.readFileSync);\n"
3286+
"Object.setPrototypeOf,__fs.readFileSync);\n"
33163287
);
33173288

33183289
size = str.length;
@@ -3323,7 +3294,9 @@ ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf)
33233294
+ preload[i].path.len;
33243295
}
33253296

3326-
start = ngx_pnalloc(cf->pool, size);
3297+
size += sizeof("}\n") - 1;
3298+
3299+
start = njs_mp_alloc(njs_vm_memory_pool(vm), size);
33273300
if (start == NULL) {
33283301
return NGX_ERROR;
33293302
}
@@ -3339,27 +3312,24 @@ ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf)
33393312
p = ngx_cpymem(p, "');\n", sizeof("');\n") - 1);
33403313
}
33413314

3315+
p = ngx_cpymem(p, "}\n", sizeof("}\n") - 1);
3316+
33423317
ret = njs_vm_compile(vm, &start, start + size);
33433318
if (ret != NJS_OK) {
3344-
goto error;
3319+
return NGX_ERROR;
33453320
}
33463321

33473322
ret = njs_vm_start(vm, njs_value_arg(&retval));
33483323
if (ret != NJS_OK) {
3349-
goto error;
3324+
return NGX_ERROR;
33503325
}
33513326

3352-
conf->preload_vm = vm;
3353-
3354-
return NGX_OK;
3355-
3356-
error:
3357-
3358-
if (vm != NULL) {
3359-
njs_vm_destroy(vm);
3327+
ret = njs_vm_reuse(vm);
3328+
if (ret != NJS_OK) {
3329+
return NGX_ERROR;
33603330
}
33613331

3362-
return NGX_ERROR;
3332+
return NGX_OK;
33633333
}
33643334

33653335

@@ -3384,9 +3354,6 @@ ngx_js_merge_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
33843354
conf->type = prev->type;
33853355
conf->paths = prev->paths;
33863356
conf->engine = prev->engine;
3387-
3388-
conf->preload_vm = prev->preload_vm;
3389-
33903357
return NGX_OK;
33913358
}
33923359
}
@@ -3836,12 +3803,6 @@ ngx_js_init_conf_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
38363803
return NGX_ERROR;
38373804
}
38383805

3839-
if (conf->preload_objects != NGX_CONF_UNSET_PTR) {
3840-
if (ngx_js_init_preload_vm(cf, (ngx_js_loc_conf_t *)conf) != NGX_OK) {
3841-
return NGX_ERROR;
3842-
}
3843-
}
3844-
38453806
size = 0;
38463807

38473808
import = conf->imports->elts;
@@ -3950,10 +3911,6 @@ ngx_js_cleanup_vm(void *data)
39503911
ngx_js_loc_conf_t *jscf = data;
39513912

39523913
jscf->engine->destroy(jscf->engine, NULL, NULL);
3953-
3954-
if (jscf->preload_objects != NGX_CONF_UNSET_PTR) {
3955-
njs_vm_destroy(jscf->preload_vm);
3956-
}
39573914
}
39583915

39593916

nginx/ngx_js.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ typedef struct {
126126
ngx_array_t *imports; \
127127
ngx_array_t *paths; \
128128
\
129-
njs_vm_t *preload_vm; \
130129
ngx_array_t *preload_objects; \
131130
\
132131
size_t buffer_size; \
@@ -393,7 +392,6 @@ void ngx_js_logger(ngx_connection_t *c, ngx_uint_t level,
393392
char * ngx_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
394393
char * ngx_js_engine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
395394
char * ngx_js_preload_object(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
396-
ngx_int_t ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf);
397395
ngx_int_t ngx_js_merge_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf,
398396
ngx_js_loc_conf_t *prev,
399397
ngx_int_t (*init_vm)(ngx_conf_t *cf, ngx_js_loc_conf_t *conf));

0 commit comments

Comments
 (0)