[njs] MemoryError reimplemented without its own prototype.

Dmitry Volyntsev xeioex at nginx.com
Mon Nov 20 16:25:31 UTC 2017


details:   http://hg.nginx.org/njs/rev/5f619bcb0e7d
branches:  
changeset: 430:5f619bcb0e7d
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Nov 20 19:24:58 2017 +0300
description:
MemoryError reimplemented without its own prototype.

MemoryError is a special preallocated immutable object.  Its value type
is NJS_OBJECT_INTERNAL_ERROR. Initially the object had its own prototype
object.  It introduced inconsistency between value types and prototype
types, because some routines (for example, njs_object_prototype_to_string())
expect them to be pairwise aligned.

diffstat:

 njs/njs_builtin.c        |    1 -
 njs/njs_error.c          |  103 ++++++++++++++++++++++++----------------------
 njs/njs_error.h          |    1 -
 njs/njs_vm.h             |    9 +--
 njs/test/njs_unit_test.c |    5 +-
 5 files changed, 58 insertions(+), 61 deletions(-)

diffs (222 lines):

diff -r e51a848edba3 -r 5f619bcb0e7d njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Nov 20 19:24:56 2017 +0300
+++ b/njs/njs_builtin.c	Mon Nov 20 19:24:58 2017 +0300
@@ -78,7 +78,6 @@ const njs_object_init_t  *njs_prototype_
     &njs_syntax_error_prototype_init,
     &njs_type_error_prototype_init,
     &njs_uri_error_prototype_init,
-    &njs_memory_error_prototype_init,
 };
 
 
diff -r e51a848edba3 -r 5f619bcb0e7d njs/njs_error.c
--- a/njs/njs_error.c	Mon Nov 20 19:24:56 2017 +0300
+++ b/njs/njs_error.c	Mon Nov 20 19:24:58 2017 +0300
@@ -498,7 +498,7 @@ njs_set_memory_error(njs_vm_t *vm)
 
     nxt_lvlhsh_init(&object->hash);
     nxt_lvlhsh_init(&object->shared_hash);
-    object->__proto__ = &prototypes[NJS_PROTOTYPE_MEMORY_ERROR].object;
+    object->__proto__ = &prototypes[NJS_PROTOTYPE_INTERNAL_ERROR].object;
     object->type = NJS_OBJECT_INTERNAL_ERROR;
     object->shared = 1;
 
@@ -532,6 +532,30 @@ njs_memory_error_constructor(njs_vm_t *v
 }
 
 
+static njs_ret_t
+njs_memory_error_prototype_create(njs_vm_t *vm, njs_value_t *value)
+{
+    int32_t         index;
+    njs_value_t     *proto;
+    njs_function_t  *function;
+
+    /* MemoryError has no its own prototype. */
+
+    index = NJS_PROTOTYPE_INTERNAL_ERROR;
+
+    function = value->data.u.function;
+    proto = njs_property_prototype_create(vm, &function->object.hash,
+                                          &vm->prototypes[index].object);
+    if (proto == NULL) {
+        proto = (njs_value_t *) &njs_value_void;
+    }
+
+    vm->retval = *proto;
+
+    return NXT_OK;
+}
+
+
 static const njs_object_prop_t  njs_memory_error_constructor_properties[] =
 {
     /* MemoryError.name == "MemoryError". */
@@ -552,7 +576,7 @@ static const njs_object_prop_t  njs_memo
     {
         .type = NJS_NATIVE_GETTER,
         .name = njs_string("prototype"),
-        .value = njs_native_getter(njs_object_prototype_create),
+        .value = njs_native_getter(njs_memory_error_prototype_create),
     },
 };
 
@@ -701,6 +725,26 @@ const njs_object_init_t  njs_eval_error_
 };
 
 
+static njs_ret_t
+njs_internal_error_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    if (nargs >= 1 && njs_is_object(&args[0])) {
+
+        /* MemoryError is a nonextensible internal error. */
+        if (!args[0].data.u.object->extensible) {
+            static const njs_value_t name = njs_string("MemoryError");
+
+            vm->retval = name;
+
+            return NJS_OK;
+        }
+    }
+
+    return njs_error_prototype_to_string(vm, args, nargs, unused);
+}
+
+
 static const njs_object_prop_t  njs_internal_error_prototype_properties[] =
 {
     {
@@ -708,6 +752,13 @@ static const njs_object_prop_t  njs_inte
         .name = njs_string("name"),
         .value = njs_string("InternalError"),
     },
+
+    {
+        .type = NJS_METHOD,
+        .name = njs_string("toString"),
+        .value = njs_native_function(njs_internal_error_prototype_to_string,
+                                     0, 0),
+    },
 };
 
 
@@ -801,51 +852,3 @@ const njs_object_init_t  njs_uri_error_p
     njs_uri_error_prototype_properties,
     nxt_nitems(njs_uri_error_prototype_properties),
 };
