diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 9809871e38..0865547a89 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_FILE_EXT ".jsc" pthread_t debugThread; string inData; @@ -445,45 +445,57 @@ void ScriptingCore::createGlobalContext() { } } +static std::string RemoveFileExt(const std::string& filePath) { + size_t pos = filePath.rfind('.'); + if (0 < pos) { + return filePath.substr(0, pos); + } + else { + return filePath; + } +} + 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 fullPath = futil->fullPathForFilename(path); if (global == NULL) { global = global_; } if (cx == NULL) { cx = cx_; } - + JSScript *script = NULL; js::RootedObject obj(cx, global); JS::CompileOptions options(cx); - options.setUTF8(true).setFileAndLine(rpath.c_str(), 1); + options.setUTF8(true).setFileAndLine(fullPath.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 + // a) check js file first #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); + CCString* content = CCString::createWithContentsOfFile(path); + if (content) { + // Not supported in SpiderMonkey 19.0 + //JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1); + const char* contentCStr = content->getCString(); + script = JS::Compile(cx, obj, options, contentCStr, strlen(contentCStr)); + } #else - // Removed in SpiderMonkey 19.0 - //JSScript* script = JS_CompileUTF8File(cx, global, rpath.c_str()); - JSScript *script = JS::Compile(cx, obj, options, rpath.c_str()); + script = JS::Compile(cx, obj, options, fullPath.c_str()); #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(byteCodePath.c_str(), + "rb", + &length); + if (data) { + script = JS_DecodeScript(cx, data, length, NULL, NULL); + } + } JSBool evaluatedOK = false; if (script) { jsval rval; 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 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..c34f4f45c3 --- /dev/null +++ b/tools/jsbcc/src/main.cpp @@ -0,0 +1,154 @@ +// +// 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"\ + " Or\n"\ + " ls *.js | jsbcc -p"; +const char *BYTE_CODE_FILE_EXT = ".jsc"; + +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; +} + +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; + if (!outputFilePath.empty()) { + ofp = outputFilePath; + } + else { + 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); + 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)) { + 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; + } + } + } + + } +Exit: + if (context) { + JS_DestroyContext(context); + context = NULL; + } + if (runtime) { + JS_DestroyRuntime(runtime); + runtime = NULL; + } + return result; +} + +int main(int argc, const char * argv[]) +{ + std::string inputFilePath, outputFilePath; + if (1 == argc) { + std::cerr << USAGE << std::endl; + return EC_ERROR; + } + else { + 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]; + } + if (CompileFile(inputFilePath, outputFilePath)) { + return EC_OK; + } + return EC_ERROR; + } +}