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

Function CreateClassInstance

bridge.c:802–855  ·  view source on GitHub ↗

CreateClassInstance - encapsulates the object creation logic for Scheme C MODIFIED FOR SCHEME C: This function now handles instance property binding This function handles: 1. Getting prototype from constructor 2. Creating JS object with correct prototype and class 3. Binding instance properties to the created object (NEW for Scheme C) 4. Error handling and cleanup Note: Go object association is n

Source from the content-addressed store, hash-verified

800// Returns JS_EXCEPTION on any error, proper JSValue on success
801// This corresponds to the logic in point.c example with instance property support
802JSValue CreateClassInstance(JSContext *ctx, JSValue constructor,
803 JSClassID class_id,
804 const PropertyEntry *instance_properties,
805 int instance_property_count) {
806 JSValue proto, obj;
807
808 // Check QuickJS limits
809 if (class_id >= (1 << 16)) {
810 return JS_ThrowRangeError(ctx, "class ID exceeds maximum value");
811 }
812
813 // Step 1: Get prototype from constructor
814 // Corresponds to point.c: proto = JS_GetPropertyStr(ctx, new_target, "prototype")
815 proto = JS_GetPropertyStr(ctx, constructor, "prototype");
816 if (JS_IsException(proto)) {
817 // Return the exception directly, caller will handle cleanup
818 return proto;
819 }
820
821 // Step 2: Create JS object with correct prototype and class
822 // Corresponds to point.c: obj = JS_NewObjectProtoClass(ctx, proto, js_point_class_id)
823 obj = JS_NewObjectProtoClass(ctx, proto, class_id);
824
825 // Free prototype reference (always needed, regardless of obj creation result)
826 JS_FreeValue(ctx, proto);
827
828 if (JS_IsException(obj)) {
829 // Return the exception directly, caller will handle cleanup
830 return obj;
831 }
832
833 // Step 3: NEW FOR SCHEME C - Bind instance properties before constructor call
834 // This ensures instance properties are available when the constructor function runs
835 if (instance_properties && instance_property_count > 0) {
836 for (int i = 0; i < instance_property_count; i++) {
837 const PropertyEntry *property = &instance_properties[i];
838
839 // Only process instance properties (static properties handled elsewhere)
840 if (property->is_static == 0) {
841 JSValue property_result = BindPropertyToObject(ctx, obj, property);
842 if (JS_IsException(property_result)) {
843 // Clean up object on property binding failure
844 JS_FreeValue(ctx, obj);
845 return property_result;
846 }
847 }
848 }
849 }
850
851 // Step 4: Return instance (Go object association handled by constructor proxy)
852 // In Scheme C, the constructor proxy will call this function, then call the
853 // constructor function, and finally associate the returned Go object
854 return obj;
855}
856
857// ============================================================================
858// INTERRUPT HANDLERS

Callers

nothing calls this directly

Calls 2

JS_IsExceptionFunction · 0.85
BindPropertyToObjectFunction · 0.85

Tested by

no test coverage detected