[njs] Added global njs object.

Dmitry Volyntsev xeioex at nginx.com
Tue Apr 3 10:57:19 UTC 2018


details:   http://hg.nginx.org/njs/rev/a191987786fd
branches:  
changeset: 482:a191987786fd
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Apr 03 13:56:54 2018 +0300
description:
Added global njs object.

diffstat:

 njs/njs_builtin.c            |  19 +++++++++++++++++++
 njs/njs_generator.c          |   1 +
 njs/njs_lexer_keyword.c      |   1 +
 njs/njs_parser.c             |   1 +
 njs/njs_parser.h             |   1 +
 njs/njs_vm.h                 |   1 +
 njs/test/njs_expect_test.exp |   5 +++++
 njs/test/njs_unit_test.c     |   3 +++
 8 files changed, 32 insertions(+), 0 deletions(-)

diffs (119 lines):

diff -r 92b532d27d8d -r a191987786fd njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_builtin.c	Tue Apr 03 13:56:54 2018 +0300
@@ -50,9 +50,11 @@ static nxt_array_t *njs_vm_expression_co
     nxt_str_t *expression);
 static nxt_array_t *njs_object_completions(njs_vm_t *vm, njs_object_t *object);
 
+const njs_object_init_t     njs_njs_object_init;
 
 const njs_object_init_t    *njs_object_init[] = {
     NULL,                         /* global this        */
+    &njs_njs_object_init,         /* global njs object  */
     &njs_math_object_init,        /* Math               */
     &njs_json_object_init,        /* JSON               */
 };
@@ -1086,3 +1088,20 @@ njs_builtin_match_native_function(njs_vm
 
     return NXT_DECLINED;
 }
+
+
+static const njs_object_prop_t  njs_njs_object_properties[] =
+{
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("version"),
+        .value = njs_string(NJS_VERSION),
+    },
+};
+
+
+const njs_object_init_t  njs_njs_object_init = {
+    nxt_string("njs"),
+    njs_njs_object_properties,
+    nxt_nitems(njs_njs_object_properties),
+};
diff -r 92b532d27d8d -r a191987786fd njs/njs_generator.c
--- a/njs/njs_generator.c	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_generator.c	Tue Apr 03 13:56:54 2018 +0300
@@ -307,6 +307,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t
         return njs_generate_name(vm, parser, node);
 
     case NJS_TOKEN_GLOBAL_THIS:
+    case NJS_TOKEN_NJS:
     case NJS_TOKEN_MATH:
     case NJS_TOKEN_JSON:
     case NJS_TOKEN_EVAL:
diff -r 92b532d27d8d -r a191987786fd njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_lexer_keyword.c	Tue Apr 03 13:56:54 2018 +0300
@@ -68,6 +68,7 @@ static const njs_keyword_t  njs_keywords
     /* Builtin objects. */
 
     { nxt_string("this"),          NJS_TOKEN_THIS, 0 },
+    { nxt_string("njs"),           NJS_TOKEN_NJS, 0 },
     { nxt_string("Math"),          NJS_TOKEN_MATH, 0 },
     { nxt_string("JSON"),          NJS_TOKEN_JSON, 0 },
 
diff -r 92b532d27d8d -r a191987786fd njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_parser.c	Tue Apr 03 13:56:54 2018 +0300
@@ -1989,6 +1989,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
 
         /* Fall through. */
 
+    case NJS_TOKEN_NJS:
     case NJS_TOKEN_MATH:
     case NJS_TOKEN_JSON:
         return njs_parser_builtin_object(vm, parser, node);
diff -r 92b532d27d8d -r a191987786fd njs/njs_parser.h
--- a/njs/njs_parser.h	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_parser.h	Tue Apr 03 13:56:54 2018 +0300
@@ -165,6 +165,7 @@ typedef enum {
 #define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_GLOBAL_THIS
 
     NJS_TOKEN_GLOBAL_THIS,
+    NJS_TOKEN_NJS,
     NJS_TOKEN_MATH,
     NJS_TOKEN_JSON,
 
diff -r 92b532d27d8d -r a191987786fd njs/njs_vm.h
--- a/njs/njs_vm.h	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/njs_vm.h	Tue Apr 03 13:56:54 2018 +0300
@@ -870,6 +870,7 @@ enum njs_constructor_e {
 
 enum njs_object_e {
     NJS_OBJECT_THIS = 0,
+    NJS_OBJECT_NJS,
     NJS_OBJECT_MATH,
     NJS_OBJECT_JSON,
 #define NJS_OBJECT_MAX         (NJS_OBJECT_JSON + 1)
diff -r 92b532d27d8d -r a191987786fd njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/test/njs_expect_test.exp	Tue Apr 03 13:56:54 2018 +0300
@@ -23,6 +23,11 @@ type console.help() for more information
     expect eof
 }
 
+njs_test {
+    {"njs.version\r\n"
+     "njs.version\r\n\*\.\*\.\*"}
+}
+
 # simple multi line interation
 njs_test {
     {"var a = 1\r\n"
diff -r 92b532d27d8d -r a191987786fd njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Apr 02 18:01:34 2018 +0300
+++ b/njs/test/njs_unit_test.c	Tue Apr 03 13:56:54 2018 +0300
@@ -5741,6 +5741,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("this"),
       nxt_string("[object Object]") },
 
+    { nxt_string("njs"),
+      nxt_string("[object Object]") },
+
     { nxt_string("var o = Object(); o"),
       nxt_string("[object Object]") },
 


More information about the nginx-devel mailing list