Ejemplo n.º 1
0
    std::ostream& operator<<( std::ostream &s, const v8::TryCatch * try_catch ){
        HandleScope handle_scope;
        v8::String::Utf8Value exception(try_catch->Exception());
        Handle<v8::Message> message = try_catch->Message();
    
        if (message.IsEmpty()) {
            s << *exception << endl;
        } 
        else {

            v8::String::Utf8Value filename(message->GetScriptResourceName());
            int linenum = message->GetLineNumber();
            cout << *filename << ":" << linenum << " " << *exception << endl;

            v8::String::Utf8Value sourceline(message->GetSourceLine());
            cout << *sourceline << endl;

            int start = message->GetStartColumn();
            for (int i = 0; i < start; i++)
                cout << " ";

            int end = message->GetEndColumn();
            for (int i = start; i < end; i++)
                cout << "^";

            cout << endl;
        }    

        //if ( try_catch->next_ ) // disabled for v8 bleeding edge
        //    s << try_catch->next_;

        return s;
    }
Ejemplo n.º 2
0
void V8Util::openJSErrorDialog(TryCatch &tryCatch)
{
	JNIEnv *env = JNIUtil::getJNIEnv();
	if (!env) {
		return;
	}

	Handle<Message> message = tryCatch.Message();

	jstring title = env->NewStringUTF("Runtime Error");
	jstring errorMessage = TypeConverter::jsValueToJavaString(message->Get());
	jstring resourceName = TypeConverter::jsValueToJavaString(message->GetScriptResourceName());
	jstring sourceLine = TypeConverter::jsValueToJavaString(message->GetSourceLine());

	env->CallStaticVoidMethod(
		JNIUtil::krollRuntimeClass,
		JNIUtil::krollRuntimeDispatchExceptionMethod,
		title,
		errorMessage,
		resourceName,
		message->GetLineNumber(),
		sourceLine,
		message->GetEndColumn());

	env->DeleteLocalRef(title);
	env->DeleteLocalRef(errorMessage);
	env->DeleteLocalRef(resourceName);
	env->DeleteLocalRef(sourceLine);

}
Ejemplo n.º 3
0
/* Handler for Javascript Exceptions. Not exposed to JavaScript, used internally.
   Most of the code from v8 shell example.
*/
void msV8ReportException(TryCatch* try_catch, const char *msg = "")
{
  HandleScope handle_scope;

  if (!try_catch || !try_catch->HasCaught()) {
    msSetError(MS_V8ERR, "%s.", "msV8ReportException()", msg);
    return;
  }

  String::Utf8Value exception(try_catch->Exception());
  const char* exception_string = *exception;
  Handle<Message> message = try_catch->Message();
  if (message.IsEmpty()) {
    msSetError(MS_V8ERR, "Javascript Exception: %s.", "msV8ReportException()",
               exception_string);
  } else {
    String::Utf8Value filename(message->GetScriptResourceName());
    const char* filename_string = *filename;
    int linenum = message->GetLineNumber();
    msSetError(MS_V8ERR, "Javascript Exception: %s:%i: %s", "msV8ReportException()",
               filename_string, linenum, exception_string);
    String::Utf8Value sourceline(message->GetSourceLine());
    const char* sourceline_string = *sourceline;
    msSetError(MS_V8ERR, "Exception source line: %s", "msV8ReportException()",
               sourceline_string);
    String::Utf8Value stack_trace(try_catch->StackTrace());
    if (stack_trace.length() > 0) {
      const char* stack_trace_string = *stack_trace;
      msSetError(MS_V8ERR, "Exception StackTrace: %s", "msV8ReportException()",
                 stack_trace_string);
    }
  }
}
bool MetadataNode::GetExtendLocation(string& extendLocation)
{
	stringstream extendLocationStream;
	Local<StackTrace> stackTrace = StackTrace::CurrentStackTrace(Isolate::GetCurrent(), 1, StackTrace::kOverview);
	if (!stackTrace.IsEmpty())
	{
		Handle<StackFrame> frame = stackTrace->GetFrame(0);
		if (!frame.IsEmpty())
		{
			auto scriptName = frame->GetScriptName();
			if (scriptName.IsEmpty())
			{
				extendLocationStream << "unkown location";
				extendLocation = extendLocationStream.str();
				return false;
			}

			string srcFileName = ConvertToString(scriptName);

			string hardcodedPathToSkip = Constants::APP_ROOT_FOLDER_PATH;

			int startIndex = hardcodedPathToSkip.length();
			int strToTakeLen = (srcFileName.length() - startIndex - 3); // 3 refers to .js at the end of file name
			string fullPathToFile = srcFileName.substr(startIndex, strToTakeLen);

			std::replace(fullPathToFile.begin(), fullPathToFile.end(), '/', '_');
			std::replace(fullPathToFile.begin(), fullPathToFile.end(), '.', '_');
			int lineNumber = frame->GetLineNumber();
			if (lineNumber < 0)
			{
				extendLocationStream << fullPathToFile.c_str() << " unkown line number";
				extendLocation = extendLocationStream.str();
				return false;
			}

			if (lineNumber > Constants::MODULE_LINES_OFFSET)
			{
				lineNumber -= Constants::MODULE_LINES_OFFSET;
			}

			int column = frame->GetColumn();
			if (column < 0)
			{
				extendLocationStream << fullPathToFile.c_str() << " line:" << lineNumber << " unkown column number";
				extendLocation = extendLocationStream.str();
				return false;
			}


			extendLocationStream << "f" << fullPathToFile.c_str() << "_l" << lineNumber << "_c" << column << "__";
			//DEBUG_WRITE("EXTEND_LOCATION %s", extendLocationStream.str().c_str());
		}
	}

	extendLocation = extendLocationStream.str();
	return true;
}
Ejemplo n.º 5
0
static void logV8Exception(Handle<Message> msg, Handle<Value> data)
{
	HandleScope scope;

	// Log reason and location of the error.
	LOGD(TAG, *String::Utf8Value(msg->Get()));
	LOGD(TAG, "%s @ %d >>> %s",
		*String::Utf8Value(msg->GetScriptResourceName()),
		msg->GetLineNumber(),
		*String::Utf8Value(msg->GetSourceLine()));
}
Ejemplo n.º 6
0
void ReportException(TryCatch* tryCatch)
{
    //	HandleScope handleScope;
    
	String::Utf8Value exception(tryCatch->Exception());
	const char* exceptionString = ToCString(exception);
	Handle<Message> message = tryCatch->Message();
    
	if (message.IsEmpty()) {
		// V8 didn't provide any extra information about this error; just
		// print the exception.
		throw  std::runtime_error("Unknown error "+ std::string(exceptionString));
        
	} else {
		// Print (filename):(line number): (message).
		String::Utf8Value filename(message->GetScriptResourceName());
		const char* filenameString = ToCString(filename);
		int linenum = message->GetLineNumber();
        
        std::stringstream ss;
        
        // Print filename and linenum if applicable
        if(!message->GetScriptResourceName()->IsUndefined()) {
            ss << filenameString <<":"<< linenum <<" ";
        }
        
		ss << exceptionString << std::endl;
        
		// Print line of source code.
		String::Utf8Value sourceline(message->GetSourceLine());
		const char* sourceline_string = ToCString(sourceline);
        
		ss << sourceline_string << std::endl;
        
		// Print wavy underline (GetUnderline is deprecated).
		int start = message->GetStartColumn();
		for (int i = 0; i < start; i++) {
			ss << " ";
		}
		int end = message->GetEndColumn();
		for (int i = start; i < end; i++) {
			ss << "^";
		}
		ss << std::endl;
        
		String::Utf8Value stack_trace(tryCatch->StackTrace());
		if (stack_trace.length() > 0) {
			const char* stack_trace_string = ToCString(stack_trace);
			ss << stack_trace_string;
		}
        
        throw std::runtime_error(ss.str());
	}
}
Ejemplo n.º 7
0
void report_error(TryCatch * err) {
  String::Utf8Value exception(err->Exception());
  String::Utf8Value stack(err->StackTrace());
  Handle<Message> message = err->Message();
  fprintf(stderr, "%s\n", *exception);
  if(! message.IsEmpty()) {
    String::Utf8Value file(message->GetScriptResourceName());
    int line = message->GetLineNumber();
    fprintf(stderr, "%s:%d\n", *file, line);
  }
  if(stack.length() > 0) 
    fprintf(stderr, "%s\n", *stack);
}
void DisplayExceptionLine (TryCatch &try_catch, std::string& err_msg) {
  // Prevent re-entry into this function.
  static bool displayed_error = false;

  if (displayed_error) return;
  displayed_error = true;

  HandleScope scope;

  Handle<Message> message = try_catch.Message();

  fprintf(stderr, "\n");
  err_msg = "JavaScript Exception\n\n";

  if (!message.IsEmpty()) {
    // Print (filename):(line number): (message).
    String::Utf8Value filename(message->GetScriptResourceName());
    const char* filename_string = *filename;
    int linenum = message->GetLineNumber();
    fprintf(stderr, "%s:%i\n", filename_string, linenum);
    err_msg += filename_string;
    err_msg += ":";
    char num_conv[10];
    sprintf(num_conv, "%i", linenum);
    err_msg += num_conv;
    err_msg += "\n";
    // Print line of source code.
    String::Utf8Value sourceline(message->GetSourceLine());
    const char* sourceline_string = *sourceline;

    int start = message->GetStartColumn();
    int end = message->GetEndColumn();

    // fprintf(stderr, "---\nsourceline:%s\noffset:%d\nstart:%d\nend:%d\n---\n", sourceline_string, start, end);

    fprintf(stderr, "%s\n", sourceline_string);
    err_msg += sourceline_string;
    err_msg += "\n";
    // Print wavy underline (GetUnderline is deprecated).
    for (int i = 0; i < start; i++) {
      fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr);
    }
    for (int i = start; i < end; i++) {
      fputc('^', stderr);
    }
    fputc('\n', stderr);
    err_msg += "\n";
  }
}
Ejemplo n.º 9
0
   ////////////////////////////////////////////////////////////////////////////////
   // lifted from node.js file node.cc
   void ReportException(TryCatch *try_catch) 
   {
        
	  HandleScope scope;

      Handle<Message> message = try_catch->Message();
	  std::string filename_string = "";
	  std::string sourceline_string = "";
	  int linenum = 0;
	  int start = 0;
	  int end = 0;

      if (!message.IsEmpty()) {
       
         String::Utf8Value filename(message->GetScriptResourceName());
         filename_string = *filename;
         linenum = message->GetLineNumber();       
         String::Utf8Value sourceline(message->GetSourceLine());
         sourceline_string = *sourceline;
         start = message->GetStartColumn();       
         end = message->GetEndColumn();

      }

     String::Utf8Value trace(try_catch->StackTrace());
     std::string tracestr;

     if(trace.length() != 0)
     {
         tracestr = *trace;
     }
     
	  std::ostringstream os;
     os << std::endl << sourceline_string << std::endl;	  

	  for (int i = 0; i < start; i++) 
	  {
         os << " ";
	  }
	  for (int i = start; i < end; i++) 
	  {
         os << "^";
      }

     os << tracestr << "\n";
	 
	  std::string msg = os.str();
      dtEntity::LogManager::GetInstance().LogMessage(dtEntity::LogLevel::LVL_ERROR, filename_string, "", linenum, msg);     
   }
Ejemplo n.º 10
0
void set_perl_error(const TryCatch& try_catch) {
    Handle<Message> msg = try_catch.Message();

    char message[1024];
    snprintf(
        message,
        1024,
        "%s at %s:%d",
        *(String::Utf8Value(try_catch.Exception())),
        !msg.IsEmpty() ? *(String::AsciiValue(msg->GetScriptResourceName())) : "EVAL",
        !msg.IsEmpty() ? msg->GetLineNumber() : 0
    );

    sv_setpv(ERRSV, message);
    sv_utf8_upgrade(ERRSV);
}
Ejemplo n.º 11
0
std::string stringifyException(const v8::TryCatch& try_catch, bool suppressBacktrace) {
	HandleScope scope;
	std::string output = "";

	Handle<Message> message = try_catch.Message();
	if (suppressBacktrace || message.IsEmpty()) {

		std::string exception_string = V8::stringToStdString(try_catch.Exception().As<String>());

		// V8 didn't provide any extra information about this error; just
		// print the exception.
		output += exception_string;
		output += "\n";

	} else {

		// filename:line
		output += V8::stringToStdString(
			message->GetScriptResourceName().As<String>()
		) + ":" + boost::lexical_cast<std::string>(
			message->GetLineNumber()
		) + "\n";

		// Print line of source code.
		output += V8::stringToStdString(message->GetSourceLine()) + "\n";

		// Print wavy underline (GetUnderline is deprecated).
		int start = message->GetStartColumn();
		for (int i = 0; i < start; i++) {
			output += " ";
		}
		int end = message->GetEndColumn();
		for (int i = start; i < end; i++) {
			output += "^";
		}
		output += "\n";

		// Print out the stack trace.
		std::string stackTrace = V8::stringToStdString(try_catch.StackTrace().As<String>());
		if (stackTrace.length() > 0) {
			output += stackTrace + "\n";
		}
	}

	return output;
}
Ejemplo n.º 12
0
  ~ScriptScopeImpl ()
  {
    GumScriptPrivate * priv = parent->priv;

    if (trycatch.HasCaught ())
    {
      Handle<Message> message = trycatch.Message ();
      Handle<Value> exception = trycatch.Exception ();
      String::Utf8Value exception_str (exception);
      gchar * exception_str_escaped = g_strescape (*exception_str, "");
      gchar * error = g_strdup_printf (
          "{\"type\":\"error\",\"lineNumber\":%d,\"description\":\"%s\"}",
          message->GetLineNumber (), exception_str_escaped);
      _gum_script_core_emit_message (&priv->core, error, NULL, 0);
      g_free (exception_str_escaped);
      g_free (error);
    }
  }
