Преглед изворни кода

LibJS: Support LoadRequestedModule AO on SyntheticModule records

This allows test-js to run all the module tests in the new world.
Andreas Kling пре 1 година
родитељ
комит
fc31a0d506

+ 1 - 1
Userland/Libraries/LibJS/CyclicModule.cpp

@@ -848,7 +848,7 @@ void continue_dynamic_import(NonnullGCPtr<PromiseCapability> promise_capability,
     auto& module = *module_completion.value();
 
     // 3. Let loadPromise be module.LoadRequestedModules().
-    auto& load_promise = verify_cast<CyclicModule>(module).load_requested_modules({});
+    auto& load_promise = module.load_requested_modules({});
 
     // 4. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures promiseCapability and performs the
     //    following steps when called:

+ 1 - 33
Userland/Libraries/LibJS/CyclicModule.h

@@ -22,38 +22,6 @@ enum class ModuleStatus {
     Evaluated
 };
 
-class CyclicModule;
-
-// https://tc39.es/ecma262/#graphloadingstate-record
-struct GraphLoadingState : public Cell {
-    JS_CELL(GraphLoadingState, Cell);
-
-public:
-    struct HostDefined : Cell {
-        JS_CELL(HostDefined, Cell);
-
-    public:
-        virtual ~HostDefined() = default;
-    };
-
-    GCPtr<PromiseCapability> promise_capability; // [[PromiseCapability]]
-    bool is_loading { false };                   // [[IsLoading]]
-    size_t pending_module_count { 0 };           // [[PendingModulesCount]]
-    HashTable<CyclicModule*> visited;            // [[Visited]]
-    GCPtr<HostDefined> host_defined;             // [[HostDefined]]
-
-private:
-    GraphLoadingState(GCPtr<PromiseCapability> promise_capability, bool is_loading, size_t pending_module_count, HashTable<CyclicModule*> visited, GCPtr<HostDefined> host_defined)
-        : promise_capability(move(promise_capability))
-        , is_loading(is_loading)
-        , pending_module_count(pending_module_count)
-        , visited(move(visited))
-        , host_defined(move(host_defined))
-    {
-    }
-    virtual void visit_edges(Cell::Visitor&) override;
-};
-
 // 16.2.1.5 Cyclic Module Records, https://tc39.es/ecma262/#cyclic-module-record
 class CyclicModule : public Module {
     JS_CELL(CyclicModule, Module);
@@ -65,7 +33,7 @@ public:
     virtual ThrowCompletionOr<void> link(VM& vm) override final;
     virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) override final;
 
-    virtual PromiseCapability& load_requested_modules(GCPtr<GraphLoadingState::HostDefined>);
+    virtual PromiseCapability& load_requested_modules(GCPtr<GraphLoadingState::HostDefined>) override;
     virtual void inner_module_loading(GraphLoadingState& state);
 
     Vector<ModuleRequest> const& requested_modules() const { return m_requested_modules; }

+ 32 - 0
Userland/Libraries/LibJS/Module.h

@@ -56,6 +56,36 @@ struct ResolvedBinding {
     }
 };
 
+// https://tc39.es/ecma262/#graphloadingstate-record
+struct GraphLoadingState : public Cell {
+    JS_CELL(GraphLoadingState, Cell);
+
+public:
+    struct HostDefined : Cell {
+        JS_CELL(HostDefined, Cell);
+
+    public:
+        virtual ~HostDefined() = default;
+    };
+
+    GCPtr<PromiseCapability> promise_capability; // [[PromiseCapability]]
+    bool is_loading { false };                   // [[IsLoading]]
+    size_t pending_module_count { 0 };           // [[PendingModulesCount]]
+    HashTable<CyclicModule*> visited;            // [[Visited]]
+    GCPtr<HostDefined> host_defined;             // [[HostDefined]]
+
+private:
+    GraphLoadingState(GCPtr<PromiseCapability> promise_capability, bool is_loading, size_t pending_module_count, HashTable<CyclicModule*> visited, GCPtr<HostDefined> host_defined)
+        : promise_capability(move(promise_capability))
+        , is_loading(is_loading)
+        , pending_module_count(pending_module_count)
+        , visited(move(visited))
+        , host_defined(move(host_defined))
+    {
+    }
+    virtual void visit_edges(Cell::Visitor&) override;
+};
+
 // 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records
 class Module : public Cell {
     JS_CELL(Module, Cell);
@@ -84,6 +114,8 @@ public:
     virtual ThrowCompletionOr<u32> inner_module_linking(VM& vm, Vector<Module*>& stack, u32 index);
     virtual ThrowCompletionOr<u32> inner_module_evaluation(VM& vm, Vector<Module*>& stack, u32 index);
 
+    virtual PromiseCapability& load_requested_modules(GCPtr<GraphLoadingState::HostDefined>) = 0;
+
 protected:
     Module(Realm&, DeprecatedString filename, Script::HostDefined* host_defined = nullptr);
 

+ 12 - 0
Userland/Libraries/LibJS/SyntheticModule.cpp

@@ -8,6 +8,8 @@
 #include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/GlobalEnvironment.h>
 #include <LibJS/Runtime/ModuleEnvironment.h>
+#include <LibJS/Runtime/PromiseCapability.h>
+#include <LibJS/Runtime/PromiseConstructor.h>
 #include <LibJS/Runtime/VM.h>
 #include <LibJS/SyntheticModule.h>
 
@@ -159,4 +161,14 @@ ThrowCompletionOr<NonnullGCPtr<Module>> parse_json_module(StringView source_text
     return SyntheticModule::create_default_export_synthetic_module(json, realm, filename);
 }
 
+// 1.2.3.1 LoadRequestedModules ( ), https://tc39.es/proposal-json-modules/#sec-smr-LoadRequestedModules
+PromiseCapability& SyntheticModule::load_requested_modules(GCPtr<GraphLoadingState::HostDefined>)
+{
+    // 1. Return ! PromiseResolve(%Promise%, undefined).
+    auto& constructor = *vm().current_realm()->intrinsics().promise_constructor();
+    auto promise_capability = MUST(new_promise_capability(vm(), &constructor));
+    MUST(call(vm(), *promise_capability->resolve(), js_undefined(), js_undefined()));
+    return promise_capability;
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/SyntheticModule.h

@@ -26,6 +26,7 @@ public:
     virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) override;
     virtual ThrowCompletionOr<Vector<DeprecatedFlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set) override;
     virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, DeprecatedFlyString const& export_name, Vector<ResolvedBinding> resolve_set) override;
+    virtual PromiseCapability& load_requested_modules(GCPtr<GraphLoadingState::HostDefined>) override;
 
 private:
     SyntheticModule(Vector<DeprecatedFlyString> export_names, EvaluationFunction evaluation_steps, Realm& realm, StringView filename);