diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index fad55b3ddf..dd5fc749d6 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -45,7 +45,7 @@ #include "js_bindings_config.h" -#define BYTE_CODE_SUFFIX ".bc" +#define BYTE_CODE_FILE_EXT ".jsc" pthread_t debugThread; string inData; @@ -446,12 +446,22 @@ void ScriptingCore::createGlobalContext() { } } -std::string TransScriptPath(const std::string& path) { - if (path.c_str()[0] == '/') { - return path; +std::string TransFilePath(const std::string& filePath) { + if (filePath.c_str()[0] == '/') { + return filePath; } else { - return cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()); + return cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(filePath.c_str()); + } +} + +std::string RemoveFileExt(const std::string& filePath) { + size_t pos = filePath.rfind('.'); + if (0 < pos) { + return filePath.substr(0, pos); + } + else { + return filePath; } } @@ -461,8 +471,7 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c return false; } cocos2d::CCFileUtils *futil = cocos2d::CCFileUtils::sharedFileUtils(); - std::string rpath = TransScriptPath(path); - std::string bcPath = TransScriptPath(std::string(path) + BYTE_CODE_SUFFIX); + std::string rpath = TransFilePath(path); if (global == NULL) { global = global_; } @@ -474,29 +483,27 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c JS::CompileOptions options(cx); options.setUTF8(true).setFileAndLine(rpath.c_str(), 1); - // this will always compile the script, we can actually check if the script - // was compiled before, because it can be in the global map - unsigned long length = 0; - // Removed in SpiderMonkey 19.0 - //JSScript* script = JS_CompileUTF8File(cx, global, rpath.c_str()); - bool notify = futil->isPopupNotify(); - futil->setPopupNotify(false); - void *data = futil->getFileData(bcPath.c_str(), "rb", &length); - futil->setPopupNotify(notify); - if (data) { - script = JS_DecodeScript(cx, data, length, NULL, NULL); - } - else { + // a) check js file first #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - unsigned char *content = (unsigned char*)CCString::createWithContentsOfFile(rpath.c_str())->getCString(); - if (content) { - // Not supported in SpiderMonkey 19.0 - //JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1); - script = JS::Compile(cx, obj, options, (char*)content, strlen((char*)content)); - } + unsigned char *content = (unsigned char*)CCString::createWithContentsOfFile(rpath.c_str())->getCString(); + if (content) { + // Not supported in SpiderMonkey 19.0 + //JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1); + script = JS::Compile(cx, obj, options, (char*)content, strlen((char*)content)); + } #else - script = JS::Compile(cx, obj, options, rpath.c_str()); + script = JS::Compile(cx, obj, options, rpath.c_str()); #endif + // b) no js file, check jsc file + if (!script) { + std::string byteCodeFilePath = TransFilePath(RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT); + unsigned long length = 0; + // Removed in SpiderMonkey 19.0 + //JSScript* script = JS_CompileUTF8File(cx, global, rpath.c_str()); + void *data = futil->getFileData(byteCodeFilePath.c_str(), "rb", &length); + if (data) { + script = JS_DecodeScript(cx, data, length, NULL, NULL); + } } JSBool evaluatedOK = false; if (script) { diff --git a/tools/jsbcc/src/main.cpp b/tools/jsbcc/src/main.cpp index 9c3f8916f8..5db16b862e 100644 --- a/tools/jsbcc/src/main.cpp +++ b/tools/jsbcc/src/main.cpp @@ -17,7 +17,7 @@ const char *USAGE = "Usage: jsbcc input_js_file [byte_code_file]\n"\ " Pipe supported"; -const char *BYTE_CODE_FILE_SUFFIX = ".bc"; +const char *BYTE_CODE_FILE_EXT = ".jsc"; enum ErrorCode { EC_OK = 0, @@ -49,6 +49,17 @@ bool WriteFile(const std::string &filePath, void *data, uint32_t length) { } return false; } + +std::string RemoveFileExt(const std::string &filePath) { + size_t pos = filePath.rfind('.'); + if (0 < pos) { + return filePath.substr(0, pos); + } + else { + return filePath; + } +} + bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) { bool result = false; std::string ofp; @@ -56,7 +67,7 @@ bool CompileFile(const std::string &inputFilePath, const std::string &outputFile ofp = outputFilePath; } else { - ofp = inputFilePath + BYTE_CODE_FILE_SUFFIX; + ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT; } std::cout << "Input file: " << inputFilePath << std::endl; JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS);