重新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文件