Welcome! Log In Create A New Profile

Advanced

[njs] Added support for module mode of execution.

Dmitry Volyntsev
April 12, 2019 01:00PM
details: https://hg.nginx.org/njs/rev/f582672967ad
branches:
changeset: 886:f582672967ad
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Wed Apr 10 17:46:29 2019 +0300
description:
Added support for module mode of execution.

According to ES6:15.2 global this is undefined in module
mode.

diffstat:

njs/njs.h | 1 +
njs/njs_generator.c | 13 ++++++++++++-
njs/njs_parser_terminal.c | 5 +++++
njs/njs_shell.c | 34 +++++++++++++++++++++++++++-------
njs/test/njs_expect_test.exp | 12 ++++++++++++
5 files changed, 57 insertions(+), 8 deletions(-)

diffs (144 lines):

diff -r 1213e0a2b485 -r f582672967ad njs/njs.h
--- a/njs/njs.h Fri Apr 12 18:36:02 2019 +0300
+++ b/njs/njs.h Wed Apr 10 17:46:29 2019 +0300
@@ -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 -r 1213e0a2b485 -r f582672967ad njs/njs_generator.c
--- a/njs/njs_generator.c Fri Apr 12 18:36:02 2019 +0300
+++ b/njs/njs_generator.c Wed Apr 10 17:46:29 2019 +0300
@@ -382,7 +382,6 @@ njs_generator(njs_vm_t *vm, njs_generato
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_generato
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 -r 1213e0a2b485 -r f582672967ad njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c Fri Apr 12 18:36:02 2019 +0300
+++ b/njs/njs_parser_terminal.c Wed Apr 10 17:46:29 2019 +0300
@@ -228,6 +228,11 @@ njs_parser_reference(njs_vm_t *vm, njs_p

node->token = NJS_TOKEN_GLOBAL_THIS;

+ if (vm->options.module) {
+ node->u.value = njs_value_undefined;
+ break;
+ }
+
/* Fall through. */

case NJS_TOKEN_NJS:
diff -r 1213e0a2b485 -r f582672967ad njs/njs_shell.c
--- a/njs/njs_shell.c Fri Apr 12 18:36:02 2019 +0300
+++ b/njs/njs_shell.c Wed Apr 10 17:46:29 2019 +0300
@@ -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 ar
"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 ar
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 -r 1213e0a2b485 -r f582672967ad njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp Fri Apr 12 18:36:02 2019 +0300
+++ b/njs/test/njs_expect_test.exp Wed Apr 10 17:46:29 2019 +0300
@@ -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 {
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Added support for module mode of execution.

Dmitry Volyntsev 122 April 12, 2019 01:00PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 108
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready