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

Function CreateClass

bridge.c:578–654  ·  view source on GitHub ↗

CreateClass - Complete class creation function This function handles all QuickJS class creation steps: 1. Allocate class_id internally 2. Register class definition (Go layer manages JSClassDef memory) 3. Create prototype object 4. Bind instance members to prototype 5. Create constructor function 6. Associate constructor with prototype 7. Set class prototype 8. Bind static members to constructor R

Source from the content-addressed store, hash-verified

576// Returns constructor JSValue on success, JS_EXCEPTION on failure
577// class_id is returned via pointer parameter
578JSValue CreateClass(JSContext *ctx,
579 JSClassID *class_id, // C function allocates internally
580 JSClassDef *class_def, // Go layer manages memory
581 int32_t constructor_id,
582 const MethodEntry *methods, int method_count,
583 const AccessorEntry *accessors, int accessor_count,
584 const PropertyEntry *properties, int property_count) {
585
586 JSValue proto, constructor;
587 JSRuntime *rt = JS_GetRuntime(ctx);
588
589 // Step 1: Input validation
590 if (!class_def || !class_def->class_name) {
591 return JS_ThrowInternalError(ctx, "class_def or class_name is null");
592 }
593
594 // Check for empty class name
595 if (strlen(class_def->class_name) == 0) {
596 return JS_ThrowInternalError(ctx, "class_name cannot be empty");
597 }
598
599 // Step 2: Allocate class_id internally (corresponds to point.c: JS_NewClassID(rt, &js_point_class_id))
600 JS_NewClassID(rt, class_id);
601
602 // Check QuickJS limits
603 if (*class_id >= (1 << 16)) {
604 return JS_ThrowRangeError(ctx, "class ID exceeds maximum value");
605 }
606
607 // Step 3: Register class definition (corresponds to point.c: JS_NewClass)
608 // Go layer manages class_def memory, we just use it
609 int class_result = JS_NewClass(rt, *class_id, class_def);
610 if (class_result != 0) {
611 return JS_ThrowInternalError(ctx, "JS_NewClass failed: result=%d", class_result);
612 }
613
614 // Step 4: Create prototype object (corresponds to point.c: point_proto = JS_NewObject(ctx))
615 proto = JS_NewObject(ctx);
616 if (JS_IsException(proto)) {
617 return proto;
618 }
619
620 // Step 5: Bind instance members to prototype
621 JSValue proto_result = BindMembersToObject(ctx, proto, methods, method_count,
622 accessors, accessor_count,
623 properties, property_count, 0);
624 if (JS_IsException(proto_result)) {
625 JS_FreeValue(ctx, proto);
626 return proto_result;
627 }
628
629 // Step 6: Create constructor function (corresponds to point.c: JS_NewCFunction2)
630 constructor = CreateCFunction(ctx, class_def->class_name, 2,
631 JS_CFUNC_constructor_magic, constructor_id);
632 if (JS_IsException(constructor)) {
633 JS_FreeValue(ctx, proto);
634 return constructor;
635 }

Callers

nothing calls this directly

Calls 3

JS_IsExceptionFunction · 0.85
BindMembersToObjectFunction · 0.85
CreateCFunctionFunction · 0.85

Tested by

no test coverage detected