std::string V8Engine::compileScript(std::string script) { HandleScope handleScope; TryCatch tc; Local<String> source = String::New(script.c_str()); // Compile the source code. Local<Script> code = Script::Compile(source); if (!code.IsEmpty()) return ""; // There were errors, return them std::string ret = ""; Handle<Object> exception = tc.Exception()->ToObject(); String::AsciiValue exception_str(exception); ret += *exception_str; ret += "\n"; Local<Message> message = tc.Message(); ret += *(v8::String::Utf8Value( message->Get() )); ret += "\n"; ret += "Source line: "; ret += *(v8::String::Utf8Value( message->GetSourceLine() )); ret += "\n"; ret += "Source line number: "; ret += Utility::toString(message->GetLineNumber()); ret += "\n"; return ret; }
static void logV8Exception(Local<Message> msg, Local<Value> data) { HandleScope scope(V8Runtime::v8_isolate); // Log reason and location of the error. LOGD(TAG, *v8::String::Utf8Value(msg->Get())); LOGD(TAG, "%s @ %d >>> %s", *v8::String::Utf8Value(msg->GetScriptResourceName()), msg->GetLineNumber(), *v8::String::Utf8Value(msg->GetSourceLine())); }
char *JSMain::GetStackInfo(Isolate *isolate, int *lineNumber) { HandleScope handle_scope(isolate); const char *file = __FILE__; /* Use current filename if we can't find the correct from JS stack */ int line = __LINE__; /* Use current line number if we can't find the correct from JS stack */ char *ret = NULL; /* Try to get the current stack trace (script file) */ Local<StackTrace> stFile = StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kScriptName); if (!stFile.IsEmpty()) { Local<StackFrame> sf = stFile->GetFrame(0); if (!sf.IsEmpty()) { Local<String> fn = sf->GetScriptName(); if (!fn.IsEmpty()) { String::Utf8Value str(fn); if (*str) { js_strdup(ret, *str); // We must dup here } } } } /* dup current filename if we got nothing from stack */ if (ret == NULL) { js_strdup(ret, file); } /* Try to get the current stack trace (line number) */ if (lineNumber) { *lineNumber = 0; Local<StackTrace> stLine = StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kLineNumber); if (!stLine.IsEmpty()) { Local<StackFrame> sf = stLine->GetFrame(0); if (!sf.IsEmpty()) { *lineNumber = sf->GetLineNumber(); } } /* Use current file number if we got nothing from stack */ if (*lineNumber == 0) { *lineNumber = line; } } /* Return dup'ed value - this must be freed by the calling function */ return ret; }
jsvalue JsEngine::ErrorFromV8(TryCatch& trycatch) { jsvalue v; HandleScope scope; Local<Value> exception = trycatch.Exception(); v.type = JSVALUE_TYPE_UNKNOWN_ERROR; v.value.str = 0; v.length = 0; // If this is a managed exception we need to place its ID inside the jsvalue // and set the type JSVALUE_TYPE_MANAGED_ERROR to make sure the CLR side will // throw on it. if (exception->IsObject()) { Local<Object> obj = Local<Object>::Cast(exception); if (obj->InternalFieldCount() == 1) { Local<External> wrap = Local<External>::Cast(obj->GetInternalField(0)); ManagedRef* ref = (ManagedRef*)wrap->Value(); v.type = JSVALUE_TYPE_MANAGED_ERROR; v.length = ref->Id(); return v; } } jserror *error = new jserror(); memset(error, 0, sizeof(jserror)); Local<Message> message = trycatch.Message(); if (!message.IsEmpty()) { error->line = message->GetLineNumber(); error->column = message->GetStartColumn(); error->resource = AnyFromV8(message->GetScriptResourceName()); error->message = AnyFromV8(message->Get()); } if (exception->IsObject()) { Local<Object> obj2 = Local<Object>::Cast(exception); error->type = AnyFromV8(obj2->GetConstructorName()); } error->exception = AnyFromV8(exception); v.type = JSVALUE_TYPE_ERROR; v.value.ptr = error; return v; }
void handleException(TryCatch& tc) { Logging::log(Logging::ERROR, "V8 exception:\r\n"); HandleScope handleScope; Handle<Object> exception = tc.Exception()->ToObject(); String::AsciiValue exception_str(exception); Logging::log(Logging::ERROR, " : %s\r\n", *exception_str); /* Handle<Array> names = exception->GetPropertyNames(); for (unsigned int i = 0; i < names->Length(); i++) { std::string strI = Utility::toString((int)i); Logging::log(Logging::ERROR, " %d : %s : %s\r\n", i, *(v8::String::Utf8Value(names->Get(String::New(strI.c_str()))->ToString())), *(v8::String::Utf8Value(exception->Get(names->Get(String::New(strI.c_str()))->ToString())->ToString())) ); } */ Local<Message> message = tc.Message(); Logging::log(Logging::ERROR, "Message: Get: %s\r\n", *(v8::String::Utf8Value( message->Get() ))); Logging::log(Logging::ERROR, "Message: GetSourceLine: %s\r\n", *(v8::String::Utf8Value( message->GetSourceLine() ))); Logging::log(Logging::ERROR, "Message: GetScriptResourceName: %s\r\n", *(v8::String::Utf8Value( message->GetScriptResourceName()->ToString() ))); Logging::log(Logging::ERROR, "Message: GetLineNumber: %d\r\n", message->GetLineNumber() ); Local<Value> stackTrace = tc.StackTrace(); if (!stackTrace.IsEmpty()) { Logging::log(Logging::ERROR, "Stack trace: %s\r\n", *(v8::String::Utf8Value( stackTrace->ToString() ))); printf("\r\n\r\n^Stack trace^: %s\r\n", *(v8::String::Utf8Value( stackTrace->ToString() ))); } else Logging::log(Logging::ERROR, "No stack trace available in C++ handler (see above for possible in-script stack trace)\r\n"); #ifdef SERVER std::string clientMessage = *(v8::String::Utf8Value( message->Get() )); clientMessage += " - "; clientMessage += *(v8::String::Utf8Value( message->GetSourceLine() )); ServerSystem::fatalMessageToClients(clientMessage); #endif // assert(0); throw ScriptException("Bad!"); }
void DumpJSStack() { Local<StackTrace> st = StackTrace::CurrentStackTrace(25); OutputDebugStringA("\n\n--- Javascript Stack ---\n"); for( int i = 0; i < st->GetFrameCount(); i++ ) { Local<StackFrame> sf = st->GetFrame(i); char szOut[8 * 1024]; String::Utf8Value scriptName(sf->GetScriptName()); String::Utf8Value funcName(sf->GetFunctionName()); sprintf(szOut, "%s (%d,%d):%s\n", *scriptName, sf->GetLineNumber(), sf->GetColumn(), *funcName); OutputDebugStringA(szOut); } }
Handle<Value> LogJs::log(const Arguments& args, Log::WarningLevel level) { HandleScope scope; Context::Scope context_scope(Context::GetCurrent()); if (level >= Log::getInstance().getLevel()) { Local<StackTrace> stack = StackTrace::CurrentStackTrace(1); Local<StackFrame> frame = stack->GetFrame(0); int lineNumber = -1; QString script("<unknown>"); QString functionName("<unknown>"); if (stack->GetFrameCount() >= 1) { lineNumber = frame->GetLineNumber(); script = toString(frame->GetScriptName()); functionName = toString(frame->GetFunctionName()); } std::stringstream rMessage; for (int i = 0; i < args.Length(); i++) { if (i != 0) { rMessage << " "; } rMessage << args[i]; } QString message = QString::fromUtf8(rMessage.str().data()); int logLimit = ConfigOptions().getOgrLogLimit(); int messageCount = getLogCount(message); if (messageCount == logLimit) { message = QString("Received %1 of the same message. Silencing: ").arg(messageCount) + message; } if (messageCount <= logLimit) { Log::getInstance().log(level, message, script, functionName, lineNumber); } } return scope.Close(Undefined()); }
/** * 吉里吉里に対して例外通知 */ void JSEXCEPTION(Isolate* isolate, TryCatch *try_catch) { HandleScope handle_scope(isolate); //HandleScope handle_scope; String::Value exception(try_catch->Exception()); Local<Message> message = try_catch->Message(); if (!message.IsEmpty()) { // 例外表示 String::Value filename(message->GetScriptResourceName()); ttstr msg; msg += *filename; msg += ":"; msg += tTJSVariant(message->GetLineNumber()); msg += ":"; msg += *exception; TVPAddLog(msg); // Print (filename):(line number): (message). String::Value sourceline(message->GetSourceLine()); TVPAddLog(ttstr(*sourceline)); // エラー行表示 ttstr wavy; int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { wavy += " "; } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { wavy +="^"; } TVPAddLog(wavy); // スタックトレース表示 String::Value stack_trace(try_catch->StackTrace()); if (stack_trace.length() > 0) { TVPAddLog(ttstr(*stack_trace)); } } TVPThrowExceptionMessage(*exception); }