@@ -55,18 +55,24 @@ func init() {
55
55
}
56
56
57
57
func Test_InternalField (t * testing.T ) {
58
+ iCache := make ([]interface {}, 0 )
58
59
ot := engine .NewObjectTemplate ()
59
60
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 )
63
68
obj := ot .NewObject ().ToObject ()
64
- obj .SetInternalField (0 , data )
69
+ obj .SetInternalField (0 , str1 )
65
70
str2 := obj .GetInternalField (0 ).(string )
66
71
if str1 != str2 {
67
72
t .Fatal ("data not match" )
68
73
}
69
74
})
75
+ context .SetPrivateData (nil )
70
76
}
71
77
72
78
func Test_GetVersion (t * testing.T ) {
@@ -76,7 +82,7 @@ func Test_GetVersion(t *testing.T) {
76
82
func Test_Allocator (t * testing.T ) {
77
83
SetArrayBufferAllocator (nil , nil )
78
84
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 ) {
80
86
exception := cs .TryCatch (true , func () {
81
87
value := script .Run ()
82
88
if value .ToString () != "a" {
@@ -90,7 +96,7 @@ func Test_Allocator(t *testing.T) {
90
96
}
91
97
92
98
func Test_MessageListener (t * testing.T ) {
93
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
99
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
94
100
cs .AddMessageListener (true , func (message string , data interface {}) {
95
101
println ("golang" , message )
96
102
}, nil )
@@ -122,7 +128,7 @@ func Test_MessageListener(t *testing.T) {
122
128
}
123
129
124
130
func Test_HelloWorld (t * testing.T ) {
125
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
131
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
126
132
if cs .Eval ("'Hello ' + 'World!'" ).ToString () != "Hello World!" {
127
133
t .Fatal ("result not match" )
128
134
}
@@ -132,7 +138,7 @@ func Test_HelloWorld(t *testing.T) {
132
138
}
133
139
134
140
func Test_TryCatch (t * testing.T ) {
135
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
141
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
136
142
cs .TryCatch (true , func () {
137
143
engine .Compile ([]byte ("a[=1" ), nil , nil )
138
144
})
@@ -148,7 +154,7 @@ func Test_TryCatch(t *testing.T) {
148
154
}
149
155
150
156
func Test_PreCompile (t * testing.T ) {
151
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
157
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
152
158
// pre-compile
153
159
code := []byte ("'Hello ' + 'PreCompile!'" )
154
160
scriptData1 := engine .PreCompile (code )
@@ -179,7 +185,7 @@ func Test_PreCompile(t *testing.T) {
179
185
}
180
186
181
187
func Test_Values (t * testing.T ) {
182
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
188
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
183
189
184
190
if ! engine .Undefined ().IsUndefined () {
185
191
t .Fatal ("Undefined() not match" )
@@ -281,7 +287,7 @@ func Test_Values(t *testing.T) {
281
287
}
282
288
283
289
func Test_Object (t * testing.T ) {
284
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
290
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
285
291
script := engine .Compile ([]byte ("a={};" ), nil , nil )
286
292
value := script .Run ()
287
293
object := value .ToObject ()
@@ -436,7 +442,7 @@ func Test_Object(t *testing.T) {
436
442
}
437
443
438
444
func Test_Array (t * testing.T ) {
439
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
445
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
440
446
script := engine .Compile ([]byte ("[1,2,3]" ), nil , nil )
441
447
value := script .Run ()
442
448
result := value .ToArray ()
@@ -486,7 +492,7 @@ func Test_Array(t *testing.T) {
486
492
}
487
493
488
494
func Test_Function (t * testing.T ) {
489
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
495
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
490
496
script := engine .Compile ([]byte (`
491
497
a = function(x,y,z){
492
498
return x+y+z;
@@ -535,7 +541,7 @@ func Test_Function(t *testing.T) {
535
541
}
536
542
537
543
func Test_Accessor (t * testing.T ) {
538
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
544
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
539
545
template := engine .NewObjectTemplate ()
540
546
var propertyValue int32
541
547
@@ -636,7 +642,7 @@ func Test_NamedPropertyHandler(t *testing.T) {
636
642
info .ReturnValue ().Set (func_template .NewFunction ())
637
643
}, nil , nil , PA_None )
638
644
639
- engine .NewContext (global_template ).Scope (func (cs * ContextScope ) {
645
+ engine .NewContext (global_template ).Scope (func (cs ContextScope ) {
640
646
object := obj_template .NewObject ().ToObject ()
641
647
642
648
object .GetProperty ("abc" )
@@ -705,7 +711,7 @@ func Test_IndexedPropertyHandler(t *testing.T) {
705
711
info .ReturnValue ().Set (func_template .NewFunction ())
706
712
}, nil , nil , PA_None )
707
713
708
- engine .NewContext (global_template ).Scope (func (cs * ContextScope ) {
714
+ engine .NewContext (global_template ).Scope (func (cs ContextScope ) {
709
715
object := obj_template .NewObject ().ToObject ()
710
716
711
717
object .GetElement (1 )
@@ -732,9 +738,8 @@ func Test_ObjectConstructor(t *testing.T) {
732
738
type MyClass struct {
733
739
name string
734
740
}
741
+ data := new (MyClass )
735
742
ftConstructor := engine .NewFunctionTemplate (func (info FunctionCallbackInfo ) {
736
- cs := info .CurrentScope ()
737
- data := cs .NewExternal (new (MyClass ))
738
743
info .This ().SetInternalField (0 , data )
739
744
}, nil )
740
745
ftConstructor .SetClassName ("MyClass" )
@@ -780,7 +785,7 @@ func Test_ObjectConstructor(t *testing.T) {
780
785
)
781
786
obj_template .SetInternalFieldCount (1 )
782
787
783
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
788
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
784
789
cs .Global ().SetProperty ("MyClass" , ftConstructor .NewFunction (), PA_None )
785
790
786
791
if ! cs .Eval ("(new MyClass) instanceof MyClass" ).IsTrue () {
@@ -816,7 +821,7 @@ func Test_Context(t *testing.T) {
816
821
script2 := engine .Compile ([]byte ("Test_Context = 1;" ), nil , nil )
817
822
script3 := engine .Compile ([]byte ("Test_Context = Test_Context + 7;" ), nil , nil )
818
823
819
- test_func := func (cs * ContextScope ) {
824
+ test_func := func (cs ContextScope ) {
820
825
if script1 .Run ().IsFalse () {
821
826
t .Fatal (`script1.Run(c).IsFalse()` )
822
827
}
@@ -830,7 +835,7 @@ func Test_Context(t *testing.T) {
830
835
}
831
836
}
832
837
833
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
838
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
834
839
engine .NewContext (nil ).Scope (test_func )
835
840
engine .NewContext (nil ).Scope (test_func )
836
841
test_func (cs )
@@ -849,12 +854,12 @@ func Test_Context(t *testing.T) {
849
854
info .ReturnValue ().Set (functionTemplate .NewFunction ())
850
855
}, nil , nil , PA_None )
851
856
852
- engine .NewContext (globalTemplate ).Scope (func (cs * ContextScope ) {
857
+ engine .NewContext (globalTemplate ).Scope (func (cs ContextScope ) {
853
858
cs .Eval (`log("Hello World!")` )
854
859
})
855
860
856
861
// Test Global Object
857
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
862
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
858
863
global := cs .Global ()
859
864
860
865
if ! global .SetProperty ("println" , functionTemplate .NewFunction (), PA_None ) {
@@ -874,7 +879,7 @@ func Test_Context(t *testing.T) {
874
879
}
875
880
876
881
func Test_UnderscoreJS (t * testing.T ) {
877
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
882
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
878
883
code , err := ioutil .ReadFile ("labs/underscore.js" )
879
884
880
885
if err != nil {
@@ -907,7 +912,7 @@ func Test_UnderscoreJS(t *testing.T) {
907
912
}
908
913
909
914
func Test_JSON (t * testing.T ) {
910
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
915
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
911
916
json := `{"a":1,"b":2,"c":"xyz","e":true,"f":false,"g":null,"h":[4,5,6]}`
912
917
913
918
value := cs .ParseJSON (json )
@@ -993,7 +998,7 @@ func Test_ThreadSafe1(t *testing.T) {
993
998
for i := 0 ; i < 100 ; i ++ {
994
999
wg .Add (1 )
995
1000
go func () {
996
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1001
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
997
1002
script := engine .Compile ([]byte ("'Hello ' + 'World!'" ), nil , nil )
998
1003
value := script .Run ()
999
1004
result := value .ToString ()
@@ -1021,7 +1026,7 @@ func Test_ThreadSafe2(t *testing.T) {
1021
1026
for i := 0 ; i < 100 ; i ++ {
1022
1027
wg .Add (1 )
1023
1028
go func () {
1024
- context .Scope (func (cs * ContextScope ) {
1029
+ context .Scope (func (cs ContextScope ) {
1025
1030
rand_sched (200 )
1026
1031
1027
1032
script := engine .Compile ([]byte ("'Hello ' + 'World!'" ), nil , nil )
@@ -1051,7 +1056,7 @@ func Test_ThreadSafe3(t *testing.T) {
1051
1056
for i := 0 ; i < 100 ; i ++ {
1052
1057
wg .Add (1 )
1053
1058
go func () {
1054
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1059
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1055
1060
rand_sched (200 )
1056
1061
1057
1062
value := script .Run ()
@@ -1081,7 +1086,7 @@ func Test_ThreadSafe4(t *testing.T) {
1081
1086
for i := 0 ; i < 100 ; i ++ {
1082
1087
wg .Add (1 )
1083
1088
go func () {
1084
- context .Scope (func (cs * ContextScope ) {
1089
+ context .Scope (func (cs ContextScope ) {
1085
1090
rand_sched (200 )
1086
1091
1087
1092
value := script .Run ()
@@ -1133,7 +1138,7 @@ func Test_ThreadSafe6(t *testing.T) {
1133
1138
context := <- contextChan
1134
1139
script := <- scriptChan
1135
1140
1136
- context .Scope (func (cs * ContextScope ) {
1141
+ context .Scope (func (cs ContextScope ) {
1137
1142
result := script .Run ().ToString ()
1138
1143
fail = fail || result != "Hello World!"
1139
1144
})
@@ -1158,7 +1163,7 @@ func Benchmark_NewContext(b *testing.B) {
1158
1163
}
1159
1164
1160
1165
func Benchmark_NewInteger (b * testing.B ) {
1161
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1166
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1162
1167
for i := 0 ; i < b .N ; i ++ {
1163
1168
cs .NewInteger (int64 (i ))
1164
1169
}
@@ -1170,7 +1175,7 @@ func Benchmark_NewInteger(b *testing.B) {
1170
1175
}
1171
1176
1172
1177
func Benchmark_NewString (b * testing.B ) {
1173
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1178
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1174
1179
for i := 0 ; i < b .N ; i ++ {
1175
1180
cs .NewString ("Hello World!" )
1176
1181
}
@@ -1182,7 +1187,7 @@ func Benchmark_NewString(b *testing.B) {
1182
1187
}
1183
1188
1184
1189
func Benchmark_NewObject (b * testing.B ) {
1185
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1190
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1186
1191
for i := 0 ; i < b .N ; i ++ {
1187
1192
cs .NewObject ()
1188
1193
}
@@ -1194,7 +1199,7 @@ func Benchmark_NewObject(b *testing.B) {
1194
1199
}
1195
1200
1196
1201
func Benchmark_NewArray0 (b * testing.B ) {
1197
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1202
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1198
1203
for i := 0 ; i < b .N ; i ++ {
1199
1204
cs .NewArray (0 )
1200
1205
}
@@ -1206,7 +1211,7 @@ func Benchmark_NewArray0(b *testing.B) {
1206
1211
}
1207
1212
1208
1213
func Benchmark_NewArray5 (b * testing.B ) {
1209
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1214
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1210
1215
for i := 0 ; i < b .N ; i ++ {
1211
1216
cs .NewArray (5 )
1212
1217
}
@@ -1218,7 +1223,7 @@ func Benchmark_NewArray5(b *testing.B) {
1218
1223
}
1219
1224
1220
1225
func Benchmark_NewArray20 (b * testing.B ) {
1221
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1226
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1222
1227
for i := 0 ; i < b .N ; i ++ {
1223
1228
cs .NewArray (20 )
1224
1229
}
@@ -1230,7 +1235,7 @@ func Benchmark_NewArray20(b *testing.B) {
1230
1235
}
1231
1236
1232
1237
func Benchmark_NewArray100 (b * testing.B ) {
1233
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1238
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1234
1239
for i := 0 ; i < b .N ; i ++ {
1235
1240
cs .NewArray (100 )
1236
1241
}
@@ -1282,7 +1287,7 @@ func Benchmark_RunScript(b *testing.B) {
1282
1287
script := engine .Compile ([]byte ("1+1" ), nil , nil )
1283
1288
b .StartTimer ()
1284
1289
1285
- context .Scope (func (cs * ContextScope ) {
1290
+ context .Scope (func (cs ContextScope ) {
1286
1291
for i := 0 ; i < b .N ; i ++ {
1287
1292
script .Run ()
1288
1293
}
@@ -1302,7 +1307,7 @@ func Benchmark_JsFunction(b *testing.B) {
1302
1307
}
1303
1308
` ), nil , nil )
1304
1309
1305
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1310
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1306
1311
value := script .Run ()
1307
1312
b .StartTimer ()
1308
1313
@@ -1317,7 +1322,7 @@ func Benchmark_JsFunction(b *testing.B) {
1317
1322
}
1318
1323
1319
1324
func Benchmark_GoFunction (b * testing.B ) {
1320
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1325
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1321
1326
b .StopTimer ()
1322
1327
value := engine .NewFunctionTemplate (func (info FunctionCallbackInfo ) {
1323
1328
info .ReturnValue ().SetInt32 (123 )
@@ -1336,7 +1341,7 @@ func Benchmark_GoFunction(b *testing.B) {
1336
1341
}
1337
1342
1338
1343
func Benchmark_Getter (b * testing.B ) {
1339
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1344
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1340
1345
b .StopTimer ()
1341
1346
var propertyValue int32 = 1234
1342
1347
@@ -1371,7 +1376,7 @@ func Benchmark_Getter(b *testing.B) {
1371
1376
}
1372
1377
1373
1378
func Benchmark_Setter (b * testing.B ) {
1374
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1379
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1375
1380
b .StopTimer ()
1376
1381
1377
1382
var propertyValue int32 = 1234
@@ -1407,7 +1412,7 @@ func Benchmark_Setter(b *testing.B) {
1407
1412
}
1408
1413
1409
1414
func Benchmark_TryCatch (b * testing.B ) {
1410
- engine .NewContext (nil ).Scope (func (cs * ContextScope ) {
1415
+ engine .NewContext (nil ).Scope (func (cs ContextScope ) {
1411
1416
for i := 0 ; i < b .N ; i ++ {
1412
1417
cs .TryCatch (false , func () {
1413
1418
cs .Eval ("a[=1;" )
0 commit comments