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

Function createFieldSetter

class_reflect.go:395–422  ·  view source on GitHub ↗

createFieldSetter creates a setter function for a struct field

(field reflect.StructField, fieldIndex int)

Source from the content-addressed store, hash-verified

393
394// createFieldSetter creates a setter function for a struct field
395func createFieldSetter(field reflect.StructField, fieldIndex int) ClassSetterFunc {
396 return func(ctx *Context, this *Value, value *Value) *Value {
397 objValue, err := getValidObjectValue(ctx, this)
398 if err != nil {
399 return ctx.ThrowError(err)
400 }
401
402 // In our implementation, fieldIndex is always valid and fieldValue
403 // is always settable for exported fields that we bind
404 fieldValue := objValue.Field(fieldIndex)
405
406 // Use Unmarshal to convert JavaScript value to Go value
407 tempVar := reflect.New(fieldValue.Type())
408 if err := ctx.Unmarshal(value, tempVar.Interface()); err != nil {
409 return ctx.ThrowError(fmt.Errorf("failed to unmarshal value for field %s: %w", field.Name, err))
410 }
411
412 // Set the field value
413 fieldValue.Set(tempVar.Elem())
414
415 // Return the new JavaScript value
416 // Note: In practice, if a JavaScript value can be successfully unmarshaled to a Go field,
417 // that field value can almost always be marshaled back to JavaScript. The error case
418 // is extremely rare and would indicate a deeper issue with the marshal/unmarshal system.
419 returnValue, _ := ctx.Marshal(fieldValue.Interface())
420 return returnValue
421 }
422}
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) {

Callers 1

addReflectionAccessorsFunction · 0.85

Calls 6

getValidObjectValueFunction · 0.85
ThrowErrorMethod · 0.80
NewMethod · 0.80
UnmarshalMethod · 0.80
SetMethod · 0.80
MarshalMethod · 0.80

Tested by

no test coverage detected