From 6f48390b095a471f4f531d5b914a124e1ec3afed Mon Sep 17 00:00:00 2001 From: unknown <348063288@qq.com> Date: Wed, 28 Sep 2022 21:29:38 +0800 Subject: [PATCH] [unity] add `puer:` prefix in import.meta.url && optimize dts gen --- .../Resources/puerts/templates/dts.tpl.mjs | 16 ++++++---------- .../Puerts/Runtime/Resources/puerts/modular.mjs | 4 ++++ unity/native_src/Src/JSEngine_Eval.cpp | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/unity/Assets/Puerts/Editor/Resources/puerts/templates/dts.tpl.mjs b/unity/Assets/Puerts/Editor/Resources/puerts/templates/dts.tpl.mjs index c822bbd8f0..d093183ba1 100644 --- a/unity/Assets/Puerts/Editor/Resources/puerts/templates/dts.tpl.mjs +++ b/unity/Assets/Puerts/Editor/Resources/puerts/templates/dts.tpl.mjs @@ -10,10 +10,9 @@ * * TODO 待node.js版本成熟之后直接接入typescript formatter,现在先用手动指定indent的方式 * @param {DTS.TypingGenInfo} data - * @param {boolean} notGlobalStyle will treat csharp as a module instead of global variable CS. * @returns */ - export default function TypingTemplate(data, notGlobalStyle) { + export default function TypingTemplate(data) { let ret = ''; function _es6tplJoin(str, ...values) { @@ -46,14 +45,13 @@ ret += newLines.join('\n'); } - const baseIndent = notGlobalStyle ? 4 : 0; + const baseIndent = 0; tt` -${notGlobalStyle ? 'declare module "csharp"' : 'declare namespace CS'} { + declare namespace CS { //keep type incompatibility / 此属性保持类型不兼容 const __keep_incompatibility: unique symbol; -${notGlobalStyle ? 'namespace CSharp {' : ''} interface $Ref { value: T } @@ -204,13 +202,11 @@ ${notGlobalStyle ? 'namespace CSharp {' : ''} }) t.indent = 0; - if (notGlobalStyle) { - t` - } - ` - } t` } + declare module 'csharp' { + export = CS; + } ` return ret.replace(/\n(\s*)\n/g, '\n'); diff --git a/unity/Assets/Puerts/Runtime/Resources/puerts/modular.mjs b/unity/Assets/Puerts/Runtime/Resources/puerts/modular.mjs index 3a8cb78b3c..d73756c970 100644 --- a/unity/Assets/Puerts/Runtime/Resources/puerts/modular.mjs +++ b/unity/Assets/Puerts/Runtime/Resources/puerts/modular.mjs @@ -43,6 +43,10 @@ function executeModule(fullPath, script, debugPath, sid) { } function genRequire(requiringDir) { + if (requiringDir.indexOf(":") != -1) { + if (requiringDir.startsWith("puer:")) requiringDir = requiringDir.substr(5) + else { throw new Error("puer's genRequire can only support prefix with puer:"); } + } let localModuleCache = Object.create(null); function require(moduleName) { moduleName = moduleName.startsWith('./') ? moduleName.substr(2) : moduleName; diff --git a/unity/native_src/Src/JSEngine_Eval.cpp b/unity/native_src/Src/JSEngine_Eval.cpp index c95c86c6be..3e9ff4e16d 100644 --- a/unity/native_src/Src/JSEngine_Eval.cpp +++ b/unity/native_src/Src/JSEngine_Eval.cpp @@ -180,7 +180,7 @@ namespace puerts { meta->CreateDataProperty( Context, FV8Utils::V8String(Context->GetIsolate(), "url"), - FV8Utils::V8String(Context->GetIsolate(), iter->second.c_str()) + FV8Utils::V8String(Context->GetIsolate(), ("puer:" + iter->second).c_str()) ).ToChecked(); } } @@ -217,7 +217,7 @@ namespace puerts { auto module_ = (JSModuleDef *) JS_VALUE_GET_PTR(func_val); auto obj = JS_GetImportMeta(ctx, module_); - JS_SetProperty(ctx, obj, JS_NewAtom(ctx, "url"), JS_NewString(ctx, name)); + JS_SetProperty(ctx, obj, JS_NewAtom(ctx, "url"), JS_NewString(ctx, ("puer:" + name_std).c_str())); JS_FreeValue(ctx, obj); JsEngine->PathToModuleMap[name_std] = module_;