Skip to content

Commit

Permalink
[unity] add puer: prefix in import.meta.url && optimize dts gen
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieyang committed Sep 28, 2022
1 parent 20280b6 commit 6f48390
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 6 additions & 10 deletions unity/Assets/Puerts/Editor/Resources/puerts/templates/dts.tpl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<T> {
value: T
}
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions unity/Assets/Puerts/Runtime/Resources/puerts/modular.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions unity/native_src/Src/JSEngine_Eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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_;
Expand Down

0 comments on commit 6f48390

Please sign in to comment.