LibJS: Implement Symbol.hasInstance

This commit is contained in:
Matthew Olsson 2020-07-11 15:33:04 -07:00 committed by Andreas Kling
parent dd49ec17a2
commit b0296735a5
Notes: sideshowbarker 2024-07-19 04:49:48 +09:00
2 changed files with 13 additions and 0 deletions

View file

@ -51,6 +51,7 @@ void FunctionPrototype::initialize(Interpreter& interpreter, GlobalObject& globa
define_native_function("bind", bind, 1, attr);
define_native_function("call", call, 1, attr);
define_native_function("toString", to_string, 0, attr);
define_native_function(interpreter.well_known_symbol_has_instance(), symbol_has_instance, 1, 0);
define_property("length", Value(0), Attribute::Configurable);
define_property("name", js_string(heap(), ""), Attribute::Configurable);
}
@ -167,4 +168,15 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::to_string)
return js_string(interpreter, function_source);
}
JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::symbol_has_instance)
{
auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
if (!this_object)
return {};
if (!this_object->is_function())
return interpreter.throw_exception<TypeError>(ErrorType::NotA, "Function");
return ordinary_has_instance(interpreter, interpreter.argument(0), this_object);
}
}

View file

@ -43,6 +43,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(bind);
JS_DECLARE_NATIVE_FUNCTION(call);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(symbol_has_instance);
};
}