-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathfunctojs.go
More file actions
268 lines (210 loc) · 6.78 KB
/
functojs.go
File metadata and controls
268 lines (210 loc) · 6.78 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
package qjs
import (
"fmt"
"reflect"
)
// FuncToJS converts a Go function to a JavaScript function.
func FuncToJS(c *Context, v any) (_ *Value, err error) {
if v == nil {
return c.NewNull(), nil
}
defer func() {
if err != nil {
err = fmt.Errorf("[FuncToJS] %w", err)
}
}()
rval := reflect.ValueOf(v)
rtype := reflect.TypeOf(v)
if rtype.Kind() == reflect.Ptr {
if rval.IsNil() {
return c.NewNull(), nil
}
rval = rval.Elem()
rtype = rtype.Elem()
}
if rtype.Kind() != reflect.Func {
return nil, newInvalidGoTypeErr("function", v)
}
if rval.IsNil() {
return c.NewNull(), nil
}
fnType := rval.Type()
if err := VerifyGoFunc(fnType, v); err != nil {
return nil, err
}
return c.Function(func(this *This) (*Value, error) {
goArgs, err := JsFuncArgsToGo(this.Args(), fnType)
if err != nil {
return nil, err
}
var results []reflect.Value
if fnType.IsVariadic() {
results = rval.CallSlice(goArgs)
} else {
results = rval.Call(goArgs)
}
return GoFuncResultToJs(c, results)
}), nil
}
// JsFuncArgsToGo converts JS arguments to Go arguments in both variadic and non-variadic functions,
// filling missing arguments with zero values.
func JsFuncArgsToGo(jsArgs []*Value, fnType reflect.Type) ([]reflect.Value, error) {
numGoArgs := fnType.NumIn()
goArgs := make([]reflect.Value, 0, numGoArgs)
numJSArgs := len(jsArgs)
if fnType.IsVariadic() {
fixedArgs := Min(numGoArgs-1, numJSArgs)
for i := range fixedArgs {
goVal, err := JsArgToGo(jsArgs[i], fnType.In(i))
if err != nil {
return nil, newArgConversionErr(i, err)
}
goArgs = append(goArgs, goVal)
}
// Handle variadic slice
variadicSlice, err := CreateVariadicSlice(jsArgs[fixedArgs:], fnType.In(numGoArgs-1), fixedArgs)
if err != nil {
return nil, err
}
return append(goArgs, variadicSlice), nil
}
// Limit JS args to Go args count to avoid index out of bounds
argsToProcess := Min(numJSArgs, numGoArgs)
for i := range argsToProcess {
goVal, err := JsArgToGo(jsArgs[i], fnType.In(i))
if err != nil {
return nil, newArgConversionErr(i, err)
}
goArgs = append(goArgs, goVal)
}
// Fill missing arguments with zero values
for i := argsToProcess; i < numGoArgs; i++ {
goArgs = append(goArgs, reflect.Zero(fnType.In(i)))
}
return goArgs, nil
}
// handlePointerArgument processes JS arguments for pointer types.
func handlePointerArgument(jsArg *Value, argType reflect.Type) (reflect.Value, error) {
if jsArg.IsNull() || jsArg.IsUndefined() {
return reflect.Zero(argType), nil
}
underlyingType := argType.Elem()
zeroVal := reflect.New(underlyingType).Elem()
goVal, err := ToGoValue(jsArg, zeroVal.Interface())
if err != nil {
return reflect.Value{}, newJsToGoErr(jsArg, err, "function param pointer to "+jsArg.Type())
}
ptrVal := reflect.New(underlyingType)
ptrVal.Elem().Set(reflect.ValueOf(goVal))
return ptrVal, nil
}
// CreateNonNilSample creates appropriate non-nil samples for types that have nil zero values.
func CreateNonNilSample(argType reflect.Type) any {
switch argType.Kind() {
case reflect.Interface:
// return nil to let the conversion
// use the default logic which can handle dynamic type inference
return nil
case reflect.Ptr:
elemType := argType.Elem()
elemZero := reflect.Zero(elemType)
ptr := reflect.New(elemType)
ptr.Elem().Set(elemZero)
return ptr.Interface()
case reflect.Array:
return reflect.New(argType).Elem().Interface()
case reflect.Slice:
return reflect.MakeSlice(argType, 0, 0).Interface()
case reflect.Map:
return reflect.MakeMap(argType).Interface()
case reflect.Chan:
return reflect.MakeChan(argType, 1).Interface() // size 1 buffer to avoid blocking
case reflect.Func:
return createDummyFunction(argType)
default:
// For other types (shouldn't happen), return zero value
return reflect.Zero(argType).Interface()
}
}
// createDummyFunction creates a dummy function with the specified signature for type inference.
func createDummyFunction(funcType reflect.Type) any {
fn := reflect.MakeFunc(funcType, func(_ []reflect.Value) []reflect.Value {
results := make([]reflect.Value, funcType.NumOut())
for i := range funcType.NumOut() {
results[i] = reflect.Zero(funcType.Out(i))
}
return results
})
return fn.Interface()
}
// JsArgToGo converts a single JS argument to a Go value with enhanced type handling.
func JsArgToGo(jsArg *Value, argType reflect.Type) (reflect.Value, error) {
if argType.Kind() == reflect.Ptr {
return handlePointerArgument(jsArg, argType)
}
goZeroVal := CreateNonNilSample(argType)
goVal, err := ToGoValue(jsArg, goZeroVal)
if err != nil {
return reflect.Value{}, newJsToGoErr(jsArg, err, "function param "+jsArg.Type())
}
return reflect.ValueOf(goVal), nil
}
// CreateVariadicSlice creates a reflect.Value slice for variadic arguments.
// Converts remaining JS arguments to the slice element type and returns as a slice value.
func CreateVariadicSlice(jsArgs []*Value, sliceType reflect.Type, fixedArgsCount int) (reflect.Value, error) {
varArgType := sliceType.Elem()
numVarArgs := len(jsArgs)
variadicSlice := reflect.MakeSlice(sliceType, numVarArgs, numVarArgs)
for i, jsArg := range jsArgs {
goVal, err := JsArgToGo(jsArg, varArgType)
if err != nil {
return reflect.Value{}, newArgConversionErr(fixedArgsCount+i, err)
}
variadicSlice.Index(i).Set(goVal)
}
return variadicSlice, nil
}
// GoFuncResultToJs processes Go function call results and converts them to JS values.
// If last return value is a non-nil error, it's thrown in JS context.
// The remaining return values are converted to JS value or JS array if there are multiple.
func GoFuncResultToJs(c *Context, results []reflect.Value) (*Value, error) {
if len(results) == 0 {
return nil, nil
}
// Check if last return value is an error
lastIdx := len(results) - 1
lastResult := results[lastIdx]
// Last return is error
if IsImplementError(lastResult.Type()) {
// Last return is non-nil error -> throw in JS context
if !lastResult.IsNil() {
resultErr, _ := lastResult.Interface().(error)
return nil, resultErr
}
// Error is nil, handle remaining return values
remaining := results[:lastIdx]
if len(remaining) == 0 {
return nil, nil
}
// Single remaining value -> return that value
if len(remaining) == 1 {
return ToJsValue(c, remaining[0].Interface())
}
// Multiple remaining values -> return as JS array
jsValues := make([]any, len(remaining))
for i, result := range remaining {
jsValues[i] = result.Interface()
}
return ToJsValue(c, jsValues)
}
// Single return value -> return that value
if len(results) == 1 {
return ToJsValue(c, results[0].Interface())
}
// Multiple return values -> return as JS array
jsValues := make([]any, len(results))
for i, result := range results {
jsValues[i] = result.Interface()
}
return ToJsValue(c, jsValues)
}