1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

feat: support null value returns for toValue (#325)

Add nullValue as the return value for the nil case in toValue.

Co-authored-by: Ryan Macleod <ryan.macleod@cumul8.com>
This commit is contained in:
Ryan 2021-09-27 07:47:21 -03:00 committed by GitHub
parent 67c8da69a8
commit 26cb6ccce6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 4 deletions

View File

@ -228,7 +228,7 @@ func Test_reflectStruct(t *testing.T) {
test(` test(`
ret = abc.FuncReturn2(); ret = abc.FuncReturn2();
if (ret && ret.length && ret.length == 2 && ret[0] == "def" && ret[1] === undefined) { if (ret && ret.length && ret.length == 2 && ret[0] == "def" && ret[1] === null) {
true; true;
} else { } else {
false; false;

View File

@ -325,8 +325,7 @@ func toValue(value interface{}) Value {
case _result: case _result:
return Value{valueResult, value} return Value{valueResult, value}
case nil: case nil:
// TODO Ugh. return nullValue
return Value{}
case reflect.Value: case reflect.Value:
for value.Kind() == reflect.Ptr { for value.Kind() == reflect.Ptr {
// We were given a pointer, so we'll drill down until we get a non-pointer // We were given a pointer, so we'll drill down until we get a non-pointer

View File

@ -39,7 +39,8 @@ func TestToValue(t *testing.T) {
vm := tester.vm vm := tester.vm
value, _ := vm.ToValue(nil) value, _ := vm.ToValue(nil)
is(value, "undefined") is(value, "null")
is(value, nullValue)
value, _ = vm.ToValue((*byte)(nil)) value, _ = vm.ToValue((*byte)(nil))
is(value, "undefined") is(value, "undefined")