Skip to content

Commit b5495b3

Browse files
authored
feat: 添加--runtime-globals选项及对应测试用例 (#19)
* feat: 添加--runtime-globals选项及对应测试用例 * fix: addon compile fail
1 parent 662f73d commit b5495b3

File tree

8 files changed

+100
-32
lines changed

8 files changed

+100
-32
lines changed

.mocharc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
// 'test/spec/wcc/module/module.spec.ts',
1515
// 'test/spec/wcc/wxs/wxs.spec.ts',
1616
// 'test/spec/wcsc/raw/raw.spec.ts',
17+
// 'test/spec/wcc/empty/version.spec.ts'
1718
],
1819
// spec: "test/spec/wcc/wxs/wxs.spec.ts",
1920
timeout: 20000,

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
🚀Features
44
- 架构:添加龙芯支持
55
- 编译器:跟进v2.01.2510250版本
6+
- 编译器:添加 `--runtime-globals` 选项,移除 `--gdc` 选项
67

78
## 🌈 v0.1.5 / 2025-05-02
89

src/include/define.h

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/include/wxml.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -717,19 +717,31 @@ namespace WXML
717717

718718
} // namespace EXPRLib
719719

720-
namespace GlassEaselWxs {
721-
const std::string sWrapper = "function e(nv_require,nv_exports,nv_module){\u000a%s\u000a}const n={nv_exports:{}};e((function(e){return e.startsWith(\u0022p_\u0022)&&(e=\u0022/\u0022+e.slice(2)),e.endsWith(\u0022.wxs\u0022)&&(e=e.slice(0,e.length-4)),()=>gdc(require(e)(),\u0022nv_\u0022,2)}),n.nv_exports,n),Object.assign(module,gdc(n,void 0,2));";
722-
const std::string sGenFuncDeepCopy = "var gdc=function(){var e=function(){return void 0!==this?this:\"undefined\"!=typeof globalThis?globalThis:\"undefine"
723-
"d\"!=typeof self?self:\"undefined\"!=typeof window?window:\"undefined\"!=typeof global?global:{}}();return function("
724-
"n,t,o,i=e){if(null==n)return n;if(\"undefined\"!=typeof __wxCodeSpace__&&\"function\"==typeof __wxCodeSpace__.isWxsS"
725-
"tateObject&&__wxCodeSpace__.isWxsStateObject(n))return n;if(\"string\"==typeof n||\"boolean\"==typeof n||\"number\"="
726-
"=typeof n)return n;if(Array.isArray(n)){const e=[];for(let i=0;i<n.length;i++)e.push(gdc(n[i],t,o,n));return e}if(n "
727-
"instanceof Date){const e=new Date;return e.setTime(n.getTime()),e}if(n instanceof RegExp){let e=\"\";return n.global"
728-
"&&(e+=\"g\"),n.ignoreCase&&(e+=\"i\"),n.multiline&&(e+=\"m\"),new RegExp(n.source,e)}if(\"object\"==typeof n){const "
729-
"e={};for(let i in n)Object.prototype.hasOwnProperty.call(n,i)&&(void 0===t?e[i.substring(3)]=gdc(n[i],t,o,n):e[t+i]="
730-
"gdc(n[i],t,o,n));return e}return\"function\"==typeof n?1===o?null:function(...e){const o=gdc(e,void 0===t?\"nv_\":vo"
731-
"id 0),r=n.apply(i,o);return gdc(r,t)}:null}}();";
732-
const std::string sWxsEnvInit = aUndefinedTypeo;
720+
namespace GlassEasel{
721+
const std::string sRuntimeGlobals = "var X=function(n){return null==n?Object.create(null):n},Y=function(n){return null==n?\"\":String(n)},Z=function(n,t)"
722+
"{return!0===n||(n?n[t]:void 0)},P=function(n){return function(...t){if(\"function\"==typeof n)try{return gdc(n.apply"
723+
"(null,gdc(t,\"nv_\")))}catch(n){return console.error(\"WXS Error:\"),void console.error(n)}}},Q={a:function(n){for(v"
724+
"ar t=0;t<n.length;t++)if(n[t])return n},b:function(n){for(var t=Object.values(n),r=0;r<t.length;r++)if(t[r])return t"
725+
"},A:function(n){return n},B:function(n){return n}},D=function(){const n={},t=t=>{const o=n[t];if(void 0===o)throw ne"
726+
"w Error(`no such WXS module: ${t}`);if(!o.t){const n={exports:{}};let e;o.o=n.exports,o.t=!0;try{e=o.u(r(t),n.export"
727+
"s,n)}catch(n){throw o.t=!1,n}o.o=void 0!==n.exports?n.exports:e}return o.o},r=n=>function(r){if(\"string\"!=typeof r"
728+
")throw new Error(\"WXS require args must be a string\");if(r.startsWith(\"p_\")&&r.endsWith(\".wxs\")){const n=r.sli"
729+
"ce(2,-4);return()=>gdc(t(n),\"nv_\",2)}{const o=((n,t)=>{let r;r=\"/\"!==n[0]?t.split(\"/\").slice(0,-1).concat(n.sp"
730+
"lit(\"/\")):n.split(\"/\");const o=[];for(let n=0;n<r.length;n+=1){const t=r[n];\"\"!==t&&\".\"!==t&&(\"..\"!==t?o.p"
731+
"ush(t):o.pop())}return o.join(\"/\")})(r,n);return()=>t(o)}};return(r,o)=>(((t,r)=>{void 0===n[t]&&(n[t]={t:!1,u:r,o"
732+
":null})})(r,o),()=>t(r))}(),gdc=function(n,t,r){if(null==n)return n;if(\"string\"==typeof n||\"boolean\"==typeof n||"
733+
"\"number\"==typeof n)return n;if(Array.isArray(n)){const o=[];for(let e=0;e<n.length;e++)o.push(gdc(n[e],t,r));retur"
734+
"n o}if(n instanceof Date){const t=new Date;return t.setTime(n.getTime()),t}if(n instanceof RegExp){let t=\"\";return"
735+
" n.global&&(t+=\"g\"),n.ignoreCase&&(t+=\"i\"),n.multiline&&(t+=\"m\"),new RegExp(n.source,t)}if(\"function\"==typeo"
736+
"f n)return 1==r?gdc(n(),void 0,2):2==r?n:null;if(\"object\"==typeof n){const o={};for(let e in n)Object.prototype.ha"
737+
"sOwnProperty.call(n,e)&&(void 0===t?o[e.substring(3)]=gdc(n[e],t,r):o[t+e]=gdc(n[e],t,r));return o}return null};";
738+
const std::string sGitHash = "";
739+
namespace WXS {
740+
const std::string sWrapper = "function e(nv_require,nv_exports,nv_module){\u000a%s\u000a}const n={nv_exports:{}};e((function(e){return e.startsWith(\u0022p_\u0022)&&(e=\u0022/\u0022+e.slice(2)),e.endsWith(\u0022.wxs\u0022)&&(e=e.slice(0,e.length-4)),()=>gdc(require(e)(),\u0022nv_\u0022,2)}),n.nv_exports,n),Object.assign(module,gdc(n,void 0,2));";
741+
742+
const std::string sEnvInit = aUndefinedTypeo;
743+
const std::string sGitHash = "e8f2c53f408bfdf50d9acfef2cfe46b9358589d6";
744+
};
733745
};
734746
}
735747
#endif

