Added support for null and undefined JS types

This commit is contained in:
ImVexed 2019-10-08 12:05:55 -05:00
parent ab1289ec7c
commit 7bcc4b646b
3 changed files with 41 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
go.sum
webfiles/ab0x.go
.vscode/
.vscode/
/*.dll

14
muon.go
View File

@ -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

View File

@ -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)
}
}