Skip to content

Commit

Permalink
Code review commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuatpocketgems committed Dec 15, 2020
1 parent 1a28b1c commit be597aa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 65 deletions.
13 changes: 8 additions & 5 deletions unity/Assets/Puerts/Src/Editor/CJSImporter.cs
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 0 additions & 3 deletions unity/Assets/Puerts/Src/Resources/puerts/init.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ var global = global || (function () { return this; }());
return eval(script);
}
delete global.__tgjsEvalScript;

puerts.evalModule = global.__tgjsEvalModule;
delete global.__tgjsEvalModule;
}(global));
14 changes: 4 additions & 10 deletions unity/Assets/Puerts/Src/Resources/puerts/modular.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
45 changes: 0 additions & 45 deletions unity/native_src/Src/JSEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,6 @@ namespace puerts
Info.GetReturnValue().Set(Result.ToLocalChecked());
}

static void EvalModuleWithPath(const v8::FunctionCallbackInfo<v8::Value>& Info)
{
// Input is 3-tuple: (code, path, arguments)
v8::Isolate* Isolate = Info.GetIsolate();
v8::Isolate::Scope IsolateScope(Isolate);
v8::HandleScope HandleScope(Isolate);
v8::Local<v8::Context> 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<v8::String> Source = Info[0]->ToString(Context).ToLocalChecked();
v8::Local<v8::String> Name = Info[1]->ToString(Context).ToLocalChecked();
v8::Local<v8::Array> Arguments = v8::Local<v8::Array>::Cast(Info[2]);

v8::ScriptOrigin Origin(Name);
v8::MaybeLocal<v8::Script> Script = v8::Script::Compile(Context, Source, &Origin);
if (Script.IsEmpty())
{
return;
}

v8::MaybeLocal<v8::Value> Wrapped = Script.ToLocalChecked()->Run(Context);
if (Wrapped.IsEmpty()) {
return;
}
v8::Handle<v8::Function> Fn = v8::Local<v8::Function>::Cast(Wrapped.ToLocalChecked()->ToObject(Context).ToLocalChecked());

std::vector<v8::Local<v8::Object>> ContextArgs;
if (!Arguments.IsEmpty()) {
for (uint32_t n = 0; n < Arguments->Length(); n++) {
v8::Local<v8::Value> Val;
if (!Arguments->Get(Context, n).ToLocal(&Val)) return;
ContextArgs.push_back(Val.As<v8::Object>());
}
}

Fn->Call(Context, Context->Global(), (int)ContextArgs.size(), (v8::Local<v8::Value> *)ContextArgs.data());
}

JSEngine::JSEngine()
{
GeneralDestructor = nullptr;
Expand Down Expand Up @@ -136,7 +92,6 @@ namespace puerts
v8::Local<v8::Object> 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<JSEngine>);
Global->Set(Context, FV8Utils::V8String(Isolate, "__tgjsSetPromiseRejectCallback"), v8::FunctionTemplate::New(Isolate, &SetPromiseRejectCallback<JSEngine>)->GetFunction(Context).ToLocalChecked()).Check();
Expand Down

0 comments on commit be597aa

Please sign in to comment.