src/module/wcc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ Napi::Value compile(Napi::Env &env, WCCOptions &options) {
7979
{
8080
throw Napi::Error::New(env, "Error: " + v146 + "\n");
8181
}
82-
auto v31 = snprintf(0, 0, WXML::GlassEaselWxs::sWrapper.c_str(),
82+
auto v31 = snprintf(0, 0, WXML::GlassEasel::WXS::sWrapper.c_str(),
8383
v146.c_str()) + 1; char *v32 = (char *)operator new[](v31);
84-
snprintf(v32, v31, WXML::GlassEaselWxs::sWrapper.c_str(),
84+
snprintf(v32, v31, WXML::GlassEasel::WXS::sWrapper.c_str(),
8585
v146.c_str()); wxsMap[v152] = v32; delete[] v32;
8686
}
8787
}

src/wcc.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ int main(int argc, const char **argv)
6868
bool isLLA = false;
6969
bool hasLL = false;
7070
bool isWxs = false;
71-
bool isGDC = false;
7271
bool isWxsEnv = false;
72+
bool isRuntimeGlobals = false;
7373
string xc_Or_completeCode_Param;
7474
string outputFileName;
7575
vector<string> fileList;
@@ -270,14 +270,14 @@ int main(int argc, const char **argv)
270270
{
271271
isWxs = true;
272272
}
273-
else if (!param.compare("--gdc"))
274-
{
275-
isGDC = true;
276-
}
277273
else if (!param.compare("--wxs-env"))
278274
{
279275
isWxsEnv = true;
280276
}
277+
else if (!param.compare("--runtime-globals"))
278+
{
279+
isRuntimeGlobals = true;
280+
}
281281

282282
}
283283
}
@@ -330,9 +330,9 @@ int main(int argc, const char **argv)
330330
fprintf(stderr, "Error in file %s: %s\n", v152.c_str(), v146.c_str());
331331
return -2;
332332
}
333-
auto v31 = snprintf(0, 0, WXML::GlassEaselWxs::sWrapper.c_str(), v146.c_str()) + 1;
333+
auto v31 = snprintf(0, 0, WXML::GlassEasel::WXS::sWrapper.c_str(), v146.c_str()) + 1;
334334
char *v32 = (char *)operator new[](v31);
335-
snprintf(v32, v31, WXML::GlassEaselWxs::sWrapper.c_str(), v146.c_str());
335+
snprintf(v32, v31, WXML::GlassEasel::WXS::sWrapper.c_str(), v146.c_str());
336336
cfg[v152] = v32;
337337
delete[] v32;
338338
}
@@ -345,9 +345,9 @@ int main(int argc, const char **argv)
345345
return fclose(f);
346346
return 0;
347347
}
348-
if (isGDC)
348+
if (isWxsEnv)
349349
{
350-
std::string v136 = WXML::GlassEaselWxs::sGenFuncDeepCopy.c_str();
350+
std::string v136 = WXML::GlassEasel::WXS::sEnvInit.c_str();
351351
FILE *f = stdout;
352352
if (!outputFileName.empty())
353353
f = fopen(outputFileName.c_str(), "w");
@@ -356,9 +356,9 @@ int main(int argc, const char **argv)
356356
return fclose(f);
357357
return 0;
358358
}
359-
if (isWxsEnv)
359+
if (isRuntimeGlobals)
360360
{
361-
std::string v136 = WXML::GlassEaselWxs::sWxsEnvInit.c_str();
361+
std::string v136 = WXML::GlassEasel::sRuntimeGlobals.c_str();
362362
FILE *f = stdout;
363363
if (!outputFileName.empty())
364364
f = fopen(outputFileName.c_str(), "w");

test/spec/wcc/empty/version.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,60 @@ describe("wcc - empty", function () {
3030
fs.mkdirSync(storagePath, { recursive: true });
3131
} catch (error) {}
3232

33+
fs.writeFileSync(`${storagePath}/wine-output.js`, w);
34+
fs.writeFileSync(`${storagePath}/node-output.js`, n);
35+
assert.equal(n, w, `wine: ${w}\n\nlinux: ${n}`);
36+
});
37+
it("wxs-env", async function () {
38+
const projectPath = __dirname;
39+
const args: string[] = [
40+
"--wxs-env"
41+
];
42+
let w = ""
43+
try
44+
{
45+
w = (await windows.wcc(args, projectPath)).replace(/\r\n/g, '\n')
46+
}
47+
catch(err: any){
48+
console.error('error:', err)
49+
w = err.stdout.replace(/\r\n/g, '\n')
50+
}
51+
const n = await linux.wcc(args, projectPath);
52+
const storagePath = path.resolve(
53+
__dirname,
54+
`wxs-env/${this.test?.title}`
55+
);
56+
try {
57+
fs.mkdirSync(storagePath, { recursive: true });
58+
} catch (error) {}
59+
60+
fs.writeFileSync(`${storagePath}/wine-output.js`, w);
61+
fs.writeFileSync(`${storagePath}/node-output.js`, n);
62+
assert.equal(n, w, `wine: ${w}\n\nlinux: ${n}`);
63+
});
64+
it("runtime-globals", async function () {
65+
const projectPath = __dirname;
66+
const args: string[] = [
67+
"--runtime-globals"
68+
];
69+
let w = ""
70+
try
71+
{
72+
w = (await windows.wcc(args, projectPath)).replace(/\r\n/g, '\n')
73+
}
74+
catch(err: any){
75+
console.error('error:', err)
76+
w = err.stdout.replace(/\r\n/g, '\n')
77+
}
78+
const n = await linux.wcc(args, projectPath);
79+
const storagePath = path.resolve(
80+
__dirname,
81+
`runtime-globals/${this.test?.title}`
82+
);
83+
try {
84+
fs.mkdirSync(storagePath, { recursive: true });
85+
} catch (error) {}
86+
3387
fs.writeFileSync(`${storagePath}/wine-output.js`, w);
3488
fs.writeFileSync(`${storagePath}/node-output.js`, n);
3589
assert.equal(n, w, `wine: ${w}\n\nlinux: ${n}`);

test/wcc.disassembly.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195705,7 +195705,7 @@ int __cdecl main(int argc, const char **argv, const char **envp)
195705195705
else
195706195706
{
195707195707
v12 = *(_DWORD *)v103;
195708-
if ( *(_BYTE *)(*(_DWORD *)v103 + 1) == 99 && *(_BYTE *)(v12 + 2) == 98 )
195708+
if ( *(_BYTE *)(*(_DWORD *)v103 + 1) == 99/*c*/ && *(_BYTE *)(v12 + 2) == 98 /*b*/)
195709195709
{
195710195710
++v5;
195711195711
LOBYTE(v170[0]) = 0;
@@ -195733,7 +195733,7 @@ int __cdecl main(int argc, const char **argv, const char **envp)
195733195733
std::string::_M_dispose((void **)&String);
195734195734
++v5;
195735195735
}
195736-
else if ( v89 == 108 && *(_BYTE *)(v12 + 2) == 108 )
195736+
else if ( v89 == 108/*l*/ && *(_BYTE *)(v12 + 2) == 108/*l*/ )
195737195737
{
195738195738
v16 = *(_BYTE *)(v12 + 3);
195739195739
if ( v16 != 119 )
@@ -195811,15 +195811,15 @@ int __cdecl main(int argc, const char **argv, const char **envp)
195811195811
fclose(v24);
195812195812
std::string::_M_dispose((void **)&v168);
195813195813
}
195814-
if ( !v88 )
195814+
if ( !v88 ) // wxs
195815195815
{
195816-
if ( v87 )
195816+
if ( v87 ) // isRuntimeGlobals
195817195817
{
195818195818
v39 = (const char **)&WXML::GlassEasel::sRuntimeGlobals;
195819195819
LABEL_112:
195820195820
main::{lambda(std::string const&)#1}::operator()((const char **)&FileName, v39);
195821195821
v111 = 0;
195822-
goto LABEL_192;
195822+
goto LABEL_192; // return
195823195823
}
195824195824
v39 = (const char **)&WXML::GlassEasel::WXS::sEnvInit;
195825195825
if ( v93 )

0 commit comments

Comments
 (0)