stdext::ustring JavaScriptContext::ExecuteReturnString(const stdext::ustring & source, const stdext::ustring & name, stdext::ustring & error) { stdext::ustring resultString; error = wstringToUstring(L""); Locker locker(m_isolate); Isolate::Scope isolate_scope(m_isolate); { Context::Scope contextScope(*m_ctx); HandleScope scope; Local<String> scriptSource = String::New(reinterpret_cast<const uint16_t *>(source.c_str())); Local<String> scriptName = String::New(reinterpret_cast<const uint16_t *>(name.c_str())); Local<Script> script = Script::New(scriptSource, scriptName); Local<Value> result; { TryCatch tryCatch; result = script->Run(); if (!result.IsEmpty()) { String::Value value(result); resultString.append(reinterpret_cast<const char16_t *>(*value)); } if (tryCatch.HasCaught()) { error.append(wstringToUstring(L"Error running script: ")); error.append(name); error.append(wstringToUstring(L" - ")); String::Value stackTrace(tryCatch.StackTrace()); error.append(reinterpret_cast<const char16_t*>(*stackTrace)); } } } return resultString; }
void ScriptGame::_mouseMoved( const float x, const float y ) { HandleScope hs; PersistentContext ctx = MagnetiteCore::Singleton->getScriptManager()->getContext(); Context::Scope scope( ctx ); bool eval = false; if( !mScriptObject.IsEmpty() && mScriptObject->Has( String::New("mouseMoved") ) ) { Local<Value> onLoadVal = mScriptObject->Get( String::New("mouseMoved") ); if( onLoadVal->IsFunction() ) { TryCatch ct; Local<Function> onLoad = Local<Function>::Cast( onLoadVal ); Handle<Value> args[2]; args[0] = Number::New(x); args[1] = Number::New(y); auto r = onLoad->Call( mScriptObject, 2, args ); if( r.IsEmpty() ) { Util::log(strize(ct.StackTrace())); } else { eval = r->BooleanValue(); } } } if(!eval && getLocalPlayer() ) { mPlayer->getCamera()->pitch( y ); mPlayer->getCamera()->yaw( x ); } }
std::string GetException(TryCatch &try_catch, result_t hr) { if (try_catch.HasCaught()) { v8::String::Utf8Value exception(try_catch.Exception()); v8::Local<v8::Message> message = try_catch.Message(); if (message.IsEmpty()) return ToCString(exception); else { v8::Local<v8::Value> trace_value = try_catch.StackTrace(); if (!IsEmpty(trace_value)) { v8::String::Utf8Value stack_trace(trace_value); return ToCString(stack_trace); } std::string strError; v8::String::Utf8Value filename(message->GetScriptResourceName()); if (qstrcmp(ToCString(exception), "SyntaxError: ", 13)) { strError.append(ToCString(exception)); strError.append("\n at "); } else { strError.append((ToCString(exception) + 13)); strError.append("\n at "); } strError.append(ToCString(filename)); int lineNumber = message->GetLineNumber(); if (lineNumber > 0) { char numStr[32]; strError.append(1, ':'); sprintf(numStr, "%d", lineNumber); strError.append(numStr); strError.append(1, ':'); sprintf(numStr, "%d", message->GetStartColumn() + 1); strError.append(numStr); } return strError; } } else if (hr < 0) return getResultMessage(hr); return ""; }
void V8Util::reportException(TryCatch &tryCatch, bool showLine) { HandleScope scope; Handle<Message> message = tryCatch.Message(); if (nameSymbol.IsEmpty()) { nameSymbol = SYMBOL_LITERAL("name"); messageSymbol = SYMBOL_LITERAL("message"); } if (showLine) { Handle<Message> message = tryCatch.Message(); if (!message.IsEmpty()) { String::Utf8Value filename(message->GetScriptResourceName()); String::Utf8Value msg(message->Get()); int linenum = message->GetLineNumber(); LOGE(EXC_TAG, "Exception occurred at %s:%i: %s", *filename, linenum, *msg); } } Local<Value> stackTrace = tryCatch.StackTrace(); String::Utf8Value trace(tryCatch.StackTrace()); if (trace.length() > 0 && !stackTrace->IsUndefined()) { LOGD(EXC_TAG, *trace); } else { Local<Value> exception = tryCatch.Exception(); if (exception->IsObject()) { Handle<Object> exceptionObj = exception->ToObject(); Handle<Value> message = exceptionObj->Get(messageSymbol); Handle<Value> name = exceptionObj->Get(nameSymbol); if (!message->IsUndefined() && !name->IsUndefined()) { String::Utf8Value nameValue(name); String::Utf8Value messageValue(message); LOGE(EXC_TAG, "%s: %s", *nameValue, *messageValue); } } else { String::Utf8Value error(exception); LOGE(EXC_TAG, *error); } } }
void ReportException(TryCatch &try_catch, bool show_line, std::string& err_msg) { HandleScope scope; if (show_line) DisplayExceptionLine(try_catch, err_msg); String::Utf8Value trace(try_catch.StackTrace()); // range errors have a trace member set to undefined if (trace.length() > 0 && !try_catch.StackTrace()->IsUndefined()) { fprintf(stderr, "%s\n", *trace); err_msg += *trace; err_msg += "\n"; } else { // this really only happens for RangeErrors, since they're the only // kind that won't have all this info in the trace, or when non-Error // objects are thrown manually. Local<Value> er = try_catch.Exception(); bool isErrorObject = er->IsObject() && !(er->ToObject()->Get(String::New("message"))->IsUndefined()) && !(er->ToObject()->Get(String::New("name"))->IsUndefined()); if (isErrorObject) { String::Utf8Value name(er->ToObject()->Get(String::New("name"))); fprintf(stderr, "%s: ", *name); err_msg += *name; err_msg += ": "; } String::Utf8Value msg(!isErrorObject ? er : er->ToObject()->Get(String::New("message"))); fprintf(stderr, "%s\n", *msg); err_msg += *msg; err_msg += "\n"; } fflush(stderr); }
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!"); }
Local<String> V8EngineProxy::GetErrorMessage(TryCatch &tryCatch) { auto msg = tryCatch.Exception()->ToString(); auto stack = tryCatch.StackTrace(); bool showStackMsg = !stack.IsEmpty() && !stack->IsUndefined(); Local<String> stackStr; if (showStackMsg) { stackStr = stack->ToString(); // ... detect if the start of the stack message is the same as the exception message, then remove it (seems to happen when managed side returns an error) ... if (stackStr->Length() >= msg->Length()) { uint16_t* ss = new uint16_t[stackStr->Length() + 1]; stack->ToString()->Write(ss); auto subStackStr = NewSizedUString(ss, msg->Length()); auto stackPartStr = NewSizedUString(ss + msg->Length(), stackStr->Length() - msg->Length()); delete[] ss; if (msg->Equals(subStackStr)) stackStr = stackPartStr; } } msg = msg->Concat(msg, NewString("\r\n")); msg = msg->Concat(msg, NewString(" Line: ")); auto line = NewInteger(tryCatch.Message()->GetLineNumber())->ToString(); msg = msg->Concat(msg, line); msg = msg->Concat(msg, NewString(" Column: ")); auto col = NewInteger(tryCatch.Message()->GetStartColumn())->ToString(); msg = msg->Concat(msg, col); msg = msg->Concat(msg, NewString("\r\n")); if (showStackMsg) { msg = msg->Concat(msg, NewString(" Stack: ")); msg = msg->Concat(msg, stackStr); msg = msg->Concat(msg, NewString("\r\n")); } return msg; }
void ScriptGame::uiPaint(Renderer* r) { HandleScope hs; PersistentContext ctx = MagnetiteCore::Singleton->getScriptManager()->getContext(); Context::Scope scope( ctx ); if( !mScriptObject.IsEmpty() && mScriptObject->Has( String::New("draw") ) ) { Local<Value> onLoadVal = mScriptObject->Get( String::New("draw") ); if( onLoadVal->IsFunction() ) { TryCatch ct; Local<Function> onLoad = Local<Function>::Cast( onLoadVal ); auto r = onLoad->Call( mScriptObject, 0, NULL ); if( r.IsEmpty() ) { Util::log(strize(ct.StackTrace())); } } } }
void ScriptGame::think( float dt ) { HandleScope hs; PersistentContext ctx = MagnetiteCore::Singleton->getScriptManager()->getContext(); Context::Scope scope( ctx ); if( !mScriptObject.IsEmpty() && mScriptObject->Has( String::New("think") ) ) { Local<Value> onLoadVal = mScriptObject->Get( String::New("think") ); if( onLoadVal->IsFunction() ) { TryCatch ct; Local<Function> onLoad = Local<Function>::Cast( onLoadVal ); Handle<Value> args[1]; args[0] = Number::New( dt ); auto r = onLoad->Call( mScriptObject, 1, args ); if( r.IsEmpty() ) { Util::log(strize(ct.StackTrace())); } } } }
//rtError rtNodeContext::runScript(const std::string &script, rtValue* retVal /*= NULL*/, const char* /* args = NULL*/) rtError rtNodeContext::runScript(const char* script, rtValue* retVal /*= NULL*/, const char *args /*= NULL*/) { rtLogDebug(__FUNCTION__); if(!script || strlen(script) == 0) { rtLogError(" %s ... no script given.",__PRETTY_FUNCTION__); return RT_FAIL; } {//scope Locker locker(mIsolate); Isolate::Scope isolate_scope(mIsolate); HandleScope handle_scope(mIsolate); // Create a stack-allocated handle scope. // Get a Local context... Local<Context> local_context = node::PersistentToLocal<Context>(mIsolate, mContext); Context::Scope context_scope(local_context); // !CLF TODO: TEST FOR MT #ifdef RUNINMAIN #ifdef ENABLE_NODE_V_6_9 TryCatch tryCatch(mIsolate); #else TryCatch tryCatch; #endif // ENABLE_NODE_V_6_9 #endif Local<String> source = String::NewFromUtf8(mIsolate, script); // Compile the source code. Local<Script> run_script = Script::Compile(source); // Run the script to get the result. Local<Value> result = run_script->Run(); // !CLF TODO: TEST FOR MT #ifdef RUNINMAIN if (tryCatch.HasCaught()) { String::Utf8Value trace(tryCatch.StackTrace()); rtLogWarn("%s", *trace); return RT_FAIL; } #endif if(retVal) { // Return val rtWrapperError error; *retVal = js2rt(local_context, result, &error); if(error.hasError()) { rtLogError("js2rt() - return from script error"); return RT_FAIL; } } return RT_OK; }//scope return RT_FAIL; }
//Report the error from an exception, store it in lastError void BeaContext::reportError(TryCatch& try_catch){ lastError = *v8::String::Utf8Value(try_catch.Exception()); if (m_logger) m_logger(*v8::String::Utf8Value(try_catch.StackTrace())); }