Selaa lähdekoodia

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

In C++, this is a raw pointer to a Script::HostDefined.
Andreas Kling 3 vuotta sitten
vanhempi
commit
6ddbe8f953
2 muutettua tiedostoa jossa 13 lisäystä ja 7 poistoa
  1. 4 3
      Userland/Libraries/LibJS/Script.cpp
  2. 9 4
      Userland/Libraries/LibJS/Script.h

+ 4 - 3
Userland/Libraries/LibJS/Script.cpp

@@ -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)
 {
 }
 

+ 9 - 4
Userland/Libraries/LibJS/Script.h

@@ -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]]
 };
 
 }