| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef shell_ModuleLoader_h |
| 8 |
#define shell_ModuleLoader_h |
| 9 |
|
| 10 |
#include "builtin/ModuleObject.h" |
| 11 |
#include "js/RootingAPI.h" |
| 12 |
|
| 13 |
namespace js { |
| 14 |
namespace shell { |
| 15 |
|
| 16 |
class ModuleLoader { |
| 17 |
public: |
| 18 |
bool init(JSContext* cx, HandleString loadPath); |
| 19 |
bool loadRootModule(JSContext* cx, HandleString path); |
| 20 |
|
| 21 |
// Testing hook to register a module that wasn't loaded by the module loader. |
| 22 |
bool registerTestModule(JSContext* cx, HandleObject moduleRequest, |
| 23 |
Handle<ModuleObject*> module); |
| 24 |
|
| 25 |
// Testing hook to clear all loaded modules. |
| 26 |
void clearModules(JSContext* cx); |
| 27 |
|
| 28 |
private: |
| 29 |
static JSObject* ResolveImportedModule(JSContext* cx, |
| 30 |
HandleValue referencingPrivate, |
| 31 |
HandleObject moduleRequest); |
| 32 |
static bool GetImportMetaProperties(JSContext* cx, HandleValue privateValue, |
| 33 |
HandleObject metaObject); |
| 34 |
static bool ImportMetaResolve(JSContext* cx, unsigned argc, Value* vp); |
| 35 |
static bool ImportModuleDynamically(JSContext* cx, |
| 36 |
HandleValue referencingPrivate, |
| 37 |
HandleObject moduleRequest, |
| 38 |
HandleObject promise); |
| 39 |
|
| 40 |
static bool DynamicImportDelayFulfilled(JSContext* cx, unsigned argc, |
| 41 |
Value* vp); |
| 42 |
static bool DynamicImportDelayRejected(JSContext* cx, unsigned argc, |
| 43 |
Value* vp); |
| 44 |
|
| 45 |
bool loadAndExecute(JSContext* cx, HandleString path, |
| 46 |
HandleObject moduleRequestArg, MutableHandleValue); |
| 47 |
JSObject* resolveImportedModule(JSContext* cx, HandleValue referencingPrivate, |
| 48 |
HandleObject moduleRequest); |
| 49 |
bool populateImportMeta(JSContext* cx, HandleValue privateValue, |
| 50 |
HandleObject metaObject); |
| 51 |
bool importMetaResolve(JSContext* cx, |
| 52 |
JS::Handle<JS::Value> referencingPrivate, |
| 53 |
JS::Handle<JSString*> specifier, |
| 54 |
JS::MutableHandle<JSString*> urlOut); |
| 55 |
bool dynamicImport(JSContext* cx, HandleValue referencingPrivate, |
| 56 |
HandleObject moduleRequest, HandleObject promise); |
| 57 |
bool doDynamicImport(JSContext* cx, HandleValue referencingPrivate, |
| 58 |
HandleObject moduleRequest, HandleObject promise); |
| 59 |
bool tryDynamicImport(JSContext* cx, HandleValue referencingPrivate, |
| 60 |
HandleObject moduleRequest, HandleObject promise, |
| 61 |
MutableHandleValue rval); |
| 62 |
JSObject* loadAndParse(JSContext* cx, HandleString path, |
| 63 |
HandleObject moduleRequestArg); |
| 64 |
bool lookupModuleInRegistry(JSContext* cx, JS::ModuleType moduleType, |
| 65 |
HandleString path, MutableHandleObject moduleOut); |
| 66 |
bool addModuleToRegistry(JSContext* cx, JS::ModuleType moduleType, |
| 67 |
HandleString path, HandleObject module); |
| 68 |
JSLinearString* resolve(JSContext* cx, HandleObject moduleRequestArg, |
| 69 |
HandleValue referencingInfo); |
| 70 |
JSLinearString* resolve(JSContext* cx, HandleString specifier, |
| 71 |
HandleValue referencingInfo); |
| 72 |
bool getScriptPath(JSContext* cx, HandleValue privateValue, |
| 73 |
MutableHandle<JSLinearString*> pathOut); |
| 74 |
JSLinearString* normalizePath(JSContext* cx, Handle<JSLinearString*> path); |
| 75 |
JSObject* getOrCreateModuleRegistry(JSContext* cx, JS::ModuleType moduleType); |
| 76 |
JSString* fetchSource(JSContext* cx, Handle<JSLinearString*> path); |
| 77 |
|
| 78 |
// The following are used for pinned atoms which do not need rooting. |
| 79 |
JSAtom* loadPathStr = nullptr; |
| 80 |
JSAtom* pathSeparatorStr = nullptr; |
| 81 |
|
| 82 |
// The slot stored in ImportMetaResolve function. |
| 83 |
enum { ModulePrivateSlot = 0, SlotCount }; |
| 84 |
|
| 85 |
// The number of args in ImportMetaResolve. |
| 86 |
static const uint32_t ImportMetaResolveNumArgs = 1; |
| 87 |
// The index of the 'specifier' argument in ImportMetaResolve. |
| 88 |
static const uint32_t ImportMetaResolveSpecifierArg = 0; |
| 89 |
} JS_HAZ_NON_GC_POINTER; |
| 90 |
|
| 91 |
} // namespace shell |
| 92 |
} // namespace js |
| 93 |
|
| 94 |
#endif // shell_ModuleLoader_h |