Ejemplo n.º 13
0
const string JSMain::GetExceptionInfo(Isolate* isolate, TryCatch* try_catch)
{
	HandleScope handle_scope(isolate);
	String::Utf8Value exception(try_catch->Exception());
	const char *exception_string = js_safe_str(*exception);
	Handle<Message> message = try_catch->Message();
	string res;

	if (message.IsEmpty()) {
		// V8 didn't provide any extra information about this error; just return the exception.
		res = exception_string;
	} else {
		String::Utf8Value filename(message->GetScriptResourceName());
		const char *filename_string = js_safe_str(*filename);
		int linenum = message->GetLineNumber();

		ostringstream ss;

		ss << filename_string << ":" << linenum << ": " << exception_string << "\r\n";
		
		// Print line of source code.
		String::Utf8Value sourceline(message->GetSourceLine());
		const char *sourceline_string = js_safe_str(*sourceline);

		ss << sourceline_string << "\r\n";

		// Print wavy underline.
		int start = message->GetStartColumn();

		for (int i = 0; i < start; i++) {
			ss << " ";
		}

		int end = message->GetEndColumn();

		for (int i = start; i < end; i++) {
			ss << "^";
		}

		res = ss.str();
	}

	return res;
}
Ejemplo n.º 14
0
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);
		}
	}
}
Ejemplo n.º 15
0
void SMJS_Plugin::ReportException(TryCatch* try_catch){
	HandleScope handle_scope(isolate);

	fprintf(stderr, "----------------------------- JAVASCRIPT EXCEPTION -----------------------------");

	String::Utf8Value exception(try_catch->Exception());
	const char* exception_string = ToCString(exception);
	Handle<Message> message = try_catch->Message();
	if (message.IsEmpty()) {
		// V8 didn't provide any extra information about this error; just
		// print the exception.
		fprintf(stderr, "%s\n", exception_string);
	} else {
		// Print (filename):(line number): (message).
		String::Utf8Value filename(message->GetScriptResourceName());
		const char* filename_string = ToCString(filename);
		int linenum = message->GetLineNumber();
		fprintf(stderr, "%s:%d: %s\n", filename_string, linenum, exception_string);
		// Print line of source code.
		String::Utf8Value sourceline(message->GetSourceLine());
		const char* sourceline_string = ToCString(sourceline);
		fprintf(stderr, "%s\n", sourceline_string);
		// Print wavy underline (GetUnderline is deprecated).
		int start = message->GetStartColumn();
		for (int i = 0; i < start; i++) {
			fprintf(stderr, " ");
		}
		int end = message->GetEndColumn();
		for (int i = start; i < end; i++) {
			fprintf(stderr, "^");
		}
		fprintf(stderr, "\n");
		String::Utf8Value stack_trace(try_catch->StackTrace());
		if (stack_trace.length() > 0) {
			const char* stack_trace_string = ToCString(stack_trace);
			fprintf(stderr, "%s\n", stack_trace_string);
		}
	}

	fprintf(stderr, "--------------------------------------------------------------------------------");
}
Ejemplo n.º 16
0
    std::string toSTLString( const v8::TryCatch * try_catch ) {

        stringstream ss;

        //while ( try_catch ){ // disabled for v8 bleeding edge

        v8::String::Utf8Value exception(try_catch->Exception());
        Handle<v8::Message> message = try_catch->Message();

        if (message.IsEmpty()) {
            ss << *exception << endl;
        }
        else {

            v8::String::Utf8Value filename(message->GetScriptResourceName());
            if (*filename) {
                int linenum = message->GetLineNumber();
                ss << *filename << ":" << linenum << " ";
            }
            ss << *exception << endl;

            v8::String::Utf8Value sourceline(message->GetSourceLine());
            ss << *sourceline << endl;

            int start = message->GetStartColumn();
            for (int i = 0; i < start; i++)
                ss << " ";

            int end = message->GetEndColumn();
            for (int i = start; i < end; i++)
                ss << "^";

            ss << endl;
        }

        //try_catch = try_catch->next_;
        //}

        return ss.str();
    }
