出于性能考虑,开启 shared-jsvm-context 比较好,但现在各 script 是共享的全局 this,并且直接在这个 scope 上运行,命名很容易冲突(不只是脚本之间,单个脚本如果使用了 const、let ,再次执行时也有问题)
建议考虑下这个方案:
以下是思路:
/* script 初始化,仅执行一次 */
// 增加 async wrapper、exception handler
const userCode1Exec = (async function(){
try{
userCode1 源码
} catch(e){}
}).bind(
// clone API 对象,增加 global 变量
this.userCode1Scope = Object.assign({global: this}, APIs)
);
const userCode2Exec = (async function(){
try{
userCode2 源码
} catch(e){}
}).bind(
this.userCode2Scope = Object.assign({global: this}, APIs)
);
/* 执行部分,可以反复执行 */
userCode1Exec();
userCode2Exec();
/* scope 互访,源码中用其它 script 的 scope 名即可获取 */
global.userCode1Scope;
global.userCode2Scope;
@SurgeTeam