1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-04-07 07:19:58 +02:00

Added .Clone() method to runtime Value

This commit is contained in:
Tim Voronov 2018-09-27 11:53:26 -04:00
parent eeee30856e
commit 69a7295da5
14 changed files with 80 additions and 11 deletions

View File

@ -1288,11 +1288,11 @@ func TestFunctionCall(t *testing.T) {
So(string(out), ShouldEqual, `"int"`)
})
Convey("Should compile SLEEP(10) RETURN 1", t, func() {
Convey("Should compile WAIT(10) RETURN 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
SLEEP(10)
WAIT(10)
RETURN 1
`)
@ -1305,13 +1305,13 @@ func TestFunctionCall(t *testing.T) {
So(string(out), ShouldEqual, `1`)
})
Convey("Should compile LET duration = 10 SLEEP(duration) RETURN 1", t, func() {
Convey("Should compile LET duration = 10 WAIT(duration) RETURN 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
LET duration = 10
SLEEP(duration)
WAIT(duration)
RETURN 1
`)
@ -1332,7 +1332,7 @@ func TestFunctionCall(t *testing.T) {
FOR i IN [1, 2, 3, 4]
LET duration = 10
SLEEP(duration)
WAIT(duration)
RETURN i * 2
`)

View File

@ -45,6 +45,7 @@ type Value interface {
Compare(other Value) int
Unwrap() interface{}
Hash() int
Clone() Value
}
func IsTypeOf(value Value, check Type) bool {

View File

@ -95,6 +95,16 @@ func (t *Array) Hash() int {
return out
}
func (t *Array) Clone() core.Value {
c := NewArray(len(t.value))
for _, el := range t.value {
c.Push(el)
}
return c
}
func (t *Array) Length() Int {
return Int(len(t.value))
}

View File

@ -74,6 +74,14 @@ func (b *Binary) Hash() int {
return out
}
func (b *Binary) Clone() core.Value {
c := make([]byte, len(b.values))
copy(c, b.values)
return NewBinary(c)
}
func (b *Binary) Length() Int {
return NewInt(len(b.values))
}

View File

@ -109,3 +109,7 @@ func (t Boolean) Hash() int {
return out
}
func (t Boolean) Clone() core.Value {
return t
}

View File

@ -97,3 +97,7 @@ func (t DateTime) Hash() int {
return out
}
func (t DateTime) Clone() core.Value {
return NewDateTime(t.Time)
}

View File

@ -129,3 +129,7 @@ func (t Float) Hash() int {
return out
}
func (t Float) Clone() core.Value {
return t
}

View File

@ -113,3 +113,7 @@ func (t Int) Unwrap() interface{} {
func (t Int) Hash() int {
return int(t)
}
func (t Int) Clone() core.Value {
return t
}

View File

@ -36,3 +36,7 @@ func (t *none) Unwrap() interface{} {
func (t *none) Hash() int {
return 0
}
func (t *none) Clone() core.Value {
return None
}

View File

@ -106,6 +106,16 @@ func (t *Object) Hash() int {
return out
}
func (t *Object) Clone() core.Value {
c := NewObject()
for k, v := range t.value {
c.Set(NewString(k), v)
}
return c
}
func (t *Object) Length() Int {
return Int(len(t.value))
}

View File

@ -105,6 +105,10 @@ func (t String) Hash() int {
return out
}
func (t String) Clone() core.Value {
return t
}
func (t String) Length() Int {
return Int(len(t))
}

View File

@ -201,6 +201,10 @@ func (doc *HtmlDocument) Hash() int {
return out
}
func (doc *HtmlDocument) Clone() core.Value {
return values.None
}
func (doc *HtmlDocument) Compare(other core.Value) int {
doc.Lock()
defer doc.Unlock()

View File

@ -216,6 +216,10 @@ func (el *HtmlElement) Value() core.Value {
return values.None
}
func (el *HtmlElement) Clone() core.Value {
return values.None
}
func (el *HtmlElement) Length() values.Int {
return values.NewInt(len(el.children))
}
@ -235,7 +239,8 @@ func (el *HtmlElement) GetAttributes() core.Value {
return values.None
}
return val
// returning shallow copy
return val.Clone()
}
func (el *HtmlElement) GetAttribute(name values.String) core.Value {
@ -384,19 +389,19 @@ func (el *HtmlElement) handleAttrModified(message interface{}) {
val, err := el.attributes.Value()
// failed to load
// TODO: Log
if err != nil {
return
}
el.Lock()
defer el.Unlock()
attrs, ok := val.(*values.Object)
// TODO: Log
if !ok {
return
}
// TODO: actually, we need to sync it too...
attrs.Set(values.NewString(reply.Name), values.NewString(reply.Value))
}
@ -422,14 +427,15 @@ func (el *HtmlElement) handleAttrRemoved(message interface{}) {
val, err := el.attributes.Value()
// failed to load
// TODO: Log
if err != nil {
return
}
el.Lock()
defer el.Unlock()
attrs, ok := val.(*values.Object)
// TODO: Log
if !ok {
return
}

View File

@ -77,6 +77,12 @@ func (el *HtmlElement) Hash() int {
return out
}
func (el *HtmlElement) Clone() core.Value {
c, _ := NewHtmlElement(el.selection.Clone())
return c
}
func (el *HtmlElement) NodeType() values.Int {
nodes := el.selection.Nodes