diff --git a/unity/Assets/Puerts/Src/Editor/CJSImporter.cs b/unity/Assets/Puerts/Src/Editor/CJSImporter.cs index cb7f8be59c..ce642e96b8 100644 --- a/unity/Assets/Puerts/Src/Editor/CJSImporter.cs +++ b/unity/Assets/Puerts/Src/Editor/CJSImporter.cs @@ -1,18 +1,21 @@ +#if UNITY_2018_1_OR_NEWER using System.IO; using UnityEditor.Experimental.AssetImporters; using UnityEngine; - -using Puerts; [ScriptedImporter(1, "cjs")] public class CJSImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { - TextAsset subAsset = new TextAsset( File.ReadAllText( ctx.assetPath ) ); + TextAsset subAsset = new TextAsset(File.ReadAllText(ctx.assetPath)); ctx.AddObjectToAsset("text", subAsset); - ctx.SetMainObject( subAsset ); + ctx.SetMainObject(subAsset); - JsEnv.ClearAllModuleCaches(); +#if ENABLE_CJS_AUTO_RELOAD + Puerts.JsEnv.ClearAllModuleCaches(); +#endif } } + +#endif diff --git a/unity/Assets/Puerts/Src/JsEnv.cs b/unity/Assets/Puerts/Src/JsEnv.cs index 47bcf52a3f..a8c6435ad3 100644 --- a/unity/Assets/Puerts/Src/JsEnv.cs +++ b/unity/Assets/Puerts/Src/JsEnv.cs @@ -177,10 +177,11 @@ public void ClearModuleCache () Eval("global.clearModuleCache()"); } - public static void ClearAllModuleCaches () { + public static void ClearAllModuleCaches () + { lock (jsEnvs) { - foreach (JsEnv jsEnv in jsEnvs) + foreach (var jsEnv in jsEnvs) { jsEnv.ClearModuleCache(); } diff --git a/unity/Assets/Puerts/Src/Resources/puerts/init.js.txt b/unity/Assets/Puerts/Src/Resources/puerts/init.js.txt index 49134e39c1..30befab6db 100644 --- a/unity/Assets/Puerts/Src/Resources/puerts/init.js.txt +++ b/unity/Assets/Puerts/Src/Resources/puerts/init.js.txt @@ -20,7 +20,4 @@ var global = global || (function () { return this; }()); return eval(script); } delete global.__tgjsEvalScript; - - puerts.evalModule = global.__tgjsEvalModule; - delete global.__tgjsEvalModule; }(global)); diff --git a/unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt b/unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt index 78e05535ee..dc8ffa8036 100644 --- a/unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt +++ b/unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt @@ -41,19 +41,13 @@ var global = global || (function () { return this; }()); let exports = {}; let module = puerts.getModuleBySID(sid); module.exports = exports; - puerts.evalModule( + 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, - [ - exports, - puerts.genRequire(fullDirInJs), - module, - fullPathInJs, - fullDirInJs, - ] - ); + debugPath + ) + wrapped(exports, puerts.genRequire(fullDirInJs), module, fullPathInJs, fullDirInJs) return module.exports; } diff --git a/unity/native_src/Src/JSEngine.cpp b/unity/native_src/Src/JSEngine.cpp index 4f7225ff40..1560df0fc6 100644 --- a/unity/native_src/Src/JSEngine.cpp +++ b/unity/native_src/Src/JSEngine.cpp @@ -52,50 +52,6 @@ namespace puerts Info.GetReturnValue().Set(Result.ToLocalChecked()); } - static void EvalModuleWithPath(const v8::FunctionCallbackInfo& Info) - { - // Input is 3-tuple: (code, path, arguments) - v8::Isolate* Isolate = Info.GetIsolate(); - v8::Isolate::Scope IsolateScope(Isolate); - v8::HandleScope HandleScope(Isolate); - v8::Local Context = Isolate->GetCurrentContext(); - v8::Context::Scope ContextScope(Context); - - if (Info.Length() != 3 || !Info[0]->IsString() || !Info[1]->IsString() || !Info[2]->IsArray()) - { - FV8Utils::ThrowException(Isolate, "invalid argument for evalModule"); - return; - } - - v8::Local Source = Info[0]->ToString(Context).ToLocalChecked(); - v8::Local Name = Info[1]->ToString(Context).ToLocalChecked(); - v8::Local Arguments = v8::Local::Cast(Info[2]); - - v8::ScriptOrigin Origin(Name); - v8::MaybeLocal Script = v8::Script::Compile(Context, Source, &Origin); - if (Script.IsEmpty()) - { - return; - } - - v8::MaybeLocal Wrapped = Script.ToLocalChecked()->Run(Context); - if (Wrapped.IsEmpty()) { - return; - } - v8::Handle Fn = v8::Local::Cast(Wrapped.ToLocalChecked()->ToObject(Context).ToLocalChecked()); - - std::vector> ContextArgs; - if (!Arguments.IsEmpty()) { - for (uint32_t n = 0; n < Arguments->Length(); n++) { - v8::Local Val; - if (!Arguments->Get(Context, n).ToLocal(&Val)) return; - ContextArgs.push_back(Val.As()); - } - } - - Fn->Call(Context, Context->Global(), (int)ContextArgs.size(), (v8::Local *)ContextArgs.data()); - } - JSEngine::JSEngine() { GeneralDestructor = nullptr; @@ -136,7 +92,6 @@ namespace puerts v8::Local Global = Context->Global(); Global->Set(Context, FV8Utils::V8String(Isolate, "__tgjsEvalScript"), v8::FunctionTemplate::New(Isolate, &EvalWithPath)->GetFunction(Context).ToLocalChecked()).Check(); - Global->Set(Context, FV8Utils::V8String(Isolate, "__tgjsEvalModule"), v8::FunctionTemplate::New(Isolate, &EvalModuleWithPath)->GetFunction(Context).ToLocalChecked()).Check(); Isolate->SetPromiseRejectCallback(&PromiseRejectCallback); Global->Set(Context, FV8Utils::V8String(Isolate, "__tgjsSetPromiseRejectCallback"), v8::FunctionTemplate::New(Isolate, &SetPromiseRejectCallback)->GetFunction(Context).ToLocalChecked()).Check();