-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathv8_test.go
1160 lines (995 loc) · 25.9 KB
/
v8_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package v8
import (
"errors"
"fmt"
"regexp"
"runtime"
"strings"
"sync"
"testing"
"text/template"
"time"
)
func toJsonOrFatal(v *Value, t *testing.T) string {
json, err := v.ToJSON()
if err != nil {
t.Fatal("Could not convert value to JSON:", err)
}
return json
}
func TestCreateV8Context(t *testing.T) {
ctx := NewContext()
res, err := ctx.Eval(`
var a = 10;
var b = 20;
var c = a + b;
c;
`, NO_FILE)
if err != nil {
t.Error("Error evaluating javascript, err: ", err)
}
if res.(float64) != 30 {
t.Error("Expected 30, got ", res)
}
}
func TestAddFunc(t *testing.T) {
ctx := NewContext()
logFunc := "_console_log"
ctx.AddFunc(logFunc, func(args ...interface{}) interface{} {
for i, arg := range args {
fmt.Printf(" Arg %d (%T): %#v\n", i, arg, arg)
}
return nil
})
_, present := ctx.funcs[logFunc]
if !present {
t.Error("Expected function %v to be present but was not", logFunc)
}
ctx.Eval(`
_console_log("Added console.log function", 5);
`, NO_FILE)
}
func v8Recurse(ctx *V8Context, t *testing.T) func(args ...interface{}) interface{} {
return func(args ...interface{}) interface{} {
arg0 := args[0].(float64)
if arg0 > 0 {
res, err := ctx.Run("'x' + recurse", arg0-1)
if err != nil {
t.Fatal(err)
}
return res.(string)
}
return "y"
}
}
func TestParallelV8Contexts(t *testing.T) {
done := make(chan error)
NUM_VMS := 100
SLEEP_DURATION := 30 * time.Millisecond
fmt.Println("Starting", NUM_VMS, "VMs")
startTime := time.Now()
for i := 0; i < NUM_VMS; i++ {
go func() {
// Separate isolates to allow concurrent running: it's only one
// thread per isolate.
ctx := NewContextInIsolate(NewIsolate())
ctx.AddFunc("sleep", func(args ...interface{}) interface{} {
time.Sleep(SLEEP_DURATION)
return nil
})
_, err := ctx.Run("sleep")
done <- err
}()
}
fmt.Println("All VMs started, waiting")
for i := 0; i < NUM_VMS; i++ {
if err := <-done; err != nil {
t.Fatal(err)
}
}
elapsed := time.Since(startTime)
fmt.Println("Done in ", elapsed)
if elapsed < SLEEP_DURATION {
t.Fatal("Expected to sleep for at least", SLEEP_DURATION)
}
}
func TestManyV8RunsInSameContext(t *testing.T) {
ctx := NewContext()
startTime := time.Now()
for i := 0; i < 1000; i++ {
_, err := ctx.Eval("function sum(x, y) {return x + y};", NO_FILE)
if err != nil {
t.Fatal(err)
}
_, err = ctx.Run("sum", i, i*2)
if err != nil {
t.Fatal(err)
}
}
elapsed := time.Since(startTime)
fmt.Println("Done in ", elapsed)
}
func TestManyV8Contexts(t *testing.T) {
startTime := time.Now()
for i := 0; i < 1000; i++ {
ctx := NewContext()
_, err := ctx.Eval("function sum(x, y) {return x + y};", NO_FILE)
if err != nil {
t.Fatal(err)
}
_, err = ctx.Run("sum", i, i*2)
if err != nil {
t.Fatal(err)
}
}
elapsed := time.Since(startTime)
fmt.Println("Done in ", elapsed)
}
func TestRecursiveCallsIntoV8(t *testing.T) {
ctx := NewContext()
ctx.AddFunc("recurse", v8Recurse(ctx, t))
result, err := ctx.Run("recurse", 5)
if err != nil {
t.Fatal(err)
}
if result != "xxxxxy" {
t.Fatal("Got %v instead", result)
}
fmt.Println("Got:", result)
}
var _ = time.Time{}
func TestV8PingPong(t *testing.T) {
iso := NewIsolate()
vm1, vm2 := NewContext(), NewContextInIsolate(iso)
ball := make(chan string)
pingpong := func(args ...interface{}) interface{} {
done := args[0].(bool)
v := <-ball
if done {
ball <- "done"
return "done"
}
if v == "done" {
return "done"
}
if v == "ping" {
ball <- "pong"
} else {
ball <- "ping"
}
return v
}
vm1.AddFunc("pingpong", pingpong)
vm2.AddFunc("pingpong", pingpong)
out := make(chan string)
done1 := make(chan bool)
done2 := make(chan bool)
go func() {
<-done1
<-done2
close(out)
}()
go func() {
for i := 0; i < 10; i++ {
res, err := vm1.Run("pingpong", false)
if err != nil {
t.Fatal(err)
}
out <- "VM1:" + res.(string)
}
_, err := vm1.Run("pingpong", true)
if err != nil {
t.Fatal(err)
}
done1 <- true
}()
go func() {
for {
res, err := vm2.Run("pingpong", false)
if err != nil {
t.Fatal(err)
}
out <- "VM2:" + res.(string)
if res == "done" {
break
}
}
done2 <- true
}()
ball <- "ping"
fmt.Print("Ball: ")
for r := range out {
fmt.Print(r, " | ")
}
fmt.Println("DONE")
}
func TestMultipleGoroutinesAccessingContext(t *testing.T) {
NUM_GOROUTINES := 100
runtime.GOMAXPROCS(NUM_GOROUTINES)
ctx := NewContext()
ctx.AddFunc("sleep", func(args ...interface{}) interface{} {
time.Sleep(1 * time.Millisecond)
return nil
})
results := make(chan string)
for i := 0; i < NUM_GOROUTINES; i++ {
go func() {
ctx.Run("sleep")
results <- "x"
}()
}
for i := 0; i < NUM_GOROUTINES; i++ {
<-results
}
}
func ExampleAddConsoleLog() {
ctx := NewContext()
logFunc := "_console_log"
ctx.AddFunc(logFunc, func(args ...interface{}) interface{} {
for _, arg := range args {
fmt.Printf("%v", arg)
}
return nil
})
ctx.Eval(`
this.console = { "log": function(args) { _console_log(args) } };
console.log("Example of logging to console");
`, NO_FILE)
// Output: Example of logging to console
}
func TestJSReferenceError(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`
dne; // dne = does not exist. Should cause error in v8.
`, "my_file.js")
t.Logf("Error: [%v]", err)
match, _ := regexp.MatchString(
"Stack trace: ReferenceError: dne is not defined.*\n.*at my_file.js",
err.Error())
if !match {
t.Error("Expected 'ReferenceError' in error string, got: ", err)
}
}
func TestSyntaxError(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`
// blah blah blah
function(asdf) { // error, no function name
return 0
}
`, "my_file.js")
t.Logf("Err is:\n%v\n", err)
if !strings.Contains(err.Error(), `Uncaught exception`) ||
!strings.Contains(err.Error(), `SyntaxError`) ||
!strings.Contains(err.Error(), `Unexpected token`) ||
!strings.Contains(err.Error(), `my_file.js:3:10`) ||
!strings.Contains(err.Error(), `function(asdf)`) {
t.Errorf("Failed to find expected substrings in error.")
}
}
func TestJSTypeError(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`
var x = undefined;
x.blah;
`, "my_file.js")
t.Logf("Error: [%v]", err)
match, _ := regexp.MatchString(
"Stack trace: TypeError: Cannot read property 'blah' of undefined.*\n.*at my_file.js",
err.Error())
if !match {
t.Error("Expected 'TypeError' in error string, got: ", err)
}
}
func TestJSThrowString(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`throw 'badness'`, "my_file.js")
t.Logf("Error: [%v]", err)
match, _ := regexp.MatchString("Uncaught exception: badness", err.Error())
if !match {
t.Error("Expected 'Uncaught exception: badness'")
}
}
func TestJSThrowObject(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`throw {msg:"died", data:3}`, "my_file.js")
t.Logf("Error: [%v]", err)
match, _ := regexp.MatchString("Uncaught exception:.*died", err.Error())
if !match {
t.Error("Expected 'Uncaught exception: ... died ...'")
}
}
func TestTerminate(t *testing.T) {
ctx := NewContext()
// Run an infinite loop in a goroutine.
var err error
done := make(chan bool)
go func() {
fmt.Println("Running infinite loop:")
_, err = ctx.Eval("while(1){}", NO_FILE)
fmt.Println("Completed infinite loop!")
done <- true
}()
// Sleep for a bit to make sure that it's
fmt.Println("Verifying that v8 is spinning")
select {
case <-done:
t.Fatal("V8 infinite loop failed to loop infinitely")
case <-time.After(10 * time.Millisecond):
}
fmt.Println("Interrupting V8")
ctx.Terminate()
fmt.Println("Waiting for infinite loop")
<-done
// Make sure that we got an ErrTerminated
if err != ErrTerminated {
t.Fatalf("Expected ErrTerminated but got %v", err)
}
// Make sure you can call terminate when nothing is running, and it
// works alright.
ctx.Terminate()
fmt.Println("I didn't crash!")
}
// Verify that terminate doesn't affect other running vms.
func TestTerminateOnlySpecificVM(t *testing.T) {
runloop := func(vm *V8Context) chan bool {
done := make(chan bool)
go func() {
vm.Eval("while(1){}", NO_FILE)
done <- true
}()
return done
}
vm1, vm2 := NewContext(), NewContextInIsolate(NewIsolate())
done1, done2 := runloop(vm1), runloop(vm2)
select {
case <-done1:
t.Fatal("Premature loop exit")
case <-done2:
t.Fatal("Premature loop exit")
case <-time.After(10 * time.Millisecond):
}
vm2.Terminate()
<-done2
select {
case <-done1:
t.Fatal("Premature loop exit")
case <-time.After(10 * time.Millisecond):
}
vm1.Terminate() // Stop the infinite loop.
}
func TestRunningCodeInContextAfterThrowingError(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`
function fail(a,b) {
this.c = a+b;
throw "some failure";
}
function work(a,b) {
this.c = a+b+2;
}
x = new fail(3,5);`, "file1.js")
if err == nil {
t.Fatal("Expected an exception.")
}
res, err := ctx.Eval(`y = new work(3,6); y.c`, "file2.js")
if err != nil {
t.Fatal("Expected it to work, but got:", err)
}
if res.(float64) != 11 {
t.Errorf("Expected 11, got: %#V", res)
}
}
func TestManyContextsThrowingErrors(t *testing.T) {
prog := `
function work(N, depth, fail) {
if (depth == 0) { return 1; }
var sum = 0;
for (i = 0; i < N; i++) { sum *= work(N, depth-1); }
if (fail) {
throw "Failed";
}
return sum;
}`
const N = 100 // num parallel contexts
runtime.GOMAXPROCS(N)
var done sync.WaitGroup
var ctxs []*V8Context
done.Add(N)
for i := 0; i < N; i++ {
ctx := NewContext()
ctx.Eval(prog, "prog.js")
ctxs = append(ctxs, ctx)
go func(ctx *V8Context, i int) {
ctx.Run("work", 100000, 100, (i%5 == 0))
ctx.Run("work", 100000, 100, (i%5 == 0))
ctx.Run("work", 100000, 100, (i%5 == 0))
done.Done()
}(ctx, i)
}
done.Wait()
}
func TestErrorsInNativeCode(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`[].map(undefined);`, "map_undef.js")
if err == nil {
t.Fatal("Expected error.")
}
t.Log("Got expected error: ", err)
}
func TestStackOverflow(t *testing.T) {
// TODO(aroman) There's a way to handle this gracefully.
t.Skip("Need to figure out how to handle stack overflow.")
ctx := NewContext()
_, err := ctx.Eval(`function a(x,y) { return a(x,y) + a(y,x); }; a(1,2)`,
"stack_attack.js")
if err == nil {
t.Fatal("Expected error.")
}
t.Log("Got expected error: ", err)
}
func TestRunFunc(t *testing.T) {
ctx := NewContext()
_, err := ctx.Eval(`function add(a,b,c) { return a+b.Val+c[0]+c[1]; }`, NO_FILE)
if err != nil {
t.Fatal(err)
}
res, err := ctx.Run("add", 3, struct{ Val int }{5}, []int{7, 13})
if err != nil {
t.Fatal(err)
}
resnum, ok := res.(float64)
if !ok {
t.Fatalf("Expected to get a number out of v8, got: %#V", res)
}
if resnum != 28 {
t.Fatal("Expected 28, got ", resnum)
}
}
func TestEvalRaw(t *testing.T) {
ctx := NewContext()
val, err := ctx.EvalRaw(`x = {a:3,b:{c:'asdf'}}`, "new eval hotness!")
if err != nil {
t.Fatal(err)
}
jsonstr := toJsonOrFatal(val, t)
expected := `{"a":3,"b":{"c":"asdf"}}`
if jsonstr != expected {
t.Fatalf("JSON mismatch:\n Expected:'%s'\n Got: '%s'", expected, jsonstr)
}
}
func TestFromJson(t *testing.T) {
ctx := NewContext()
json := `{"a":3,"b":{"c":"asdf"}}`
val, err := ctx.FromJSON(json)
if err != nil {
t.Fatal(err)
}
if toJsonOrFatal(val, t) != json {
t.Fatalf("JSON mismatch:\n Expected:'%s'\n Got: '%s'", json, toJsonOrFatal(val, t))
}
}
func TestApply(t *testing.T) {
ctx := NewContext()
must := func(val *Value, e error) *Value {
if e != nil {
t.Fatal(e)
}
return val
}
f := must(ctx.CreateJS(`function(a,b) { return a+b; }`, NO_FILE))
a := must(ctx.CreateJS(`3`, NO_FILE))
b := must(ctx.CreateJS(`7`, NO_FILE))
res := must(ctx.Apply(f, f, a, b))
t.Log(res)
if toJsonOrFatal(res, t) != "10" {
t.Fatal("Expected 10, got ", toJsonOrFatal(res, t))
}
}
// Test "apply" when we manually specify the "this" context for the func.
func TestApplyWithThis(t *testing.T) {
ctx := NewContext()
must := func(val *Value, e error) *Value {
if e != nil {
t.Fatal(e)
}
return val
}
f := must(ctx.CreateJS(`function(x) { return x+this.y; }`, NO_FILE))
x := must(ctx.CreateJS(`5`, NO_FILE))
y1 := must(ctx.CreateJS(`{y:3}`, NO_FILE))
y2 := must(ctx.CreateJS(`{y:7}`, NO_FILE))
res := must(ctx.Apply(f, y1, x))
t.Log(res)
if toJsonOrFatal(res, t) != "8" {
t.Fatal("Expected 8, got ", toJsonOrFatal(res, t))
}
res = must(ctx.Apply(f, y2, x))
t.Log(res)
if toJsonOrFatal(res, t) != "12" {
t.Fatal("Expected 12, got ", toJsonOrFatal(res, t))
}
}
func TestRawFunc(t *testing.T) {
ctx := NewContext()
callback := func(src Loc, args ...*Value) (*Value, error) {
if src.Filename != "some_test_file.js" {
t.Errorf("Wrong calling filename in callback: %q", src.Filename)
}
return args[len(args)-1], nil
}
ctx.AddRawFunc("lastarg", callback)
arg, err := ctx.EvalRaw("lastarg(1,2,3,4,'blah')", "some_test_file.js")
if err != nil {
t.Fatal(err)
}
if toJsonOrFatal(arg, t) != `"blah"` {
t.Fatal("Expected '\"blah\"', got ", toJsonOrFatal(arg, t))
}
arg, err = ctx.EvalRaw("lastarg(1,2,3,4)", "some_test_file.js")
if err != nil {
t.Fatal(err)
}
if toJsonOrFatal(arg, t) != `4` {
t.Fatal("Expected '4', got ", toJsonOrFatal(arg, t))
}
}
func TestRawFuncReturnNull(t *testing.T) {
ctx := NewContext()
ctx.AddRawFunc("undef", func(_ Loc, args ...*Value) (*Value, error) { return nil, nil })
arg, err := ctx.EvalRaw("undef(1,2,3)", "test")
if err != nil {
t.Fatal("error", err)
}
if toJsonOrFatal(arg, t) != "undefined" {
t.Fatal("Expected undefined, got ", toJsonOrFatal(arg, t))
}
}
func TestRawFuncResultError(t *testing.T) {
ctx := NewContext()
ctx.AddRawFunc("die", func(_ Loc, args ...*Value) (*Value, error) {
return nil, fmt.Errorf("diediedie")
})
arg, err := ctx.EvalRaw("die(1,2,3)", "test")
if err == nil || arg != nil {
t.Error("Expected an error result, got:\n Val: %v\n Err: %v", arg, err)
}
if !strings.Contains(err.Error(), `diediedie`) {
t.Errorf(`Expected an exception containing "diediedie", got:\n%v`, err)
}
}
func TestRawFuncOrigin(t *testing.T) {
ctx := NewContext()
ctx.AddRawFunc("require", func(src Loc, args ...*Value) (*Value, error) {
// All calls are from the single inner.js call below, regardless of
// which script initiated the call to inner().
if src != (Loc{"inner", "inner.js", 3, 4}) {
t.Errorf("Wrong caller location provided to raw function callback: %#v", src)
}
return nil, nil
})
ctx.Eval(`
function inner() {
require('foo');
};
inner();`, "inner.js")
ctx.Eval(`
function middle() {
inner();
};
inner();
middle();`, "middle.js")
ctx.Eval(`
inner();
middle();`, "outer.js")
}
func TestValueToString(t *testing.T) {
ctx := NewContext()
val, err := ctx.EvalRaw(`"lalala"`, "test")
if err != nil {
t.Fatal(err)
}
str, err := val.ToString()
if err != nil {
t.Fatal(err)
}
if str != "lalala" {
t.Fatalf("Expected 'lalala', got '%s'", str)
}
val, err = ctx.EvalRaw(`123`, "test")
if err != nil {
t.Fatal(err)
}
str, err = val.ToString()
if err == nil {
t.Fatalf("Expected error, but got nil and the string '%s'", str)
}
}
func TestCreateJS(t *testing.T) {
ctx := NewContext()
val, err := ctx.CreateJS("{a:1, b:{c:3}}", NO_FILE)
if err != nil {
t.Fatal(err)
}
expected := `{"a":1,"b":{"c":3}}`
if toJsonOrFatal(val, t) != expected {
t.Fatalf("Expected '%s', got '%s'", expected, toJsonOrFatal(val, t))
}
}
type Example struct {
One int
Two string
}
func TestToValue(t *testing.T) {
ctx := NewContext()
val, err := ctx.ToValue(Example{1, "two"})
if err != nil {
t.Fatal(err)
}
expected := `{"One":1,"Two":"two"}`
if toJsonOrFatal(val, t) != expected {
t.Fatalf("Expected '%s', got '%s'", expected, toJsonOrFatal(val, t))
}
}
func TestToValueFunc(t *testing.T) {
ctx := NewContext()
called := false
f, err := ctx.ToValue(func(_ Loc, args ...*Value) (*Value, error) {
called = true
return ctx.ToValue(17)
})
if err != nil {
t.Fatal(err)
}
val, err := ctx.Apply(f, nil)
if err != nil {
t.Fatal(err)
}
if !called {
t.Fatal("Function was never called")
}
expected := "17"
if toJsonOrFatal(val, t) != expected {
t.Fatalf("Expected '%s', got '%s'", expected, toJsonOrFatal(val, t))
}
}
func TestBurst(t *testing.T) {
ctx := NewContext()
ob, err := ctx.CreateJS("{a:1, b:{c:3}}", NO_FILE)
if err != nil {
t.Fatal(err)
}
vals, err := ob.Burst()
if err != nil {
t.Fatal(err)
}
if len(vals) != 2 {
t.Fatal("Expected 2 vals, got ", len(vals), ":", vals)
}
if a, ok := vals["a"]; !ok {
t.Fatal("Vals missing field 'a': ", vals)
} else if toJsonOrFatal(a, t) != "1" {
t.Fatal("Expected a to be 1, got ", toJsonOrFatal(a, t))
}
if b, ok := vals["b"]; !ok {
t.Fatal("Vals missing field 'b': ", vals)
} else if toJsonOrFatal(b, t) != `{"c":3}` {
t.Fatal(`Expected b to be '{"c":3}', got `, toJsonOrFatal(b, t))
}
}
func TestBurstOnNonObjectError(t *testing.T) {
ctx := NewContext()
num, err := ctx.CreateJS("5", NO_FILE)
if err != nil {
t.Fatal(err)
}
vals, err := num.Burst()
if err == nil {
t.Fatalf("Expected an error, got %v", vals)
}
}
func TestGetObjectField(t *testing.T) {
ctx := NewContext()
ob, err := ctx.CreateJS("{a:1}", NO_FILE)
if err != nil {
t.Fatal(err)
}
if a, err := ob.Get("a"); err != nil {
t.Fatal("Failed getting value: ", err)
} else {
if toJsonOrFatal(a, t) != "1" {
t.Fatal("Expected '1', got ", a)
}
}
}
func TestGetObjectNonexistentField(t *testing.T) {
ctx := NewContext()
ob, err := ctx.CreateJS("{}", NO_FILE)
if err != nil {
t.Fatal(err)
}
if _, err := ob.Get("a"); err == nil {
t.Fatal("Expected an error but did not get one.")
}
}
func TestSetObjectField(t *testing.T) {
ctx := NewContext()
// Create an object.
ob, err := ctx.CreateJS("{}", NO_FILE)
if err != nil {
t.Fatal(err)
}
// Create a simple number.
num, err := ctx.CreateJS("3", NO_FILE)
if err != nil {
t.Fatal(err)
}
// See if we can set fields on the object.
if err := ob.Set("foo", num); err != nil {
t.Fatal(err)
}
// Make sure the object actually holds those fields:
if toJsonOrFatal(ob, t) != `{"foo":3}` {
t.Errorf("Object should have foo field, but is: [%v]", toJsonOrFatal(ob, t))
}
// Make sure we can't set fields on a number:
if err := num.Set("bar", ob); err == nil {
t.Error("We're not supposed to be able to set a field on a number!")
}
// Hey, let's also create a function and put that on the object.
called := false
fn, err := ctx.CreateRawFunc(func(_ Loc, args ...*Value) (*Value, error) {
called = true
return num, nil
})
if err != nil {
t.Fatal(err)
}
// Assign the function to a field on the object.
if err := ob.Set("func", fn); err != nil {
t.Fatal(err)
}
// Ok, now how do we verify that it works? We need to get a hold of the
// object in JS and then call the function.
ctx.AddRawFunc("getObject", func(_ Loc, args ...*Value) (*Value, error) { return ob, nil })
if res, err := ctx.Eval("getObject().func()", "test"); err != nil {
t.Fatal(err)
} else if !called {
t.Error("Function was not called.")
} else if res.(float64) != 3.0 {
t.Fatalf("Got [%#v] instead of 3.0", res)
}
}
func TestFromJsonWithSingleQuotes(t *testing.T) {
ctx := NewContext()
actual_key := `a'x<\>"`
json_escaped_key := `a'x<\\>\"`
s := `{"` + json_escaped_key + `":3}`
v, err := ctx.FromJSON(s)
if err != nil {
t.Error("Can't parse key: ", s, "\n", template.JSEscapeString(s))
t.Fatal(err)
}
m, err := v.Burst()
if err != nil {
t.Fatal(err)
}
if _, exists := m[actual_key]; !exists {
t.Fatalf("Failed to find key [%s] in map: %v", actual_key, m)
}
}
func TestValueAcrossContextsFails(t *testing.T) {
// This test verifies that Values CANNOT be used across contexts.
// The expectation is that attempts to do so will panic.
// Create a value in ctx1 and try to use that value in ctx2. The value
// is injected into ctx2 via the getVal() RawFunc.
// NOTE: The contexts are created in disposable isolates, because
// right now panicing in go code screws up some internal state of
// V8.
ctx1, ctx2 := NewContextInIsolate(NewIsolate()),
NewContextInIsolate(NewIsolate())
// Create the value. Note that if we use ctx2 here (so that the values
// are always from the same context), this test works fine.
value_from_ctx1, err := ctx1.EvalRaw(`x={"s":4};x`, NO_FILE)
if err != nil {
t.Fatal(err)
}
ctx2.AddRawFunc("getVal", func(Loc, ...*Value) (*Value, error) {
return value_from_ctx1, nil
})
// Catch the panic:
defer func() {
if err := recover(); err != nil {
errmsg := err.(error).Error()
if !strings.HasPrefix(errmsg, "Error processing return value") {
t.Fatal("Unexpected panic message:", err)
}
}
}()
val, err := ctx2.EvalRaw("getVal()", NO_FILE) // expect panic() here.
if err != nil {
t.Fatal(err)
}
t.Fatal("Should never reach here unless using values across " +
"contexts somehow magically works.")
// In the future, if this actually works, here's how we'd verify it:
m, err := val.Burst()
if err != nil {
t.Fatal(err)
}
if _, found := m["s"]; !found {
t.Fatal("Missing expected map key:", m)
}
if toJsonOrFatal(val, t) != `{"s":4}` {
t.Error("Value does not work across contexts.")
}
}
func TestCreateRawFunc(t *testing.T) {
ctx := NewContext()
calls := 0
f, err := ctx.CreateRawFunc(func(src Loc, args ...*Value) (*Value, error) {
if src.Filename != "" {
t.Errorf("Expected filename to be blank when using .Apply, but got %q", src.Filename)
}
calls++
return nil, nil
})
if err != nil {
t.Fatal(err)
}
ctx.Apply(f, f)
if calls != 1 {
t.Errorf("Expected calls to be 1, got %d", calls)
}
}
func TestThrow(t *testing.T) {
ctx := NewContext()
ctx.AddRawFunc("die", func(_ Loc, args ...*Value) (*Value, error) {
return nil, fmt.Errorf("bart")
})
_, err := ctx.EvalRaw("die()", NO_FILE)
if err == nil {
t.Error("Expected an error.")
} else if !strings.Contains(err.Error(), "bart") {
t.Error("Error did not contain expected error message: ", err)
}
// After an exception, we should still be able to use the context:
res, err := ctx.Eval(`1+2`, NO_FILE)
if err != nil {
t.Error("Unexpected error: ", err)