Skip to content

Commit

Permalink
Module api bugs (#6992)
Browse files Browse the repository at this point in the history
* Check for errored module before getting Namespace

* Check for missing fetchImportedModuleFromScriptCallback
  • Loading branch information
rhuanjl committed May 16, 2024
1 parent 615a614 commit 682026b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
34 changes: 18 additions & 16 deletions lib/Jsrt/Core/JsrtContextCore.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Runtime.h"
Expand Down Expand Up @@ -105,34 +106,35 @@ void JsrtContextCore::OnScriptLoad(Js::JavascriptFunction * scriptFunction, Js::

HRESULT ChakraCoreHostScriptContext::FetchImportedModule(Js::ModuleRecordBase* referencingModule, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
{
return FetchImportedModuleHelper(
[=](Js::JavascriptString *specifierVar, JsModuleRecord *dependentRecord) -> JsErrorCode
if (fetchImportedModuleCallback == nullptr)
{
return E_INVALIDARG;
}
Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
{
AUTO_NO_EXCEPTION_REGION;
JsErrorCode errorCode = fetchImportedModuleCallback(referencingModule, specifierVar, &dependentRecord);
if (errorCode == JsNoError)
{
return fetchImportedModuleCallback(referencingModule, specifierVar, dependentRecord);
}, specifier, dependentModuleRecord);
*dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);
return NOERROR;
}
}
return E_INVALIDARG;
}

HRESULT ChakraCoreHostScriptContext::FetchImportedModuleFromScript(JsSourceContext dwReferencingSourceContext, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
{
return FetchImportedModuleHelper(
[=](Js::JavascriptString *specifierVar, JsModuleRecord *dependentRecord) -> JsErrorCode
{
return fetchImportedModuleFromScriptCallback(dwReferencingSourceContext, specifierVar, dependentRecord);
}, specifier, dependentModuleRecord);
}

template<typename Fn>
HRESULT ChakraCoreHostScriptContext::FetchImportedModuleHelper(Fn fetch, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord)
{
if (fetchImportedModuleCallback == nullptr)
if (fetchImportedModuleFromScriptCallback == nullptr)
{
return E_INVALIDARG;
}
Js::JavascriptString* specifierVar = Js::JavascriptString::NewCopySz(specifier, GetScriptContext());
JsModuleRecord dependentRecord = JS_INVALID_REFERENCE;
{
AUTO_NO_EXCEPTION_REGION;
JsErrorCode errorCode = fetch(specifierVar, &dependentRecord);
JsErrorCode errorCode = fetchImportedModuleFromScriptCallback(dwReferencingSourceContext, specifierVar, &dependentRecord);
if (errorCode == JsNoError)
{
*dependentModuleRecord = static_cast<Js::ModuleRecordBase*>(dependentRecord);
Expand Down
3 changes: 1 addition & 2 deletions lib/Jsrt/Core/JsrtContextCore.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
Expand Down Expand Up @@ -266,8 +267,6 @@ class ChakraCoreHostScriptContext sealed : public HostScriptContext
#endif

private:
template<typename Fn>
HRESULT FetchImportedModuleHelper(Fn fetch, LPCOLESTR specifier, Js::ModuleRecordBase** dependentModuleRecord);
FetchImportedModuleCallBack fetchImportedModuleCallback;
FetchImportedModuleFromScriptCallBack fetchImportedModuleFromScriptCallback;
NotifyModuleReadyCallback notifyModuleReadyCallback;
Expand Down
6 changes: 5 additions & 1 deletion lib/Jsrt/Core/JsrtCore.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "JsrtPch.h"
Expand Down Expand Up @@ -268,6 +268,10 @@ CHAKRA_API JsGetModuleNamespace(_In_ JsModuleRecord requestModule, _Outptr_resul
{
return JsErrorModuleNotEvaluated;
}
if (moduleRecord->GetErrorObject() != nullptr)
{
return JsErrorInvalidArgument;
}
*moduleNamespace = static_cast<JsValueRef>(moduleRecord->GetNamespace());
return JsNoError;
}
Expand Down

0 comments on commit 682026b

Please sign in to comment.