| 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 |
/* JavaScript module (as in, the syntactic construct) operations. */ |
| 8 |
|
| 9 |
#ifndef js_Modules_h |
| 10 |
#define js_Modules_h |
| 11 |
|
| 12 |
#include <stdint.h> // uint32_t |
| 13 |
|
| 14 |
#include "jstypes.h" // JS_PUBLIC_API |
| 15 |
|
| 16 |
#include "js/AllocPolicy.h" // js::SystemAllocPolicy |
| 17 |
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin |
| 18 |
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions |
| 19 |
#include "js/RootingAPI.h" // JS::{Mutable,}Handle |
| 20 |
#include "js/Value.h" // JS::Value |
| 21 |
#include "js/Vector.h" // js::Vector |
| 22 |
|
| 23 |
struct JS_PUBLIC_API JSContext; |
| 24 |
class JS_PUBLIC_API JSObject; |
| 25 |
struct JS_PUBLIC_API JSRuntime; |
| 26 |
class JS_PUBLIC_API JSString; |
| 27 |
|
| 28 |
namespace JS { |
| 29 |
template <typename UnitT> |
| 30 |
class SourceText; |
| 31 |
} // namespace JS |
| 32 |
|
| 33 |
namespace mozilla { |
| 34 |
union Utf8Unit; |
| 35 |
} |
| 36 |
|
| 37 |
namespace JS { |
| 38 |
|
| 39 |
// This enum is used to index into an array, and we assume that we have |
| 40 |
// sequential numbers starting at zero for the unknown type. |
| 41 |
enum class ModuleType : uint32_t { |
| 42 |
Unknown = 0, |
| 43 |
JavaScript, |
| 44 |
JSON, |
| 45 |
|
| 46 |
Limit = JSON, |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* The HostResolveImportedModule hook. |
| 51 |
* |
| 52 |
* See: https://tc39.es/ecma262/#sec-hostresolveimportedmodule |
| 53 |
* |
| 54 |
* This embedding-defined hook is used to implement module loading. It is called |
| 55 |
* to get or create a module object corresponding to |moduleRequest| occurring |
| 56 |
* in the context of the script or module with private value |
| 57 |
* |referencingPrivate|. |
| 58 |
* |
| 59 |
* The module specifier string for the request can be obtained by calling |
| 60 |
* JS::GetModuleRequestSpecifier. |
| 61 |
* |
| 62 |
* The private value for a script or module is set with JS::SetScriptPrivate or |
| 63 |
* JS::SetModulePrivate. It's assumed that the embedding can handle receiving |
| 64 |
* either here. |
| 65 |
* |
| 66 |
* This hook must obey the restrictions defined in the spec: |
| 67 |
* - Each time the hook is called with the same arguemnts, the same module must |
| 68 |
* be returned. |
| 69 |
* - If a module cannot be created for the given arguments, an exception must |
| 70 |
* be thrown. |
| 71 |
* |
| 72 |
* This is a synchronous operation. |
| 73 |
*/ |
| 74 |
using ModuleResolveHook = JSObject* (*)(JSContext* cx, |
| 75 |
Handle<Value> referencingPrivate, |
| 76 |
Handle<JSObject*> moduleRequest); |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the HostResolveImportedModule hook for the runtime. |
| 80 |
*/ |
| 81 |
extern JS_PUBLIC_API ModuleResolveHook GetModuleResolveHook(JSRuntime* rt); |
| 82 |
|
| 83 |
/** |
| 84 |
* Set the HostResolveImportedModule hook for the runtime to the given function. |
| 85 |
*/ |
| 86 |
extern JS_PUBLIC_API void SetModuleResolveHook(JSRuntime* rt, |
| 87 |
ModuleResolveHook func); |
| 88 |
|
| 89 |
/** |
| 90 |
* The module metadata hook. |
| 91 |
* |
| 92 |
* See: https://tc39.es/ecma262/#sec-hostgetimportmetaproperties |
| 93 |
* |
| 94 |
* Populate the |metaObject| object returned when import.meta is evaluated in |
| 95 |
* the context of the script or module with private value |privateValue|. |
| 96 |
* |
| 97 |
* This is based on the spec's HostGetImportMetaProperties hook but defines |
| 98 |
* properties on the meta object directly rather than returning a list. |
| 99 |
*/ |
| 100 |
using ModuleMetadataHook = bool (*)(JSContext* cx, Handle<Value> privateValue, |
| 101 |
Handle<JSObject*> metaObject); |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the hook for populating the import.meta metadata object. |
| 105 |
*/ |
| 106 |
extern JS_PUBLIC_API ModuleMetadataHook GetModuleMetadataHook(JSRuntime* rt); |
| 107 |
|
| 108 |
/** |
| 109 |
* Set the hook for populating the import.meta metadata object to the given |
| 110 |
* function. |
| 111 |
*/ |
| 112 |
extern JS_PUBLIC_API void SetModuleMetadataHook(JSRuntime* rt, |
| 113 |
ModuleMetadataHook func); |
| 114 |
|
| 115 |
/** |
| 116 |
* The HostImportModuleDynamically hook. |
| 117 |
* |
| 118 |
* See https://tc39.es/ecma262/#sec-hostimportmoduledynamically |
| 119 |
* |
| 120 |
* Used to implement dynamic module import. Called when evaluating import() |
| 121 |
* expressions. |
| 122 |
* |
| 123 |
* This starts an asynchronous operation. Some time after this hook is called |
| 124 |
* the embedding must call JS::FinishDynamicModuleImport() passing the |
| 125 |
* |referencingPrivate|, |moduleRequest| and |promise| arguments from the |
| 126 |
* call. This must happen for both success and failure cases. |
| 127 |
* |
| 128 |
* In the meantime the embedding can take whatever steps it needs to make the |
| 129 |
* module available. If successful, after calling FinishDynamicModuleImport() |
| 130 |
* the module should be returned by the resolve hook when passed |
| 131 |
* |referencingPrivate| and |moduleRequest|. |
| 132 |
*/ |
| 133 |
using ModuleDynamicImportHook = bool (*)(JSContext* cx, |
| 134 |
Handle<Value> referencingPrivate, |
| 135 |
Handle<JSObject*> moduleRequest, |
| 136 |
Handle<JSObject*> promise); |
| 137 |
|
| 138 |
/** |
| 139 |
* Get the HostImportModuleDynamically hook for the runtime. |
| 140 |
*/ |
| 141 |
extern JS_PUBLIC_API ModuleDynamicImportHook |
| 142 |
GetModuleDynamicImportHook(JSRuntime* rt); |
| 143 |
|
| 144 |
/** |
| 145 |
* Set the HostImportModuleDynamically hook for the runtime to the given |
| 146 |
* function. |
| 147 |
* |
| 148 |
* If this hook is not set (or set to nullptr) then the JS engine will throw an |
| 149 |
* exception if dynamic module import is attempted. |
| 150 |
*/ |
| 151 |
extern JS_PUBLIC_API void SetModuleDynamicImportHook( |
| 152 |
JSRuntime* rt, ModuleDynamicImportHook func); |
| 153 |
|
| 154 |
/** |
| 155 |
* This must be called after a dynamic import operation is complete. |
| 156 |
* |
| 157 |
* If |evaluationPromise| is rejected, the rejection reason will be used to |
| 158 |
* complete the user's promise. |
| 159 |
*/ |
| 160 |
extern JS_PUBLIC_API bool FinishDynamicModuleImport( |
| 161 |
JSContext* cx, Handle<JSObject*> evaluationPromise, |
| 162 |
Handle<Value> referencingPrivate, Handle<JSObject*> moduleRequest, |
| 163 |
Handle<JSObject*> promise); |
| 164 |
|
| 165 |
/** |
| 166 |
* Parse the given source buffer as a module in the scope of the current global |
| 167 |
* of cx and return a source text module record. |
| 168 |
*/ |
| 169 |
extern JS_PUBLIC_API JSObject* CompileModule( |
| 170 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 171 |
SourceText<char16_t>& srcBuf); |
| 172 |
|
| 173 |
/** |
| 174 |
* Parse the given source buffer as a module in the scope of the current global |
| 175 |
* of cx and return a source text module record. An error is reported if a |
| 176 |
* UTF-8 encoding error is encountered. |
| 177 |
*/ |
| 178 |
extern JS_PUBLIC_API JSObject* CompileModule( |
| 179 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 180 |
SourceText<mozilla::Utf8Unit>& srcBuf); |
| 181 |
|
| 182 |
/** |
| 183 |
* Parse the given source buffer as a JSON module in the scope of the current |
| 184 |
* global of cx and return a synthetic module record. |
| 185 |
*/ |
| 186 |
extern JS_PUBLIC_API JSObject* CompileJsonModule( |
| 187 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 188 |
SourceText<char16_t>& srcBuf); |
| 189 |
|
| 190 |
/** |
| 191 |
* Parse the given source buffer as a JSON module in the scope of the current |
| 192 |
* global of cx and return a synthetic module record. An error is reported if a |
| 193 |
* UTF-8 encoding error is encountered. |
| 194 |
*/ |
| 195 |
extern JS_PUBLIC_API JSObject* CompileJsonModule( |
| 196 |
JSContext* cx, const ReadOnlyCompileOptions& options, |
| 197 |
SourceText<mozilla::Utf8Unit>& srcBuf); |
| 198 |
|
| 199 |
/** |
| 200 |
* Set a private value associated with a source text module record. |
| 201 |
*/ |
| 202 |
extern JS_PUBLIC_API void SetModulePrivate(JSObject* module, |
| 203 |
const Value& value); |
| 204 |
/** |
| 205 |
* Clear the private value associated with a source text module record. |
| 206 |
* |
| 207 |
* This is used during unlinking and can be called on a gray module, skipping |
| 208 |
* the usual checks. |
| 209 |
*/ |
| 210 |
extern JS_PUBLIC_API void ClearModulePrivate(JSObject* module); |
| 211 |
|
| 212 |
/** |
| 213 |
* Get the private value associated with a source text module record. |
| 214 |
*/ |
| 215 |
extern JS_PUBLIC_API Value GetModulePrivate(JSObject* module); |
| 216 |
|
| 217 |
/** |
| 218 |
* Checks if the given module is a cyclic module. |
| 219 |
*/ |
| 220 |
extern JS_PUBLIC_API bool IsCyclicModule(JSObject* module); |
| 221 |
|
| 222 |
/* |
| 223 |
* Perform the ModuleLink operation on the given source text module record. |
| 224 |
* |
| 225 |
* This transitively resolves all module dependencies (calling the |
| 226 |
* HostResolveImportedModule hook) and initializes the environment record for |
| 227 |
* the module. |
| 228 |
*/ |
| 229 |
extern JS_PUBLIC_API bool ModuleLink(JSContext* cx, |
| 230 |
Handle<JSObject*> moduleRecord); |
| 231 |
|
| 232 |
/* |
| 233 |
* Perform the ModuleEvaluate operation on the given source text module record |
| 234 |
* and returns a bool. A result value is returned in result and is either |
| 235 |
* undefined (and ignored) or a promise (if Top Level Await is enabled). |
| 236 |
* |
| 237 |
* If this module has already been evaluated, it returns the evaluation |
| 238 |
* promise. Otherwise, it transitively evaluates all dependences of this module |
| 239 |
* and then evaluates this module. |
| 240 |
* |
| 241 |
* ModuleLink must have completed prior to calling this. |
| 242 |
*/ |
| 243 |
extern JS_PUBLIC_API bool ModuleEvaluate(JSContext* cx, |
| 244 |
Handle<JSObject*> moduleRecord, |
| 245 |
MutableHandleValue rval); |
| 246 |
|
| 247 |
enum ModuleErrorBehaviour { |
| 248 |
// Report module evaluation errors asynchronously when the evaluation promise |
| 249 |
// is rejected. This is used for web content. |
| 250 |
ReportModuleErrorsAsync, |
| 251 |
|
| 252 |
// Throw module evaluation errors synchronously by setting an exception on the |
| 253 |
// context. Does not support modules that use top-level await. |
| 254 |
ThrowModuleErrorsSync |
| 255 |
}; |
| 256 |
|
| 257 |
/* |
| 258 |
* If a module evaluation fails, unwrap the resulting evaluation promise |
| 259 |
* and rethrow. |
| 260 |
* |
| 261 |
* This does nothing if this module succeeds in evaluation. Otherwise, it |
| 262 |
* takes the reason for the module throwing, unwraps it and throws it as a |
| 263 |
* regular error rather than as an uncaught promise. |
| 264 |
* |
| 265 |
* ModuleEvaluate must have completed prior to calling this. |
| 266 |
*/ |
| 267 |
extern JS_PUBLIC_API bool ThrowOnModuleEvaluationFailure( |
| 268 |
JSContext* cx, Handle<JSObject*> evaluationPromise, |
| 269 |
ModuleErrorBehaviour errorBehaviour = ReportModuleErrorsAsync); |
| 270 |
|
| 271 |
/* |
| 272 |
* Functions to access the module specifiers of a source text module record used |
| 273 |
* to request module imports. |
| 274 |
* |
| 275 |
* Clients can use GetRequestedModulesCount() to get the number of specifiers |
| 276 |
* and GetRequestedModuleSpecifier() / GetRequestedModuleSourcePos() to get the |
| 277 |
* individual elements. |
| 278 |
*/ |
| 279 |
extern JS_PUBLIC_API uint32_t |
| 280 |
GetRequestedModulesCount(JSContext* cx, Handle<JSObject*> moduleRecord); |
| 281 |
|
| 282 |
extern JS_PUBLIC_API JSString* GetRequestedModuleSpecifier( |
| 283 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index); |
| 284 |
|
| 285 |
/* |
| 286 |
* Get the position of a requested module's name in the source. |
| 287 |
*/ |
| 288 |
extern JS_PUBLIC_API void GetRequestedModuleSourcePos( |
| 289 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index, |
| 290 |
uint32_t* lineNumber, JS::ColumnNumberOneOrigin* columnNumber); |
| 291 |
|
| 292 |
/* |
| 293 |
* Get the module type of a requested module. |
| 294 |
*/ |
| 295 |
extern JS_PUBLIC_API ModuleType GetRequestedModuleType( |
| 296 |
JSContext* cx, Handle<JSObject*> moduleRecord, uint32_t index); |
| 297 |
|
| 298 |
/* |
| 299 |
* Get the top-level script for a module which has not yet been executed. |
| 300 |
*/ |
| 301 |
extern JS_PUBLIC_API JSScript* GetModuleScript(Handle<JSObject*> moduleRecord); |
| 302 |
|
| 303 |
extern JS_PUBLIC_API JSObject* CreateModuleRequest( |
| 304 |
JSContext* cx, Handle<JSString*> specifierArg, ModuleType moduleType); |
| 305 |
extern JS_PUBLIC_API JSString* GetModuleRequestSpecifier( |
| 306 |
JSContext* cx, Handle<JSObject*> moduleRequestArg); |
| 307 |
|
| 308 |
/* |
| 309 |
* Get the module type of the specified module request. |
| 310 |
*/ |
| 311 |
extern JS_PUBLIC_API ModuleType |
| 312 |
GetModuleRequestType(JSContext* cx, Handle<JSObject*> moduleRequestArg); |
| 313 |
|
| 314 |
/* |
| 315 |
* Get the module record for a module script. |
| 316 |
*/ |
| 317 |
extern JS_PUBLIC_API JSObject* GetModuleObject(Handle<JSScript*> moduleScript); |
| 318 |
|
| 319 |
/* |
| 320 |
* Get the namespace object for a module. |
| 321 |
*/ |
| 322 |
extern JS_PUBLIC_API JSObject* GetModuleNamespace( |
| 323 |
JSContext* cx, Handle<JSObject*> moduleRecord); |
| 324 |
|
| 325 |
extern JS_PUBLIC_API JSObject* GetModuleForNamespace( |
| 326 |
JSContext* cx, Handle<JSObject*> moduleNamespace); |
| 327 |
|
| 328 |
extern JS_PUBLIC_API JSObject* GetModuleEnvironment( |
| 329 |
JSContext* cx, Handle<JSObject*> moduleObj); |
| 330 |
|
| 331 |
/* |
| 332 |
* Clear all bindings in a module's environment. Used during shutdown. |
| 333 |
*/ |
| 334 |
extern JS_PUBLIC_API void ClearModuleEnvironment(JSObject* moduleObj); |
| 335 |
|
| 336 |
extern JS_PUBLIC_API bool ModuleIsLinked(JSObject* moduleObj); |
| 337 |
|
| 338 |
} // namespace JS |
| 339 |
|
| 340 |
#endif // js_Modules_h |