]> git.kaiwu.me - njs.git/commitdiff
Added support for module mode of execution.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 10 Apr 2019 14:46:29 +0000 (17:46 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 10 Apr 2019 14:46:29 +0000 (17:46 +0300)
According to ES6:15.2 global this is undefined in module
mode.

njs/njs.h
njs/njs_generator.c
njs/njs_parser_terminal.c
njs/njs_shell.c
njs/test/njs_expect_test.exp

index 51cc6ae190fa9837c5bd7921c5410f832dc4efeb..fe79b8d6b4e3df03708440dba784807944ccbb83 100644 (file)
--- 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;
 
 
index 038e854d4f8c7be6b40e5a9e2b4bbdb5ea5ea7e8..f114332333b1473e262e788a00ace5c88081ef6c 100644 (file)
@@ -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:
index 813a636d1c84f66655ceb3a2fc18f66cdc5eeb20..554619026ae2b8f89625f7aaca22f7e6a901c534 100644 (file)
@@ -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:
index 76100046607bf55599135166071ec3cb84617366..c58a9662e9330d8a1d3adc0e6f82b61c62077e2d 100644 (file)
@@ -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"
-        "  <filename> | -  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"
+        "  <filename> | -    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) {
index 1be2742916d41f4448db3a09c0a785ded119d8d2..4f030de93dc2a106afc747c7a1996b01cc8e0176 100644 (file)
@@ -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 {