-
-
-static njs_ret_t
-njs_memory_error_prototype_to_string(njs_vm_t *vm, njs_value_t *args,
-    nxt_uint_t nargs, njs_index_t unused)
-{
-    static const njs_value_t  name = njs_string("MemoryError");
-
-    vm->retval = name;
-
-    return NJS_OK;
-}
-
-
-static const njs_object_prop_t  njs_memory_error_prototype_properties[] =
-{
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("name"),
-        .value = njs_string("MemoryError"),
-    },
-
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_string("message"),
-        .value = njs_string(""),
-    },
-
-    {
-        .type = NJS_METHOD,
-        .name = njs_string("valueOf"),
-        .value = njs_native_function(njs_error_prototype_value_of, 0, 0),
-    },
-
-    {
-        .type = NJS_METHOD,
-        .name = njs_string("toString"),
-        .value = njs_native_function(njs_memory_error_prototype_to_string,
-                                     0, 0),
-    },
-};
-
-
-const njs_object_init_t  njs_memory_error_prototype_init = {
-    nxt_string("MemoryError"),
-    njs_memory_error_prototype_properties,
-    nxt_nitems(njs_memory_error_prototype_properties),
-};
diff -r e51a848edba3 -r 5f619bcb0e7d njs/njs_error.h
--- a/njs/njs_error.h	Mon Nov 20 19:24:56 2017 +0300
+++ b/njs/njs_error.h	Mon Nov 20 19:24:58 2017 +0300
@@ -71,7 +71,6 @@ extern const njs_object_init_t  njs_ref_
 extern const njs_object_init_t  njs_syntax_error_prototype_init;
 extern const njs_object_init_t  njs_type_error_prototype_init;
 extern const njs_object_init_t  njs_uri_error_prototype_init;
-extern const njs_object_init_t  njs_memory_error_prototype_init;
 
 
 #endif /* _NJS_BOOLEAN_H_INCLUDED_ */
diff -r e51a848edba3 -r 5f619bcb0e7d njs/njs_vm.h
--- a/njs/njs_vm.h	Mon Nov 20 19:24:56 2017 +0300
+++ b/njs/njs_vm.h	Mon Nov 20 19:24:58 2017 +0300
@@ -799,8 +799,7 @@ enum njs_prototypes_e {
     NJS_PROTOTYPE_SYNTAX_ERROR,
     NJS_PROTOTYPE_TYPE_ERROR,
     NJS_PROTOTYPE_URI_ERROR,
-    NJS_PROTOTYPE_MEMORY_ERROR,
-#define NJS_PROTOTYPE_MAX      (NJS_PROTOTYPE_MEMORY_ERROR + 1)
+#define NJS_PROTOTYPE_MAX      (NJS_PROTOTYPE_URI_ERROR + 1)
 };
 
 
@@ -833,7 +832,8 @@ enum njs_constructor_e {
     NJS_CONSTRUCTOR_SYNTAX_ERROR =   NJS_PROTOTYPE_SYNTAX_ERROR,
     NJS_CONSTRUCTOR_TYPE_ERROR =     NJS_PROTOTYPE_TYPE_ERROR,
     NJS_CONSTRUCTOR_URI_ERROR =      NJS_PROTOTYPE_URI_ERROR,
-    NJS_CONSTRUCTOR_MEMORY_ERROR =   NJS_PROTOTYPE_MEMORY_ERROR,
+    /* MemoryError has no its own prototype. */
+    NJS_CONSTRUCTOR_MEMORY_ERROR,
 #define NJS_CONSTRUCTOR_MAX    (NJS_CONSTRUCTOR_MEMORY_ERROR + 1)
 };
 
@@ -975,8 +975,7 @@ struct njs_vm_s {
 
     /*
      * MemoryError is statically allocated immutable Error object
-     * with the generic type NJS_OBJECT_INTERNAL_ERROR but its own prototype
-     * object NJS_PROTOTYPE_MEMORY_ERROR.
+     * with the generic type NJS_OBJECT_INTERNAL_ERROR.
      */
     njs_object_t             memory_error_object;
 
diff -r e51a848edba3 -r 5f619bcb0e7d njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Nov 20 19:24:56 2017 +0300
+++ b/njs/test/njs_unit_test.c	Mon Nov 20 19:24:58 2017 +0300
@@ -5291,9 +5291,6 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("URIError('e').name + ': ' + URIError('e').message"),
       nxt_string("URIError: e") },
 
-    { nxt_string("MemoryError('e').name + ': ' + MemoryError('e').message"),
-      nxt_string("MemoryError: ") },
-
     { nxt_string("var e = EvalError('e'); e.name = 'E'; e"),
       nxt_string("E: e") },
 
@@ -5342,7 +5339,7 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("URIError") },
 
     { nxt_string("MemoryError.prototype.name"),
-      nxt_string("MemoryError") },
+      nxt_string("InternalError") },
 
     { nxt_string("EvalError.prototype.message"),
       nxt_string("") },


More information about the nginx-devel mailing list