mirror of https://github.com/ImVexed/muon.git
Merge pull request #12 from ImVexed/feat-edgetypes
Added support for null and undefined JS types
This commit is contained in:
commit
06751b7aab
|
@ -1,3 +1,4 @@
|
||||||
go.sum
|
go.sum
|
||||||
webfiles/ab0x.go
|
webfiles/ab0x.go
|
||||||
.vscode/
|
.vscode/
|
||||||
|
/*.dll
|
14
muon.go
14
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!")
|
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
|
||||||
|
|
28
muon_test.go
28
muon_test.go
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue