LibJS: Add [[HostDefined]] internal slot to Script objects

In C++, this is a raw pointer to a Script::HostDefined.
This commit is contained in:
Andreas Kling 2022-02-07 18:25:39 +01:00
parent 77a1ef06a4
commit 6ddbe8f953
Notes: sideshowbarker 2024-07-17 19:38:59 +09:00
2 changed files with 13 additions and 7 deletions

View file

@ -12,7 +12,7 @@
namespace JS {
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename)
Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename, HostDefined* host_defined)
{
// 1. Let body be ParseText(sourceText, Script).
auto parser = Parser(Lexer(source_text, filename));
@ -23,14 +23,15 @@ Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView so
return parser.errors();
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
return adopt_ref(*new Script(realm, filename, move(body)));
return adopt_ref(*new Script(realm, filename, move(body), host_defined));
}
Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node)
Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
: m_vm(realm.vm())
, m_realm(make_handle(&realm))
, m_parse_node(move(parse_node))
, m_filename(filename)
, m_host_defined(host_defined)
{
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -20,17 +20,21 @@ class Script
: public RefCounted<Script>
, public Weakable<Script> {
public:
struct HostDefined {
virtual ~HostDefined() = default;
};
~Script();
static Result<NonnullRefPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {});
static Result<NonnullRefPtr<Script>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {}, HostDefined* = nullptr);
Realm& realm() { return *m_realm.cell(); }
Program const& parse_node() const { return *m_parse_node; }
HostDefined* host_defined() { return m_host_defined; }
StringView filename() const { return m_filename; }
private:
Script(Realm&, StringView filename, NonnullRefPtr<Program>);
Script(Realm&, StringView filename, NonnullRefPtr<Program>, HostDefined* = nullptr);
// Handles are not safe unless we keep the VM alive.
NonnullRefPtr<VM> m_vm;
@ -39,6 +43,7 @@ private:
// Needed for potential lookups of modules.
String m_filename;
HostDefined* m_host_defined { nullptr }; // [[HostDefined]]
};
}