Skip to content

Commit

Permalink
add delete object
Browse files Browse the repository at this point in the history
  • Loading branch information
realPy committed Oct 31, 2024
1 parent 94b6a15 commit c17004b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
18 changes: 18 additions & 0 deletions base/baseobject/baseobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var setFunc js.Value
var getFunc js.Value
var deleteFunc js.Value
var callFunc js.Value
var invokeFunc js.Value
var newFunc js.Value
Expand All @@ -19,8 +20,10 @@ func SetSyscall() {
eval_(`hCall = (obj,method,args) => { try { func=Reflect.get(obj,method); return [true,Reflect.apply(func,obj,args)] } catch (err) { return [false,err] } }`)
eval_(`hInvoke = (func,args) => { try { return [true,Reflect.apply(func,undefined,args)] } catch (err) { return [false,err] } }`)
eval_(`hNew = (func,args) => { try { return [true,Reflect.construct(func,args)] } catch (err) { return [false,err] } }`)
eval_(`hDelete = (obj,property) => { try { return [true,Reflect.deleteProperty(obj,property)] }catch(err){ return [false,err] } }`)
setFunc = js.Global().Get("hSet")
getFunc = js.Global().Get("hGet")
deleteFunc = js.Global().Get("hDelete")
callFunc = js.Global().Get("hCall")
invokeFunc = js.Global().Get("hInvoke")
newFunc = js.Global().Get("hNew")
Expand All @@ -39,6 +42,16 @@ func Set(obj js.Value, name string, val interface{}) error {
return err
}

func Delete(obj js.Value, property string) (js.Value, error) {
ret := deleteFunc.Invoke(obj, js.ValueOf(property))

if ret.Index(0).Bool() {
return ret.Index(1), nil
} else {
return ret.Index(1), errors.New(ret.Index(1).Get("message").String())
}
}

func Get(obj js.Value, i interface{}) (js.Value, error) {
var invokvar interface{}

Expand Down Expand Up @@ -546,6 +559,11 @@ func (b BaseObject) SetAttributeInt(attribute string, value int) error {
return b.Set(attribute, js.ValueOf(value))
}

func (b BaseObject) SetAttributeInt64(attribute string, value int64) error {

return b.Set(attribute, js.ValueOf(value))
}

func (b BaseObject) GetAttributeDouble(attribute string) (float64, error) {

var err error
Expand Down
65 changes: 65 additions & 0 deletions base/baseobject/baseobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,29 @@ func TestExport(t *testing.T) {

}

func TestDelete(t *testing.T) {

var err error
var obj js.Value
var b BaseObject

Eval("customobject=new Object()")
Eval("customobject.Value=\"HelloWorld\"")

if obj, err = Get(js.Global(), "customobject"); err == nil {

if b, err = NewFromJSObject(obj); err == nil {
Delete(b.JSObject(), "Value")

v, _ := b.Get("Value")
if !v.IsUndefined() {
t.Error("delete should set undefined")
}

}
}

}
func TestGetAttributeString(t *testing.T) {

var err error
Expand Down Expand Up @@ -825,6 +848,48 @@ func TestGetAttributeInt(t *testing.T) {

}

func TestSetAttributeInt64(t *testing.T) {
var obj js.Value
var err error
var b BaseObject

Eval("customobject=new Object()")

if obj, err = Get(js.Global(), "customobject"); err == nil {

if b, err = NewFromJSObject(obj); err == nil {
if err = b.SetAttributeInt64("Value", 69); err == nil {
var v int64
if v, err = b.GetAttributeInt64("Value"); err == nil {
if v != 69 {
t.Errorf("Must be equal to 69")
}
} else {

t.Errorf(err.Error())

}

} else {

t.Errorf(err.Error())

}

} else {

t.Errorf(err.Error())

}

} else {

t.Errorf(err.Error())

}

}

func TestSetAttributeInt(t *testing.T) {
var obj js.Value
var err error
Expand Down
4 changes: 4 additions & 0 deletions base/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestParse(t *testing.T) {
}`

if j, err := Parse(str); testingutils.AssertErr(t, err) {

k, _ := j.Keys()
l, _ := k.Length()
testingutils.AssertExpect(t, 3, l)
goValue := j.Map()

testingutils.AssertExpect(t, "John", goValue.(map[string]interface{})["name"])
Expand Down
5 changes: 5 additions & 0 deletions base/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ func (o Object) Map() (objectmap.ObjectMap, error) {
}
return newMap, err
}

func (o Object) Delete(property string) error {
_, err := baseobject.Delete(o.JSObject(), property)
return err
}
20 changes: 20 additions & 0 deletions base/object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ func TestMethods(t *testing.T) {

}
}

func TestDeleteProperty(t *testing.T) {

baseobject.Eval(`
objnew= new Object();
objnew.test=3;
`)

if obj, err := baseobject.Get(js.Global(), "objnew"); testingutils.AssertErr(t, err) {
if o, err := NewFromJSObject(obj); testingutils.AssertErr(t, err) {
v, _ := o.GetAttributeInt64("test")
testingutils.AssertExpect(t, int64(3), v)
testingutils.AssertExpect(t, nil, o.Delete("test"))
_, err := o.GetAttributeInt64("test")
testingutils.AssertExpect(t, "Undefined value", err.Error())

}
}

}

0 comments on commit c17004b

Please sign in to comment.