Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSValue casting #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ typedef struct JSRefCountHeader {

#define JS_FLOAT64_NAN NAN

#if defined(_MSC_VER)
#define JS_VALUE_CAST(t, v) v
#else
#define JS_VALUE_CAST(t, v) (t)v
#endif

#ifdef CONFIG_CHECK_JSVALUE
/* JSValue consistency : it is not possible to run the code in this
mode, but it is useful to detect simple reference counting
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down