Browse Source

Fixed panic on null js value conversion.

Dmitry Panov 8 years ago
parent
commit
0013582602
2 changed files with 26 additions and 1 deletions
  1. 3 1
      runtime.go
  2. 23 0
      runtime_test.go

+ 3 - 1
runtime.go

@@ -1097,7 +1097,9 @@ func (r *Runtime) toReflectValue(v Value, typ reflect.Type) (reflect.Value, erro
 	}
 	}
 
 
 	et := v.ExportType()
 	et := v.ExportType()
-
+	if et == nil {
+		return reflect.Zero(typ), nil
+	}
 	if et.AssignableTo(typ) {
 	if et.AssignableTo(typ) {
 		return reflect.ValueOf(v.Export()), nil
 		return reflect.ValueOf(v.Export()), nil
 	} else if et.ConvertibleTo(typ) {
 	} else if et.ConvertibleTo(typ) {

+ 23 - 0
runtime_test.go

@@ -760,6 +760,29 @@ func TestNilCallArg(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestNullCallArg(t *testing.T) {
+	const SCRIPT = `
+	f(null);
+	`
+	vm := New()
+	prg, err := Compile("test.js", SCRIPT, false)
+	if err != nil {
+		t.Fatal(err)
+	}
+	vm.Set("f", func(x *int) bool {
+		return x == nil
+	})
+
+	v, err := vm.RunProgram(prg)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if !v.StrictEquals(valueTrue) {
+		t.Fatalf("Unexpected result: %v", v)
+	}
+}
+
 func TestObjectKeys(t *testing.T) {
 func TestObjectKeys(t *testing.T) {
 	const SCRIPT = `
 	const SCRIPT = `
 	var o = { a: 1, b: 2, c: 3, d: 4 };
 	var o = { a: 1, b: 2, c: 3, d: 4 };