CreateCFunction - encapsulates C function creation logic This function handles: 1. Function type validation and proxy selection 2. JS_NewCFunction2 call with proper parameters 3. Error handling Parameters match JS_NewCFunction2: ctx, name, length, cproto, magic Returns JS_EXCEPTION on any error, proper JSValue on success
| 507 | // Parameters match JS_NewCFunction2: ctx, name, length, cproto, magic |
| 508 | // Returns JS_EXCEPTION on any error, proper JSValue on success |
| 509 | JSValue CreateCFunction(JSContext *ctx, const char *name, |
| 510 | int length, int func_type, int32_t handler_id) { |
| 511 | // Get magic enum values for comparison |
| 512 | int constructor_magic = JS_CFUNC_constructor_magic; |
| 513 | int generic_magic = JS_CFUNC_generic_magic; |
| 514 | int getter_magic = JS_CFUNC_getter_magic; |
| 515 | int setter_magic = JS_CFUNC_setter_magic; |
| 516 | |
| 517 | // Create the C function based on type - each type needs proper casting |
| 518 | JSValue jsFunc; |
| 519 | |
| 520 | if (func_type == constructor_magic) { |
| 521 | // Constructor function: JSValue (*)(JSContext *, JSValueConst, int, JSValueConst *, int) |
| 522 | jsFunc = JS_NewCFunction2(ctx, (JSCFunction *)GoClassConstructorProxy, name, length, |
| 523 | (JSCFunctionEnum)func_type, handler_id); |
| 524 | } else if (func_type == generic_magic) { |
| 525 | // Generic method: JSValue (*)(JSContext *, JSValueConst, int, JSValueConst *, int) |
| 526 | jsFunc = JS_NewCFunction2(ctx, (JSCFunction *)GoClassMethodProxy, name, length, |
| 527 | (JSCFunctionEnum)func_type, handler_id); |
| 528 | } else if (func_type == getter_magic) { |
| 529 | // Getter function: JSValue (*)(JSContext *, JSValueConst, int) |
| 530 | // Note: QuickJS will handle the signature mismatch internally based on the JSCFunctionEnum |
| 531 | jsFunc = JS_NewCFunction2(ctx, (JSCFunction *)GoClassGetterProxy, name, length, |
| 532 | (JSCFunctionEnum)func_type, handler_id); |
| 533 | } else if (func_type == setter_magic) { |
| 534 | // Setter function: JSValue (*)(JSContext *, JSValueConst, JSValueConst, int) |
| 535 | // Note: QuickJS will handle the signature mismatch internally based on the JSCFunctionEnum |
| 536 | jsFunc = JS_NewCFunction2(ctx, (JSCFunction *)GoClassSetterProxy, name, length, |
| 537 | (JSCFunctionEnum)func_type, handler_id); |
| 538 | } else { |
| 539 | // Return exception for unsupported function type |
| 540 | return JS_ThrowTypeError(ctx, "unsupported function type: %d", func_type); |
| 541 | } |
| 542 | |
| 543 | // JS_NewCFunction2 returns JS_EXCEPTION on failure |
| 544 | // No need to check explicitly, just return the result |
| 545 | return jsFunc; |
| 546 | } |
| 547 | |
| 548 | // ============================================================================ |
| 549 | // CLASS CREATION HELPER FUNCTIONS |
no outgoing calls
no test coverage detected