Skip to content

Commit

Permalink
[unity]判断文件存在返回成功,读取文件返回null导致崩溃,fix #1691
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Apr 23, 2024
1 parent 579ac39 commit a59f5e5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
7 changes: 5 additions & 2 deletions unity/native_src/Src/BackendEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "BackendEnv.h"
#include "Log.h"
#include "PromiseRejectCallback.hpp"
#include "V8Utils.h"

#if WITH_NODEJS

Expand Down Expand Up @@ -827,11 +828,13 @@ v8::MaybeLocal<v8::Module> esmodule::_ResolveModule(

std::string pathForDebug;
maybeRet = CallRead(Isolate, Context, Specifier, pathForDebug);
if (maybeRet.IsEmpty())
v8::Local<v8::Value> ReadRet;
if (!maybeRet.ToLocal(&ReadRet) || !ReadRet->IsString())
{
FV8Utils::ThrowException(Isolate, "Load Module Context fail!");
return v8::MaybeLocal<v8::Module> {};
}
v8::Local<v8::String> Code = v8::Local<v8::String>::Cast(maybeRet.ToLocalChecked());
v8::Local<v8::String> Code = v8::Local<v8::String>::Cast(ReadRet);

v8::ScriptOrigin Origin(pathForDebug.size() == 0 ?
Specifier :
Expand Down
50 changes: 50 additions & 0 deletions unity/test/Src/Cases/API/EvalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down
26 changes: 26 additions & 0 deletions unity/test/Src/UnitTestLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -94,6 +100,13 @@ public void AddMockFileContent(string fileName, string content)
{
mockFileContent[fileName] = content;
}

private HashSet<string> nullFiles = new HashSet<string>();
[UnityEngine.Scripting.Preserve]
public void AddNullFile(string fileName)
{
nullFiles.Add(fileName);
}
}
public class UnitTestLoader : ILoader
{
Expand All @@ -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))
{
Expand All @@ -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)) {
Expand All @@ -149,5 +168,12 @@ public void AddMockFileContent(string fileName, string content)
{
mockFileContent[fileName] = content;
}

private HashSet<string> nullFiles = new HashSet<string>();
[UnityEngine.Scripting.Preserve]
public void AddNullFile(string fileName)
{
nullFiles.Add(fileName);
}
}
}
20 changes: 20 additions & 0 deletions unity/test/dotnet/Src/TxtLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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)))
{
Expand Down Expand Up @@ -105,6 +119,12 @@ public void AddMockFileContent(string fileName, string content)
{
mockFileContent.Add(fileName, content);
}

private HashSet<string> nullFiles = new HashSet<string>();
public void AddNullFile(string fileName)
{
nullFiles.Add(fileName);
}
}

namespace UnityEngine.Scripting
Expand Down

0 comments on commit a59f5e5

Please sign in to comment.