Ejemplo n.º 17
0
	int ReportError(TryCatch& try_catch){
		cs::String str;
		HandleScope handle_scope;
		v8::String::Value exception(try_catch.Exception());
		const wchar_t* exception_string = ToCString(exception);
		Handle<v8::Message> message = try_catch.Message();
		if (message.IsEmpty()) {
			str.Format(L"%s\n",exception_string);
		} else {
			cs::String buf;
			v8::String::Value filename(message->GetScriptResourceName());
			const wchar_t* filename_string = ToCString(filename);
			int linenum = message->GetLineNumber();
			buf.Format(L"file:\t%s\r\nline:\t%i\r\n%s\r\n\r\n", filename_string, linenum, exception_string);
			str += buf;
			v8::String::Value sourceline(message->GetSourceLine());
			const wchar_t* sourceline_string = ToCString(sourceline);
			buf.Format(L"%s", sourceline_string);
			int start = message->GetStartColumn();
			for (int i = 0; i < start; i++) {
				//str += L" ";
			}
			buf.Insert('^',start);
			buf.Trim();
			str += buf;
			int end = message->GetEndColumn();
			for (int i = start; i < end; i++) {
				//str += L"^";
			}
			/*str += L"\n";
			v8::String::Value stack_trace(try_catch.StackTrace());
			if (stack_trace.length() > 0) {
				const wchar_t* stack_trace_string = ToCString(stack_trace);
				buf.Format(L"%s\n", stack_trace_string);
				str += buf;
			}*/
		}
		return MessageBox(0,str,L"Error",MB_ICONERROR);
	}
