From 52c9b6e26ce7b8f785082826b5d67bc3bd278ec2 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 10 Oct 2026 16:23:33 +0200 Subject: [PATCH] exported JS_ToPrimitive(), JS_ToObject() and JS_ToInt32Sat() --- quickjs.c | 60 ++++++++++++++++++++++++++----------------------------- quickjs.h | 6 ++++++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/quickjs.c b/quickjs.c index 92dc724..88504ca 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1241,10 +1241,7 @@ static void js_promise_resolve_function_finalizer(JSRuntime *rt, JSValue val); static void js_promise_resolve_function_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func); -#define HINT_STRING 0 -#define HINT_NUMBER 1 -#define HINT_NONE 2 -#define HINT_FORCE_ORDINARY (1 << 4) // don't try Symbol.toPrimitive +#define JS_HINT_FORCE_ORDINARY (1 << 4) // don't try Symbol.toPrimitive static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint); static JSValue JS_ToStringFree(JSContext *ctx, JSValue val); static int JS_ToBoolFree(JSContext *ctx, JSValue val); @@ -1270,7 +1267,6 @@ static BOOL js_strict_eq2(JSContext *ctx, JSValueConst op1, JSValueConst op2, static BOOL js_strict_eq(JSContext *ctx, JSValueConst op1, JSValueConst op2); static BOOL js_same_value(JSContext *ctx, JSValueConst op1, JSValueConst op2); static BOOL js_same_value_zero(JSContext *ctx, JSValueConst op1, JSValueConst op2); -static JSValue JS_ToObject(JSContext *ctx, JSValueConst val); static JSValue JS_ToObjectFree(JSContext *ctx, JSValue val); static JSProperty *add_property(JSContext *ctx, JSObject *p, JSAtom prop, int prop_flags); @@ -11095,8 +11091,8 @@ static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint) JSValue method, ret; if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT) return val; - force_ordinary = hint & HINT_FORCE_ORDINARY; - hint &= ~HINT_FORCE_ORDINARY; + force_ordinary = hint & JS_HINT_FORCE_ORDINARY; + hint &= ~JS_HINT_FORCE_ORDINARY; if (!force_ordinary) { method = JS_GetProperty(ctx, val, JS_ATOM_Symbol_toPrimitive); if (JS_IsException(method)) @@ -11107,14 +11103,14 @@ static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint) JSAtom atom; JSValue arg; switch(hint) { - case HINT_STRING: + case JS_HINT_STRING: atom = JS_ATOM_string; break; - case HINT_NUMBER: + case JS_HINT_NUMBER: atom = JS_ATOM_number; break; default: - case HINT_NONE: + case JS_HINT_NONE: atom = JS_ATOM_default; break; } @@ -11130,8 +11126,8 @@ static JSValue JS_ToPrimitiveFree(JSContext *ctx, JSValue val, int hint) return JS_ThrowTypeError(ctx, "toPrimitive"); } } - if (hint != HINT_STRING) - hint = HINT_NUMBER; + if (hint != JS_HINT_STRING) + hint = JS_HINT_NUMBER; for(i = 0; i < 2; i++) { if ((i ^ hint) == 0) { method_name = JS_ATOM_toString; @@ -11160,7 +11156,7 @@ exception: return JS_EXCEPTION; } -static JSValue JS_ToPrimitive(JSContext *ctx, JSValueConst val, int hint) +JSValue JS_ToPrimitive(JSContext *ctx, JSValueConst val, int hint) { return JS_ToPrimitiveFree(ctx, JS_DupValue(ctx, val), hint); } @@ -12999,7 +12995,7 @@ static JSValue JS_ToNumberHintFree(JSContext *ctx, JSValue val, ret = JS_NAN; break; case JS_TAG_OBJECT: - val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER); + val = JS_ToPrimitiveFree(ctx, val, JS_HINT_NUMBER); if (JS_IsException(val)) return JS_EXCEPTION; goto redo; @@ -13197,8 +13193,8 @@ int JS_ToInt32Sat(JSContext *ctx, int *pres, JSValueConst val) return JS_ToInt32SatFree(ctx, pres, JS_DupValue(ctx, val)); } -int JS_ToInt32Clamp(JSContext *ctx, int *pres, JSValueConst val, - int min, int max, int min_offset) +static int JS_ToInt32Clamp(JSContext *ctx, int *pres, JSValueConst val, + int min, int max, int min_offset) { int res = JS_ToInt32SatFree(ctx, pres, JS_DupValue(ctx, val)); if (res == 0) { @@ -13668,7 +13664,7 @@ static JSValue JS_ToStringInternal(JSContext *ctx, JSValueConst val, BOOL is_ToP case JS_TAG_OBJECT: { JSValue val1, ret; - val1 = JS_ToPrimitive(ctx, val, HINT_STRING); + val1 = JS_ToPrimitive(ctx, val, JS_HINT_STRING); if (JS_IsException(val1)) return val1; ret = JS_ToStringInternal(ctx, val1, is_ToPropertyKey); @@ -14724,7 +14720,7 @@ static JSValue JS_ToBigIntFree(JSContext *ctx, JSValue val) return val; goto redo; case JS_TAG_OBJECT: - val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER); + val = JS_ToPrimitiveFree(ctx, val, JS_HINT_NUMBER); if (JS_IsException(val)) return val; goto redo; @@ -15291,13 +15287,13 @@ static no_inline __exception int js_add_slow(JSContext *ctx, JSValue *sp) } if (tag1 == JS_TAG_OBJECT || tag2 == JS_TAG_OBJECT) { - op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NONE); + op1 = JS_ToPrimitiveFree(ctx, op1, JS_HINT_NONE); if (JS_IsException(op1)) { JS_FreeValue(ctx, op2); goto exception; } - op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NONE); + op2 = JS_ToPrimitiveFree(ctx, op2, JS_HINT_NONE); if (JS_IsException(op2)) { JS_FreeValue(ctx, op1); goto exception; @@ -15635,12 +15631,12 @@ static no_inline int js_relational_slow(JSContext *ctx, JSValue *sp, op2 = sp[-1]; tag1 = JS_VALUE_GET_NORM_TAG(op1); tag2 = JS_VALUE_GET_NORM_TAG(op2); - op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NUMBER); + op1 = JS_ToPrimitiveFree(ctx, op1, JS_HINT_NUMBER); if (JS_IsException(op1)) { JS_FreeValue(ctx, op2); goto exception; } - op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NUMBER); + op2 = JS_ToPrimitiveFree(ctx, op2, JS_HINT_NUMBER); if (JS_IsException(op2)) { JS_FreeValue(ctx, op1); goto exception; @@ -15858,12 +15854,12 @@ static no_inline __exception int js_eq_slow(JSContext *ctx, JSValue *sp, (tag_is_number(tag2) || tag_is_string(tag2) || tag2 == JS_TAG_SYMBOL)) || (tag2 == JS_TAG_OBJECT && (tag_is_number(tag1) || tag_is_string(tag1) || tag1 == JS_TAG_SYMBOL))) { - op1 = JS_ToPrimitiveFree(ctx, op1, HINT_NONE); + op1 = JS_ToPrimitiveFree(ctx, op1, JS_HINT_NONE); if (JS_IsException(op1)) { JS_FreeValue(ctx, op2); goto exception; } - op2 = JS_ToPrimitiveFree(ctx, op2, HINT_NONE); + op2 = JS_ToPrimitiveFree(ctx, op2, JS_HINT_NONE); if (JS_IsException(op2)) { JS_FreeValue(ctx, op1); goto exception; @@ -40146,7 +40142,7 @@ static JSValue js_global_isFinite(JSContext *ctx, JSValueConst this_val, /* Object class */ -static JSValue JS_ToObject(JSContext *ctx, JSValueConst val) +JSValue JS_ToObject(JSContext *ctx, JSValueConst val) { int tag = JS_VALUE_GET_NORM_TAG(val); JSValue obj; @@ -55662,7 +55658,7 @@ static JSValue js_date_constructor(JSContext *ctx, JSValueConst new_target, goto has_val; } } - v = JS_ToPrimitive(ctx, argv[0], HINT_NONE); + v = JS_ToPrimitive(ctx, argv[0], JS_HINT_NONE); if (JS_IsString(v)) { dv = js_Date_parse(ctx, JS_UNDEFINED, 1, (JSValueConst *)&v); JS_FreeValue(ctx, v); @@ -56217,16 +56213,16 @@ static JSValue js_date_Symbol_toPrimitive(JSContext *ctx, JSValueConst this_val, switch (hint) { case JS_ATOM_number: case JS_ATOM_integer: - hint_num = HINT_NUMBER; + hint_num = JS_HINT_NUMBER; break; case JS_ATOM_string: case JS_ATOM_default: - hint_num = HINT_STRING; + hint_num = JS_HINT_STRING; break; default: return JS_ThrowTypeError(ctx, "invalid hint"); } - return JS_ToPrimitive(ctx, obj, hint_num | HINT_FORCE_ORDINARY); + return JS_ToPrimitive(ctx, obj, hint_num | JS_HINT_FORCE_ORDINARY); } static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValueConst this_val, @@ -56296,7 +56292,7 @@ static JSValue js_date_toJSON(JSContext *ctx, JSValueConst this_val, tv = JS_UNDEFINED; obj = JS_ToObject(ctx, this_val); - tv = JS_ToPrimitive(ctx, obj, HINT_NUMBER); + tv = JS_ToPrimitive(ctx, obj, JS_HINT_NUMBER); if (JS_IsException(tv)) goto exception; if (JS_IsNumber(tv)) { @@ -56452,7 +56448,7 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val) val = JS_StringToBigIntErr(ctx, val); break; case JS_TAG_OBJECT: - val = JS_ToPrimitiveFree(ctx, val, HINT_NUMBER); + val = JS_ToPrimitiveFree(ctx, val, JS_HINT_NUMBER); if (JS_IsException(val)) break; goto redo; @@ -57926,7 +57922,7 @@ static JSValue js_typed_array_with(JSContext *ctx, JSValueConst this_val, if (idx < 0) idx = len + idx; - val = JS_ToPrimitive(ctx, argv[1], HINT_NUMBER); + val = JS_ToPrimitive(ctx, argv[1], JS_HINT_NUMBER); if (JS_IsException(val)) return JS_EXCEPTION; diff --git a/quickjs.h b/quickjs.h index 6165832..27750be 100644 --- a/quickjs.h +++ b/quickjs.h @@ -726,12 +726,18 @@ JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2); JS_BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2); JS_BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2); +JSValue JS_ToObject(JSContext *ctx, JSValueConst val); +#define JS_HINT_STRING 0 +#define JS_HINT_NUMBER 1 +#define JS_HINT_NONE 2 +JSValue JS_ToPrimitive(JSContext *ctx, JSValueConst val, int hint); int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */ int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val); static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val) { return JS_ToInt32(ctx, (int32_t*)pres, val); } +int JS_ToInt32Sat(JSContext *ctx, int *pres, JSValueConst val); int JS_ToInt64(JSContext *ctx, int64_t *pres, JSValueConst val); int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValueConst val); int JS_ToFloat64(JSContext *ctx, double *pres, JSValueConst val); -- 2.47.3