This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from kingland/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 3
/
v8_function_test.go
80 lines (65 loc) · 1.6 KB
/
v8_function_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package v8
import "testing"
func TestNewFunction(t *testing.T) {
engine.NewContext(nil).Scope(func(cs ContextScope) {
result := engine.NewFunction(func(info FunctionCallbackInfo) {
result := info.Get(0).ToInteger() + info.Get(1).ToInteger() + info.Get(2).ToInteger()
info.ReturnValue().Set(engine.NewInteger(result))
}, nil).Call(
engine.NewInteger(1),
engine.NewInteger(2),
engine.NewInteger(3),
)
if result.IsNumber() == false {
t.Fatal("result not a number")
}
if result.ToInteger() != 6 {
t.Fatal("result != 6")
}
})
}
func TestFunction(t *testing.T) {
engine.NewContext(nil).Scope(func(cs ContextScope) {
script := engine.Compile([]byte(`
a = function(x,y,z){
return x+y+z;
}
`), nil)
value := cs.Run(script)
if value.IsFunction() == false {
t.Fatal("value not a function")
}
result := value.ToFunction().Call(
engine.NewInteger(1),
engine.NewInteger(2),
engine.NewInteger(3),
)
if result.IsNumber() == false {
t.Fatal("result not a number")
}
if result.ToInteger() != 6 {
t.Fatal("result != 6")
}
})
}
func TestNewInstance(t *testing.T) {
engine.NewContext(nil).Scope(func(cs ContextScope) {
script := engine.Compile([]byte(`
MyClass = function(x) {
this.x = x
}
`), nil)
value := cs.Run(script)
if value.IsFunction() == false {
t.Fatal("value not a function")
}
result := value.ToFunction().NewInstance(engine.NewInteger(42))
if result.IsObject() == false {
t.Fatal("result not an object")
}
x := result.ToObject().GetProperty("x")
if x.ToInteger() != 42 {
t.Fatal("result != 42")
}
})
}