Skip to content

Commit

Permalink
改善调试环境,以及修复v8编译 (#114)
Browse files Browse the repository at this point in the history
* Have Unity auto import .cjs files as TextAssets. These files are recognized by IDEs as JavaScript files (for syntax highlighting and debugging), and can be used directly with NodeJS.

* Update CJS module import logic, so debugger (in VSCode) correctly points to the file on disk and stops at correct lines.

* Support clearing module cache

* Auto clear JS module cache on .cjs file change to streamline debugging process.

* Fix build v8 after Github Actions changed their environment.

* Code review commit.

* Force build and publish workflow to use Xcode12.0 too. The new tool chain builds for arm architecture, which breaks linker step. Will need to add steps for arm specifically in the future so Unity works on the new Macs.

* Fix casing.
  • Loading branch information
yuatpocketgems authored Dec 16, 2020
1 parent 89fdc60 commit e209cb0
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ jobs:
workflow: build_v8.yml
name: v8_bin
path: unity/native_src/
- name: Use Xcode 12.0 for x86
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Build
run: |
cd unity/native_src
Expand All @@ -69,6 +71,8 @@ jobs:
workflow: build_v8.yml
name: v8_bin
path: unity/native_src/
- name: Use Xcode 12.0 for x86
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Build
run: |
cd unity/native_src
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build_v8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ jobs:
runs-on: macos-10.15
steps:
- uses: actions/checkout@v2
- name: Use Xcode 12.0 to use SDK 10.15
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Run build script
run: |
cd $GITHUB_WORKSPACE
Expand All @@ -121,6 +123,8 @@ jobs:
runs-on: macos-10.15
steps:
- uses: actions/checkout@v2
- name: Use Xcode 12.0 to use SDK 10.15
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Run build script
run: |
cd $GITHUB_WORKSPACE
Expand All @@ -135,6 +139,8 @@ jobs:
runs-on: macos-10.15
steps:
- uses: actions/checkout@v2
- name: Use Xcode 12.0 to use SDK 10.15
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Run build script
run: |
cd $GITHUB_WORKSPACE
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
workflow: build_v8.yml
name: v8_bin
path: unity/native_src/
- name: Use Xcode 12.0 for x86
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Build
run: |
cd unity/native_src
Expand All @@ -67,6 +69,8 @@ jobs:
workflow: build_v8.yml
name: v8_bin
path: unity/native_src/
- name: Use Xcode 12.0 for x86
run: sudo xcode-select -switch "/Applications/Xcode_12.app"
- name: Build
run: |
cd unity/native_src
Expand Down
21 changes: 21 additions & 0 deletions unity/Assets/Puerts/Src/Editor/CJSImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if UNITY_2018_1_OR_NEWER
using System.IO;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;

[ScriptedImporter(1, "cjs")]
public class CJSImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
TextAsset subAsset = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("text", subAsset);
ctx.SetMainObject(subAsset);

#if ENABLE_CJS_AUTO_RELOAD
Puerts.JsEnv.ClearAllModuleCaches();
#endif
}
}

#endif
18 changes: 17 additions & 1 deletion unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ public TResult Eval<TResult>(string chunk, string chunkName = "chunk")
#endif
}

public void ClearModuleCache ()
{
Eval("global.clearModuleCache()");
}

public static void ClearAllModuleCaches ()
{
lock (jsEnvs)
{
foreach (var jsEnv in jsEnvs)
{
jsEnv.ClearModuleCache();
}
}
}

public void AddLazyStaticWrapLoader(Type type, Func<TypeRegisterInfo> lazyStaticWrapLoader)
{
#if THREAD_SAFE
Expand Down Expand Up @@ -597,4 +613,4 @@ internal void ReleasePendingJSFunctions()
}
}
}
}
}
15 changes: 12 additions & 3 deletions unity/Assets/Puerts/Src/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ public DefaultLoader(string root)
this.root = root;
}

private string PathToUse(string filepath)
{
return filepath.EndsWith(".cjs") ?
filepath.Substring(0, filepath.Length - 4) :
filepath;
}

