MCPcopy Create free account
hub / github.com/buke/quickjs-go / convertJSArgsToMethodArgs

Function convertJSArgsToMethodArgs

class_reflect.go:425–456  ·  view source on GitHub ↗

convertJSArgsToMethodArgs converts JavaScript arguments to Go reflect.Values for method calls

(method *reflect.Method, args []*Value, ctx *Context)

Source from the content-addressed store, hash-verified

423
424// convertJSArgsToMethodArgs converts JavaScript arguments to Go reflect.Values for method calls
425func convertJSArgsToMethodArgs(method *reflect.Method, args []*Value, ctx *Context) ([]reflect.Value, error) {
426 methodType := method.Type
427 numIn := methodType.NumIn()
428
429 // First argument is the receiver, so we skip it
430 numArgs := numIn - 1
431
432 if len(args) > numArgs {
433 return nil, fmt.Errorf("too many arguments: expected %d, got %d", numArgs, len(args))
434 }
435
436 // Prepare argument slice
437 reflectArgs := make([]reflect.Value, numArgs)
438
439 for i := 0; i < numArgs; i++ {
440 argType := methodType.In(i + 1) // +1 to skip receiver
441
442 if i < len(args) {
443 // Convert JavaScript value to Go value using marshal.go logic
444 argValue := reflect.New(argType).Elem()
445 if err := ctx.unmarshal(args[i], argValue); err != nil {
446 return nil, fmt.Errorf("failed to convert argument %d: %w", i, err)
447 }
448 reflectArgs[i] = argValue
449 } else {
450 // Use zero value for missing arguments
451 reflectArgs[i] = reflect.Zero(argType)
452 }
453 }
454
455 return reflectArgs, nil
456}
457
458// convertMethodResults converts method return values to JavaScript value
459func convertMethodResults(results []reflect.Value, ctx *Context) *Value {

Callers 1

createMethodWrapperFunction · 0.85

Calls 2

NewMethod · 0.80
unmarshalMethod · 0.80

Tested by

no test coverage detected