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

Function buildClassFromReflection

class_reflect.go:145–173  ·  view source on GitHub ↗

buildClassFromReflection creates a ClassBuilder from reflection analysis MODIFIED FOR SCHEME C: Constructor signature changed to receive instance and return Go object

(ctx *Context, className string, typ reflect.Type, opts *ReflectOptions)

Source from the content-addressed store, hash-verified

143// buildClassFromReflection creates a ClassBuilder from reflection analysis
144// MODIFIED FOR SCHEME C: Constructor signature changed to receive instance and return Go object
145func buildClassFromReflection(ctx *Context, className string, typ reflect.Type, opts *ReflectOptions) (*ClassBuilder, error) {
146 builder := NewClassBuilder(className)
147
148 // SCHEME C: Modified constructor with new signature
149 // Constructor now receives pre-created instance and returns Go object to associate
150 builder.Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
151 // Create new Go object instance
152 goObject := reflect.New(typ).Interface()
153
154 // Initialize from constructor arguments using mixed parameter strategy
155 if len(args) > 0 {
156 if err := initializeFromArgs(goObject, args, typ, ctx); err != nil {
157 return nil, fmt.Errorf("constructor initialization failed: %w", err)
158 }
159 }
160
161 // SCHEME C: Return Go object for automatic association with instance
162 // The framework handles accessor-based synchronization automatically
163 return goObject, nil
164 })
165
166 // Add accessors (only exported fields) - unchanged
167 addReflectionAccessors(builder, typ, opts)
168
169 // Add methods (only exported methods) - unchanged
170 addReflectionMethods(builder, typ, opts)
171
172 return builder, nil
173}
174
175// initializeFromArgs implements mixed parameter constructor strategy
176func initializeFromArgs(instance interface{}, args []*Value, typ reflect.Type, ctx *Context) error {

Callers 1

BindClassBuilderMethod · 0.85

Calls 6

ConstructorMethod · 0.95
NewClassBuilderFunction · 0.85
initializeFromArgsFunction · 0.85
addReflectionAccessorsFunction · 0.85
addReflectionMethodsFunction · 0.85
NewMethod · 0.80

Tested by

no test coverage detected