Ejemplo n.º 18
0
// Taken from Google's Shell example.
void PrettyPrinter::prettyPrintException(v8::TryCatch *try_catch, std::string *ex_string) {
  ex_string->clear();
  HandleScope handle_scope;
  String::Utf8Value exception(try_catch->Exception());
  const char* exception_string = toCString(exception);
  Handle<Message> message = try_catch->Message();
  if (message.IsEmpty()) {
    ex_string->append(exception_string);
    ex_string->append("\n");
  } else {
    String::Utf8Value filename(message->GetScriptResourceName());
    const char* filename_string = toCString(filename);
    int linenum = message->GetLineNumber();
    ex_string->append(filename_string);
    ex_string->append(":");
    ex_string->append(intToString(linenum));
    ex_string->append(": ");
    ex_string->append(exception_string);
    ex_string->append("\n");
    String::Utf8Value sourceline(message->GetSourceLine());
    const char* sourceline_string = toCString(sourceline);
    ex_string->append(sourceline_string);
    int start = message->GetStartColumn();
    for (int i = 0; i < start; i++) {
      ex_string->append(" ");
    }
    int end = message->GetEndColumn();
    for (int i = start; i < end; i++) {
      ex_string->append("^");
    }
    ex_string->append("\n");
    String::Utf8Value stack_trace(try_catch->StackTrace());
    if (stack_trace.length() > 0) {
      const char* stack_trace_string = toCString(stack_trace);
      ex_string->append(stack_trace_string);
      ex_string->append("\n");
    }
  }
}
Ejemplo n.º 19
0
// Slight modification to an original function found in the V8 sample shell.cc.
void Global::reportException(TryCatch* tryCatch) {
    HandleScope handleScope(fIsolate);
    String::Utf8Value exception(tryCatch->Exception());
    const char* exceptionString = to_cstring(exception);
    Handle<Message> message = tryCatch->Message();
    if (message.IsEmpty()) {
        // V8 didn't provide any extra information about this error; just
        // print the exception.
        fprintf(stderr, "%s\n", exceptionString);
    } else {
        // Print (filename):(line number): (message).
        String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
        const char* filenameString = to_cstring(filename);
        int linenum = message->GetLineNumber();
        fprintf(stderr,
                "%s:%i: %s\n", filenameString, linenum, exceptionString);
        // Print line of source code.
        String::Utf8Value sourceline(message->GetSourceLine());
        const char* sourceLineString = to_cstring(sourceline);
        fprintf(stderr, "%s\n", sourceLineString);
        // Print wavy underline.
        int start = message->GetStartColumn();
        for (int i = 0; i < start; i++) {
            fprintf(stderr, " ");
        }
        int end = message->GetEndColumn();
        for (int i = start; i < end; i++) {
            fprintf(stderr, "^");
        }
        fprintf(stderr, "\n");
        String::Utf8Value stackTrace(tryCatch->StackTrace());
        if (stackTrace.length() > 0) {
            const char* stackTraceString = to_cstring(stackTrace);
            fprintf(stderr, "%s\n", stackTraceString);
        }
    }
}
Ejemplo n.º 20
0
static gboolean
gum_script_create_context (GumScript * self,
                           GError ** error)
{
  GumScriptPrivate * priv = self->priv;

  g_assert (priv->context == NULL);

  {
    Locker locker (priv->isolate);
    Isolate::Scope isolate_scope (priv->isolate);
    HandleScope handle_scope (priv->isolate);

    Handle<ObjectTemplate> global_templ = ObjectTemplate::New ();
    _gum_script_core_init (&priv->core, self, priv->main_context, priv->isolate,
        global_templ);
    _gum_script_memory_init (&priv->memory, &priv->core, global_templ);
    _gum_script_process_init (&priv->process, &priv->core, global_templ);
    _gum_script_thread_init (&priv->thread, &priv->core, global_templ);
    _gum_script_module_init (&priv->module, &priv->core, global_templ);
    _gum_script_file_init (&priv->file, &priv->core, global_templ);
    _gum_script_socket_init (&priv->socket, &priv->core, global_templ);
    _gum_script_interceptor_init (&priv->interceptor, &priv->core,
        global_templ);
    _gum_script_stalker_init (&priv->stalker, &priv->core, global_templ);
    _gum_script_instruction_init (&priv->instruction, &priv->core,
        global_templ);

    Local<Context> context (Context::New (priv->isolate, NULL, global_templ));
    priv->context = new GumPersistent<Context>::type (priv->isolate, context);
    Context::Scope context_scope (context);
    _gum_script_core_realize (&priv->core);
    _gum_script_memory_realize (&priv->memory);
    _gum_script_process_realize (&priv->process);
    _gum_script_thread_realize (&priv->thread);
    _gum_script_module_realize (&priv->module);
    _gum_script_file_realize (&priv->file);
    _gum_script_socket_realize (&priv->socket);
    _gum_script_interceptor_realize (&priv->interceptor);
    _gum_script_stalker_realize (&priv->stalker);
    _gum_script_instruction_realize (&priv->instruction);

    gchar * combined_source = g_strconcat (
#include "gumscript-runtime.h"
        "\n",
        priv->source,
        static_cast<void *> (NULL));
    Local<String> source_value (String::NewFromUtf8 (priv->isolate, combined_source));
    g_free (combined_source);
    TryCatch trycatch;
    Handle<Script> raw_script = Script::Compile (source_value);
    if (raw_script.IsEmpty ())
    {
      Handle<Message> message = trycatch.Message ();
      Handle<Value> exception = trycatch.Exception ();
      String::Utf8Value exception_str (exception);
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Script(line %d): %s",
          message->GetLineNumber () - GUM_SCRIPT_RUNTIME_SOURCE_LINE_COUNT,
          *exception_str);
    }
    else
    {
      priv->raw_script =
          new GumPersistent<Script>::type (priv->isolate, raw_script);
    }
  }

  if (priv->raw_script == NULL)
  {
    gum_script_destroy_context (self);
    return FALSE;
  }

  return TRUE;
}
Ejemplo n.º 21
0
static void reportException(v8::TryCatch &try_catch, bool show_line)
{
  using namespace v8;
  
  Handle<Message> message = try_catch.Message();

  v8::String::Utf8Value error(try_catch.Exception());
  if (error.length() > 0)
  {

    std::ostringstream errorMsg;

    /*
     02:50:22.863: [CID=00000000] JS: File undefined:12
    [CID=00000000] JS: Source Line   var notheetoo = nothere.subst(0, 10);
    [CID=00000000] JS:
                              ^
    [CID=00000000] JS:
    {TypeError: Cannot call method 'subst' of undefined
        at RouteProfile.isTellMeRoutable (unknown source)
        at RouteProfile.isRoutable (unknown source)
        at handle_request (unknown source)
    }
    */

    if (show_line && !message.IsEmpty())
    {
      // Print (filename):(line number): (message).
      String::Utf8Value filename(message->GetScriptResourceName());
      const char* filename_string = toCString(filename);
      int linenum = message->GetLineNumber();
      //fprintf(stderr, "%s:%i\n", filename_string, linenum);

      errorMsg << filename_string << ":" << linenum << std::endl;
      // Print line of source code.
      String::Utf8Value sourceline(message->GetSourceLine());
      const char* sourceline_string = toCString(sourceline);

      // HACK HACK HACK
      //
      // FIXME
      //
      // Because of how CommonJS modules work, all scripts are wrapped with a
      // "function (function (exports, __filename, ...) {"
      // to provide script local variables.
      //
      // When reporting errors on the first line of a script, this wrapper
      // function is leaked to the user. This HACK is to remove it. The length
      // of the wrapper is 62. That wrapper is defined in src/node.js
      //
      // If that wrapper is ever changed, then this number also has to be
      // updated. Or - someone could clean this up so that the two peices
      // don't need to be changed.
      //
      // Even better would be to get support into V8 for wrappers that
      // shouldn't be reported to users.
      int offset = linenum == 1 ? 62 : 0;

      //fprintf(stderr, "%s\n", sourceline_string + offset);
      errorMsg << sourceline_string + offset << std::endl;
      // Print wavy underline (GetUnderline is deprecated).
      int start = message->GetStartColumn();
      for (int i = offset; i < start; i++)
      {
        errorMsg << " ";
      }
      int end = message->GetEndColumn();
      for (int i = start; i < end; i++)
      {
        errorMsg << "^";
      }
      errorMsg << std::endl;
    }

    String::Utf8Value trace(try_catch.StackTrace());

    if (trace.length() > 0)
    {
      errorMsg << *trace;
    }
    OSS_LOG_ERROR("\t[CID=00000000] JS: " << *error << std::endl << "{" << std::endl << errorMsg.str() << std::endl << "}");
  }
}
Ejemplo n.º 22
0
static gboolean
gum_script_create_context (GumScript * self,
                           GError ** error)
{
  GumScriptPrivate * priv = self->priv;

  g_assert (priv->context == NULL);

  {
    Handle<ObjectTemplate> global_templ = ObjectTemplate::New ();
    _gum_script_core_init (&priv->core, self, gum_script_emit_message,
        gum_script_get_scheduler (), priv->isolate, global_templ);
    _gum_script_memory_init (&priv->memory, &priv->core, global_templ);
    _gum_script_process_init (&priv->process, &priv->core, global_templ);
    _gum_script_thread_init (&priv->thread, &priv->core, global_templ);
    _gum_script_module_init (&priv->module, &priv->core, global_templ);
    _gum_script_file_init (&priv->file, &priv->core, global_templ);
    _gum_script_socket_init (&priv->socket, &priv->core, global_templ);
    _gum_script_interceptor_init (&priv->interceptor, &priv->core,
        global_templ);
    _gum_script_stalker_init (&priv->stalker, &priv->core, global_templ);
    _gum_script_symbol_init (&priv->symbol, &priv->core, global_templ);
    _gum_script_instruction_init (&priv->instruction, &priv->core,
        global_templ);

    Local<Context> context (Context::New (priv->isolate, NULL, global_templ));
    priv->context = new GumPersistent<Context>::type (priv->isolate, context);
    Context::Scope context_scope (context);
    _gum_script_core_realize (&priv->core);
    _gum_script_memory_realize (&priv->memory);
    _gum_script_process_realize (&priv->process);
    _gum_script_thread_realize (&priv->thread);
    _gum_script_module_realize (&priv->module);
    _gum_script_file_realize (&priv->file);
    _gum_script_socket_realize (&priv->socket);
    _gum_script_interceptor_realize (&priv->interceptor);
    _gum_script_stalker_realize (&priv->stalker);
    _gum_script_symbol_realize (&priv->symbol);
    _gum_script_instruction_realize (&priv->instruction);

    gchar * resource_name_str = g_strconcat (priv->name, ".js",
        (gpointer) NULL);
    Local<String> resource_name (String::NewFromUtf8 (priv->isolate,
        resource_name_str));
    ScriptOrigin origin (resource_name);
    g_free (resource_name_str);

    Local<String> source (String::NewFromUtf8 (priv->isolate,
        priv->source));

    TryCatch trycatch;
    MaybeLocal<Script> maybe_code =
        Script::Compile (context, source, &origin);
    Local<Script> code;
    if (maybe_code.ToLocal (&code))
    {
      priv->code =
          new GumPersistent<Script>::type (priv->isolate, code);
    }
    else
    {
      Handle<Message> message = trycatch.Message ();
      Handle<Value> exception = trycatch.Exception ();
      String::Utf8Value exception_str (exception);
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Script(line %d): %s",
          message->GetLineNumber (), *exception_str);
    }
  }

  if (priv->code == NULL)
  {
    gum_script_destroy_context (self);
    return FALSE;
  }

  return TRUE;
}
int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char* javaScript,
                                MESSAGELOOPENTRY messageLoopEntry, void* context)
{
    HandleScope handleScope;
    objectFactory_ = objectFactory;
    globalTemplate_ = ObjectTemplate::New();
    TiV8EventContainerFactory* eventFactory = TiV8EventContainerFactory::createEventContainerFactory(globalTemplate_);
    objectFactory->setEventContainerFactory(eventFactory);
    onSetGetPropertyCallback(&globalTemplate_);
    onSetFunctionCallback(&globalTemplate_);
    g_rootTemplate = ObjectTemplate::New();
    context_ = Context::New(NULL, g_rootTemplate);
    forceSetValue(context_->Global());
    context_->Global()->SetHiddenValue(String::New("globalTemplate_"), External::New(&globalTemplate_));
    context_->Global()->SetHiddenValue(String::New("context_"), External::New(&context_));
    setTiObjectToJsObject(context_->Global(), this);
    Context::Scope context_scope(context_);
    initializeTiObject(NULL);

    const char* bootstrapFilename = "bootstrap.js";
    string bootstrapJavascript;
    {
        ifstream ifs((string("app/native/framework/") + bootstrapFilename).c_str());
        if (!ifs)
        {
            TiLogger::getInstance().log(Ti::Msg::ERROR__Cannot_load_bootstrap_js);
            return -1;
        }
        getline(ifs, bootstrapJavascript, string::traits_type::to_char_type(string::traits_type::eof()));
        ifs.close();
    }

    TryCatch tryCatch;
    Handle<Script> compiledBootstrapScript = Script::Compile(String::New(bootstrapJavascript.c_str()), String::New(bootstrapFilename));
    if (compiledBootstrapScript.IsEmpty())
    {
        String::Utf8Value error(tryCatch.Exception());
        TiLogger::getInstance().log(*error);
        return -1;
    }
    Handle<Value> bootstrapResult = compiledBootstrapScript->Run();
    if (bootstrapResult.IsEmpty())
    {
        Local<Value> exception = tryCatch.Exception();
        // FIXME: need a way to prevent double "filename + line" output
        Handle<Message> msg = tryCatch.Message();
        stringstream ss;
        ss << bootstrapFilename << " line ";
        if (msg.IsEmpty())
        {
            ss << "?";
        }
        else
        {
            ss << msg->GetLineNumber();
        }
        ss << ": " << *String::Utf8Value(exception);
        TiLogger::getInstance().log(ss.str().c_str());
        return -1;
    }

    const char* filename = "app.js";
    Handle<Script> compiledScript = Script::Compile(String::New(javaScript), String::New(filename));
    if (compiledScript.IsEmpty())
    {
        ReportException(tryCatch, true);
        return 1;
    }
    compiledScript->Run();
    if (tryCatch.HasCaught())
    {
        ReportException(tryCatch, true);
        return 1;
    }
    onStartMessagePump();
    return (messageLoopEntry)(context);
}