[njs] Interactive shell: introduced global print() alias to console.log().

Dmitry Volyntsev xeioex at nginx.com
Tue Nov 26 12:42:35 UTC 2019


details:   https://hg.nginx.org/njs/rev/e5917fbde8d2
branches:  
changeset: 1260:e5917fbde8d2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Nov 26 15:11:40 2019 +0300
description:
Interactive shell: introduced global print() alias to console.log().

diffstat:

 src/njs_extern.c         |  13 +++++++++----
 src/njs_shell.c          |  32 ++++++++++++++++++++++++--------
 test/njs_expect_test.exp |   8 ++++++--
 3 files changed, 39 insertions(+), 14 deletions(-)

diffs (111 lines):

diff -r 68c9971d7366 -r e5917fbde8d2 src/njs_extern.c
--- a/src/njs_extern.c	Tue Nov 26 15:09:45 2019 +0300
+++ b/src/njs_extern.c	Tue Nov 26 15:11:40 2019 +0300
@@ -181,10 +181,15 @@ njs_vm_external_create(njs_vm_t *vm, njs
 
     memcpy(obj, &object, sizeof(void *));
 
-    ext_val->type = NJS_EXTERNAL;
-    ext_val->data.truth = 1;
-    ext_val->external.proto = proto;
-    ext_val->external.index = vm->external_objects->items - 1;
+    if (proto->type != NJS_EXTERN_METHOD) {
+        ext_val->type = NJS_EXTERNAL;
+        ext_val->data.truth = 1;
+        ext_val->external.proto = proto;
+        ext_val->external.index = vm->external_objects->items - 1;
+
+    } else {
+        njs_set_function(ext_val, proto->function);
+    }
 
     return NJS_OK;
 }
diff -r 68c9971d7366 -r e5917fbde8d2 src/njs_shell.c
--- a/src/njs_shell.c	Tue Nov 26 15:09:45 2019 +0300
+++ b/src/njs_shell.c	Tue Nov 26 15:11:40 2019 +0300
@@ -604,17 +604,16 @@ njs_console_init(njs_vm_t *vm, njs_conso
 
 
 static njs_int_t
-njs_externals_init(njs_vm_t *vm, njs_console_t *console)
+njs_externals_add(njs_vm_t *vm, njs_external_t *definition,
+    const njs_str_t *name, njs_external_ptr_t object)
 {
-    njs_uint_t          ret;
+    njs_int_t           ret;
     njs_value_t         *value;
     const njs_extern_t  *proto;
 
-    static const njs_str_t name = njs_str("console");
-
-    proto = njs_vm_external_prototype(vm, &njs_externals[0]);
+    proto = njs_vm_external_prototype(vm, definition);
     if (njs_slow_path(proto == NULL)) {
-        njs_stderror("failed to add console proto\n");
+        njs_stderror("failed to add \"%V\" proto\n", name);
         return NJS_ERROR;
     }
 
@@ -623,12 +622,29 @@ njs_externals_init(njs_vm_t *vm, njs_con
         return NJS_ERROR;
     }
 
-    ret = njs_vm_external_create(vm, value, proto, console);
+    ret = njs_vm_external_create(vm, value, proto, object);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
 
-    ret = njs_vm_bind(vm, &name, value, 1);
+    return njs_vm_bind(vm, name, value, 1);
+}
+
+
+static njs_int_t
+njs_externals_init(njs_vm_t *vm, njs_console_t *console)
+{
+    njs_int_t  ret;
+
+    static const njs_str_t  console_name = njs_str("console");
+    static const njs_str_t  print_name = njs_str("print");
+
+    ret = njs_externals_add(vm, &njs_externals[0], &console_name, console);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    ret = njs_externals_add(vm, &njs_ext_console[0], &print_name, console);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
diff -r 68c9971d7366 -r e5917fbde8d2 test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Tue Nov 26 15:09:45 2019 +0300
+++ b/test/njs_expect_test.exp	Tue Nov 26 15:11:40 2019 +0300
@@ -214,6 +214,8 @@ njs_test {
      "console.log(1)\r\n1\r\nundefined\r\n>> "}
     {"console.log(1, 'a')\r\n"
      "console.log(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
+    {"print(1, 'a')\r\n"
+     "print(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
     {"console.log('\\tабв\\nгд')\r\n"
      "console.log('\\\\tабв\\\\nгд')\r\n\tабв\r\nгд\r\nundefined\r\n>> "}
     {"console.dump()\r\n"
@@ -270,13 +272,15 @@ njs_test {
     {"var print = console.dump.bind(console); print(1, 'a', [1, 2])\r\n"
      "1 a \\\[\r\n 1,\r\n 2\r\n]\r\nundefined\r\n>> "}
     {"var print = console.log.bind(console); print(console.a.a)\r\n"
-     "TypeError: cannot get property \"a\" of undefined*at console.log"}
+     "TypeError: cannot get property \"a\" of undefined*at print"}
+    {"print(console.a.a)\r\n"
+     "TypeError: cannot get property \"a\" of undefined*at print"}
 }
 
 # Backtraces for external objects
 njs_test {
     {"console.log(console.a.a)\r\n"
-     "console.log(console.a.a)\r\nTypeError:*at console.log (native)"}
+     "console.log(console.a.a)\r\nTypeError:*at print (native)"}
 }
 
 # dumper


More information about the nginx-devel mailing list