ModuleLoader.h (4477B)
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 bool LoadImportedModule(JSContext* cx, JS::Handle<JSScript*> referrer, 30 HandleObject moduleRequest, 31 HandleValue hostDefined, HandleValue payload, 32 uint32_t lineNumber, 33 JS::ColumnNumberOneOrigin columnNumber); 34 35 static bool GetImportMetaProperties(JSContext* cx, HandleValue privateValue, 36 HandleObject metaObject); 37 static bool ImportMetaResolve(JSContext* cx, unsigned argc, Value* vp); 38 39 static bool DynamicImportDelayFulfilled(JSContext* cx, unsigned argc, 40 Value* vp); 41 static bool DynamicImportDelayRejected(JSContext* cx, unsigned argc, 42 Value* vp); 43 44 bool loadAndExecute(JSContext* cx, HandleString path, 45 HandleObject moduleRequestArg, MutableHandleValue rval); 46 bool loadAndExecute(JSContext* cx, HandleObject module, 47 MutableHandleValue rval); 48 static bool LoadResolved(JSContext* cx, HandleValue hostDefined); 49 static bool LoadRejected(JSContext* cx, HandleValue hostDefined, 50 HandleValue error); 51 bool loadImportedModule(JSContext* cx, HandleScript referrer, 52 HandleObject moduleRequest, HandleValue payload); 53 bool populateImportMeta(JSContext* cx, HandleValue privateValue, 54 HandleObject metaObject); 55 bool importMetaResolve(JSContext* cx, 56 JS::Handle<JS::Value> referencingPrivate, 57 JS::Handle<JSString*> specifier, 58 JS::MutableHandle<JSString*> urlOut); 59 bool dynamicImport(JSContext* cx, HandleScript referrer, 60 HandleObject moduleRequest, HandleValue payload); 61 bool doDynamicImport(JSContext* cx, HandleScript referrer, 62 HandleObject moduleRequest, HandleValue payload); 63 JSObject* loadAndParse(JSContext* cx, HandleString path, 64 HandleObject moduleRequestArg); 65 bool lookupModuleInRegistry(JSContext* cx, JS::ModuleType moduleType, 66 HandleString path, MutableHandleObject moduleOut); 67 bool addModuleToRegistry(JSContext* cx, JS::ModuleType moduleType, 68 HandleString path, HandleObject module); 69 JSLinearString* resolve(JSContext* cx, HandleObject moduleRequestArg, 70 HandleScript referrer); 71 JSLinearString* resolve(JSContext* cx, HandleString specifier, 72 HandleValue referencingInfo); 73 bool getScriptPath(JSContext* cx, HandleValue privateValue, 74 MutableHandle<JSLinearString*> pathOut); 75 JSLinearString* normalizePath(JSContext* cx, Handle<JSLinearString*> path); 76 JSObject* getOrCreateModuleRegistry(JSContext* cx, JS::ModuleType moduleType); 77 JSString* fetchSource(JSContext* cx, Handle<JSLinearString*> path); 78 79 // The following are used for pinned atoms which do not need rooting. 80 JSAtom* loadPathStr = nullptr; 81 JSAtom* pathSeparatorStr = nullptr; 82 83 // The slot stored in ImportMetaResolve function. 84 enum { ModulePrivateSlot = 0, SlotCount }; 85 86 // The number of args in ImportMetaResolve. 87 static const uint32_t ImportMetaResolveNumArgs = 1; 88 // The index of the 'specifier' argument in ImportMetaResolve. 89 static const uint32_t ImportMetaResolveSpecifierArg = 0; 90 } JS_HAZ_NON_GC_POINTER; 91 92 } // namespace shell 93 } // namespace js 94 95 #endif // shell_ModuleLoader_h