MCPcopy Create free account
hub / github.com/9miao/CrossApp / runScript

Method runScript

scripting/javascript/bindings/ScriptingCore.cpp:490–549  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

488}
489
490JSBool ScriptingCore::runScript(const char *path, JSObject* global, JSContext* cx)
491{
492 if (!path) {
493 return false;
494 }
495 CCFileUtils *futil = CCFileUtils::sharedFileUtils();
496 std::string fullPath = futil->fullPathForFilename(path);
497 if (global == NULL) {
498 global = global_;
499 }
500 if (cx == NULL) {
501 cx = cx_;
502 }
503 JSScript *script = NULL;
504 js::RootedObject obj(cx, global);
505 JS::CompileOptions options(cx);
506 options.setUTF8(true).setFileAndLine(fullPath.c_str(), 1);
507
508 // a) check jsc file first
509 std::string byteCodePath = RemoveFileExt(std::string(path)) + BYTE_CODE_FILE_EXT;
510 unsigned long length = 0;
511 unsigned char* data = futil->getFileData(byteCodePath.c_str(),
512 "rb",
513 &length);
514 if (data) {
515 script = JS_DecodeScript(cx, data, length, NULL, NULL);
516 CC_SAFE_DELETE_ARRAY(data);
517 }
518
519 // b) no jsc file, check js file
520 if (!script) {
521 /* Clear any pending exception from previous failed decoding. */
522 ReportException(cx);
523
524#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
525 CCString* content = CCString::createWithContentsOfFile(path);
526 if (content) {
527 // Not supported in SpiderMonkey 19.0
528 //JSScript* script = JS_CompileScript(cx, global, (char*)content, contentSize, path, 1);
529 const char* contentCStr = content->getCString();
530 script = JS::Compile(cx, obj, options, contentCStr, strlen(contentCStr));
531 }
532#else
533
534 script = JS::Compile(cx, obj, options, fullPath.c_str());
535#endif
536 }
537 JSBool evaluatedOK = false;
538 if (script) {
539 jsval rval;
540 filename_script[path] = script;
541 JSAutoCompartment ac(cx, global);
542 evaluatedOK = JS_ExecuteScript(cx, global, script, &rval);
543 if (JS_FALSE == evaluatedOK) {
544 CCLog("(evaluatedOK == JS_FALSE)");
545 JS_ReportPendingException(cx);
546 }
547 }

Callers 1

executeScriptMethod · 0.80

Calls 8

RemoveFileExtFunction · 0.85
ReportExceptionFunction · 0.85
CompileClass · 0.85
stringFunction · 0.50
CCLogFunction · 0.50
fullPathForFilenameMethod · 0.45
c_strMethod · 0.45
getFileDataMethod · 0.45

Tested by

no test coverage detected