-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (59 loc) · 1.34 KB
/
Copy pathmain.go
File metadata and controls
70 lines (59 loc) · 1.34 KB
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
package main
import (
"errors"
"fmt"
"time"
"go.kuoruan.net/v8go-polyfills/console"
"go.kuoruan.net/v8go-polyfills/fetch"
"go.kuoruan.net/v8go-polyfills/timers"
"rogchap.com/v8go"
)
func main() {
iso := v8go.NewIsolate()
global := v8go.NewObjectTemplate(iso)
if err := fetch.InjectTo(iso, global); err != nil {
panic(err)
}
if err := timers.InjectTo(iso, global); err != nil {
panic(err)
}
ctx := v8go.NewContext(iso, global)
if err := console.InjectTo(ctx); err != nil {
panic(err)
}
_, err := ctx.RunScript("console.log('hola desde v8')", "hello.js")
if err != nil {
panic(err)
}
_, err = ctx.RunScript(`
console.log(new Date().toUTCString());
setTimeout(function() {
console.log("Finish Timer v8go.");
console.log(new Date().toUTCString());
}, 90)`, "set_timeout.js")
if err != nil {
panic(err)
}
val, err := ctx.RunScript("fetch('https://www.example.com').then(res => res.text())", "fetch.js")
if err != nil {
panic(err)
}
proms, err := val.AsPromise()
if err != nil {
panic(err)
}
done := make(chan bool, 1)
go func() {
for proms.State() == v8go.Pending {
continue
}
done <- true
}()
select {
case <-time.After(time.Second * 10):
panic(errors.New("request timeout"))
case <-done:
html := proms.Result().String()
fmt.Printf("fecth finish example.com\n100 bytes\n%s\n", html[:100])
}
}