Skip to content

Commit 72302b4

Browse files
committed
[MERGE #5881 @rhuanjl] Non-breaking revision to JsSetModuleHostInfo per #5875
Merge pull request #5881 from rhuanjl:reviseJsSetModuleHostInfo This change allows a nullptr to be supplied as requestModule in JsSetModuleHostInfo in order to use the currentContext when setting callbacks. This means that an embedder can setup module loading callbacks including the dynamic import from script callback without needing a throw away module record. Note I have updated ch to use this new mechanism whilst leaving the native tests that use the old mechanism untouched so that both options are tested, one by ch and the other by the native tests. CC: @liminzhu closes #5875
2 parents 303f24f + fc3fedd commit 72302b4

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

bin/ch/WScriptJsrt.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ JsErrorCode WScriptJsrt::InitializeModuleInfo(JsValueRef specifier, JsModuleReco
419419
{
420420
errorCode = ChakraRTInterface::JsSetModuleHostInfo(moduleRecord, JsModuleHostInfo_NotifyModuleReadyCallback, (void*)WScriptJsrt::NotifyModuleReadyCallback);
421421

422-
if (errorCode == JsNoError)
422+
if (errorCode == JsNoError && moduleRecord != nullptr)
423423
{
424424
errorCode = ChakraRTInterface::JsSetModuleHostInfo(moduleRecord, JsModuleHostInfo_HostDefined, specifier);
425425
}
@@ -1130,14 +1130,7 @@ bool WScriptJsrt::Initialize()
11301130

11311131
JsErrorCode WScriptJsrt::InitializeModuleCallbacks()
11321132
{
1133-
JsModuleRecord moduleRecord = JS_INVALID_REFERENCE;
1134-
JsErrorCode errorCode = ChakraRTInterface::JsInitializeModuleRecord(nullptr, nullptr, &moduleRecord);
1135-
if (errorCode == JsNoError)
1136-
{
1137-
errorCode = InitializeModuleInfo(nullptr, moduleRecord);
1138-
}
1139-
1140-
return errorCode;
1133+
return InitializeModuleInfo(nullptr, nullptr);
11411134
}
11421135

11431136
bool WScriptJsrt::Uninitialize()

lib/Jsrt/ChakraCore.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,26 @@ JsModuleEvaluation(
310310
/// <remarks>
311311
/// This is used for four things:
312312
/// 1. Setting up the callbacks for module loading - note these are actually
313-
/// set on the current Context not the module so only have to be set for
314-
/// the first root module in any given context.
313+
/// set on the module's Context not the module itself so only have to be set
314+
/// for the first root module in any given context.
315+
/// Alternatively you can set these on the currentContext by supplying a nullptr
316+
/// as the requestModule
315317
/// 2. Setting host defined info on a module record - can be anything that
316318
/// you wish to associate with your modules.
317319
/// 3. Setting a URL for a module to be used for stack traces/debugging -
318320
/// note this must be set before calling JsParseModuleSource on the module
319321
/// or it will be ignored.
320322
/// 4. Setting an exception on the module object - only relevant prior to it being Parsed.
321323
/// </remarks>
322-
/// <param name="requestModule">The request module.</param>
324+
/// <param name="requestModule">The request module, optional for setting callbacks, required for other uses.</param>
323325
/// <param name="moduleHostInfo">The type of host info to be set.</param>
324326
/// <param name="hostInfo">The host info to be set.</param>
325327
/// <returns>
326328
/// The code <c>JsNoError</c> if the operation succeeded, a failure code otherwise.
327329
/// </returns>
328330
CHAKRA_API
329331
JsSetModuleHostInfo(
330-
_In_ JsModuleRecord requestModule,
332+
_In_opt_ JsModuleRecord requestModule,
331333
_In_ JsModuleHostInfoKind moduleHostInfo,
332334
_In_ void* hostInfo);
333335

lib/Jsrt/Core/JsrtCore.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,27 @@ JsModuleEvaluation(
138138

139139
CHAKRA_API
140140
JsSetModuleHostInfo(
141-
_In_ JsModuleRecord requestModule,
141+
_In_opt_ JsModuleRecord requestModule,
142142
_In_ JsModuleHostInfoKind moduleHostInfo,
143143
_In_ void* hostInfo)
144144
{
145+
Js::ScriptContext* scriptContext;
146+
Js::SourceTextModuleRecord* moduleRecord;
145147
if (!Js::SourceTextModuleRecord::Is(requestModule))
146148
{
147-
return JsErrorInvalidArgument;
149+
if (moduleHostInfo != JsModuleHostInfo_FetchImportedModuleCallback &&
150+
moduleHostInfo != JsModuleHostInfo_FetchImportedModuleFromScriptCallback &&
151+
moduleHostInfo != JsModuleHostInfo_NotifyModuleReadyCallback)
152+
{
153+
return JsErrorInvalidArgument;
154+
}
155+
scriptContext = JsrtContext::GetCurrent()->GetScriptContext();
156+
}
157+
else
158+
{
159+
moduleRecord = Js::SourceTextModuleRecord::FromHost(requestModule);
160+
scriptContext = moduleRecord->GetScriptContext();
148161
}
149-
Js::SourceTextModuleRecord* moduleRecord = Js::SourceTextModuleRecord::FromHost(requestModule);
150-
Js::ScriptContext* scriptContext = moduleRecord->GetScriptContext();
151162
JsrtContext* jsrtContext = (JsrtContext*)scriptContext->GetLibrary()->GetJsrtContext();
152163
JsErrorCode errorCode = SetContextAPIWrapper(jsrtContext, [&](Js::ScriptContext *scriptContext) -> JsErrorCode {
153164
JsrtContextCore* currentContext = static_cast<JsrtContextCore*>(JsrtContextCore::GetCurrent());

0 commit comments

Comments
 (0)