Browse Source

ToValue() returns null for Zero values.

Dmitry Panov 9 years ago
parent
commit
19bc415db8
2 changed files with 15 additions and 1 deletions
  1. 5 1
      runtime.go
  2. 10 0
      runtime_test.go

+ 5 - 1
runtime.go

@@ -799,7 +799,7 @@ reflect based types.
 func (r *Runtime) ToValue(i interface{}) Value {
 func (r *Runtime) ToValue(i interface{}) Value {
 	switch i := i.(type) {
 	switch i := i.(type) {
 	case nil:
 	case nil:
-		return _undefined
+		return _null
 	case Value:
 	case Value:
 		// TODO: prevent importing Objects from a different runtime
 		// TODO: prevent importing Objects from a different runtime
 		return i
 		return i
@@ -884,6 +884,10 @@ func (r *Runtime) ToValue(i interface{}) Value {
 		value = reflect.Indirect(value)
 		value = reflect.Indirect(value)
 	}
 	}
 
 
+	if !value.IsValid() {
+		return _null
+	}
+
 	switch value.Kind() {
 	switch value.Kind() {
 	case reflect.Map:
 	case reflect.Map:
 		if value.Type().Name() == "" {
 		if value.Type().Name() == "" {

+ 10 - 0
runtime_test.go

@@ -486,6 +486,16 @@ func TestGoFuncError(t *testing.T) {
 	}
 	}
 }
 }
 
 
+func TestToValueNil(t *testing.T) {
+	type T struct{}
+	var a *T
+	vm := New()
+
+	if v := vm.ToValue(a); !IsNull(v) {
+		t.Fatalf("Expected null, got: %v", v)
+	}
+}
+
 func TestJSONEscape(t *testing.T) {
 func TestJSONEscape(t *testing.T) {
 	const SCRIPT = `
 	const SCRIPT = `
 	var a = "\\+1";
 	var a = "\\+1";