Repository navigation
Expand file tree
/
Copy pathconvert_jsc.go
More file actions
273 lines (252 loc) · 8.26 KB
/
Copy pathconvert_jsc.go
File metadata and controls
273 lines (252 loc) · 8.26 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
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
//go:build !goja && !qjswasm
package ramune
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"unsafe"
)
// JSType constants matching JavaScriptCore's JSType enum.
const (
jsTypeUndefined int32 = 0
jsTypeNull int32 = 1
jsTypeBoolean int32 = 2
jsTypeNumber int32 = 3
jsTypeString int32 = 4
jsTypeObject int32 = 5
jsTypeSymbol int32 = 6
)
// goToJS converts a Go value to a JSValueRef.
// Must be called with rt.mu held.
func (r *Runtime) goToJS(v any) (uintptr, error) {
switch val := v.(type) {
case *Value:
if val == nil || val.ptr == 0 {
return r.jsValueMakeNull(r.ctx), nil
}
runtime.KeepAlive(val)
return val.ptr, nil
case *JSFunc:
if val == nil || val.ptr == 0 {
return r.jsValueMakeNull(r.ctx), nil
}
return val.ptr, nil
case bool:
return r.jsValueMakeBoolean(r.ctx, val), nil
case int:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case int8:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case int16:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case int32:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case int64:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case uint:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case uint8:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case uint16:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case uint32:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case float32:
return r.jsValueMakeNumber(r.ctx, float64(val)), nil
case float64:
return r.jsValueMakeNumber(r.ctx, val), nil
case string:
jsStr := r.jsStringCreateWithUTF8CString(val)
defer r.jsStringRelease(jsStr)
return r.jsValueMakeString(r.ctx, jsStr), nil
case nil:
return r.jsValueMakeNull(r.ctx), nil
case []byte:
if r.jsObjectMakeTypedArray == nil {
return 0, fmt.Errorf("ramune: TypedArray API not available")
}
var exc uintptr
obj := r.jsObjectMakeTypedArray(r.ctx, jsTypedArrayTypeUint8Array, uint64(len(val)), uintptr(unsafe.Pointer(&exc)))
if exc != 0 {
msg := r.jsValueToGoString(exc)
return 0, fmt.Errorf("ramune: JSObjectMakeTypedArray: %s", msg)
}
if len(val) > 0 {
bytesPtr := r.jsObjectGetTypedArrayBytesPtr(r.ctx, obj, 0)
if bytesPtr != 0 {
dst := unsafe.Slice((*byte)(unsafe.Pointer(bytesPtr)), len(val))
copy(dst, val)
}
}
return obj, nil
case map[string]any:
return r.jsonParseToJS(val)
case []any:
return r.jsonParseToJS(val)
default:
rv := reflect.ValueOf(v)
origVal := rv
// Check for Promise type (has Await() method) → convert to JS Promise
if rv.Kind() == reflect.Ptr && rv.MethodByName("Await").IsValid() {
return r.promiseToJS(rv)
}
// Handle arbitrary slices via reflection (e.g. []int, []string)
if rv.Kind() == reflect.Slice {
return r.jsonParseToJS(val)
}
// Handle arbitrary maps with string keys via reflection (e.g. map[string]int)
if rv.Kind() == reflect.Map && rv.Type().Key().Kind() == reflect.String {
return r.jsonParseToJS(val)
}
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if rv.Kind() == reflect.Struct {
return r.structToJSObject(origVal, rv)
}
return 0, fmt.Errorf("ramune: unsupported Go type %T", v)
}
}
// promiseToJS converts a Go *promise.Promise[T] to a JS Promise.
// It spawns a goroutine that calls Await(), then resolves/rejects the JS Promise.
func (r *Runtime) promiseToJS(rv reflect.Value) (uintptr, error) {
r.nativeMethodSeq++
seq := r.nativeMethodSeq
resolveName := fmt.Sprintf("__promise_resolve_%d", seq)
rejectName := fmt.Sprintf("__promise_reject_%d", seq)
// Create JS Promise that stores resolve/reject as globals
jsCode := fmt.Sprintf(
`new Promise(function(resolve, reject) {
globalThis.%s = function(v) { resolve(v); };
globalThis.%s = function(e) { reject(e); };
})`,
resolveName, rejectName,
)
jsPromise, err := r.evalScriptLocked(jsCode, "promise-bridge")
if err != nil {
return 0, fmt.Errorf("promiseToJS: %w", err)
}
awaitMethod := rv.MethodByName("Await")
// Mark this bridge as pending so RunEventLoopFor doesn't return before
// the resolver fires. Decrement after the dispatched resolve/reject runs.
r.nativePromiseCount.Add(1)
go func() {
defer r.nativePromiseCount.Add(-1)
results := awaitMethod.Call(nil)
// results[0] = value, results[1] = error
r.dispatch(func() {
if len(results) >= 2 && !results[1].IsNil() {
errMsg := results[1].Interface().(error).Error()
r.execLocked(fmt.Sprintf("globalThis.%s(%q);delete globalThis.%s;delete globalThis.%s;",
rejectName, errMsg, resolveName, rejectName))
} else if len(results) >= 1 {
val := results[0].Interface()
// Convert value to JSON for passing to JS
jsVal, convErr := r.goToJS(val)
if convErr != nil {
r.execLocked(fmt.Sprintf("globalThis.%s(%q);delete globalThis.%s;delete globalThis.%s;",
rejectName, convErr.Error(), resolveName, rejectName))
return
}
// Store value in temp, resolve, cleanup
tmpName := fmt.Sprintf("__pval_%d", seq)
jsKey := r.jsStringCreateWithUTF8CString(tmpName)
globalObj := r.jsContextGetGlobalObject(r.ctx)
r.jsObjectSetProperty(r.ctx, globalObj, jsKey, jsVal, 0, 0)
r.jsStringRelease(jsKey)
r.execLocked(fmt.Sprintf("globalThis.%s(globalThis.%s);delete globalThis.%s;delete globalThis.%s;delete globalThis.%s;",
resolveName, tmpName, resolveName, rejectName, tmpName))
}
})
r.Wake()
}()
return jsPromise, nil
}
// ensureJSONParse caches the JSON.parse function reference.
// Must be called on the dedicated JSC goroutine.
func (r *Runtime) ensureJSONParse() {
if r.jsonParseFn != 0 {
return
}
global := r.jsContextGetGlobalObject(r.ctx)
jsonName := r.jsStringCreateWithUTF8CString("JSON")
defer r.jsStringRelease(jsonName)
jsonObj := r.jsObjectGetProperty(r.ctx, global, jsonName, 0)
parseName := r.jsStringCreateWithUTF8CString("parse")
defer r.jsStringRelease(parseName)
fn := r.jsObjectGetProperty(r.ctx, jsonObj, parseName, 0)
r.jsValueProtect(r.ctx, fn)
r.jsonParseFn = fn
}
// jsonParseToJS converts a Go value to a JSValueRef via JSON.parse.
// This avoids using jsObjectMake + jsObjectSetProperty which can corrupt
// JSC's structure table under rapid structure transitions.
// Must be called on the dedicated JSC goroutine.
func (r *Runtime) jsonParseToJS(v any) (uintptr, error) {
data, err := json.Marshal(v)
if err != nil {
return 0, fmt.Errorf("ramune: json.Marshal: %w", err)
}
r.ensureJSONParse()
fnObj := r.jsValueToObject(r.ctx, r.jsonParseFn, 0)
if fnObj == 0 {
return 0, fmt.Errorf("ramune: JSON.parse not available")
}
jsStr := r.jsStringCreateWithUTF8CString(string(data))
jsVal := r.jsValueMakeString(r.ctx, jsStr)
r.jsStringRelease(jsStr)
args := []uintptr{jsVal}
var exc uintptr
result := r.jsObjectCallAsFunction(r.ctx, fnObj, 0, 1, args, uintptr(unsafe.Pointer(&exc)))
if exc != 0 {
msg := r.jsValueToGoString(exc)
return 0, fmt.Errorf("ramune: JSON.parse: %s", msg)
}
if result == 0 {
return r.jsValueMakeNull(r.ctx), nil
}
return result, nil
}
// structToJSObject converts a Go struct to a JS object with live getter/setter
// properties and callable methods. Uses per-type callback registration to avoid
// leaking GoFuncs for every instance.
func (r *Runtime) structToJSObject(origVal, rv reflect.Value) (uintptr, error) {
methodTarget := origVal
if methodTarget.Kind() != reflect.Ptr {
ptr := reflect.New(rv.Type())
ptr.Elem().Set(rv)
methodTarget = ptr
}
r.ensureNativeReg()
info := r.nativeReg.ensureTypeRegistered(r, rv.Type())
instanceID := r.nativeReg.registerInstance(methodTarget)
jsCode := info.generateJSObject(instanceID)
jsObj, err := r.evalScriptLocked(jsCode, "native-struct")
if err != nil {
r.nativeReg.releaseInstance(instanceID)
return 0, fmt.Errorf("structToJSObject: %w", err)
}
return jsObj, nil
}
// jsToGo converts a JSValueRef to a Go value.
// Must be called with rt.mu held.
func (r *Runtime) jsToGo(ptr uintptr) (any, error) {
if ptr == 0 {
return nil, nil
}
typ := r.jsValueGetType(r.ctx, ptr)
switch typ {
case jsTypeUndefined, jsTypeNull:
return nil, nil
case jsTypeBoolean:
return r.jsValueToBoolean(r.ctx, ptr), nil
case jsTypeNumber:
return r.jsValueToNumber(r.ctx, ptr, 0), nil
case jsTypeString:
return r.jsValueToGoString(ptr), nil
default:
// Object, Symbol, etc. — return as *Value.
return r.newValue(ptr), nil
}
}