-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathquickjs_test.go
More file actions
192 lines (168 loc) · 5.02 KB
/
quickjs_test.go
File metadata and controls
192 lines (168 loc) · 5.02 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
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
package quickjs_test
import (
"fmt"
"runtime"
"time"
"github.com/buke/quickjs-go"
)
// User represents a common user struct for demonstrating Marshal and Class features
type User struct {
ID int64 `js:"id"`
Name string `js:"name"`
Email string `js:"email"`
Age int `js:"age"`
IsActive bool `js:"is_active"`
Scores []float32 `js:"scores"` // Demonstrates TypedArray support
}
// GetFullInfo returns the user's full information
func (u *User) GetFullInfo() string {
return fmt.Sprintf("%s (%s) - Age: %d", u.Name, u.Email, u.Age)
}
// GetAverageScore calculates the average score
func (u *User) GetAverageScore() float64 {
if len(u.Scores) == 0 {
return 0
}
var sum float32
for _, score := range u.Scores {
sum += score
}
return float64(sum) / float64(len(u.Scores))
}
func Example() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Create a new runtime
rt := quickjs.NewRuntime()
defer rt.Close()
// Create a new context
ctx := rt.NewContext()
defer ctx.Close()
// Create a new object
test := ctx.NewObject()
defer test.Free()
// bind properties to the object
test.Set("A", ctx.NewString("String A"))
test.Set("B", ctx.NewInt32(0))
test.Set("C", ctx.NewBool(false))
// bind go function to js object - UPDATED: function signature now uses pointers
test.Set("hello", ctx.NewFunction(func(ctx *quickjs.Context, this *quickjs.Value, args []*quickjs.Value) *quickjs.Value {
return ctx.NewString("Hello " + args[0].ToString())
}))
// bind "test" object to global object
ctx.Globals().Set("test", test)
// call js function by js - FIXED: removed error handling
js_ret := ctx.Eval(`test.hello("Javascript!")`)
defer js_ret.Free()
// Check for exceptions instead of error
if js_ret.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println(js_ret.ToString())
// call js function by go
go_ret := ctx.Globals().Get("test").Call("hello", ctx.NewString("Golang!"))
defer go_ret.Free()
fmt.Println(go_ret.ToString())
// bind go function to Javascript async function using Function + Promise - UPDATED: the promise resolves asynchronously
ctx.Globals().Set("testAsync", ctx.NewFunction(func(ctx *quickjs.Context, this *quickjs.Value, args []*quickjs.Value) *quickjs.Value {
return ctx.NewPromise(func(resolve, reject func(*quickjs.Value)) {
go func() {
time.Sleep(10 * time.Millisecond)
ctx.Schedule(func(inner *quickjs.Context) {
val := inner.NewString("Hello Async Function!")
resolve(val)
val.Free()
})
}()
})
}))
promiseResult := ctx.Eval(`testAsync()`)
defer promiseResult.Free()
if promiseResult.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
asyncRet := ctx.Await(promiseResult)
defer asyncRet.Free()
if asyncRet.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println(asyncRet.ToString())
// Demonstrate TypedArray functionality
floatData := []float32{95.5, 87.2, 92.0}
typedArray := ctx.NewFloat32Array(floatData)
ctx.Globals().Set("floatData", typedArray)
arrayResult := ctx.Eval(`floatData instanceof Float32Array`)
defer arrayResult.Free()
// Check for exceptions instead of error
if arrayResult.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("TypedArray:", arrayResult.ToBool())
// Demonstrate Marshal/Unmarshal functionality with User struct
user := User{
ID: 123,
Name: "Alice",
Email: "alice@example.com",
Age: 25,
IsActive: true,
Scores: []float32{95.5, 87.2, 92.0},
}
jsVal, err := ctx.Marshal(user)
if err != nil {
fmt.Printf("Marshal error: %v\n", err)
return
}
ctx.Globals().Set("userData", jsVal)
marshalResult := ctx.Eval(`userData.name + " avg: " + (userData.scores.reduce((s,v) => s+v) / userData.scores.length).toFixed(1)`)
defer marshalResult.Free()
// Check for exceptions instead of error
if marshalResult.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Marshal:", marshalResult.ToString())
// Demonstrate Class Binding functionality with the same User struct
userConstructor, _ := ctx.BindClass(&User{})
if userConstructor.IsException() {
defer userConstructor.Free()
err := ctx.Exception()
fmt.Printf("BindClass error: %v\n", err)
return
}
ctx.Globals().Set("User", userConstructor)
classResult := ctx.Eval(`
const user = new User({
id: 456,
name: "Bob",
email: "bob@example.com",
age: 30,
is_active: true,
scores: [88.0, 92.5, 85.0]
});
user.GetAverageScore().toFixed(1)
`)
defer classResult.Free()
// Check for exceptions instead of error
if classResult.IsException() {
err := ctx.Exception()
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Class binding:", classResult.ToString())
// Output:
// Hello Javascript!
// Hello Golang!
// Hello Async Function!
// TypedArray: true
// Marshal: Alice avg: 91.6
// Class binding: 88.5
}