]> git.kaiwu.me - njs.git/commitdiff
Added global njs object.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 3 Apr 2018 10:56:54 +0000 (13:56 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 3 Apr 2018 10:56:54 +0000 (13:56 +0300)
njs/njs_builtin.c
njs/njs_generator.c
njs/njs_lexer_keyword.c
njs/njs_parser.c
njs/njs_parser.h
njs/njs_vm.h
njs/test/njs_expect_test.exp
njs/test/njs_unit_test.c

index a9c25d2e7c1fcd971079933db962e976f340a7c5..1e1b668a5100fb3a0773aa07b9c1f4902054ae6e 100644 (file)
@@ -50,9 +50,11 @@ static nxt_array_t *njs_vm_expression_completions(njs_vm_t *vm,
     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_t *vm, njs_function_t *function,
 
     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),
+};
index 7e73a1a2fc5e05687e4bd57e005fdab2f90a0d95..cc3ee547bb8b83ff8a3480b09acbeb5d9310e8d4 100644 (file)
@@ -307,6 +307,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node)
         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:
index 872369555a279a9860ee812692de798f38fc78db..939bde297012e6fd5729e70cbde105ee516d2cc4 100644 (file)
@@ -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 },
 
index 7b33e7e7c44727a5d0df990eb33c598af51d0f8f..3b01043072bf97c1d8a3fceb447829e21c64b59e 100644 (file)
@@ -1989,6 +1989,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token)
 
         /* Fall through. */
 
+    case NJS_TOKEN_NJS:
     case NJS_TOKEN_MATH:
     case NJS_TOKEN_JSON:
         return njs_parser_builtin_object(vm, parser, node);
index c2a33db4ccfdc96ef2ef0a9d2e37f25770f3f830..6d1b261db99ebad4d878a0ab232facd1359b651d 100644 (file)
@@ -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,
 
index 6d605c434853372948b26185a603ebab130cee0e..c182ddd7fe2418f066d3f48c28bb748e202e8063 100644 (file)
@@ -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)
index ba91edc7946f0dc93166adccb5a3ef5217d9d18e..ce61f3799acdb9b27991ccd9fcff7694840e1350 100644 (file)
@@ -23,6 +23,11 @@ type console.help() for more information\r
     expect eof
 }
 
+njs_test {
+    {"njs.version\r\n"
+     "njs.version\r\n\*\.\*\.\*"}
+}
+
 # simple multi line interation
 njs_test {
     {"var a = 1\r\n"
index 900074c10552b81ef40c0c6a614147289f23447a..88f40e37e7cfeb21958e2f98576c21506e4aa004 100644 (file)
@@ -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]") },