From b0c2d79fd4eca9363685c0ad884e6cabbcc6d687 Mon Sep 17 00:00:00 2001 From: TAiGA <> Date: Thu, 23 May 2024 14:52:18 +0800 Subject: [PATCH] Fix JSValue casting --- quickjs.c | 40 +++++++++++++++++++++------------------- quickjs.h | 10 ++++++++-- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/quickjs.c b/quickjs.c index f000ff7..059a277 100644 --- a/quickjs.c +++ b/quickjs.c @@ -49,7 +49,7 @@ #define OPTIMIZE 1 #define SHORT_OPCODES 1 -#if defined(EMSCRIPTEN) +#if defined(EMSCRIPTEN) || defined(_MSC_VER) #define DIRECT_DISPATCH 0 #else #define DIRECT_DISPATCH 1 @@ -7308,7 +7308,7 @@ static int JS_DefinePrivateField(JSContext *ctx, JSValueConst obj, JS_ThrowTypeErrorNotASymbol(ctx); goto fail; } - prop = js_symbol_to_atom(ctx, (JSValue)name); + prop = js_symbol_to_atom(ctx, JS_VALUE_CAST(JSValue, name)); p = JS_VALUE_GET_OBJ(obj); prs = find_own_property(&pr, p, prop); if (prs) { @@ -7339,7 +7339,7 @@ static JSValue JS_GetPrivateField(JSContext *ctx, JSValueConst obj, /* safety check */ if (unlikely(JS_VALUE_GET_TAG(name) != JS_TAG_SYMBOL)) return JS_ThrowTypeErrorNotASymbol(ctx); - prop = js_symbol_to_atom(ctx, (JSValue)name); + prop = js_symbol_to_atom(ctx, JS_VALUE_CAST(JSValue, name)); p = JS_VALUE_GET_OBJ(obj); prs = find_own_property(&pr, p, prop); if (!prs) { @@ -7366,7 +7366,7 @@ static int JS_SetPrivateField(JSContext *ctx, JSValueConst obj, JS_ThrowTypeErrorNotASymbol(ctx); goto fail; } - prop = js_symbol_to_atom(ctx, (JSValue)name); + prop = js_symbol_to_atom(ctx, JS_VALUE_CAST(JSValue, name)); p = JS_VALUE_GET_OBJ(obj); prs = find_own_property(&pr, p, prop); if (!prs) { @@ -7465,7 +7465,7 @@ static int JS_CheckBrand(JSContext *ctx, JSValueConst obj, JSValueConst func) return -1; } p = JS_VALUE_GET_OBJ(obj); - prs = find_own_property(&pr, p, js_symbol_to_atom(ctx, (JSValue)brand)); + prs = find_own_property(&pr, p, js_symbol_to_atom(ctx, JS_VALUE_CAST(JSValue, brand))); return (prs != NULL); } @@ -9085,7 +9085,7 @@ int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj, return -1; } /* this code relies on the fact that Uint32 are never allocated */ - val = (JSValueConst)JS_NewUint32(ctx, array_length); + val = JS_VALUE_CAST(JSValueConst, JS_NewUint32(ctx, array_length)); /* prs may have been modified */ prs = find_own_property(&pr, p, prop); assert(prs != NULL); @@ -10292,7 +10292,8 @@ static JSValue js_atof(JSContext *ctx, const char *str, const char **pp, } else #endif { - double d = 1.0 / 0.0; + double z = 0.0; + double d = 1.0 / z; if (is_neg) d = -d; val = JS_NewFloat64(ctx, d); @@ -16002,7 +16003,7 @@ static JSValue js_call_c_function(JSContext *ctx, JSValueConst func_obj, #else sf->js_mode = 0; #endif - sf->cur_func = (JSValue)func_obj; + sf->cur_func = JS_VALUE_CAST(JSValue, func_obj); sf->arg_count = argc; arg_buf = argv; @@ -16247,7 +16248,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj, sf->js_mode = b->js_mode; arg_buf = argv; sf->arg_count = argc; - sf->cur_func = (JSValue)func_obj; + sf->cur_func = JS_VALUE_CAST(JSValue, func_obj); init_list_head(&sf->var_ref_list); var_refs = p->u.func.var_refs; @@ -40442,8 +40443,8 @@ static int64_t JS_FlattenIntoArray(JSContext *ctx, JSValueConst target, if (!JS_IsUndefined(mapperFunction)) { JSValueConst args[3] = { element, JS_NewInt64(ctx, sourceIndex), source }; element = JS_Call(ctx, mapperFunction, thisArg, 3, args); - JS_FreeValue(ctx, (JSValue)args[0]); - JS_FreeValue(ctx, (JSValue)args[1]); + JS_FreeValue(ctx, JS_VALUE_CAST(JSValue, args[0])); + JS_FreeValue(ctx, JS_VALUE_CAST(JSValue, args[1])); if (JS_IsException(element)) return -1; } @@ -42009,7 +42010,7 @@ static JSValue js_string_match(JSContext *ctx, JSValueConst this_val, str = JS_NewString(ctx, "g"); if (JS_IsException(str)) goto fail; - args[args_len++] = (JSValueConst)str; + args[args_len++] = JS_VALUE_CAST(JSValueConst, str); } rx = JS_CallConstructor(ctx, ctx->regexp_ctor, args_len, args); JS_FreeValue(ctx, str); @@ -43140,7 +43141,8 @@ static JSValue js_math_min_max(JSContext *ctx, JSValueConst this_val, uint32_t tag; if (unlikely(argc == 0)) { - return __JS_NewFloat64(ctx, is_max ? -1.0 / 0.0 : 1.0 / 0.0); + double z = 0.0; + return __JS_NewFloat64(ctx, is_max ? -1.0 / z : 1.0 / z); } tag = JS_VALUE_GET_TAG(argv[0]); @@ -47223,7 +47225,7 @@ static JSMapRecord *map_add_record(JSContext *ctx, JSMapState *s, } else { JS_DupValue(ctx, key); } - mr->key = (JSValue)key; + mr->key = JS_VALUE_CAST(JSValue, key); h = map_hash_key(ctx, key) & (s->hash_size - 1); list_add_tail(&mr->hash_link, &s->hash_table[h]); list_add_tail(&mr->link, &s->records); @@ -47445,7 +47447,7 @@ static JSValue js_map_forEach(JSContext *ctx, JSValueConst this_val, args[0] = args[1]; else args[0] = JS_DupValue(ctx, mr->value); - args[2] = (JSValue)this_val; + args[2] = JS_VALUE_CAST(JSValue, this_val); ret = JS_Call(ctx, func, this_arg, 3, (JSValueConst *)args); JS_FreeValue(ctx, args[0]); if (!magic) @@ -48547,7 +48549,7 @@ static JSValue js_promise_all(JSContext *ctx, JSValueConst this_val, goto fail_reject; } resolve_element_data[0] = JS_NewBool(ctx, FALSE); - resolve_element_data[1] = (JSValueConst)JS_NewInt32(ctx, index); + resolve_element_data[1] = JS_VALUE_CAST(JSValueConst, JS_NewInt32(ctx, index)); resolve_element_data[2] = values; resolve_element_data[3] = resolving_funcs[is_promise_any]; resolve_element_data[4] = resolve_element_env; @@ -48906,7 +48908,7 @@ static JSValue js_async_from_sync_iterator_unwrap_func_create(JSContext *ctx, { JSValueConst func_data[1]; - func_data[0] = (JSValueConst)JS_NewBool(ctx, done); + func_data[0] = JS_VALUE_CAST(JSValueConst, JS_NewBool(ctx, done)); return JS_NewCFunctionData(ctx, js_async_from_sync_iterator_unwrap, 1, 0, 1, func_data); } @@ -54676,8 +54678,8 @@ static int js_TA_cmp_generic(const void *a, const void *b, void *opaque) { psc->exception = 2; } done: - JS_FreeValue(ctx, (JSValue)argv[0]); - JS_FreeValue(ctx, (JSValue)argv[1]); + JS_FreeValue(ctx, JS_VALUE_CAST(JSValue, argv[0])); + JS_FreeValue(ctx, JS_VALUE_CAST(JSValue, argv[1])); } return cmp; } diff --git a/quickjs.h b/quickjs.h index edc7b47..46db704 100644 --- a/quickjs.h +++ b/quickjs.h @@ -207,6 +207,12 @@ typedef struct JSValue { #define JSValueConst JSValue +#if defined(_MSC_VER) +#define JS_VALUE_CAST(t, v) v +#else +#define JS_VALUE_CAST(t, v) (t)v +#endif + #define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag) /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */ #define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v) @@ -672,7 +678,7 @@ static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v) JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); p->ref_count++; } - return (JSValue)v; + return JS_VALUE_CAST(JSValue, v); } static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v) @@ -681,7 +687,7 @@ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v) JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v); p->ref_count++; } - return (JSValue)v; + return JS_VALUE_CAST(JSValue, v); } JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2);