|
| 1 | +#include "qjs.h" |
| 2 | + |
| 3 | +// toString method for QJS_PROXY_VALUE class |
| 4 | +static JSValue qjs_proxy_value_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) |
| 5 | +{ |
| 6 | + JSValue proxy_id = JS_GetPropertyStr(ctx, this_val, "proxyId"); |
| 7 | + if (JS_IsException(proxy_id)) |
| 8 | + return proxy_id; |
| 9 | + |
| 10 | + const char *proxy_id_str = JS_ToCString(ctx, proxy_id); |
| 11 | + JS_FreeValue(ctx, proxy_id); |
| 12 | + |
| 13 | + if (!proxy_id_str) |
| 14 | + return JS_EXCEPTION; |
| 15 | + |
| 16 | + char buffer[256]; |
| 17 | + snprintf(buffer, sizeof(buffer), "[object QJS_PROXY_VALUE(proxyId: %s)]", proxy_id_str); |
| 18 | + JS_FreeCString(ctx, proxy_id_str); |
| 19 | + |
| 20 | + return JS_NewString(ctx, buffer); |
| 21 | +} |
| 22 | + |
| 23 | +// Constructor function for QJS_PROXY_VALUE class |
| 24 | +static JSValue qjs_proxy_value_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) |
| 25 | +{ |
| 26 | + JSValue obj; |
| 27 | + JSValue proto; |
| 28 | + |
| 29 | + if (JS_IsUndefined(new_target)) |
| 30 | + { |
| 31 | + // Called as function, not constructor |
| 32 | + return JS_ThrowTypeError(ctx, "QJS_PROXY_VALUE must be called with new"); |
| 33 | + } |
| 34 | + |
| 35 | + // Get prototype from new_target |
| 36 | + proto = JS_GetPropertyStr(ctx, new_target, "prototype"); |
| 37 | + if (JS_IsException(proto)) |
| 38 | + return proto; |
| 39 | + |
| 40 | + // Create object with proper prototype |
| 41 | + obj = JS_NewObjectProto(ctx, proto); |
| 42 | + JS_FreeValue(ctx, proto); |
| 43 | + |
| 44 | + if (JS_IsException(obj)) |
| 45 | + return obj; |
| 46 | + |
| 47 | + // Set the proxyId property |
| 48 | + if (argc > 0) |
| 49 | + { |
| 50 | + if (JS_SetPropertyStr(ctx, obj, "proxyId", JS_DupValue(ctx, argv[0])) < 0) |
| 51 | + { |
| 52 | + JS_FreeValue(ctx, obj); |
| 53 | + return JS_EXCEPTION; |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + if (JS_SetPropertyStr(ctx, obj, "proxyId", JS_UNDEFINED) < 0) |
| 59 | + { |
| 60 | + JS_FreeValue(ctx, obj); |
| 61 | + return JS_EXCEPTION; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return obj; |
| 66 | +} |
| 67 | + |
| 68 | +// Initialize QJS_PROXY_VALUE class and add it to global object |
| 69 | +int init_qjs_proxy_value_class(JSContext *ctx) |
| 70 | +{ |
| 71 | + JSValue global_obj = JS_GetGlobalObject(ctx); |
| 72 | + |
| 73 | + // Create prototype object with toString method |
| 74 | + JSValue proto = JS_NewObject(ctx); |
| 75 | + JSValue toString_func = JS_NewCFunction(ctx, qjs_proxy_value_toString, "toString", 0); |
| 76 | + if (JS_SetPropertyStr(ctx, proto, "toString", toString_func) < 0) |
| 77 | + { |
| 78 | + JS_FreeValue(ctx, proto); |
| 79 | + JS_FreeValue(ctx, global_obj); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + // Create the constructor function |
| 84 | + JSValue ctor = JS_NewCFunction2(ctx, qjs_proxy_value_constructor, "QJS_PROXY_VALUE", 1, JS_CFUNC_constructor, 0); |
| 85 | + |
| 86 | + // Set proto.constructor and ctor.prototype using QuickJS helper |
| 87 | + JS_SetConstructor(ctx, ctor, proto); |
| 88 | + |
| 89 | + // Add the constructor to the global object |
| 90 | + if (JS_SetPropertyStr(ctx, global_obj, "QJS_PROXY_VALUE", ctor) < 0) |
| 91 | + { |
| 92 | + JS_FreeValue(ctx, global_obj); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + JS_FreeValue(ctx, global_obj); |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +// Create a new QJS_PROXY_VALUE instance directly in C for better performance |
| 101 | +JSValue QJS_NewProxyValue(JSContext *ctx, int64_t proxyId) |
| 102 | +{ |
| 103 | + // Get the QJS_PROXY_VALUE constructor from global object |
| 104 | + JSValue global_obj = JS_GetGlobalObject(ctx); |
| 105 | + JSValue ctor = JS_GetPropertyStr(ctx, global_obj, "QJS_PROXY_VALUE"); |
| 106 | + JS_FreeValue(ctx, global_obj); |
| 107 | + |
| 108 | + if (JS_IsException(ctor) || JS_IsUndefined(ctor)) |
| 109 | + { |
| 110 | + JS_FreeValue(ctx, ctor); |
| 111 | + return JS_ThrowReferenceError(ctx, "QJS_PROXY_VALUE is not defined"); |
| 112 | + } |
| 113 | + |
| 114 | + // Create argument for the constructor (proxyId) |
| 115 | + JSValue arg = JS_NewInt64(ctx, proxyId); |
| 116 | + JSValue args[1] = {arg}; |
| 117 | + |
| 118 | + // Call the constructor with 'new' |
| 119 | + JSValue result = JS_CallConstructor(ctx, ctor, 1, args); |
| 120 | + |
| 121 | + // Clean up |
| 122 | + JS_FreeValue(ctx, ctor); |
| 123 | + JS_FreeValue(ctx, arg); |
| 124 | + |
| 125 | + return result; |
| 126 | +} |
0 commit comments