Repository navigation
Expand file tree
/
Copy pathcallback_dispatch.go
More file actions
185 lines (156 loc) · 4.87 KB
/
Copy pathcallback_dispatch.go
File metadata and controls
185 lines (156 loc) · 4.87 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
package quickjs
import (
"errors"
"unsafe"
)
/*
#include <stdint.h>
#include "bridge.h"
*/
import "C"
// convertCArgsToGoValues converts C arguments to Go Value slice (unified helper).
// Reused by all proxy functions for consistent parameter conversion.
func convertCArgsToGoValues(argc C.int, argv *C.JSValueConst, ctx *Context) []*Value {
if argc == 0 {
return nil
}
cArgs := unsafe.Slice(argv, int(argc))
goArgs := make([]*Value, int(argc))
for i, cArg := range cArgs {
goArgs[i] = &Value{ctx: ctx, ref: C.JSValue(cArg)}
}
return goArgs
}
// getContextAndObject retrieves context and callback target from HandleStore.
func getContextAndObject(ctx *C.JSContext, magic C.int, notFoundErr proxyError) (*Context, interface{}, *proxyError) {
goCtx := getContextFromJS(ctx)
if goCtx == nil {
return nil, nil, &errContextNotFound
}
funcID := int32(magic)
fn := goCtx.loadFunctionFromHandleID(funcID)
if fn == nil {
return goCtx, nil, ¬FoundErr
}
return goCtx, fn, nil
}
//export goInterruptHandler
func goInterruptHandler(ctx *C.JSContext, runtimePtr *C.JSRuntime) C.int {
runtime := getRuntimeFromJS(runtimePtr)
if runtime == nil {
return C.int(0)
}
return C.int(runtime.callInterruptHandler(getContextFromJS(ctx)))
}
//export goDynamicImport
func goDynamicImport(ctx *C.JSContext, basename *C.char, filename *C.char, token C.JSValue, attributes C.JSValueConst) {
goCtx := getContextFromJS(ctx)
if goCtx == nil || goCtx.runtime == nil {
msg := C.CString("quickjs: context not found for dynamic import")
C.FinishDynamicImportWithError(ctx, token, msg)
C.free(unsafe.Pointer(msg))
return
}
rt := goCtx.runtime
tokenVal := &Value{ctx: goCtx, ref: token}
attrsVal := &Value{ctx: goCtx, ref: C.JSValue(attributes), borrowed: true}
promise, err := rt.callDynamicImportHandler(goCtx, C.GoString(basename), C.GoString(filename), tokenVal, attrsVal)
if errors.Is(err, ErrDynamicImportPending) {
rt.registerPendingDynamicImport(goCtx, tokenVal)
return
}
if err != nil {
msg := C.CString(err.Error())
C.FinishDynamicImportWithError(ctx, tokenVal.ref, msg)
C.free(unsafe.Pointer(msg))
tokenVal.ref = C.JS_NewUndefined()
if promise != nil {
promise.Free()
}
return
}
if promise == nil || !promise.belongsTo(goCtx) {
msg := C.CString("quickjs: dynamic import handler returned nil promise")
C.FinishDynamicImportWithError(ctx, tokenVal.ref, msg)
C.free(unsafe.Pointer(msg))
tokenVal.ref = C.JS_NewUndefined()
return
}
C.FinishDynamicImportSuccess(ctx, tokenVal.ref, promise.ref)
tokenVal.ref = C.JS_NewUndefined()
promise.ref = C.JS_NewUndefined()
}
//export goFunctionProxy
func goFunctionProxy(ctx *C.JSContext, thisVal C.JSValueConst,
argc C.int, argv *C.JSValueConst, magic C.int) C.JSValue {
goCtx, fn, err := getContextAndObject(ctx, magic, errFunctionNotFound)
if err != nil {
return throwProxyError(ctx, *err)
}
goFn, ok := fn.(func(*Context, *Value, []*Value) *Value)
if !ok {
return throwProxyError(ctx, errInvalidFunctionType)
}
args := convertCArgsToGoValues(argc, argv, goCtx)
thisValue := &Value{ctx: goCtx, ref: C.JSValue(thisVal)}
result := goFn(goCtx, thisValue, args)
if result == nil {
return C.JS_NewUndefined()
}
return result.ref
}
//export goClassMethodProxy
func goClassMethodProxy(ctx *C.JSContext, thisVal C.JSValueConst,
argc C.int, argv *C.JSValueConst, magic C.int) C.JSValue {
goCtx, fn, err := getContextAndObject(ctx, magic, errMethodNotFound)
if err != nil {
return throwProxyError(ctx, *err)
}
method, ok := fn.(ClassMethodFunc)
if !ok {
return throwProxyError(ctx, errInvalidMethodType)
}
thisValue := &Value{ctx: goCtx, ref: C.JSValue(thisVal)}
args := convertCArgsToGoValues(argc, argv, goCtx)
result := method(goCtx, thisValue, args)
if result == nil {
return C.JS_NewUndefined()
}
return result.ref
}
//export goClassGetterProxy
func goClassGetterProxy(ctx *C.JSContext, thisVal C.JSValueConst, magic C.int) C.JSValue {
goCtx, fn, err := getContextAndObject(ctx, magic, errGetterNotFound)
if err != nil {
return throwProxyError(ctx, *err)
}
getter, ok := fn.(ClassGetterFunc)
if !ok {
return throwProxyError(ctx, errInvalidGetterType)
}
thisValue := &Value{ctx: goCtx, ref: C.JSValue(thisVal)}
result := getter(goCtx, thisValue)
if result == nil {
return C.JS_NewUndefined()
}
return result.ref
}
//export goClassSetterProxy
func goClassSetterProxy(ctx *C.JSContext, thisVal C.JSValueConst,
val C.JSValueConst, magic C.int) C.JSValue {
goCtx, fn, err := getContextAndObject(ctx, magic, errSetterNotFound)
if err != nil {
return throwProxyError(ctx, *err)
}
setter, ok := fn.(ClassSetterFunc)
if !ok {
return throwProxyError(ctx, errInvalidSetterType)
}
thisValue := &Value{ctx: goCtx, ref: C.JSValue(thisVal)}
setValue := &Value{ctx: goCtx, ref: C.JSValue(val)}
result := setter(goCtx, thisValue, setValue)
if result == nil {
return C.JS_NewUndefined()
}
return result.ref
}