mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-15 01:25:00 +02:00
Added .Clone() method to runtime Value
This commit is contained in:
@ -1288,11 +1288,11 @@ func TestFunctionCall(t *testing.T) {
|
|||||||
So(string(out), ShouldEqual, `"int"`)
|
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()
|
c := compiler.New()
|
||||||
|
|
||||||
prog, err := c.Compile(`
|
prog, err := c.Compile(`
|
||||||
SLEEP(10)
|
WAIT(10)
|
||||||
RETURN 1
|
RETURN 1
|
||||||
`)
|
`)
|
||||||
|
|
||||||
@ -1305,13 +1305,13 @@ func TestFunctionCall(t *testing.T) {
|
|||||||
So(string(out), ShouldEqual, `1`)
|
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()
|
c := compiler.New()
|
||||||
|
|
||||||
prog, err := c.Compile(`
|
prog, err := c.Compile(`
|
||||||
LET duration = 10
|
LET duration = 10
|
||||||
|
|
||||||
SLEEP(duration)
|
WAIT(duration)
|
||||||
|
|
||||||
RETURN 1
|
RETURN 1
|
||||||
`)
|
`)
|
||||||
@ -1332,7 +1332,7 @@ func TestFunctionCall(t *testing.T) {
|
|||||||
FOR i IN [1, 2, 3, 4]
|
FOR i IN [1, 2, 3, 4]
|
||||||
LET duration = 10
|
LET duration = 10
|
||||||
|
|
||||||
SLEEP(duration)
|
WAIT(duration)
|
||||||
|
|
||||||
RETURN i * 2
|
RETURN i * 2
|
||||||
`)
|
`)
|
||||||
|
@ -45,6 +45,7 @@ type Value interface {
|
|||||||
Compare(other Value) int
|
Compare(other Value) int
|
||||||
Unwrap() interface{}
|
Unwrap() interface{}
|
||||||
Hash() int
|
Hash() int
|
||||||
|
Clone() Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsTypeOf(value Value, check Type) bool {
|
func IsTypeOf(value Value, check Type) bool {
|
||||||
|
@ -95,6 +95,16 @@ func (t *Array) Hash() int {
|
|||||||
return out
|
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 {
|
func (t *Array) Length() Int {
|
||||||
return Int(len(t.value))
|
return Int(len(t.value))
|
||||||
}
|
}
|
||||||
|
@ -74,6 +74,14 @@ func (b *Binary) Hash() int {
|
|||||||
return out
|
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 {
|
func (b *Binary) Length() Int {
|
||||||
return NewInt(len(b.values))
|
return NewInt(len(b.values))
|
||||||
}
|
}
|
||||||
|
@ -109,3 +109,7 @@ func (t Boolean) Hash() int {
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t Boolean) Clone() core.Value {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
@ -97,3 +97,7 @@ func (t DateTime) Hash() int {
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t DateTime) Clone() core.Value {
|
||||||
|
return NewDateTime(t.Time)
|
||||||
|
}
|
||||||
|
@ -129,3 +129,7 @@ func (t Float) Hash() int {
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t Float) Clone() core.Value {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
@ -113,3 +113,7 @@ func (t Int) Unwrap() interface{} {
|
|||||||
func (t Int) Hash() int {
|
func (t Int) Hash() int {
|
||||||
return int(t)
|
return int(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t Int) Clone() core.Value {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
@ -36,3 +36,7 @@ func (t *none) Unwrap() interface{} {
|
|||||||
func (t *none) Hash() int {
|
func (t *none) Hash() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *none) Clone() core.Value {
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
@ -106,6 +106,16 @@ func (t *Object) Hash() int {
|
|||||||
return out
|
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 {
|
func (t *Object) Length() Int {
|
||||||
return Int(len(t.value))
|
return Int(len(t.value))
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,10 @@ func (t String) Hash() int {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t String) Clone() core.Value {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
func (t String) Length() Int {
|
func (t String) Length() Int {
|
||||||
return Int(len(t))
|
return Int(len(t))
|
||||||
}
|
}
|
||||||
|
@ -201,6 +201,10 @@ func (doc *HtmlDocument) Hash() int {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (doc *HtmlDocument) Clone() core.Value {
|
||||||
|
return values.None
|
||||||
|
}
|
||||||
|
|
||||||
func (doc *HtmlDocument) Compare(other core.Value) int {
|
func (doc *HtmlDocument) Compare(other core.Value) int {
|
||||||
doc.Lock()
|
doc.Lock()
|
||||||
defer doc.Unlock()
|
defer doc.Unlock()
|
||||||
|
@ -216,6 +216,10 @@ func (el *HtmlElement) Value() core.Value {
|
|||||||
return values.None
|
return values.None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (el *HtmlElement) Clone() core.Value {
|
||||||
|
return values.None
|
||||||
|
}
|
||||||
|
|
||||||
func (el *HtmlElement) Length() values.Int {
|
func (el *HtmlElement) Length() values.Int {
|
||||||
return values.NewInt(len(el.children))
|
return values.NewInt(len(el.children))
|
||||||
}
|
}
|
||||||
@ -235,7 +239,8 @@ func (el *HtmlElement) GetAttributes() core.Value {
|
|||||||
return values.None
|
return values.None
|
||||||
}
|
}
|
||||||
|
|
||||||
return val
|
// returning shallow copy
|
||||||
|
return val.Clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (el *HtmlElement) GetAttribute(name values.String) core.Value {
|
func (el *HtmlElement) GetAttribute(name values.String) core.Value {
|
||||||
@ -384,19 +389,19 @@ func (el *HtmlElement) handleAttrModified(message interface{}) {
|
|||||||
val, err := el.attributes.Value()
|
val, err := el.attributes.Value()
|
||||||
|
|
||||||
// failed to load
|
// failed to load
|
||||||
// TODO: Log
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
el.Lock()
|
||||||
|
defer el.Unlock()
|
||||||
|
|
||||||
attrs, ok := val.(*values.Object)
|
attrs, ok := val.(*values.Object)
|
||||||
|
|
||||||
// TODO: Log
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: actually, we need to sync it too...
|
|
||||||
attrs.Set(values.NewString(reply.Name), values.NewString(reply.Value))
|
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()
|
val, err := el.attributes.Value()
|
||||||
|
|
||||||
// failed to load
|
// failed to load
|
||||||
// TODO: Log
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
el.Lock()
|
||||||
|
defer el.Unlock()
|
||||||
|
|
||||||
attrs, ok := val.(*values.Object)
|
attrs, ok := val.(*values.Object)
|
||||||
|
|
||||||
// TODO: Log
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,12 @@ func (el *HtmlElement) Hash() int {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (el *HtmlElement) Clone() core.Value {
|
||||||
|
c, _ := NewHtmlElement(el.selection.Clone())
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
func (el *HtmlElement) NodeType() values.Int {
|
func (el *HtmlElement) NodeType() values.Int {
|
||||||
nodes := el.selection.Nodes
|
nodes := el.selection.Nodes
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user