1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:47:46 +00:00

LibJS: Introduce Builtins

Builtins are functions that can be detected during bytecode generation
and enable fast-paths in the JIT.
This commit is contained in:
Simon Wanner 2023-11-17 11:48:30 +01:00 committed by Andreas Kling
parent b9141d85d8
commit 86b85aa68b
13 changed files with 191 additions and 4 deletions

View file

@ -1269,10 +1269,12 @@ Value Object::get_without_side_effects(PropertyKey const& property_key) const
return {};
}
void Object::define_native_function(Realm& realm, PropertyKey const& property_key, Function<ThrowCompletionOr<Value>(VM&)> native_function, i32 length, PropertyAttributes attribute)
void Object::define_native_function(Realm& realm, PropertyKey const& property_key, Function<ThrowCompletionOr<Value>(VM&)> native_function, i32 length, PropertyAttributes attribute, Optional<Bytecode::Builtin> builtin)
{
auto function = NativeFunction::create(realm, move(native_function), length, property_key, &realm);
define_direct_property(property_key, function, attribute);
if (builtin.has_value())
realm.define_builtin(builtin.value(), function);
}
// 20.1.2.3.1 ObjectDefineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-objectdefineproperties

View file

@ -177,7 +177,7 @@ public:
using IntrinsicAccessor = Value (*)(Realm&);
void define_intrinsic_accessor(PropertyKey const&, PropertyAttributes attributes, IntrinsicAccessor accessor);
void define_native_function(Realm&, PropertyKey const&, Function<ThrowCompletionOr<Value>(VM&)>, i32 length, PropertyAttributes attributes);
void define_native_function(Realm&, PropertyKey const&, Function<ThrowCompletionOr<Value>(VM&)>, i32 length, PropertyAttributes attributes, Optional<Bytecode::Builtin> builtin = {});
void define_native_accessor(Realm&, PropertyKey const&, Function<ThrowCompletionOr<Value>(VM&)> getter, Function<ThrowCompletionOr<Value>(VM&)> setter, PropertyAttributes attributes);
virtual bool is_dom_node() const { return false; }

View file

@ -11,8 +11,10 @@
#include <AK/OwnPtr.h>
#include <AK/StringView.h>
#include <AK/Weakable.h>
#include <LibJS/Bytecode/Builtins.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Intrinsics.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
@ -48,7 +50,13 @@ public:
HostDefined* host_defined() { return m_host_defined; }
void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); }
void define_builtin(Bytecode::Builtin builtin, Value value)
{
m_builtins[to_underlying(builtin)] = value;
}
static FlatPtr global_environment_offset() { return OFFSET_OF(Realm, m_global_environment); }
static FlatPtr builtins_offset() { return OFFSET_OF(Realm, m_builtins); }
private:
Realm() = default;
@ -59,6 +67,7 @@ private:
GCPtr<Object> m_global_object; // [[GlobalObject]]
GCPtr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]]
OwnPtr<HostDefined> m_host_defined; // [[HostDefined]]
AK::Array<Value, to_underlying(Bytecode::Builtin::__Count)> m_builtins;
};
}