public bool FileExists(string filepath)
{
#if PUERTS_GENERAL
return File.Exists(Path.Combine(root, filepath));
#else
return UnityEngine.Resources.Load(filepath) != null;
string pathToUse = this.PathToUse(filepath);
return UnityEngine.Resources.Load(pathToUse) != null;
#endif
}

Expand All @@ -45,7 +53,8 @@ public string ReadFile(string filepath, out string debugpath)
debugpath = Path.Combine(root, filepath);
return File.ReadAllText(debugpath);
#else
UnityEngine.TextAsset file = (UnityEngine.TextAsset)UnityEngine.Resources.Load(filepath);
string pathToUse = this.PathToUse(filepath);
UnityEngine.TextAsset file = (UnityEngine.TextAsset)UnityEngine.Resources.Load(pathToUse);
debugpath = System.IO.Path.Combine(root, filepath);
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
debugpath = debugpath.Replace("/", "\\");
Expand All @@ -54,4 +63,4 @@ public string ReadFile(string filepath, out string debugpath)
#endif
}
}
}
}
1 change: 1 addition & 0 deletions unity/Assets/Puerts/Src/Resources/puerts/cjsload.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var global = global || (function () { return this; }());
return searchModuleInDirWithExt(dir, requiredModule);
} else {
return searchModuleInDirWithExt(dir, requiredModule + ".js")
|| searchModuleInDirWithExt(dir, requiredModule + ".cjs")
|| searchModuleInDirWithExt(dir, requiredModule + "/index.js")
|| searchModuleInDirWithExt(dir, requiredModule + "/package.json");
}
Expand Down
35 changes: 25 additions & 10 deletions unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ var global = global || (function () { return this; }());
return {fullPath: path, debugPath: debugPath, script: context}
}

let tmpModuleStorage = [];

let moduleCache = Object.create(null); // key to sid
let tmpModuleStorage = []; // sid to module

function addModule(m) {
for (var i = 0; i < tmpModuleStorage.length; i++) {
if (!tmpModuleStorage[i]) {
Expand All @@ -32,18 +33,22 @@ var global = global || (function () { return this; }());
return tmpModuleStorage[id];
}

let moduleCache = Object.create(null);
let buildinModule = Object.create(null);
function executeModule(fullPath, script, debugPath, sid) {
sid = (typeof sid == 'undefined') ? 0 : sid;
let fullPathInJs = fullPath.replace(/\\/g, '\\\\');
let fullDirInJs = (fullPath.indexOf('/') != -1) ? fullPath.substring(0, fullPath.lastIndexOf("/")) : fullPath.substring(0, fullPath.lastIndexOf("\\")).replace(/\\/g, '\\\\');
let executeScript = "(function() { var __filename = '"
+ fullPathInJs + "', __dirname = '"
+ fullDirInJs + "', module = puerts.getModuleBySID(" + sid + "), exports = module.exports; module.filename = __filename ; (function (exports, require, console, prompt) { "
+ script + "\n})(exports, puerts.genRequire('"
+ fullDirInJs + "'), puerts.console); return module.exports})()";
return puerts.evalScript(executeScript, debugPath);
let exports = {};
let module = puerts.getModuleBySID(sid);
module.exports = exports;
let wrapped = puerts.evalScript(
// Wrap the script in the same way NodeJS does it. It is important since IDEs (VSCode) will use this wrapper pattern
// to enable stepping through original source in-place.
"(function (exports, require, module, __filename, __dirname) { " + script + "\n});",
debugPath
)
wrapped(exports, puerts.genRequire(fullDirInJs), module, fullPathInJs, fullDirInJs)
return module.exports;
}

function genRequire(requiringDir) {
Expand Down Expand Up @@ -75,6 +80,9 @@ var global = global || (function () { return this; }());
return m.exports;
}
}
require.clearModuleCache = () => {
localModuleCache = Object.create(null);
}

return require;
}
Expand All @@ -92,4 +100,11 @@ var global = global || (function () { return this; }());
puerts.registerBuildinModule = registerBuildinModule;

global.require = genRequire("");
}(global));

function clearModuleCache () {
tmpModuleStorage = [];
moduleCache = Object.create(null);
global.require.clearModuleCache();
}
global.clearModuleCache = clearModuleCache;
}(global));

0 comments on commit e209cb0

Please sign in to comment.