remove instanceof operator

This commit is contained in:
2026-01-08 12:26:11 -06:00
parent 8203f6d1c3
commit 69245f82db
4 changed files with 1 additions and 147 deletions

View File

@@ -40,7 +40,6 @@ DEF(delete, "delete")
DEF(void, "void")
DEF(new, "new")
DEF(in, "in")
DEF(instanceof, "instanceof")
DEF(do, "do")
DEF(while, "while")
DEF(for, "for")

View File

@@ -222,7 +222,6 @@ DEF( lt, 1, 2, 1, none)
DEF( lte, 1, 2, 1, none)
DEF( gt, 1, 2, 1, none)
DEF( gte, 1, 2, 1, none)
DEF( instanceof, 1, 2, 1, none)
DEF( in, 1, 2, 1, none)
DEF( eq, 1, 2, 1, none)
DEF( neq, 1, 2, 1, none)

View File

@@ -6873,105 +6873,6 @@ static JSValue JS_GetPrototypeFree(JSContext *ctx, JSValue obj)
return obj1;
}
/* return TRUE, FALSE or (-1) in case of exception */
static int JS_OrdinaryIsInstanceOf(JSContext *ctx, JSValueConst val,
JSValueConst obj)
{
JSValue obj_proto;
JSObject *proto;
const JSObject *p, *proto1;
BOOL ret;
if (!JS_IsFunction(ctx, obj))
return FALSE;
p = JS_VALUE_GET_OBJ(obj);
if (p->class_id == JS_CLASS_BOUND_FUNCTION) {
JSBoundFunction *s = p->u.bound_function;
return JS_IsInstanceOf(ctx, val, s->func_obj);
}
/* Only explicitly boxed values are instances of constructors */
if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
return FALSE;
obj_proto = JS_GetProperty(ctx, obj, JS_ATOM_prototype);
if (JS_VALUE_GET_TAG(obj_proto) != JS_TAG_OBJECT) {
if (!JS_IsException(obj_proto))
JS_ThrowTypeError(ctx, "operand 'prototype' property is not an object");
ret = -1;
goto done;
}
proto = JS_VALUE_GET_OBJ(obj_proto);
p = JS_VALUE_GET_OBJ(val);
for(;;) {
proto1 = p->shape->proto;
if (!proto1) {
/* slow case if exotic object in the prototype chain */
if (unlikely(p->is_exotic && !p->fast_array)) {
JSValue obj1;
obj1 = JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, (JSObject *)p));
for(;;) {
obj1 = JS_GetPrototypeFree(ctx, obj1);
if (JS_IsException(obj1)) {
ret = -1;
break;
}
if (JS_IsNull(obj1)) {
ret = FALSE;
break;
}
if (proto == JS_VALUE_GET_OBJ(obj1)) {
JS_FreeValue(ctx, obj1);
ret = TRUE;
break;
}
/* must check for timeout to avoid infinite loop */
if (js_poll_interrupts(ctx)) {
JS_FreeValue(ctx, obj1);
ret = -1;
break;
}
}
} else {
ret = FALSE;
}
break;
}
p = proto1;
if (proto == p) {
ret = TRUE;
break;
}
}
done:
JS_FreeValue(ctx, obj_proto);
return ret;
}
/* return TRUE, FALSE or (-1) in case of exception */
int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj)
{
JSValue method;
if (!JS_IsObject(obj))
goto fail;
method = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_hasInstance);
if (JS_IsException(method))
return -1;
if (!JS_IsNull(method) && !JS_IsNull(method)) {
JSValue ret;
ret = JS_CallFree(ctx, method, obj, 1, &val);
return JS_ToBoolFree(ctx, ret);
}
/* legacy case */
if (!JS_IsFunction(ctx, obj)) {
fail:
JS_ThrowTypeError(ctx, "invalid 'instanceof' right operand");
return -1;
}
return JS_OrdinaryIsInstanceOf(ctx, val, obj);
}
/* return the value associated to the autoinit property or an exception */
typedef JSValue JSAutoInitFunc(JSContext *ctx, JSObject *p, JSAtom atom, void *opaque);
@@ -11670,22 +11571,6 @@ static __exception int js_operator_in(JSContext *ctx, JSValue *sp)
return 0;
}
static __exception int js_operator_instanceof(JSContext *ctx, JSValue *sp)
{
JSValue op1, op2;
BOOL ret;
op1 = sp[-2];
op2 = sp[-1];
ret = JS_IsInstanceOf(ctx, op1, op2);
if (ret < 0)
return ret;
JS_FreeValue(ctx, op1);
JS_FreeValue(ctx, op2);
sp[-2] = JS_NewBool(ctx, ret);
return 0;
}
static __exception int js_operator_delete(JSContext *ctx, JSValue *sp)
{
JSValue op1, op2;
@@ -15495,12 +15380,6 @@ static JSValue JS_CallInternal_OLD(JSContext *caller_ctx, JSValueConst func_obj,
goto exception;
sp--;
BREAK;
CASE(OP_instanceof):
sf->cur_pc = pc;
if (js_operator_instanceof(ctx, sp))
goto exception;
sp--;
BREAK;
CASE(OP_delete):
sf->cur_pc = pc;
if (js_operator_delete(ctx, sp))
@@ -15844,7 +15723,6 @@ enum {
TOK_VOID,
TOK_NEW,
TOK_IN,
TOK_INSTANCEOF,
TOK_DO,
TOK_WHILE,
TOK_FOR,
@@ -16946,7 +16824,7 @@ static __exception int next_token(JSParseState *s)
flags);
if (JS_IsException(ret))
goto fail;
/* reject `10instanceof Number` */
/* reject number immediately followed by identifier */
if (JS_VALUE_IS_NAN(ret) ||
lre_js_is_ident_next(unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p1))) {
JS_FreeValue(s->ctx, ret);
@@ -20506,9 +20384,6 @@ static __exception int js_parse_expr_binary(JSParseState *s, int level,
case TOK_GTE:
opcode = OP_gte;
break;
case TOK_INSTANCEOF:
opcode = OP_instanceof;
break;
case TOK_IN:
if (parse_flags & PF_IN_ACCEPTED) {
opcode = OP_in;
@@ -28145,9 +28020,6 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
if (atom == JS_ATOM_Symbol_toPrimitive) {
/* Symbol.toPrimitive functions are not writable */
prop_flags = JS_PROP_CONFIGURABLE;
} else if (atom == JS_ATOM_Symbol_hasInstance) {
/* Function.prototype[Symbol.hasInstance] is not writable nor configurable */
prop_flags = 0;
}
}
break;
@@ -28155,9 +28027,6 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj,
if (atom == JS_ATOM_Symbol_toPrimitive) {
/* Symbol.toPrimitive functions are not writable */
prop_flags = JS_PROP_CONFIGURABLE;
} else if (atom == JS_ATOM_Symbol_hasInstance) {
/* Function.prototype[Symbol.hasInstance] is not writable nor configurable */
prop_flags = 0;
}
JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP,
(void *)e, prop_flags);
@@ -29464,23 +29333,11 @@ static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
}
}
static JSValue js_function_hasInstance(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int ret;
ret = JS_OrdinaryIsInstanceOf(ctx, argv[0], this_val);
if (ret < 0)
return JS_EXCEPTION;
else
return JS_NewBool(ctx, ret);
}
static const JSCFunctionListEntry js_function_proto_funcs[] = {
JS_CFUNC_DEF("call", 1, js_function_call ),
JS_CFUNC_MAGIC_DEF("apply", 2, js_function_apply, 0 ),
JS_CFUNC_DEF("bind", 1, js_function_bind ),
JS_CFUNC_DEF("toString", 0, js_function_toString ),
JS_CFUNC_DEF("[Symbol.hasInstance]", 1, js_function_hasInstance ),
JS_CGETSET_DEF("fileName", js_function_proto_fileName, NULL ),
JS_CGETSET_MAGIC_DEF("lineNumber", js_function_proto_lineNumber, NULL, 0 ),
JS_CGETSET_MAGIC_DEF("columnNumber", js_function_proto_lineNumber, NULL, 1 ),

View File

@@ -765,7 +765,6 @@ JSValue JS_EvalThis(JSContext *ctx, JSValueConst this_obj,
const char *input, size_t input_len,
const char *filename, int eval_flags);
JSValue JS_GetGlobalObject(JSContext *ctx);
int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj);
int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
JSAtom prop, JSValueConst val,
JSValueConst getter, JSValueConst setter, int flags);