From 163c19fac202851734b0aeca8413fee70c614d84 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Tue, 30 Apr 2013 03:29:56 +0800 Subject: [PATCH 01/11] Added support for javascript byte code --- .../javascript/bindings/ScriptingCore.cpp | 50 ++- tools/jsbcc/jsbcc.xcodeproj/project.pbxproj | 396 ++++++++++++++++++ tools/jsbcc/src/jsbcc.sh | 46 ++ tools/jsbcc/src/main.cpp | 115 +++++ 4 files changed, 587 insertions(+), 20 deletions(-) create mode 100644 tools/jsbcc/jsbcc.xcodeproj/project.pbxproj create mode 100755 tools/jsbcc/src/jsbcc.sh create mode 100644 tools/jsbcc/src/main.cpp diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 864c0b63c4..c10863ee98 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" pthread_t debugThread; string inData; @@ -438,47 +438,57 @@ void ScriptingCore::createGlobalContext() { } } +std::string TransScriptPath(const std::string& path) { + if (path.c_str()[0] == '/') { + return path; + } + else { + return cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()); + } +} + JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx) { if (!path) { return false; } cocos2d::CCFileUtils *futil = cocos2d::CCFileUtils::sharedFileUtils(); - std::string rpath; - if (path[0] == '/') { - rpath = path; - } else { - rpath = futil->fullPathForFilename(path); - } + std::string rpath = TransScriptPath(path); + std::string bcPath = TransScriptPath(std::string(path) + BYTE_CODE_SUFFIX); if (global == NULL) { global = global_; } if (cx == NULL) { cx = cx_; } - + JSScript *script = NULL; js::RootedObject obj(cx, global); JS::CompileOptions options(cx); options.setFileAndLine(rpath.c_str(), 1); // Don't setUTF8 since it will cause messy string output by cc.log . -// options.setUTF8(true).setFileAndLine(rpath.c_str(), 1); + // 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 -#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - unsigned char *content = NULL; - unsigned long contentSize = 0; - - content = (unsigned char*)CCString::createWithContentsOfFile(rpath.c_str())->getCString(); - contentSize = strlen((char*)content); - // Not supported in SpiderMonkey 19.0 - //JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1); - JSScript *script = JS::Compile(cx, obj, options, (char*)content, contentSize); -#else + unsigned long length = 0; // Removed in SpiderMonkey 19.0 //JSScript* script = JS_CompileUTF8File(cx, global, rpath.c_str()); - JSScript *script = JS::Compile(cx, obj, options, rpath.c_str()); + void *data = futil->getFileData(bcPath.c_str(), "rb", &length); + if (data) { + script = JS_DecodeScript(cx, data, length, NULL, NULL); + } + else { +#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)); + } +#else + script = JS::Compile(cx, obj, options, rpath.c_str()); #endif + } JSBool evaluatedOK = false; if (script) { jsval rval; diff --git a/tools/jsbcc/jsbcc.xcodeproj/project.pbxproj b/tools/jsbcc/jsbcc.xcodeproj/project.pbxproj new file mode 100644 index 0000000000..ba9449fda3 --- /dev/null +++ b/tools/jsbcc/jsbcc.xcodeproj/project.pbxproj @@ -0,0 +1,396 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 6579750B172D6EBE000DDA7A /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6579750A172D6EBE000DDA7A /* main.cpp */; }; + 65797553172D7244000DDA7A /* libjs_static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 65797552172D722A000DDA7A /* libjs_static.a */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 65797507172D6EBE000DDA7A /* jsbcc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = jsbcc; sourceTree = BUILT_PRODUCTS_DIR; }; + 6579750A172D6EBE000DDA7A /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; + 65797516172D722A000DDA7A /* BitArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BitArray.h; sourceTree = ""; }; + 65797518172D722A000DDA7A /* Root.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Root.h; sourceTree = ""; }; + 6579751A172D722A000DDA7A /* HashTable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HashTable.h; sourceTree = ""; }; + 6579751B172D722A000DDA7A /* HeapAPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HeapAPI.h; sourceTree = ""; }; + 6579751C172D722A000DDA7A /* LegacyIntTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LegacyIntTypes.h; sourceTree = ""; }; + 6579751D172D722A000DDA7A /* MemoryMetrics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MemoryMetrics.h; sourceTree = ""; }; + 6579751E172D722A000DDA7A /* TemplateLib.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TemplateLib.h; sourceTree = ""; }; + 6579751F172D722A000DDA7A /* Utility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Utility.h; sourceTree = ""; }; + 65797520172D722A000DDA7A /* Vector.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Vector.h; sourceTree = ""; }; + 65797521172D722A000DDA7A /* js-config.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "js-config.h"; sourceTree = ""; }; + 65797522172D722A000DDA7A /* js.msg */ = {isa = PBXFileReference; lastKnownFileType = text; path = js.msg; sourceTree = ""; }; + 65797523172D722A000DDA7A /* jsalloc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsalloc.h; sourceTree = ""; }; + 65797524172D722A000DDA7A /* jsapi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsapi.h; sourceTree = ""; }; + 65797525172D722A000DDA7A /* jsclass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsclass.h; sourceTree = ""; }; + 65797526172D722A000DDA7A /* jsclist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsclist.h; sourceTree = ""; }; + 65797527172D722A000DDA7A /* jscpucfg.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jscpucfg.h; sourceTree = ""; }; + 65797528172D722A000DDA7A /* jsdbgapi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsdbgapi.h; sourceTree = ""; }; + 65797529172D722A000DDA7A /* jsdhash.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsdhash.h; sourceTree = ""; }; + 6579752A172D722A000DDA7A /* jsfriendapi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsfriendapi.h; sourceTree = ""; }; + 6579752B172D722A000DDA7A /* jslock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jslock.h; sourceTree = ""; }; + 6579752C172D722A000DDA7A /* json.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = json.h; sourceTree = ""; }; + 6579752D172D722A000DDA7A /* jsperf.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsperf.h; sourceTree = ""; }; + 6579752E172D722A000DDA7A /* jsprf.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsprf.h; sourceTree = ""; }; + 6579752F172D722A000DDA7A /* jsprototypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsprototypes.h; sourceTree = ""; }; + 65797530172D722A000DDA7A /* jsproxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsproxy.h; sourceTree = ""; }; + 65797531172D722A000DDA7A /* jsprvtd.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsprvtd.h; sourceTree = ""; }; + 65797532172D722A000DDA7A /* jspubtd.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jspubtd.h; sourceTree = ""; }; + 65797533172D722A000DDA7A /* jstypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jstypes.h; sourceTree = ""; }; + 65797534172D722A000DDA7A /* jsutil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsutil.h; sourceTree = ""; }; + 65797535172D722A000DDA7A /* jsval.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsval.h; sourceTree = ""; }; + 65797536172D722A000DDA7A /* jsversion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jsversion.h; sourceTree = ""; }; + 65797537172D722A000DDA7A /* jswrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = jswrapper.h; sourceTree = ""; }; + 65797539172D722A000DDA7A /* Assertions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Assertions.h; sourceTree = ""; }; + 6579753A172D722A000DDA7A /* Attributes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Attributes.h; sourceTree = ""; }; + 6579753B172D722A000DDA7A /* BloomFilter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BloomFilter.h; sourceTree = ""; }; + 6579753C172D722A000DDA7A /* CheckedInt.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CheckedInt.h; sourceTree = ""; }; + 6579753D172D722A000DDA7A /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = ""; }; + 6579753E172D722A000DDA7A /* EnumSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EnumSet.h; sourceTree = ""; }; + 6579753F172D722A000DDA7A /* FloatingPoint.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FloatingPoint.h; sourceTree = ""; }; + 65797540172D722A000DDA7A /* GuardObjects.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GuardObjects.h; sourceTree = ""; }; + 65797541172D722A000DDA7A /* HashFunctions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HashFunctions.h; sourceTree = ""; }; + 65797542172D722A000DDA7A /* Likely.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Likely.h; sourceTree = ""; }; + 65797543172D722A000DDA7A /* LinkedList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LinkedList.h; sourceTree = ""; }; + 65797544172D722A000DDA7A /* MathAlgorithms.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MathAlgorithms.h; sourceTree = ""; }; + 65797545172D722A000DDA7A /* MSStdInt.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSStdInt.h; sourceTree = ""; }; + 65797546172D722A000DDA7A /* NullPtr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NullPtr.h; sourceTree = ""; }; + 65797547172D722A000DDA7A /* RangedPtr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RangedPtr.h; sourceTree = ""; }; + 65797548172D722A000DDA7A /* RefPtr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RefPtr.h; sourceTree = ""; }; + 65797549172D722A000DDA7A /* Scoped.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Scoped.h; sourceTree = ""; }; + 6579754A172D722A000DDA7A /* SHA1.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SHA1.h; sourceTree = ""; }; + 6579754B172D722A000DDA7A /* StandardInteger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StandardInteger.h; sourceTree = ""; }; + 6579754C172D722A000DDA7A /* ThreadLocal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ThreadLocal.h; sourceTree = ""; }; + 6579754D172D722A000DDA7A /* Types.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Types.h; sourceTree = ""; }; + 6579754E172D722A000DDA7A /* TypeTraits.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TypeTraits.h; sourceTree = ""; }; + 6579754F172D722A000DDA7A /* Util.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Util.h; sourceTree = ""; }; + 65797550172D722A000DDA7A /* WeakPtr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WeakPtr.h; sourceTree = ""; }; + 65797552172D722A000DDA7A /* libjs_static.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libjs_static.a; sourceTree = ""; }; + 65AE5C7B172E654500157241 /* jsbcc.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = jsbcc.sh; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 65797504172D6EBE000DDA7A /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 65797553172D7244000DDA7A /* libjs_static.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 657974FE172D6EBD000DDA7A = { + isa = PBXGroup; + children = ( + 65797513172D722A000DDA7A /* spidermonkey-ios */, + 65797509172D6EBE000DDA7A /* src */, + 65797508172D6EBE000DDA7A /* Products */, + ); + sourceTree = ""; + }; + 65797508172D6EBE000DDA7A /* Products */ = { + isa = PBXGroup; + children = ( + 65797507172D6EBE000DDA7A /* jsbcc */, + ); + name = Products; + sourceTree = ""; + }; + 65797509172D6EBE000DDA7A /* src */ = { + isa = PBXGroup; + children = ( + 65AE5C7B172E654500157241 /* jsbcc.sh */, + 6579750A172D6EBE000DDA7A /* main.cpp */, + ); + path = src; + sourceTree = ""; + }; + 65797513172D722A000DDA7A /* spidermonkey-ios */ = { + isa = PBXGroup; + children = ( + 65797514172D722A000DDA7A /* include */, + 65797551172D722A000DDA7A /* lib */, + ); + name = "spidermonkey-ios"; + path = "../../scripting/javascript/spidermonkey-ios"; + sourceTree = ""; + }; + 65797514172D722A000DDA7A /* include */ = { + isa = PBXGroup; + children = ( + 65797515172D722A000DDA7A /* ds */, + 65797517172D722A000DDA7A /* gc */, + 65797519172D722A000DDA7A /* js */, + 65797521172D722A000DDA7A /* js-config.h */, + 65797522172D722A000DDA7A /* js.msg */, + 65797523172D722A000DDA7A /* jsalloc.h */, + 65797524172D722A000DDA7A /* jsapi.h */, + 65797525172D722A000DDA7A /* jsclass.h */, + 65797526172D722A000DDA7A /* jsclist.h */, + 65797527172D722A000DDA7A /* jscpucfg.h */, + 65797528172D722A000DDA7A /* jsdbgapi.h */, + 65797529172D722A000DDA7A /* jsdhash.h */, + 6579752A172D722A000DDA7A /* jsfriendapi.h */, + 6579752B172D722A000DDA7A /* jslock.h */, + 6579752C172D722A000DDA7A /* json.h */, + 6579752D172D722A000DDA7A /* jsperf.h */, + 6579752E172D722A000DDA7A /* jsprf.h */, + 6579752F172D722A000DDA7A /* jsprototypes.h */, + 65797530172D722A000DDA7A /* jsproxy.h */, + 65797531172D722A000DDA7A /* jsprvtd.h */, + 65797532172D722A000DDA7A /* jspubtd.h */, + 65797533172D722A000DDA7A /* jstypes.h */, + 65797534172D722A000DDA7A /* jsutil.h */, + 65797535172D722A000DDA7A /* jsval.h */, + 65797536172D722A000DDA7A /* jsversion.h */, + 65797537172D722A000DDA7A /* jswrapper.h */, + 65797538172D722A000DDA7A /* mozilla */, + ); + path = include; + sourceTree = ""; + }; + 65797515172D722A000DDA7A /* ds */ = { + isa = PBXGroup; + children = ( + 65797516172D722A000DDA7A /* BitArray.h */, + ); + path = ds; + sourceTree = ""; + }; + 65797517172D722A000DDA7A /* gc */ = { + isa = PBXGroup; + children = ( + 65797518172D722A000DDA7A /* Root.h */, + ); + path = gc; + sourceTree = ""; + }; + 65797519172D722A000DDA7A /* js */ = { + isa = PBXGroup; + children = ( + 6579751A172D722A000DDA7A /* HashTable.h */, + 6579751B172D722A000DDA7A /* HeapAPI.h */, + 6579751C172D722A000DDA7A /* LegacyIntTypes.h */, + 6579751D172D722A000DDA7A /* MemoryMetrics.h */, + 6579751E172D722A000DDA7A /* TemplateLib.h */, + 6579751F172D722A000DDA7A /* Utility.h */, + 65797520172D722A000DDA7A /* Vector.h */, + ); + path = js; + sourceTree = ""; + }; + 65797538172D722A000DDA7A /* mozilla */ = { + isa = PBXGroup; + children = ( + 65797539172D722A000DDA7A /* Assertions.h */, + 6579753A172D722A000DDA7A /* Attributes.h */, + 6579753B172D722A000DDA7A /* BloomFilter.h */, + 6579753C172D722A000DDA7A /* CheckedInt.h */, + 6579753D172D722A000DDA7A /* Constants.h */, + 6579753E172D722A000DDA7A /* EnumSet.h */, + 6579753F172D722A000DDA7A /* FloatingPoint.h */, + 65797540172D722A000DDA7A /* GuardObjects.h */, + 65797541172D722A000DDA7A /* HashFunctions.h */, + 65797542172D722A000DDA7A /* Likely.h */, + 65797543172D722A000DDA7A /* LinkedList.h */, + 65797544172D722A000DDA7A /* MathAlgorithms.h */, + 65797545172D722A000DDA7A /* MSStdInt.h */, + 65797546172D722A000DDA7A /* NullPtr.h */, + 65797547172D722A000DDA7A /* RangedPtr.h */, + 65797548172D722A000DDA7A /* RefPtr.h */, + 65797549172D722A000DDA7A /* Scoped.h */, + 6579754A172D722A000DDA7A /* SHA1.h */, + 6579754B172D722A000DDA7A /* StandardInteger.h */, + 6579754C172D722A000DDA7A /* ThreadLocal.h */, + 6579754D172D722A000DDA7A /* Types.h */, + 6579754E172D722A000DDA7A /* TypeTraits.h */, + 6579754F172D722A000DDA7A /* Util.h */, + 65797550172D722A000DDA7A /* WeakPtr.h */, + ); + path = mozilla; + sourceTree = ""; + }; + 65797551172D722A000DDA7A /* lib */ = { + isa = PBXGroup; + children = ( + 65797552172D722A000DDA7A /* libjs_static.a */, + ); + path = lib; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 65797506172D6EBE000DDA7A /* jsbcc */ = { + isa = PBXNativeTarget; + buildConfigurationList = 65797510172D6EBE000DDA7A /* Build configuration list for PBXNativeTarget "jsbcc" */; + buildPhases = ( + 65797503172D6EBE000DDA7A /* Sources */, + 65797504172D6EBE000DDA7A /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = jsbcc; + productName = jsbcc; + productReference = 65797507172D6EBE000DDA7A /* jsbcc */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 657974FF172D6EBD000DDA7A /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0460; + ORGANIZATIONNAME = sunzhuoshi; + }; + buildConfigurationList = 65797502172D6EBD000DDA7A /* Build configuration list for PBXProject "jsbcc" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 657974FE172D6EBD000DDA7A; + productRefGroup = 65797508172D6EBE000DDA7A /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 65797506172D6EBE000DDA7A /* jsbcc */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 65797503172D6EBE000DDA7A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 6579750B172D6EBE000DDA7A /* main.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 6579750E172D6EBE000DDA7A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.7; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 6579750F172D6EBE000DDA7A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.7; + SDKROOT = macosx; + }; + name = Release; + }; + 65797511172D6EBE000DDA7A /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + HEADER_SEARCH_PATHS = ""; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(SRCROOT)/../../scripting/javascript/spidermonkey-ios/lib\"", + ); + OTHER_LDFLAGS = "-lz"; + PRODUCT_NAME = "$(TARGET_NAME)"; + USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../../scripting/javascript/spidermonkey-ios/include\""; + }; + name = Debug; + }; + 65797512172D6EBE000DDA7A /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + HEADER_SEARCH_PATHS = ""; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(SRCROOT)/../../scripting/javascript/spidermonkey-ios/lib\"", + ); + OTHER_LDFLAGS = "-lz"; + PRODUCT_NAME = "$(TARGET_NAME)"; + USER_HEADER_SEARCH_PATHS = "\"$(SRCROOT)/../../scripting/javascript/spidermonkey-ios/include\""; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 65797502172D6EBD000DDA7A /* Build configuration list for PBXProject "jsbcc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 6579750E172D6EBE000DDA7A /* Debug */, + 6579750F172D6EBE000DDA7A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 65797510172D6EBE000DDA7A /* Build configuration list for PBXNativeTarget "jsbcc" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 65797511172D6EBE000DDA7A /* Debug */, + 65797512172D6EBE000DDA7A /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 657974FF172D6EBD000DDA7A /* Project object */; +} diff --git a/tools/jsbcc/src/jsbcc.sh b/tools/jsbcc/src/jsbcc.sh new file mode 100755 index 0000000000..2e07c01459 --- /dev/null +++ b/tools/jsbcc/src/jsbcc.sh @@ -0,0 +1,46 @@ +#/bin/bash +USAGE="Usage: $0 dir" +sh_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +old_pwd=$PWD +if [ $# -lt 1 ] +then + echo $USAGE + exit 1 +fi +if [ ! -d $1 ] +then + echo "Invalid directory: $1" + exit 1 +fi +script_dir=$1 +ok_count=0 +error_count=0 +# check shell directory first +cmd="$sh_dir/jsbcc" +if [ ! -f $cmd ] +then +# not found in shell directory, check global + type "jsbcc" + if [ 0 -eq $? ] + then + cmd="jsbcc" + else + echo "jsbcc execuable NOT found" + exit 1 + fi +fi +cd $script_dir +for file in *.js +do + [ -f "$file" ] || continue + $cmd $file + ret=$? + if [ "$ret" == 0 ] + then + ok_count=`expr $ok_count + 1` + else + error_count=`expr $error_count + 1` + fi +done +cd $old_pwd +echo "$ok_count file(s) compiled, $error_count file(s) failed" diff --git a/tools/jsbcc/src/main.cpp b/tools/jsbcc/src/main.cpp new file mode 100644 index 0000000000..9c5913a857 --- /dev/null +++ b/tools/jsbcc/src/main.cpp @@ -0,0 +1,115 @@ +// +// main.cpp +// jsbcc +// +// Created by sun zhuoshi on 4/28/13. +// Copyright (c) 2013 sunzhuoshi. All rights reserved. +// + +#include +#include + +#include +#include +#include + +#include "jsapi.h" + +const char *USAGE = "Usage: jsbcc input_js_file [byte_code_file]\n"\ + " Pipe supported"; +const char *BYTE_CODE_FILE_SUFFIX = ".bc"; + +enum ErrorCode { + EC_OK = 0, + EC_ERROR = 1 +}; + +void Finalize(JSFreeOp *freeOp, JSObject *obj) { +} + +void ReportError(JSContext *cx, const char *message, JSErrorReport *report) { + std::cerr << "Error! " << message << std::endl; +} + +JSClass GlobalClass = { + "global", JSCLASS_GLOBAL_FLAGS, + JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, + JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize, + JSCLASS_NO_OPTIONAL_MEMBERS +}; + +bool WriteFile(const std::string &filePath, void *data, uint32_t length) { + FILE *file = fopen(filePath.c_str(), "wb"); + if (file) { + size_t ret = fwrite(data, 1, length, file); + fclose(file); + if (ret == length) { + return true; + } + } + return false; +} + +int main(int argc, const char * argv[]) +{ + std::string inputFilePath; + std::ostringstream outputFilePath; + if (1 == argc) { + fd_set fds; + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 500; // wait pipe data timeout + FD_ZERO (&fds); + FD_SET (STDIN_FILENO, &fds); + int result = select (STDIN_FILENO + 1, &fds, NULL, NULL, &tv); + if (result) { // STDIN ready to read + std::getline(std::cin, inputFilePath); + } + if (inputFilePath.empty()) { + std::cerr << USAGE << std::endl; + return EC_ERROR; + } + } + else if (1 < argc) { + inputFilePath = argv[1]; + } + if (2 < argc) { + outputFilePath << argv[2]; + } + else { + outputFilePath << inputFilePath << BYTE_CODE_FILE_SUFFIX; + } + std::cout << "Input file: " << inputFilePath << std::endl; + JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS); + JSContext *context = JS_NewContext(runtime, 10240); + JS_SetOptions(context, JSOPTION_TYPE_INFERENCE); + JS_SetVersion(context, JSVERSION_LATEST); + JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT); + JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT_ALWAYS); + JSObject* global = JS_NewGlobalObject(context, &GlobalClass, NULL); + JS_SetErrorReporter(context, &ReportError); + if (!JS_InitStandardClasses(context, global)) { + return EC_ERROR; + } + JS::CompileOptions options(context); + options.setSourcePolicy(JS::CompileOptions::NO_SOURCE); + js::RootedObject rootedObject(context, global); + std::cout << "Compiling ..." << std::endl; + JSScript *script = JS::Compile(context, rootedObject, options, inputFilePath.c_str()); + if (!script) { + return EC_ERROR; + } + void *data = NULL; + uint32_t length = 0; + std::cout << "Encoding ..." << std::endl; + data = JS_EncodeScript(context, script, &length); + if (!data) { + return EC_ERROR; + } + if (WriteFile(outputFilePath.str(), data, length)) { + std::cout << "Done! " << "Output file: " << outputFilePath.str() << std::endl; + return EC_OK; + } + return EC_ERROR; +} + From 17f16e16fc95c13f93560c1007d571f5e58f1358 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Tue, 30 Apr 2013 14:03:54 +0800 Subject: [PATCH 02/11] Added README --- tools/jsbcc/README | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tools/jsbcc/README diff --git a/tools/jsbcc/README b/tools/jsbcc/README new file mode 100644 index 0000000000..8145ebde8b --- /dev/null +++ b/tools/jsbcc/README @@ -0,0 +1,7 @@ +jsbcc stands for JavasSript Byte Code Compiler. +it compiles javascript files to SpiderMonkey byte code files +Usage: + jsbcc input_js_file [byte_code_file] + +Note: it also supports pipeline, so you can use it like this: + ls *.js | jsbcc From 29d37ad4ec5a21d558a061caf9555e7c342bf6e1 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Tue, 30 Apr 2013 17:18:23 +0800 Subject: [PATCH 03/11] Fixed issue when used via pipe --- tools/jsbcc/src/main.cpp | 118 +++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 47 deletions(-) diff --git a/tools/jsbcc/src/main.cpp b/tools/jsbcc/src/main.cpp index 9c5913a857..9c3f8916f8 100644 --- a/tools/jsbcc/src/main.cpp +++ b/tools/jsbcc/src/main.cpp @@ -49,35 +49,14 @@ bool WriteFile(const std::string &filePath, void *data, uint32_t length) { } return false; } - -int main(int argc, const char * argv[]) -{ - std::string inputFilePath; - std::ostringstream outputFilePath; - if (1 == argc) { - fd_set fds; - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500; // wait pipe data timeout - FD_ZERO (&fds); - FD_SET (STDIN_FILENO, &fds); - int result = select (STDIN_FILENO + 1, &fds, NULL, NULL, &tv); - if (result) { // STDIN ready to read - std::getline(std::cin, inputFilePath); - } - if (inputFilePath.empty()) { - std::cerr << USAGE << std::endl; - return EC_ERROR; - } - } - else if (1 < argc) { - inputFilePath = argv[1]; - } - if (2 < argc) { - outputFilePath << argv[2]; +bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) { + bool result = false; + std::string ofp; + if (!outputFilePath.empty()) { + ofp = outputFilePath; } else { - outputFilePath << inputFilePath << BYTE_CODE_FILE_SUFFIX; + ofp = inputFilePath + BYTE_CODE_FILE_SUFFIX; } std::cout << "Input file: " << inputFilePath << std::endl; JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS); @@ -88,28 +67,73 @@ int main(int argc, const char * argv[]) JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT_ALWAYS); JSObject* global = JS_NewGlobalObject(context, &GlobalClass, NULL); JS_SetErrorReporter(context, &ReportError); - if (!JS_InitStandardClasses(context, global)) { - return EC_ERROR; + if (JS_InitStandardClasses(context, global)) { + JS::CompileOptions options(context); + options.setSourcePolicy(JS::CompileOptions::NO_SOURCE); + js::RootedObject rootedObject(context, global); + std::cout << "Compiling ..." << std::endl; + JSScript *script = JS::Compile(context, rootedObject, options, inputFilePath.c_str()); + if (script) { + void *data = NULL; + uint32_t length = 0; + std::cout << "Encoding ..." << std::endl; + data = JS_EncodeScript(context, script, &length); + if (data) { + if (WriteFile(ofp, data, length)) { + std::cout << "Done! " << "Output file: " << ofp << std::endl; + result = true; + } + } + } + } - JS::CompileOptions options(context); - options.setSourcePolicy(JS::CompileOptions::NO_SOURCE); - js::RootedObject rootedObject(context, global); - std::cout << "Compiling ..." << std::endl; - JSScript *script = JS::Compile(context, rootedObject, options, inputFilePath.c_str()); - if (!script) { - return EC_ERROR; +Exit: + if (context) { + JS_DestroyContext(context); + context = NULL; } - void *data = NULL; - uint32_t length = 0; - std::cout << "Encoding ..." << std::endl; - data = JS_EncodeScript(context, script, &length); - if (!data) { - return EC_ERROR; + if (runtime) { + JS_DestroyRuntime(runtime); + runtime = NULL; } - if (WriteFile(outputFilePath.str(), data, length)) { - std::cout << "Done! " << "Output file: " << outputFilePath.str() << std::endl; - return EC_OK; - } - return EC_ERROR; + return result; } +int main(int argc, const char * argv[]) +{ + std::string inputFilePath, outputFilePath; + if (1 == argc) { // no argument or pipe mode + fd_set fds; + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 500; // wait pipe data timeout + FD_ZERO (&fds); + FD_SET (STDIN_FILENO, &fds); + int result = select (STDIN_FILENO + 1, &fds, NULL, NULL, &tv); + if (result) { // STDIN ready to read + std::string line; + while (std::getline(std::cin, line)) { + if (!line.empty()) { + CompileFile(line, ""); + } + } + return EC_OK; + } + else { + std::cerr << USAGE << std::endl; + return EC_ERROR; + } + } + else { + if (1 < argc) { + inputFilePath = argv[1]; + } + if (2 < argc) { + outputFilePath = argv[2]; + } + if (CompileFile(inputFilePath, outputFilePath)) { + return EC_OK; + } + return EC_ERROR; + } +} From 077214c4196f3c9808ab37eb2bd695b140544cbf Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Tue, 30 Apr 2013 20:39:06 +0800 Subject: [PATCH 04/11] Modified to ensure no popup notify when no byte code file --- scripting/javascript/bindings/ScriptingCore.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 86bba8c0d6..fad55b3ddf 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -479,7 +479,10 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c 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); } From de27f1137adc47ec035b5f6c3c4017b5bf60dfdc Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 11:32:23 +0800 Subject: [PATCH 05/11] Changed byte code file ext to ".jsc" Modified loading logic to load js file first --- .../javascript/bindings/ScriptingCore.cpp | 61 +++++++++++-------- tools/jsbcc/src/main.cpp | 15 ++++- 2 files changed, 47 insertions(+), 29 deletions(-) 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); From d4c41703688a54e3ad2fb413b2a02450d81bad67 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 12:02:17 +0800 Subject: [PATCH 06/11] Removed bad comment --- scripting/javascript/bindings/ScriptingCore.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index dd5fc749d6..6426d5c2b2 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -498,8 +498,6 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c 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); From 6e708848532b147ac2fae5907e6e466077e73761 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 15:43:33 +0800 Subject: [PATCH 07/11] Removed redundant code --- scripting/javascript/bindings/ScriptingCore.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 6426d5c2b2..0943966061 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -446,15 +446,6 @@ void ScriptingCore::createGlobalContext() { } } -std::string TransFilePath(const std::string& filePath) { - if (filePath.c_str()[0] == '/') { - return filePath; - } - else { - return cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(filePath.c_str()); - } -} - std::string RemoveFileExt(const std::string& filePath) { size_t pos = filePath.rfind('.'); if (0 < pos) { @@ -471,7 +462,7 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c return false; } cocos2d::CCFileUtils *futil = cocos2d::CCFileUtils::sharedFileUtils(); - std::string rpath = TransFilePath(path); + std::string rpath = futil->fullPathForFilename(path); if (global == NULL) { global = global_; } @@ -496,9 +487,11 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c #endif // b) no js file, check jsc file if (!script) { - std::string byteCodeFilePath = TransFilePath(RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT); + std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT; unsigned long length = 0; - void *data = futil->getFileData(byteCodeFilePath.c_str(), "rb", &length); + void *data = futil->getFileData(futil->fullPathForFilename(byteCodePath.c_str()).c_str(), + "rb", + &length); if (data) { script = JS_DecodeScript(cx, data, length, NULL, NULL); } From cbb782494165021507b57373d7416738f4649a8d Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 16:20:13 +0800 Subject: [PATCH 08/11] Modified jsbcc to use "-p" option to turn on pipe mode --- tools/jsbcc/src/main.cpp | 52 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/tools/jsbcc/src/main.cpp b/tools/jsbcc/src/main.cpp index 5db16b862e..c34f4f45c3 100644 --- a/tools/jsbcc/src/main.cpp +++ b/tools/jsbcc/src/main.cpp @@ -16,7 +16,8 @@ #include "jsapi.h" const char *USAGE = "Usage: jsbcc input_js_file [byte_code_file]\n"\ - " Pipe supported"; + " Or\n"\ + " ls *.js | jsbcc -p"; const char *BYTE_CODE_FILE_EXT = ".jsc"; enum ErrorCode { @@ -113,31 +114,34 @@ Exit: int main(int argc, const char * argv[]) { std::string inputFilePath, outputFilePath; - if (1 == argc) { // no argument or pipe mode - fd_set fds; - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 500; // wait pipe data timeout - FD_ZERO (&fds); - FD_SET (STDIN_FILENO, &fds); - int result = select (STDIN_FILENO + 1, &fds, NULL, NULL, &tv); - if (result) { // STDIN ready to read - std::string line; - while (std::getline(std::cin, line)) { - if (!line.empty()) { - CompileFile(line, ""); - } - } - return EC_OK; - } - else { - std::cerr << USAGE << std::endl; - return EC_ERROR; - } + if (1 == argc) { + std::cerr << USAGE << std::endl; + return EC_ERROR; } else { - if (1 < argc) { - inputFilePath = argv[1]; + if (1 < argc) { + if (std::string(argv[1]) == "-p") { // pipe mode + fd_set fds; + FD_ZERO (&fds); + FD_SET (STDIN_FILENO, &fds); + int result = select (STDIN_FILENO + 1, &fds, NULL, NULL, NULL); // infinite wait + if (result) { // STDIN ready to read + std::string line; + while (std::getline(std::cin, line)) { + if (!line.empty()) { + CompileFile(line, ""); + } + } + return EC_OK; + } + else { + std::cerr << "Failed to read from pipe" << std::endl; + return EC_ERROR; + } + } + else { + inputFilePath = argv[1]; + } } if (2 < argc) { outputFilePath = argv[2]; From 3da5a7d5cdf5d78a74355e6d5a17777369af23d7 Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 18:06:18 +0800 Subject: [PATCH 09/11] Removed redundant code --- scripting/javascript/bindings/ScriptingCore.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 0943966061..556323c791 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -462,7 +462,6 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c return false; } cocos2d::CCFileUtils *futil = cocos2d::CCFileUtils::sharedFileUtils(); - std::string rpath = futil->fullPathForFilename(path); if (global == NULL) { global = global_; } @@ -472,24 +471,24 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c JSScript *script = NULL; js::RootedObject obj(cx, global); JS::CompileOptions options(cx); - options.setUTF8(true).setFileAndLine(rpath.c_str(), 1); + options.setUTF8(true).setFileAndLine(path, 1); // a) check js file first #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - unsigned char *content = (unsigned char*)CCString::createWithContentsOfFile(rpath.c_str())->getCString(); + unsigned char *content = (unsigned char*)CCString::createWithContentsOfFile(path)->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, path); #endif // b) no js file, check jsc file if (!script) { std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT; unsigned long length = 0; - void *data = futil->getFileData(futil->fullPathForFilename(byteCodePath.c_str()).c_str(), + void *data = futil->getFileData(byteCodePath.c_str(), "rb", &length); if (data) { From fa39c41136230d57c2d880e66d3100b3d9481bab Mon Sep 17 00:00:00 2001 From: sunzhuoshi Date: Mon, 6 May 2013 19:06:16 +0800 Subject: [PATCH 10/11] Fixed bad commit --- scripting/javascript/bindings/ScriptingCore.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 556323c791..5fccaf243e 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -446,7 +446,7 @@ void ScriptingCore::createGlobalContext() { } } -std::string RemoveFileExt(const std::string& filePath) { +static std::string RemoveFileExt(const std::string& filePath) { size_t pos = filePath.rfind('.'); if (0 < pos) { return filePath.substr(0, pos); @@ -462,6 +462,7 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c return false; } cocos2d::CCFileUtils *futil = cocos2d::CCFileUtils::sharedFileUtils(); + std::string fullPath = futil->fullPathForFilename(path); if (global == NULL) { global = global_; } @@ -471,7 +472,7 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c JSScript *script = NULL; js::RootedObject obj(cx, global); JS::CompileOptions options(cx); - options.setUTF8(true).setFileAndLine(path, 1); + options.setUTF8(true).setFileAndLine(fullPath.c_str(), 1); // a) check js file first #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) @@ -482,7 +483,7 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c script = JS::Compile(cx, obj, options, (char*)content, strlen((char*)content)); } #else - script = JS::Compile(cx, obj, options, path); + script = JS::Compile(cx, obj, options, fullPath.c_str()); #endif // b) no js file, check jsc file if (!script) { From 9a7c849ce4836d8301d387e16590cc3bd52dffa7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 7 May 2013 10:50:09 +0800 Subject: [PATCH 11/11] fixed #2106: Fixing a bug on android when only jsc files exist. --- scripting/javascript/bindings/ScriptingCore.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index b0ef3563ca..0865547a89 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -475,11 +475,12 @@ JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* c // a) check js file first #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - unsigned char *content = (unsigned char*)CCString::createWithContentsOfFile(path)->getCString(); + CCString* content = CCString::createWithContentsOfFile(path); 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)); + const char* contentCStr = content->getCString(); + script = JS::Compile(cx, obj, options, contentCStr, strlen(contentCStr)); } #else script = JS::Compile(cx, obj, options, fullPath.c_str());