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

Function createClass

class.go:353–581  ·  view source on GitHub ↗

createClass implements the core class creation logic using C layer optimization MODIFIED FOR SCHEME C: Now stores entire ClassBuilder and separates static/instance properties

(ctx *Context, builder *ClassBuilder)

Source from the content-addressed store, hash-verified

351// createClass implements the core class creation logic using C layer optimization
352// MODIFIED FOR SCHEME C: Now stores entire ClassBuilder and separates static/instance properties
353func createClass(ctx *Context, builder *ClassBuilder) (*Value, uint32) {
354 // Step 1: Input validation (keep in Go layer for business logic) - unchanged
355 if err := validateClassBuilder(builder); err != nil {
356 return ctx.ThrowError(err), 0
357 }
358 snapshot := cloneClassBuilder(builder)
359
360 // Step 2: Go layer manages class name and JSClassDef memory - unchanged
361 className := C.CString(snapshot.name)
362 defer C.free(unsafe.Pointer(className))
363
364 classDef := &C.JSClassDef{
365 class_name: className,
366 finalizer: (*C.JSClassFinalizer)(unsafe.Pointer(C.GoClassFinalizerProxy)),
367 }
368
369 // Step 3: Prepare classID variable for C function to allocate internally - unchanged
370 var classID C.JSClassID
371 var methodIDs []int32
372 var accessorIDs []int32
373 var methodNames []*C.char
374 var accessorNames []*C.char
375
376 // SCHEME C STEP 4: Store entire ClassBuilder in HandleStore (not just constructor)
377 // This allows constructor proxy to access both constructor function and instance properties
378 constructorID := ctx.handleStore.Store(snapshot)
379 cleanupStoredHandlers := func() {
380 ctx.handleStore.Delete(constructorID)
381 for _, id := range methodIDs {
382 ctx.handleStore.Delete(id)
383 }
384 for _, id := range accessorIDs {
385 ctx.handleStore.Delete(id)
386 }
387 }
388
389 // Step 5: Prepare method entries for C layer - unchanged logic, same implementation
390 var cMethods []C.MethodEntry
391
392 for _, method := range snapshot.methods {
393 // Store method function in handleStore
394 handlerID := ctx.handleStore.Store(method.Func)
395 methodIDs = append(methodIDs, handlerID)
396
397 // Convert method name to C string
398 methodName := C.CString(method.Name)
399 methodNames = append(methodNames, methodName)
400 // Note: Don't defer free as C layer needs these strings during binding
401
402 // Determine length parameter
403 length := method.Length
404
405 // Convert static flag
406 isStatic := 0
407 if method.Static {
408 isStatic = 1
409 }
410

Callers 1

BuildMethod · 0.85

Calls 10

validateClassBuilderFunction · 0.85
cloneClassBuilderFunction · 0.85
isContextValueSpecFunction · 0.85
ThrowErrorMethod · 0.80
StoreMethod · 0.80
belongsToMethod · 0.80
DeleteMethod · 0.45
FreeMethod · 0.45

Tested by

no test coverage detected