Skip to content

Commit 682026b

Browse files
authored
Module api bugs (#6992)
* Check for errored module before getting Namespace * Check for missing fetchImportedModuleFromScriptCallback
1 parent 615a614 commit 682026b

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

lib/Jsrt/Core/JsrtContextCore.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#include "Runtime.h"
@@ -105,34 +106,35 @@ void JsrtContextCore::OnScriptLoad(Js::JavascriptFunction * scriptFunction, Js::
105106

106107
HRESULT ChakraCoreHostScriptContext::FetchImportedModule(Js::ModuleRecordBase* referencingModule, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
107108
{
108-
return FetchImportedModuleHelper(
109-
[=](Js::JavascriptString *specifierVar, JsModuleRecord *dependentRecord) -> JsErrorCode
109+
if (fetchImportedModuleCallback == nullptr)
110+
{
111+
return E_INVALIDARG;
112+
}
113+
Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
114+
JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
115+
{
116+
AUTO_NO_EXCEPTION_REGION;
117+
JsErrorCode errorCode = fetchImportedModuleCallback(referencingModule, specifierVar, &dependentRecord);
118+
if (errorCode == JsNoError)
110119
{
111-
return fetchImportedModuleCallback(referencingModule, specifierVar, dependentRecord);
112-
}, specifier, dependentModuleRecord);
120+
*dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);
121+
return NOERROR;
122+
}
123+
}
124+
return E_INVALIDARG;
113125
}
114126

115127
HRESULT ChakraCoreHostScriptContext::FetchImportedModuleFromScript(JsSourceContext dwReferencingSourceContext, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
116128
{
117-
return FetchImportedModuleHelper(
118-
[=](Js::JavascriptString *specifierVar, JsModuleRecord *dependentRecord) -> JsErrorCode
119-
{
120-
return fetchImportedModuleFromScriptCallback(dwReferencingSourceContext, specifierVar, dependentRecord);
121-
}, specifier, dependentModuleRecord);
122-
}
123-
124-
template<typename Fn>
125-
HRESULT ChakraCoreHostScriptContext::FetchImportedModuleHelper(Fn fetch, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
126-
{
127-
if (fetchImportedModuleCallback == nullptr)
129+
if (fetchImportedModuleFromScriptCallback == nullptr)
128130
{
129131
return E_INVALIDARG;
130132
}
131133
Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
132134
JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
133135
{
134136
AUTO_NO_EXCEPTION_REGION;
135-
JsErrorCode errorCode = fetch(specifierVar, &dependentRecord);
137+
JsErrorCode errorCode = fetchImportedModuleFromScriptCallback(dwReferencingSourceContext, specifierVar, &dependentRecord);
136138
if (errorCode == JsNoError)
137139
{
138140
*dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);

lib/Jsrt/Core/JsrtContextCore.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
34
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45
//-------------------------------------------------------------------------------------------------------
56
#pragma once
@@ -266,8 +267,6 @@ class ChakraCoreHostScriptContext sealed : public HostScriptContext
266267
#endif
267268

268269
private:
269-
template<typename Fn>
270-
HRESULT FetchImportedModuleHelper(Fn fetch, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord);
271270
FetchImportedModuleCallBack fetchImportedModuleCallback;
272271
FetchImportedModuleFromScriptCallBack fetchImportedModuleFromScriptCallback;
273272
NotifyModuleReadyCallback notifyModuleReadyCallback;

lib/Jsrt/Core/JsrtCore.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3-
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
3+
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
44
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
55
//-------------------------------------------------------------------------------------------------------
66
#include "JsrtPch.h"
@@ -268,6 +268,10 @@ CHAKRA_API JsGetModuleNamespace(_In_ JsModuleRecord requestModule, _Outptr_resul
268268
{
269269
return JsErrorModuleNotEvaluated;
270270
}
271+
if (moduleRecord->GetErrorObject() != nullptr)
272+
{
273+
return JsErrorInvalidArgument;
274+
}
271275
*moduleNamespace = static_cast<JsValueRef>(moduleRecord->GetNamespace());
272276
return JsNoError;
273277
}

0 commit comments

Comments
 (0)