Skip to content

Commit 383faf4

Browse files
committed
update
1 parent 096c7b5 commit 383faf4

File tree

5 files changed

+64
-81
lines changed

5 files changed

+64
-81
lines changed

v8_all_test.go

+48-43
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,24 @@ func init() {
5555
}
5656

5757
func Test_InternalField(t *testing.T) {
58+
iCache := make([]interface{}, 0)
5859
ot := engine.NewObjectTemplate()
5960
ot.SetInternalFieldCount(1)
60-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
61-
str1 := "data"
62-
data := cs.NewExternal(str1)
61+
context := engine.NewContext(nil)
62+
context.SetPrivateData(iCache)
63+
context.Scope(func(cs ContextScope) {
64+
cache := cs.GetPrivateData().([]interface{})
65+
str1 := "hello"
66+
cache = append(cache, str1)
67+
cs.SetPrivateData(cache)
6368
obj := ot.NewObject().ToObject()
64-
obj.SetInternalField(0, data)
69+
obj.SetInternalField(0, str1)
6570
str2 := obj.GetInternalField(0).(string)
6671
if str1 != str2 {
6772
t.Fatal("data not match")
6873
}
6974
})
75+
context.SetPrivateData(nil)
7076
}
7177

7278
func Test_GetVersion(t *testing.T) {
@@ -76,7 +82,7 @@ func Test_GetVersion(t *testing.T) {
7682
func Test_Allocator(t *testing.T) {
7783
SetArrayBufferAllocator(nil, nil)
7884
script := engine.Compile([]byte(`var data = new ArrayBuffer(10); data[0]='a'; data[0];`), nil, nil)
79-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
85+
engine.NewContext(nil).Scope(func(cs ContextScope) {
8086
exception := cs.TryCatch(true, func() {
8187
value := script.Run()
8288
if value.ToString() != "a" {
@@ -90,7 +96,7 @@ func Test_Allocator(t *testing.T) {
9096
}
9197

9298
func Test_MessageListener(t *testing.T) {
93-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
99+
engine.NewContext(nil).Scope(func(cs ContextScope) {
94100
cs.AddMessageListener(true, func(message string, data interface{}) {
95101
println("golang", message)
96102
}, nil)
@@ -122,7 +128,7 @@ func Test_MessageListener(t *testing.T) {
122128
}
123129

124130
func Test_HelloWorld(t *testing.T) {
125-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
131+
engine.NewContext(nil).Scope(func(cs ContextScope) {
126132
if cs.Eval("'Hello ' + 'World!'").ToString() != "Hello World!" {
127133
t.Fatal("result not match")
128134
}
@@ -132,7 +138,7 @@ func Test_HelloWorld(t *testing.T) {
132138
}
133139

134140
func Test_TryCatch(t *testing.T) {
135-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
141+
engine.NewContext(nil).Scope(func(cs ContextScope) {
136142
cs.TryCatch(true, func() {
137143
engine.Compile([]byte("a[=1"), nil, nil)
138144
})
@@ -148,7 +154,7 @@ func Test_TryCatch(t *testing.T) {
148154
}
149155

150156
func Test_PreCompile(t *testing.T) {
151-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
157+
engine.NewContext(nil).Scope(func(cs ContextScope) {
152158
// pre-compile
153159
code := []byte("'Hello ' + 'PreCompile!'")
154160
scriptData1 := engine.PreCompile(code)
@@ -179,7 +185,7 @@ func Test_PreCompile(t *testing.T) {
179185
}
180186

181187
func Test_Values(t *testing.T) {
182-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
188+
engine.NewContext(nil).Scope(func(cs ContextScope) {
183189

184190
if !engine.Undefined().IsUndefined() {
185191
t.Fatal("Undefined() not match")
@@ -281,7 +287,7 @@ func Test_Values(t *testing.T) {
281287
}
282288

283289
func Test_Object(t *testing.T) {
284-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
290+
engine.NewContext(nil).Scope(func(cs ContextScope) {
285291
script := engine.Compile([]byte("a={};"), nil, nil)
286292
value := script.Run()
287293
object := value.ToObject()
@@ -436,7 +442,7 @@ func Test_Object(t *testing.T) {
436442
}
437443

438444
func Test_Array(t *testing.T) {
439-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
445+
engine.NewContext(nil).Scope(func(cs ContextScope) {
440446
script := engine.Compile([]byte("[1,2,3]"), nil, nil)
441447
value := script.Run()
442448
result := value.ToArray()
@@ -486,7 +492,7 @@ func Test_Array(t *testing.T) {
486492
}
487493

488494
func Test_Function(t *testing.T) {
489-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
495+
engine.NewContext(nil).Scope(func(cs ContextScope) {
490496
script := engine.Compile([]byte(`
491497
a = function(x,y,z){
492498
return x+y+z;
@@ -535,7 +541,7 @@ func Test_Function(t *testing.T) {
535541
}
536542

537543
func Test_Accessor(t *testing.T) {
538-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
544+
engine.NewContext(nil).Scope(func(cs ContextScope) {
539545
template := engine.NewObjectTemplate()
540546
var propertyValue int32
541547

@@ -636,7 +642,7 @@ func Test_NamedPropertyHandler(t *testing.T) {
636642
info.ReturnValue().Set(func_template.NewFunction())
637643
}, nil, nil, PA_None)
638644

639-
engine.NewContext(global_template).Scope(func(cs *ContextScope) {
645+
engine.NewContext(global_template).Scope(func(cs ContextScope) {
640646
object := obj_template.NewObject().ToObject()
641647

642648
object.GetProperty("abc")
@@ -705,7 +711,7 @@ func Test_IndexedPropertyHandler(t *testing.T) {
705711
info.ReturnValue().Set(func_template.NewFunction())
706712
}, nil, nil, PA_None)
707713

708-
engine.NewContext(global_template).Scope(func(cs *ContextScope) {
714+
engine.NewContext(global_template).Scope(func(cs ContextScope) {
709715
object := obj_template.NewObject().ToObject()
710716

711717
object.GetElement(1)
@@ -732,9 +738,8 @@ func Test_ObjectConstructor(t *testing.T) {
732738
type MyClass struct {
733739
name string
734740
}
741+
data := new(MyClass)
735742
ftConstructor := engine.NewFunctionTemplate(func(info FunctionCallbackInfo) {
736-
cs := info.CurrentScope()
737-
data := cs.NewExternal(new(MyClass))
738743
info.This().SetInternalField(0, data)
739744
}, nil)
740745
ftConstructor.SetClassName("MyClass")
@@ -780,7 +785,7 @@ func Test_ObjectConstructor(t *testing.T) {
780785
)
781786
obj_template.SetInternalFieldCount(1)
782787

783-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
788+
engine.NewContext(nil).Scope(func(cs ContextScope) {
784789
cs.Global().SetProperty("MyClass", ftConstructor.NewFunction(), PA_None)
785790

786791
if !cs.Eval("(new MyClass) instanceof MyClass").IsTrue() {
@@ -816,7 +821,7 @@ func Test_Context(t *testing.T) {
816821
script2 := engine.Compile([]byte("Test_Context = 1;"), nil, nil)
817822
script3 := engine.Compile([]byte("Test_Context = Test_Context + 7;"), nil, nil)
818823

819-
test_func := func(cs *ContextScope) {
824+
test_func := func(cs ContextScope) {
820825
if script1.Run().IsFalse() {
821826
t.Fatal(`script1.Run(c).IsFalse()`)
822827
}
@@ -830,7 +835,7 @@ func Test_Context(t *testing.T) {
830835
}
831836
}
832837

833-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
838+
engine.NewContext(nil).Scope(func(cs ContextScope) {
834839
engine.NewContext(nil).Scope(test_func)
835840
engine.NewContext(nil).Scope(test_func)
836841
test_func(cs)
@@ -849,12 +854,12 @@ func Test_Context(t *testing.T) {
849854
info.ReturnValue().Set(functionTemplate.NewFunction())
850855
}, nil, nil, PA_None)
851856

852-
engine.NewContext(globalTemplate).Scope(func(cs *ContextScope) {
857+
engine.NewContext(globalTemplate).Scope(func(cs ContextScope) {
853858
cs.Eval(`log("Hello World!")`)
854859
})
855860

856861
// Test Global Object
857-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
862+
engine.NewContext(nil).Scope(func(cs ContextScope) {
858863
global := cs.Global()
859864

860865
if !global.SetProperty("println", functionTemplate.NewFunction(), PA_None) {
@@ -874,7 +879,7 @@ func Test_Context(t *testing.T) {
874879
}
875880

876881
func Test_UnderscoreJS(t *testing.T) {
877-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
882+
engine.NewContext(nil).Scope(func(cs ContextScope) {
878883
code, err := ioutil.ReadFile("labs/underscore.js")
879884

880885
if err != nil {
@@ -907,7 +912,7 @@ func Test_UnderscoreJS(t *testing.T) {
907912
}
908913

909914
func Test_JSON(t *testing.T) {
910-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
915+
engine.NewContext(nil).Scope(func(cs ContextScope) {
911916
json := `{"a":1,"b":2,"c":"xyz","e":true,"f":false,"g":null,"h":[4,5,6]}`
912917

913918
value := cs.ParseJSON(json)
@@ -993,7 +998,7 @@ func Test_ThreadSafe1(t *testing.T) {
993998
for i := 0; i < 100; i++ {
994999
wg.Add(1)
9951000
go func() {
996-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1001+
engine.NewContext(nil).Scope(func(cs ContextScope) {
9971002
script := engine.Compile([]byte("'Hello ' + 'World!'"), nil, nil)
9981003
value := script.Run()
9991004
result := value.ToString()
@@ -1021,7 +1026,7 @@ func Test_ThreadSafe2(t *testing.T) {
10211026
for i := 0; i < 100; i++ {
10221027
wg.Add(1)
10231028
go func() {
1024-
context.Scope(func(cs *ContextScope) {
1029+
context.Scope(func(cs ContextScope) {
10251030
rand_sched(200)
10261031

10271032
script := engine.Compile([]byte("'Hello ' + 'World!'"), nil, nil)
@@ -1051,7 +1056,7 @@ func Test_ThreadSafe3(t *testing.T) {
10511056
for i := 0; i < 100; i++ {
10521057
wg.Add(1)
10531058
go func() {
1054-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1059+
engine.NewContext(nil).Scope(func(cs ContextScope) {
10551060
rand_sched(200)
10561061

10571062
value := script.Run()
@@ -1081,7 +1086,7 @@ func Test_ThreadSafe4(t *testing.T) {
10811086
for i := 0; i < 100; i++ {
10821087
wg.Add(1)
10831088
go func() {
1084-
context.Scope(func(cs *ContextScope) {
1089+
context.Scope(func(cs ContextScope) {
10851090
rand_sched(200)
10861091

10871092
value := script.Run()
@@ -1133,7 +1138,7 @@ func Test_ThreadSafe6(t *testing.T) {
11331138
context := <-contextChan
11341139
script := <-scriptChan
11351140

1136-
context.Scope(func(cs *ContextScope) {
1141+
context.Scope(func(cs ContextScope) {
11371142
result := script.Run().ToString()
11381143
fail = fail || result != "Hello World!"
11391144
})
@@ -1158,7 +1163,7 @@ func Benchmark_NewContext(b *testing.B) {
11581163
}
11591164

11601165
func Benchmark_NewInteger(b *testing.B) {
1161-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1166+
engine.NewContext(nil).Scope(func(cs ContextScope) {
11621167
for i := 0; i < b.N; i++ {
11631168
cs.NewInteger(int64(i))
11641169
}
@@ -1170,7 +1175,7 @@ func Benchmark_NewInteger(b *testing.B) {
11701175
}
11711176

11721177
func Benchmark_NewString(b *testing.B) {
1173-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1178+
engine.NewContext(nil).Scope(func(cs ContextScope) {
11741179
for i := 0; i < b.N; i++ {
11751180
cs.NewString("Hello World!")
11761181
}
@@ -1182,7 +1187,7 @@ func Benchmark_NewString(b *testing.B) {
11821187
}
11831188

11841189
func Benchmark_NewObject(b *testing.B) {
1185-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1190+
engine.NewContext(nil).Scope(func(cs ContextScope) {
11861191
for i := 0; i < b.N; i++ {
11871192
cs.NewObject()
11881193
}
@@ -1194,7 +1199,7 @@ func Benchmark_NewObject(b *testing.B) {
11941199
}
11951200

11961201
func Benchmark_NewArray0(b *testing.B) {
1197-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1202+
engine.NewContext(nil).Scope(func(cs ContextScope) {
11981203
for i := 0; i < b.N; i++ {
11991204
cs.NewArray(0)
12001205
}
@@ -1206,7 +1211,7 @@ func Benchmark_NewArray0(b *testing.B) {
12061211
}
12071212

12081213
func Benchmark_NewArray5(b *testing.B) {
1209-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1214+
engine.NewContext(nil).Scope(func(cs ContextScope) {
12101215
for i := 0; i < b.N; i++ {
12111216
cs.NewArray(5)
12121217
}
@@ -1218,7 +1223,7 @@ func Benchmark_NewArray5(b *testing.B) {
12181223
}
12191224

12201225
func Benchmark_NewArray20(b *testing.B) {
1221-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1226+
engine.NewContext(nil).Scope(func(cs ContextScope) {
12221227
for i := 0; i < b.N; i++ {
12231228
cs.NewArray(20)
12241229
}
@@ -1230,7 +1235,7 @@ func Benchmark_NewArray20(b *testing.B) {
12301235
}
12311236

12321237
func Benchmark_NewArray100(b *testing.B) {
1233-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1238+
engine.NewContext(nil).Scope(func(cs ContextScope) {
12341239
for i := 0; i < b.N; i++ {
12351240
cs.NewArray(100)
12361241
}
@@ -1282,7 +1287,7 @@ func Benchmark_RunScript(b *testing.B) {
12821287
script := engine.Compile([]byte("1+1"), nil, nil)
12831288
b.StartTimer()
12841289

1285-
context.Scope(func(cs *ContextScope) {
1290+
context.Scope(func(cs ContextScope) {
12861291
for i := 0; i < b.N; i++ {
12871292
script.Run()
12881293
}
@@ -1302,7 +1307,7 @@ func Benchmark_JsFunction(b *testing.B) {
13021307
}
13031308
`), nil, nil)
13041309

1305-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1310+
engine.NewContext(nil).Scope(func(cs ContextScope) {
13061311
value := script.Run()
13071312
b.StartTimer()
13081313

@@ -1317,7 +1322,7 @@ func Benchmark_JsFunction(b *testing.B) {
13171322
}
13181323

13191324
func Benchmark_GoFunction(b *testing.B) {
1320-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1325+
engine.NewContext(nil).Scope(func(cs ContextScope) {
13211326
b.StopTimer()
13221327
value := engine.NewFunctionTemplate(func(info FunctionCallbackInfo) {
13231328
info.ReturnValue().SetInt32(123)
@@ -1336,7 +1341,7 @@ func Benchmark_GoFunction(b *testing.B) {
13361341
}
13371342

13381343
func Benchmark_Getter(b *testing.B) {
1339-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1344+
engine.NewContext(nil).Scope(func(cs ContextScope) {
13401345
b.StopTimer()
13411346
var propertyValue int32 = 1234
13421347

@@ -1371,7 +1376,7 @@ func Benchmark_Getter(b *testing.B) {
13711376
}
13721377

13731378
func Benchmark_Setter(b *testing.B) {
1374-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1379+
engine.NewContext(nil).Scope(func(cs ContextScope) {
13751380
b.StopTimer()
13761381

13771382
var propertyValue int32 = 1234
@@ -1407,7 +1412,7 @@ func Benchmark_Setter(b *testing.B) {
14071412
}
14081413

14091414
func Benchmark_TryCatch(b *testing.B) {
1410-
engine.NewContext(nil).Scope(func(cs *ContextScope) {
1415+
engine.NewContext(nil).Scope(func(cs ContextScope) {
14111416
for i := 0; i < b.N; i++ {
14121417
cs.TryCatch(false, func() {
14131418
cs.Eval("a[=1;")

0 commit comments

Comments
 (0)