Merge pull request #12 from ImVexed/feat-edgetypes

Added support for null and undefined JS types
This commit is contained in:
V-X 2019-10-08 12:11:05 -05:00 committed by GitHub
commit 06751b7aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
go.sum go.sum
webfiles/ab0x.go 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!") panic("Javascript does not support more than 1 return value!")
} }
if len(val) == 0 {
return JSValueMakeNull(ctx)
}
return toJSValue(ctx, val[0]) return toJSValue(ctx, val[0])
} }
} }
func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflect.Value, error) { func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflect.Value, error) {
if rtype == nil {
rtype = reflect.TypeOf(struct{}{})
}
var rv reflect.Value var rv reflect.Value
var err error var err error
@ -205,7 +213,7 @@ func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflec
val, err := fromJSValue(ctx, ref, rtype.Elem()) val, err := fromJSValue(ctx, ref, rtype.Elem())
if err != nil { if err != nil {
return reflect.Value{}, err return reflect.Zero(rtype), err
} }
values.Index(i).Set(val) values.Index(i).Set(val)
@ -232,8 +240,8 @@ func fromJSValue(ctx JSContextRef, value JSValueRef, rtype reflect.Type) (reflec
} }
JSStringRelease(ref) JSStringRelease(ref)
default: case KJSTypeUndefined, KJSTypeNull:
panic("Not implemented") rv = reflect.Zero(rtype)
} }
return rv, err 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]) 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)
}
}