Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Number(null) #25

Closed
drsm opened this issue Jul 1, 2018 · 2 comments
Closed

fix Number(null) #25

drsm opened this issue Jul 1, 2018 · 2 comments
Assignees
Labels

Comments

@drsm
Copy link
Contributor

drsm commented Jul 1, 2018

https://www.ecma-international.org/ecma-262/6.0/#sec-number-constructor-number-value

@xeioex
Copy link
Contributor

xeioex commented Jul 2, 2018

# HG changeset patch
# User Dmitry Volyntsev <xeioex@nginx.com>
# Date 1530552353 -10800
#      Mon Jul 02 20:25:53 2018 +0300
# Node ID bbc7f3cc0c9297bac072f4e86cc83ea9232813cd
# Parent  135f33d06df56e9064b4768bb9642f63b98a7fc9
Fixed Number() with boolean, null and undefined arguments.

This fixes #25 issue on GitHub.

diff --git a/njs/njs_number.c b/njs/njs_number.c
--- a/njs/njs_number.c
+++ b/njs/njs_number.c
@@ -377,7 +377,7 @@ njs_number_constructor(njs_vm_t *vm, njs
     }
 
     if (vm->top_frame->ctor) {
-        object = njs_object_value_alloc(vm, value, value->type);
+        object = njs_object_value_alloc(vm, value, NJS_NUMBER);
         if (nxt_slow_path(object == NULL)) {
             return NXT_ERROR;
         }
@@ -387,7 +387,7 @@ njs_number_constructor(njs_vm_t *vm, njs
         vm->retval.data.truth = 1;
 
     } else {
-        vm->retval = *value;
+        njs_value_number_set(&vm->retval, value->data.u.number);
     }
 
     return NXT_OK;
diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c
+++ b/njs/test/njs_unit_test.c
@@ -6117,6 +6117,30 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("new Number"),
       nxt_string("0") },
 
+    { nxt_string("new Number(undefined)"),
+      nxt_string("NaN") },
+
+    { nxt_string("new Number(null)"),
+      nxt_string("0") },
+
+    { nxt_string("new Number(true)"),
+      nxt_string("1") },
+
+    { nxt_string("new Number(false)"),
+      nxt_string("0") },
+
+    { nxt_string("Number(undefined)"),
+      nxt_string("NaN") },
+
+    { nxt_string("Number(null)"),
+      nxt_string("0") },
+
+    { nxt_string("Number(true)"),
+      nxt_string("1") },
+
+    { nxt_string("Number(false)"),
+      nxt_string("0") },
+
     { nxt_string("Number(123)"),
       nxt_string("123") },
 

@drsm
Copy link
Contributor Author

drsm commented Jul 2, 2018

the patch works for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants