From: Dmitry Volyntsev Date: Wed, 10 Apr 2019 14:46:29 +0000 (+0300) Subject: Added support for module mode of execution. X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=3a5298339383e5c85a80d20bc23365e918b2d8e7;p=njs.git Added support for module mode of execution. According to ES6:15.2 global this is undefined in module mode. --- diff --git a/njs/njs.h b/njs/njs.h index 51cc6ae1..fe79b8d6 100644 --- a/njs/njs.h +++ b/njs/njs.h @@ -148,6 +148,7 @@ typedef struct { uint8_t accumulative; /* 1 bit */ uint8_t backtrace; /* 1 bit */ uint8_t sandbox; /* 1 bit */ + uint8_t module; /* 1 bit */ } njs_vm_opt_t; diff --git a/njs/njs_generator.c b/njs/njs_generator.c index 038e854d..f1143323 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -382,7 +382,6 @@ njs_generator(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node) case NJS_TOKEN_NUMBER: case NJS_TOKEN_STRING: node->index = njs_value_index(vm, &node->u.value, generator->runtime); - if (nxt_fast_path(node->index != NJS_INDEX_NONE)) { return NXT_OK; } @@ -431,6 +430,18 @@ njs_generator(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node) return njs_generate_name(vm, generator, node); case NJS_TOKEN_GLOBAL_THIS: + if (vm->options.module) { + node->index = njs_value_index(vm, &node->u.value, + generator->runtime); + if (nxt_fast_path(node->index != NJS_INDEX_NONE)) { + return NXT_OK; + } + + return NXT_ERROR; + } + + /* Fall through. */ + case NJS_TOKEN_NJS: case NJS_TOKEN_MATH: case NJS_TOKEN_JSON: diff --git a/njs/njs_parser_terminal.c b/njs/njs_parser_terminal.c index 813a636d..55461902 100644 --- a/njs/njs_parser_terminal.c +++ b/njs/njs_parser_terminal.c @@ -228,6 +228,11 @@ njs_parser_reference(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token, node->token = NJS_TOKEN_GLOBAL_THIS; + if (vm->options.module) { + node->u.value = njs_value_undefined; + break; + } + /* Fall through. */ case NJS_TOKEN_NJS: diff --git a/njs/njs_shell.c b/njs/njs_shell.c index 76100046..c58a9662 100644 --- a/njs/njs_shell.c +++ b/njs/njs_shell.c @@ -35,6 +35,7 @@ typedef struct { nxt_int_t interactive; nxt_int_t sandbox; nxt_int_t quiet; + nxt_int_t module; } njs_opts_t; @@ -248,6 +249,7 @@ main(int argc, char **argv) vm_options.accumulative = opts.interactive; vm_options.backtrace = 1; vm_options.sandbox = opts.sandbox; + vm_options.module = opts.module; vm_options.ops = &njs_console_ops; vm_options.external = &njs_console; @@ -278,12 +280,13 @@ njs_get_options(njs_opts_t *opts, int argc, char** argv) "Interactive njs shell.\n" "\n" "Options:\n" - " -d print disassembled code.\n" - " -q disable interactive introduction prompt.\n" - " -s sandbox mode.\n" - " -p set path prefix for modules.\n" - " -v print njs version and exit.\n" - " | - run code from a file or stdin.\n"; + " -d print disassembled code.\n" + " -q disable interactive introduction prompt.\n" + " -s sandbox mode.\n" + " -t script|module source code type (script is default).\n" + " -p set path prefix for modules.\n" + " -v print njs version and exit.\n" + " | - run code from a file or stdin.\n"; ret = NXT_DONE; @@ -317,8 +320,25 @@ njs_get_options(njs_opts_t *opts, int argc, char** argv) opts->sandbox = 1; break; + case 't': + if (++i < argc) { + if (strcmp(argv[i], "module") == 0) { + opts->module = 1; + + } else if (strcmp(argv[i], "script") != 0) { + nxt_error("option \"-t\" unexpected source type: %s\n", + argv[i]); + return NXT_ERROR; + } + + break; + } + + nxt_error("option \"-t\" requires source type\n"); + return NXT_ERROR; + case 'p': - if (argv[++i] != NULL) { + if (++i < argc) { opts->n_paths++; paths = realloc(opts->paths, opts->n_paths * sizeof(char *)); if (paths == NULL) { diff --git a/njs/test/njs_expect_test.exp b/njs/test/njs_expect_test.exp index 1be27429..4f030de9 100644 --- a/njs/test/njs_expect_test.exp +++ b/njs/test/njs_expect_test.exp @@ -658,6 +658,18 @@ njs_test { "undefined\r\n"} } "-s" +# source type + +njs_test { + {"this\r\n" + "this\r\nundefined"} +} "-t module" + +njs_test { + {"this.NaN\r\n" + "this.NaN\r\nNaN"} +} "-t script" + # modules njs_test {