cocos2d-js 重新require JS文件

重新require JS文件指的是在调用require文件后,在ScriptingCore中重新编译该文件,在编程或者界面制作过程中提升效率,修改文件后只需要重新打开该窗口即可生效。

调用:

HelpTool.requireScript("src/pve/SuperStarShop.js", true);

ScriptingCore.cpp修改runScript:

bool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
{
	if (global == NULL) {
		global = _global;
	}
	if (cx == NULL) {
		cx = _cx;
	}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	// xiaohei:in debug recompile js file
	cleanScript(path);
	compileScript(path, global, cx);
#else
	if (!filename_script[path])
	{
		compileScript(path, global, cx);
	}
#endif

	JSScript * script = filename_script[path];
	bool evaluatedOK = false;
	if (script) {
		jsval rval;
		JSAutoCompartment ac(cx, global);
		evaluatedOK = JS_ExecuteScript(cx, global, script, &rval);
		if (false == evaluatedOK) {
			cocos2d::log("(evaluatedOK == JS_FALSE)");
			JS_ReportPendingException(cx);
		}
	}
	return evaluatedOK;
}


HelpTool中实现:

HelpTool._require_script_map = {};
HelpTool.requireScript = function(path, needReload){
    if(HelpTool._require_script_map[path]){
        // xiaohei 如果是开发模式每次都重新require文件,在ScriptingCore中重新编译该文件,只有传入needReload为true才能重新加载(有些共享内存不能重新加载)
        if(g_targetOS == TargetOS.WINDOWS && needReload){
            require(path);
        }
        return;
    }else{
        require(path);
        HelpTool._require_script_map[path]  = true;
    }
};

 

 

 

同理,自己管理HelpTool._require_script_map,避免重复加载js文件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值