diff --git a/unity/native_src/Src/BackendEnv.cpp b/unity/native_src/Src/BackendEnv.cpp index ce361d36ca..18985d19b3 100644 --- a/unity/native_src/Src/BackendEnv.cpp +++ b/unity/native_src/Src/BackendEnv.cpp @@ -7,6 +7,7 @@ #include "BackendEnv.h" #include "Log.h" #include "PromiseRejectCallback.hpp" +#include "V8Utils.h" #if WITH_NODEJS @@ -827,11 +828,13 @@ v8::MaybeLocal esmodule::_ResolveModule( std::string pathForDebug; maybeRet = CallRead(Isolate, Context, Specifier, pathForDebug); - if (maybeRet.IsEmpty()) + v8::Local ReadRet; + if (!maybeRet.ToLocal(&ReadRet) || !ReadRet->IsString()) { + FV8Utils::ThrowException(Isolate, "Load Module Context fail!"); return v8::MaybeLocal {}; } - v8::Local Code = v8::Local::Cast(maybeRet.ToLocalChecked()); + v8::Local Code = v8::Local::Cast(ReadRet); v8::ScriptOrigin Origin(pathForDebug.size() == 0 ? Specifier : diff --git a/unity/test/Src/Cases/API/EvalTest.cs b/unity/test/Src/Cases/API/EvalTest.cs index f59b17d843..99990c1eae 100644 --- a/unity/test/Src/Cases/API/EvalTest.cs +++ b/unity/test/Src/Cases/API/EvalTest.cs @@ -58,6 +58,19 @@ public void ESModuleCompileError() throw new Exception("unexpected to reach here"); } #endif + + /*[Test] + public void ESModuleCallExecuteModuleAndModuleThrow() + { + var loader = UnitTestEnv.GetLoader(); + loader.AddMockFileContent("eval-error/module1.mjs", @"throw new Error('aa');"); + //loader.AddMockFileContent("eval-error/main.mjs", @"CS.Puerts.UnitTest.UnitTestEnv.GetEnv().ExecuteModule('eval-error/module1.mjs');"); + //loader.AddMockFileContent("eval-error/main.mjs", @"globalThis.__puertsExecuteModule('eval-error/module1.mjs');"); + loader.AddMockFileContent("eval-error/main.mjs", @"require('eval-error/module1.mjs');"); + var jsEnv = UnitTestEnv.GetEnv(); + //jsEnv.ExecuteModule("eval-error/main.mjs"); + }*/ + [Test] public void ESModuleCompileErrorInNested() //https://github.com/Tencent/puerts/issues/1670 { @@ -203,6 +216,43 @@ public void ESModuleImportEvaluateError() // Assert.AreEqual(str, "hello world"); // } + + [Test] + public void ESModuleImportNullFile() //https://github.com/Tencent/puerts/issues/1670 + { + var loader = UnitTestEnv.GetLoader(); + loader.AddMockFileContent("compile-error/CModule.mjs", @"import BModule from ""./DModule.mjs"" + +class CModule +{ + +} + +console.log(`===CModule=====`); + +export default CModule;"); + loader.AddMockFileContent("compile-error/DModule.mjs", @"import NullTest from ""NullTest.mjs"" + +class DModule +{ + +} + +console.log(`===DModule=====`); + +export default DModule;"); + loader.AddNullFile("NullTest.mjs"); + var jsEnv = UnitTestEnv.GetEnv(); + try + { + jsEnv.ExecuteModule("compile-error/CModule.mjs"); + } + catch(Exception e) + { + return; + } + throw new Exception("unexpected to reach here"); + } [Test] public void ESModuleImportRelative() { diff --git a/unity/test/Src/UnitTestLoader.cs b/unity/test/Src/UnitTestLoader.cs index c00dd93383..bc51dd96b4 100644 --- a/unity/test/Src/UnitTestLoader.cs +++ b/unity/test/Src/UnitTestLoader.cs @@ -57,12 +57,18 @@ public string Resolve(string specifier, string referrer) [UnityEngine.Scripting.Preserve] public bool FileExists(string specifier) { + if (nullFiles.Contains(specifier)) return true; return !System.String.IsNullOrEmpty(Resolve(specifier, ".")); } [UnityEngine.Scripting.Preserve] public string ReadFile(string specifier, out string debugpath) { + if (nullFiles.Contains(specifier)) + { + debugpath = string.Empty; + return null; + } debugpath = ""; if (specifier != null) { if (specifier.StartsWith(UnityEngine.Application.streamingAssetsPath) || File.Exists(UnityEngine.Application.streamingAssetsPath + "/" + specifier)) { @@ -94,6 +100,13 @@ public void AddMockFileContent(string fileName, string content) { mockFileContent[fileName] = content; } + + private HashSet nullFiles = new HashSet(); + [UnityEngine.Scripting.Preserve] + public void AddNullFile(string fileName) + { + nullFiles.Add(fileName); + } } public class UnitTestLoader : ILoader { @@ -111,6 +124,7 @@ private string FixSpecifier(string specifier) [UnityEngine.Scripting.Preserve] public bool FileExists(string specifier) { + if (nullFiles.Contains(specifier)) return true; string path = UnityEngine.Application.streamingAssetsPath + "/" + specifier; if (System.IO.File.Exists(path)) { @@ -130,6 +144,11 @@ public bool FileExists(string specifier) [UnityEngine.Scripting.Preserve] public string ReadFile(string specifier, out string debugpath) { + if (nullFiles.Contains(specifier)) + { + debugpath = string.Empty; + return null; + } debugpath = ""; if (specifier != null) { if (specifier.StartsWith(UnityEngine.Application.streamingAssetsPath) || File.Exists(UnityEngine.Application.streamingAssetsPath + "/" + specifier)) { @@ -149,5 +168,12 @@ public void AddMockFileContent(string fileName, string content) { mockFileContent[fileName] = content; } + + private HashSet nullFiles = new HashSet(); + [UnityEngine.Scripting.Preserve] + public void AddNullFile(string fileName) + { + nullFiles.Add(fileName); + } } } \ No newline at end of file diff --git a/unity/test/dotnet/Src/TxtLoader.cs b/unity/test/dotnet/Src/TxtLoader.cs index d39019b956..e0b0ba3ad4 100644 --- a/unity/test/dotnet/Src/TxtLoader.cs +++ b/unity/test/dotnet/Src/TxtLoader.cs @@ -27,6 +27,11 @@ public bool IsESM(string filepath) public bool FileExists(string specifier) { + if (nullFiles.Contains(specifier)) + { + Console.WriteLine("FileExists return null for " + specifier); + return true; + } var res = !System.String.IsNullOrEmpty(Resolve(specifier, ".")); return res; } @@ -64,6 +69,10 @@ private string TryResolve(string specifier) public string Resolve(string specifier, string referrer) { + if (nullFiles.Contains(specifier)) + { + return specifier; + } if (PathHelper.IsRelative(specifier)) { specifier = PathHelper.normalize(PathHelper.Dirname(referrer) + "/" + specifier); @@ -78,6 +87,11 @@ public string Resolve(string specifier, string referrer) public string ReadFile(string filepath, out string debugpath) { + if (nullFiles.Contains(filepath)) + { + debugpath = string.Empty; + return null; + } debugpath = Path.Combine(root, filepath); if (File.Exists(Path.Combine(editorRoot, filepath))) { @@ -105,6 +119,12 @@ public void AddMockFileContent(string fileName, string content) { mockFileContent.Add(fileName, content); } + + private HashSet nullFiles = new HashSet(); + public void AddNullFile(string fileName) + { + nullFiles.Add(fileName); + } } namespace UnityEngine.Scripting