v8::Local<v8::Value> V8Proxy::evaluate(const ScriptSourceCode& source, Node* node) { ASSERT(v8::Context::InContext()); v8::Local<v8::Value> result; { // Isolate exceptions that occur when compiling and executing // the code. These exceptions should not interfere with // javascript code we might evaluate from C++ when returning // from here. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); // Compile the script. v8::Local<v8::String> code = v8ExternalString(source.source()); ChromiumBridge::traceEventBegin("v8.compile", node, ""); // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at // 1, whereas v8 starts at 0. v8::Handle<v8::Script> script = compileScript(code, source.url(), source.startLine() - 1); ChromiumBridge::traceEventEnd("v8.compile", node, ""); ChromiumBridge::traceEventBegin("v8.run", node, ""); // Set inlineCode to true for <a href="javascript:doSomething()"> // and false for <script>doSomething</script>. We make a rough guess at // this based on whether the script source has a URL. result = runScript(script, source.url().string().isNull()); } ChromiumBridge::traceEventEnd("v8.run", node, ""); return result; }
v8::MaybeLocal<v8::Script> V8ScriptRunner::compileScript(const String& code, const String& fileName, const String& sourceMapUrl, const TextPosition& textPosition, v8::Isolate* isolate, CachedMetadataHandler* cacheMetadataHandler, AccessControlStatus accessControlStatus, V8CacheOptions v8CacheOptions) { if (code.length() >= v8::String::kMaxLength) { V8ThrowException::throwGeneralError(isolate, "Source file too large."); return v8::Local<v8::Script>(); } return compileScript(v8String(isolate, code), fileName, sourceMapUrl, textPosition, isolate, nullptr, nullptr, cacheMetadataHandler, accessControlStatus, v8CacheOptions); }
v8::MaybeLocal<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions) { if (source.source().length() >= v8::String::kMaxLength) { V8ThrowException::throwGeneralError(isolate, "Source file too large."); return v8::Local<v8::Script>(); } return compileScript(v8String(isolate, source.source()), source.url(), source.sourceMapUrl(), source.startPosition(), isolate, source.resource(), source.streamer(), source.resource() ? source.resource()->cacheHandler() : nullptr, corsStatus, cacheOptions); }
v8::Local<v8::Value> V8Proxy::runScriptInternal(v8::Handle<v8::Script> script, bool isInlineCode) #endif { if (script.IsEmpty()) return notHandledByInterceptor(); V8GCController::checkMemoryUsage(); // Compute the source string and prevent against infinite recursion. if (m_recursion >= kMaxRecursionDepth) { v8::Local<v8::String> code = v8ExternalString("throw RangeError('Recursion too deep')"); // FIXME: Ideally, we should be able to re-use the origin of the // script passed to us as the argument instead of using an empty string // and 0 baseLine. script = compileScript(code, "", 0); } if (handleOutOfMemory()) ASSERT(script.IsEmpty()); if (script.IsEmpty()) return notHandledByInterceptor(); // Save the previous value of the inlineCode flag and update the flag for // the duration of the script invocation. bool previousInlineCode = inlineCode(); setInlineCode(isInlineCode); // Run the script and keep track of the current recursion depth. v8::Local<v8::Value> result; { V8ConsoleMessage::Scope scope; // See comment in V8Proxy::callFunction. m_frame->keepAlive(); m_recursion++; result = script->Run(); m_recursion--; } // Release the storage mutex if applicable. releaseStorageMutex(); if (handleOutOfMemory()) ASSERT(result.IsEmpty()); // Handle V8 internal error situation (Out-of-memory). if (result.IsEmpty()) return notHandledByInterceptor(); // Restore inlineCode flag. setInlineCode(previousInlineCode); if (v8::V8::IsDead()) handleFatalErrorInV8(); return result; }
bool startDialog::compile() { std::string script_root = getPathFromConfig("script_root"); /* if (!nd_absolute_filename(script_root.c_str(), tmpbuf, sizeof(tmpbuf))) { nd_logerror("can not found file %s\n", script_root.c_str()); return false; } char tmpbuf[ND_FILE_PATH_SIZE]; std::string absPath = tmpbuf; absPath = nd_getpath(absPath.c_str(), tmpbuf, sizeof(tmpbuf)); */ ndxml_root xmlEntry; ndxml_initroot(&xmlEntry); if (-1 == ndxml_load_ex(script_root.c_str(), &xmlEntry, apoEditorSetting::getInstant()->m_encodeName.c_str())) { return false; } ndxml_root *xml = ndxml_getnode(&xmlEntry, "script_file_manager"); if (!xml){ return false; } char projPath[ND_FILE_PATH_SIZE]; nd_getpath(script_root.c_str(), projPath, sizeof(projPath)); WorkingPathSwitchHelper __pathHelper(projPath); bool ret = true; int num = ndxml_num(xml); for (int i = 0; i < num; i++) { ndxml *node = ndxml_getnodei(xml, i); if (!node) continue; std::string scriptPath = ndxml_getval(node); if (scriptPath.size() > 0) { scriptPath += "/"; } scriptPath += ndxml_getattr_val(node, "main_file"); if (!compileScript(scriptPath.c_str(),__pathHelper.workingPath())) { ret = false; break; } } ndxml_destroy(&xmlEntry); return true; }
v8::Local<v8::Value> V8Proxy::evaluate(const ScriptSourceCode& source, Node* node) { ASSERT(v8::Context::InContext()); V8GCController::checkMemoryUsage(); #if ENABLE(INSPECTOR) if (InspectorTimelineAgent* timelineAgent = m_frame->page() ? m_frame->page()->inspectorTimelineAgent() : 0) timelineAgent->willEvaluateScript(source.url().isNull() ? String() : source.url().string(), source.startLine()); #endif v8::Local<v8::Value> result; { // Isolate exceptions that occur when compiling and executing // the code. These exceptions should not interfere with // javascript code we might evaluate from C++ when returning // from here. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); // Compile the script. v8::Local<v8::String> code = v8ExternalString(source.source()); #if PLATFORM(CHROMIUM) PlatformBridge::traceEventBegin("v8.compile", node, ""); #endif OwnPtr<v8::ScriptData> scriptData = precompileScript(code, source.cachedScript()); // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at // 1, whereas v8 starts at 0. v8::Handle<v8::Script> script = compileScript(code, source.url(), source.startLine() - 1, scriptData.get()); #if PLATFORM(CHROMIUM) PlatformBridge::traceEventEnd("v8.compile", node, ""); PlatformBridge::traceEventBegin("v8.run", node, ""); #endif // Set inlineCode to true for <a href="javascript:doSomething()"> // and false for <script>doSomething</script>. We make a rough guess at // this based on whether the script source has a URL. result = runScript(script, source.url().string().isNull()); } #if PLATFORM(CHROMIUM) PlatformBridge::traceEventEnd("v8.run", node, ""); #endif #if ENABLE(INSPECTOR) if (InspectorTimelineAgent* timelineAgent = m_frame->page() ? m_frame->page()->inspectorTimelineAgent() : 0) timelineAgent->didEvaluateScript(); #endif return result; }
v8::Local<v8::Value> V8Proxy::evaluate(const ScriptSourceCode& source, Node* node) { ASSERT(v8::Context::InContext()); V8GCController::checkMemoryUsage(); InspectorInstrumentationCookie cookie = InspectorInstrumentation::willEvaluateScript(m_frame, source.url().isNull() ? String() : source.url().string(), source.startLine()); v8::Local<v8::Value> result; { // Isolate exceptions that occur when compiling and executing // the code. These exceptions should not interfere with // javascript code we might evaluate from C++ when returning // from here. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); // Compile the script. v8::Local<v8::String> code = v8ExternalString(source.source()); #if PLATFORM(CHROMIUM) TRACE_EVENT_BEGIN0("v8", "v8.compile"); #endif OwnPtr<v8::ScriptData> scriptData = precompileScript(code, source.cachedScript()); // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at // 1, whereas v8 starts at 0. v8::Handle<v8::Script> script = compileScript(code, source.url(), source.startPosition(), scriptData.get()); #if PLATFORM(CHROMIUM) TRACE_EVENT_END0("v8", "v8.compile"); TRACE_EVENT0("v8", "v8.run"); #endif result = runScript(script); } InspectorInstrumentation::didEvaluateScript(cookie); return result; }
bool XSAppBuilder::packProject() { if(projectPath.isEmpty()) { return false; } bool ret = false; QDir dir(projectPath); QFileInfoList infoList = dir.entryInfoList(); for(int i = 0; i < infoList.size(); i++) { if(infoList.at(i).isDir()) { QDir jsDir = QDir(infoList.at(i).absoluteFilePath()); QFileInfoList jsInforList = jsDir.entryInfoList(); if(jsDir.dirName() == "scripts") { for(int j = 0; j < jsInforList.size(); j++) { if(jsInforList.at(j).suffix() == "js") { QString prefix = jsDir.dirName() + "_"; ret = compileScript(jsInforList.at(j), prefix); break; } } } continue; } if(infoList.at(i).suffix() == "xpk") { continue; } else if(infoList.at(i).suffix() == "xml" || infoList.at(i).suffix() == "json") { ret = compileObject(infoList.at(i)); } else if(infoList.at(i).suffix() == "js") { ret = compileScript(infoList.at(i), ""); } else { ret = compileBinary(infoList.at(i)); } if(ret == false) { return ret; } } ret = writeObject(); return ret; }
v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus) { return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), isolate, corsStatus); }
/** runs the specified script * \param ident scriptName = name of script to run */ void ScriptEngine::runScript(std::string scriptName) { return; // Scripting disabled for now // TODO: this code seems to fetch the script from disk and compile it on every execution? // A cache should be created. int r; // Compile the script code r = compileScript(m_engine,scriptName); if( r < 0 ) { m_engine->Release(); return; } // Create a context that will execute the script. asIScriptContext *ctx = m_engine->CreateContext(); if( ctx == 0 ) { std::cout << "Failed to create the context." << std::endl; m_engine->Release(); return; } if( r < 0 ) { std::cout << "Failed to set the line callback function." << std::endl; ctx->Release(); m_engine->Release(); return; } // Find the function for the function we want to execute. //This is how you call a normal function with arguments //asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)"); asIScriptFunction *func; if (scriptName =="collisions") { func = Scripting::Physics::registerScriptCallbacks(m_engine); } else if (scriptName == "update") { func = Scripting::Track::registerUpdateScriptCallbacks(m_engine); } else if (scriptName == "start") { func = Scripting::Track::registerStartScriptCallbacks(m_engine); } else { //trigger type can have different names func = Scripting::Track::registerScriptCallbacks(m_engine , scriptName); } if( func == 0 ) { std::cout << "The required function was not found." << std::endl; ctx->Release(); m_engine->Release(); return; } // Prepare the script context with the function we wish to execute. Prepare() // must be called on the context before each new script function that will be // executed. Note, that if you intend to execute the same function several // times, it might be a good idea to store the function returned by // GetFunctionByDecl(), so that this relatively slow call can be skipped. r = ctx->Prepare(func); if( r < 0 ) { std::cout << "Failed to prepare the context." << std::endl; ctx->Release(); m_engine->Release(); return; } // Here, we can pass parameters to the script functions. //ctx->setArgType(index, value); //for example : ctx->SetArgFloat(0, 3.14159265359f); // Execute the function r = ctx->Execute(); if( r != asEXECUTION_FINISHED ) { // The execution didn't finish as we had planned. Determine why. if( r == asEXECUTION_ABORTED ) std::cout << "The script was aborted before it could finish. Probably it timed out." << std::endl; else if( r == asEXECUTION_EXCEPTION ) { std::cout << "The script ended with an exception." << std::endl; // Write some information about the script exception asIScriptFunction *func = ctx->GetExceptionFunction(); std::cout << "func: " << func->GetDeclaration() << std::endl; std::cout << "modl: " << func->GetModuleName() << std::endl; std::cout << "sect: " << func->GetScriptSectionName() << std::endl; std::cout << "line: " << ctx->GetExceptionLineNumber() << std::endl; std::cout << "desc: " << ctx->GetExceptionString() << std::endl; } else std::cout << "The script ended for some unforeseen reason (" << r << ")." << std::endl; } else { // Retrieve the return value from the context here (for scripts that return values) // <type> returnValue = ctx->getReturnType(); for example //float returnValue = ctx->GetReturnFloat(); } // We must release the contexts when no longer using them ctx->Release(); }