説明を見る。00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "v8.h"
00030
00031 #include "api.h"
00032 #include "execution.h"
00033 #include "spaces-inl.h"
00034 #include "top.h"
00035
00036 namespace v8 { namespace internal {
00037
00038
00039
00040
00041 void MessageHandler::DefaultMessageReport(const MessageLocation* loc,
00042 Handle<Object> message_obj) {
00043 SmartPointer<char> str = GetLocalizedMessage(message_obj);
00044 if (loc == NULL) {
00045 PrintF("%s\n", *str);
00046 } else {
00047 HandleScope scope;
00048 Handle<Object> data(loc->script()->name());
00049 SmartPointer<char> data_str;
00050 if (data->IsString())
00051 data_str = Handle<String>::cast(data)->ToCString(DISALLOW_NULLS);
00052 PrintF("%s:%i: %s\n", *data_str ? *data_str : "<unknown>",
00053 loc->start_pos(), *str);
00054 }
00055 }
00056
00057
00058 void MessageHandler::ReportMessage(const char* msg) {
00059 PrintF("%s\n", msg);
00060 }
00061
00062
00063 Handle<Object> MessageHandler::MakeMessageObject(
00064 const char* type,
00065 MessageLocation* loc,
00066 Vector< Handle<Object> > args,
00067 Handle<String> stack_trace) {
00068
00069 HandleScope scope;
00070 Handle<Object> type_str = Factory::LookupAsciiSymbol(type);
00071 Handle<Object> array = Factory::NewJSArray(args.length());
00072 for (int i = 0; i < args.length(); i++)
00073 SetElement(Handle<JSArray>::cast(array), i, args[i]);
00074
00075 Handle<JSFunction> fun(Top::global_context()->make_message_fun());
00076 int start, end;
00077 Handle<Object> script;
00078 if (loc) {
00079 start = loc->start_pos();
00080 end = loc->end_pos();
00081 script = GetScriptWrapper(loc->script());
00082 } else {
00083 start = end = 0;
00084 script = Factory::undefined_value();
00085 }
00086 Handle<Object> start_handle(Smi::FromInt(start));
00087 Handle<Object> end_handle(Smi::FromInt(end));
00088 Handle<Object> stack_trace_val = stack_trace.is_null()
00089 ? Factory::undefined_value()
00090 : Handle<Object>::cast(stack_trace);
00091 const int argc = 6;
00092 Object** argv[argc] = { type_str.location(),
00093 array.location(),
00094 start_handle.location(),
00095 end_handle.location(),
00096 script.location(),
00097 stack_trace_val.location() };
00098
00099
00100
00101
00102 v8::TryCatch catcher;
00103 catcher.SetVerbose(false);
00104 catcher.SetCaptureMessage(false);
00105
00106
00107 bool caught_exception = false;
00108 Handle<Object> message =
00109 Execution::Call(fun, Factory::undefined_value(), argc, argv,
00110 &caught_exception);
00111 if (caught_exception) {
00112
00113
00114
00115
00116 if (caught_exception) return Handle<Object>();
00117 }
00118
00119 return message.EscapeFrom(&scope);
00120 }
00121
00122
00123 void MessageHandler::ReportMessage(MessageLocation* loc,
00124 Handle<Object> message) {
00125 v8::Local<v8::Message> api_message_obj = v8::Utils::MessageToLocal(message);
00126
00127 v8::NeanderArray global_listeners(Factory::message_listeners());
00128 int global_length = global_listeners.length();
00129 if (global_length == 0) {
00130 DefaultMessageReport(loc, message);
00131 } else {
00132 for (int i = 0; i < global_length; i++) {
00133 HandleScope scope;
00134 if (global_listeners.get(i)->IsUndefined()) continue;
00135 v8::NeanderObject listener(JSObject::cast(global_listeners.get(i)));
00136 Handle<Proxy> callback_obj(Proxy::cast(listener.get(0)));
00137 v8::MessageCallback callback =
00138 FUNCTION_CAST<v8::MessageCallback>(callback_obj->proxy());
00139 Handle<Object> callback_data(listener.get(1));
00140 callback(api_message_obj, v8::Utils::ToLocal(callback_data));
00141 }
00142 }
00143 }
00144
00145
00146 Handle<String> MessageHandler::GetMessage(Handle<Object> data) {
00147 Handle<String> fmt_str = Factory::LookupAsciiSymbol("FormatMessage");
00148 Handle<JSFunction> fun =
00149 Handle<JSFunction>(
00150 JSFunction::cast(
00151 Top::builtins()->GetProperty(*fmt_str)));
00152 Object** argv[1] = { data.location() };
00153
00154 bool caught_exception;
00155 Handle<Object> result =
00156 Execution::TryCall(fun, Top::builtins(), 1, argv,
00157 &caught_exception);
00158
00159 if (caught_exception || !result->IsString()) {
00160 return Factory::LookupAsciiSymbol("<error>");
00161 }
00162 Handle<String> result_string = Handle<String>::cast(result);
00163
00164
00165
00166
00167
00168 FlattenString(result_string);
00169 return result_string;
00170 }
00171
00172
00173 SmartPointer<char> MessageHandler::GetLocalizedMessage(Handle<Object> data) {
00174 HandleScope scope;
00175 return GetMessage(data)->ToCString(DISALLOW_NULLS);
00176 }
00177
00178
00179 } }