]> git.kaiwu.me - njs.git/commitdiff
Fixed Object.getPrototypeOf() according to the specification.
authorArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Tue, 19 Nov 2019 14:22:53 +0000 (17:22 +0300)
committerArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Tue, 19 Nov 2019 14:22:53 +0000 (17:22 +0300)
This closes #252 issue on Github.

src/njs_object.c
src/test/njs_unit_test.c

index 76d8698ec8f0676343220b985ea46fb94f2bd321..864298f7ea67acc1f677252e6995cb86fce17ff4 100644 (file)
@@ -1387,6 +1387,7 @@ static njs_int_t
 njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
+    uint32_t     index;
     njs_value_t  *value;
 
     value = njs_arg(args, nargs, 1);
@@ -1396,6 +1397,15 @@ njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         return NJS_OK;
     }
 
+    if (!njs_is_null_or_undefined(value)) {
+        index = njs_primitive_prototype_index(value->type);
+
+        njs_set_type_object(&vm->retval, &vm->prototypes[index].object,
+                            njs_object_value_type(value->type));
+
+        return NJS_OK;
+    }
+
     njs_type_error(vm, "cannot convert %s argument to object",
                    njs_type_string(value->type));
 
index 953c717d1c6e0ed39ed9e1fe3a4510c08872e858..b06426908380e8f31ced2a295f60f89461f224e4 100644 (file)
@@ -11064,11 +11064,9 @@ static njs_unit_test_t  njs_test[] =
                  "Object.getPrototypeOf(o) === Object.prototype"),
       njs_str("true") },
 
-    { njs_str("Object.getPrototypeOf(1)"),
-      njs_str("TypeError: cannot convert number argument to object") },
-
-    { njs_str("Object.getPrototypeOf('a')"),
-      njs_str("TypeError: cannot convert string argument to object") },
+    { njs_str("[true, 42, '' /*, Symbol()*/]"
+              ".every((x) => Object.getPrototypeOf(x) == Object.getPrototypeOf(Object(x)))"),
+      njs_str("true") },
 
     { njs_str("var p = {}; var o = Object.create(p);"
                  "p.isPrototypeOf(o)"),