forked from quickjs-go/quickjs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (134 loc) · 3.23 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"sync"
stdruntime "runtime"
"github.com/quickjs-go/quickjs-go"
)
type callback func(string) string
type Instance struct {
callbacks map[string]callback
}
func (i *Instance) Add(str string, fn callback) {
i.callbacks[str] = fn
}
func (i *Instance) Run(str string, arg string) string {
if fn, ok := i.callbacks[str]; ok {
return fn(arg)
} else {
return ""
}
}
type Demo struct {
Instance
}
func NewDemo() *Demo {
return &Demo{}
}
func (d *Demo) Register(runtime quickjs.Runtime, context *quickjs.Context, thread *quickjs.JsThread) bool {
test := context.Object()
test.SetFunction("AddCallback", func(ctx *quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.Value {
log.Println(args)
if len(args) != 2 {
return ctx.Error(errors.New("args must be 2"))
}
if args[0].IsString() && args[1].IsFunction() {
var _instance *Instance
value := test.Get("instance")
if value.IsNumber() {
if temp, ok := quickjs.ObjectId(value.Int64()).Get(); ok {
if _instance, ok = temp.(*Instance); !ok {
return ctx.Error(errors.New("instance type error"))
}
} else {
return ctx.Error(errors.New("instance not exist"))
}
} else {
return ctx.Error(errors.New("instance error"))
}
if _instance == nil {
return ctx.Error(errors.New("error:instance is nil"))
}
fn := ctx.DupValue(args[1])
_instance.Add(args[0].String(), func(str string) string {
var result string
if fn.IsFunction() {
r, err := thread.Call(context.Null(), fn, []quickjs.Value{context.String(str)})
log.Println(r, err)
result = r.String()
} else {
log.Println("fn is not function")
}
return result
})
return ctx.Bool(true)
} else {
return ctx.Error(errors.New("args type error"))
}
})
_instance := &Instance{callbacks: make(map[string]callback)}
d.Instance = *_instance
obj := quickjs.NewObjectId(_instance)
test.Set("instance", context.Int64(int64(obj)))
context.Globals().Set("test", test)
return true
}
func (d *Demo) Unregister(runtime quickjs.Runtime, context *quickjs.Context, thread *quickjs.JsThread) {
}
func Concurrency() {
n := 32
m := 10000
var wg sync.WaitGroup
wg.Add(n)
req := make(chan struct{}, n)
res := make(chan int64, m)
for i := 0; i < n; i++ {
go func() {
stdruntime.LockOSThread()
defer stdruntime.UnlockOSThread()
defer wg.Done()
runtime := quickjs.NewRuntime()
defer runtime.Free()
context := runtime.NewContext()
defer context.Free()
for range req {
result, err := context.Eval(`new Date().getTime()`, quickjs.EVAL_MODULE)
fmt.Println(err)
res <- result.Int64()
result.Free()
}
}()
}
for i := 0; i < m; i++ {
req <- struct{}{}
}
close(req)
wg.Wait()
for i := 0; i < m; i++ {
<-res
}
}
func main() {
d := &Demo{}
js := quickjs.NewJsThread(d)
if js != nil {
defer js.Close()
}
result, err := js.Eval(`
import * as os from 'os'
console.log("run 2000")
test.AddCallback("key",function(info) {
os.sleep(2000)
return "in js callback:" + info
})
`, quickjs.EVAL_MODULE)
log.Println(result, err)
d.Run("key", "i am in golang")
go func() {
d.Run("key", "i am in golang goroutine")
}()
Concurrency()
select {}
}