diff --git a/.gitignore b/.gitignore index 544dfe8..b48159b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ go.sum webfiles/ab0x.go -.vscode/ \ No newline at end of file +.vscode/ +/*.dll \ No newline at end of file diff --git a/muon.go b/muon.go index 9b6506e..e087b6e 100644 --- a/muon.go +++ b/muon.go @@ -180,12 +180,20 @@ func (w *Window) makeIPCCallback(f *ipf) func(JSContextRef, JSObjectRef, JSObjec panic("Javascript does not support more than 1 return value!") } + if len(val) == 0 { + return JSValueMakeNull(ctx) + } + return toJSValue(ctx, val[0]) } } func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflect.Value, error) { + if rtype == nil { + rtype = reflect.TypeOf(struct{}{}) + } + var rv reflect.Value var err error @@ -205,7 +213,7 @@ func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflec val, err := fromJSValue(ctx, ref, rtype.Elem()) if err != nil { - return reflect.Value{}, err + return reflect.Zero(rtype), err } values.Index(i).Set(val) @@ -232,8 +240,8 @@ func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflec } JSStringRelease(ref) - default: - panic("Not implemented") + case KJSTypeUndefined, KJSTypeNull: + rv = reflect.Zero(rtype) } return rv, err diff --git a/muon_test.go b/muon_test.go index 57aed87..5499e0e 100644 --- a/muon_test.go +++ b/muon_test.go @@ -95,3 +95,31 @@ func TestArrayType(t *testing.T) { t.Errorf("nums[2] was not 3, got %f", nums[2]) } } + +func TestEmptyType(t *testing.T) { + cfg := &Config{ + Height: 1, + Width: 1, + } + + m := New(cfg, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + + m.Bind("test", func(nullValue string, undefinedValue string) { + if nullValue != "" { + t.Errorf("nullType was not empty!") + } + if undefinedValue != "" { + t.Errorf("undefinedType was not empty!") + } + }) + + go func() { + m.Start() + }() + + _, err := m.Eval(`test(null, undefined)`, nil) + + if err != nil { + t.Error(err) + } +}