| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/d8.h" | 5 #include "src/d8.h" |
| 6 #include "src/d8-debug.h" | 6 #include "src/d8-debug.h" |
| 7 | 7 |
| 8 namespace v8 { | 8 namespace v8 { |
| 9 | 9 |
| 10 void PrintPrompt(bool is_running) { | 10 void PrintPrompt(bool is_running) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 DebugEvent event = event_details.GetEvent(); | 22 DebugEvent event = event_details.GetEvent(); |
| 23 // Check for handled event. | 23 // Check for handled event. |
| 24 if (event != Break && event != Exception && event != AfterCompile) { | 24 if (event != Break && event != Exception && event != AfterCompile) { |
| 25 return; | 25 return; |
| 26 } | 26 } |
| 27 | 27 |
| 28 TryCatch try_catch(isolate); | 28 TryCatch try_catch(isolate); |
| 29 | 29 |
| 30 // Get the toJSONProtocol function on the event and get the JSON format. | 30 // Get the toJSONProtocol function on the event and get the JSON format. |
| 31 Local<String> to_json_fun_name = | 31 Local<String> to_json_fun_name = |
| 32 String::NewFromUtf8(isolate, "toJSONProtocol", NewStringType::kNormal) | 32 String::NewFromUtf8(isolate, "toJSONProtocol"); |
| 33 .ToLocalChecked(); | 33 Handle<Object> event_data = event_details.GetEventData(); |
| 34 Local<Object> event_data = event_details.GetEventData(); | |
| 35 Local<Function> to_json_fun = | 34 Local<Function> to_json_fun = |
| 36 Local<Function>::Cast(event_data->Get(isolate->GetCurrentContext(), | 35 Local<Function>::Cast(event_data->Get(to_json_fun_name)); |
| 37 to_json_fun_name).ToLocalChecked()); | 36 Local<Value> event_json = to_json_fun->Call(event_data, 0, NULL); |
| 38 Local<Value> event_json; | 37 if (try_catch.HasCaught()) { |
| 39 if (!to_json_fun->Call(isolate->GetCurrentContext(), event_data, 0, NULL) | |
| 40 .ToLocal(&event_json)) { | |
| 41 Shell::ReportException(isolate, &try_catch); | 38 Shell::ReportException(isolate, &try_catch); |
| 42 return; | 39 return; |
| 43 } | 40 } |
| 44 | 41 |
| 45 // Print the event details. | 42 // Print the event details. |
| 46 Local<Object> details = | 43 Handle<Object> details = |
| 47 Shell::DebugMessageDetails(isolate, Local<String>::Cast(event_json)); | 44 Shell::DebugMessageDetails(isolate, Handle<String>::Cast(event_json)); |
| 48 if (try_catch.HasCaught()) { | 45 if (try_catch.HasCaught()) { |
| 49 Shell::ReportException(isolate, &try_catch); | 46 Shell::ReportException(isolate, &try_catch); |
| 50 return; | 47 return; |
| 51 } | 48 } |
| 52 String::Utf8Value str( | 49 String::Utf8Value str(details->Get(String::NewFromUtf8(isolate, "text"))); |
| 53 details->Get(isolate->GetCurrentContext(), | |
| 54 String::NewFromUtf8(isolate, "text", NewStringType::kNormal) | |
| 55 .ToLocalChecked()).ToLocalChecked()); | |
| 56 if (str.length() == 0) { | 50 if (str.length() == 0) { |
| 57 // Empty string is used to signal not to process this event. | 51 // Empty string is used to signal not to process this event. |
| 58 return; | 52 return; |
| 59 } | 53 } |
| 60 printf("%s\n", *str); | 54 printf("%s\n", *str); |
| 61 | 55 |
| 62 // Get the debug command processor. | 56 // Get the debug command processor. |
| 63 Local<String> fun_name = | 57 Local<String> fun_name = |
| 64 String::NewFromUtf8(isolate, "debugCommandProcessor", | 58 String::NewFromUtf8(isolate, "debugCommandProcessor"); |
| 65 NewStringType::kNormal).ToLocalChecked(); | 59 Handle<Object> exec_state = event_details.GetExecutionState(); |
| 66 Local<Object> exec_state = event_details.GetExecutionState(); | 60 Local<Function> fun = Local<Function>::Cast(exec_state->Get(fun_name)); |
| 67 Local<Function> fun = Local<Function>::Cast( | 61 Local<Object> cmd_processor = |
| 68 exec_state->Get(isolate->GetCurrentContext(), fun_name).ToLocalChecked()); | 62 Local<Object>::Cast(fun->Call(exec_state, 0, NULL)); |
| 69 Local<Value> cmd_processor_value; | 63 if (try_catch.HasCaught()) { |
| 70 if (!fun->Call(isolate->GetCurrentContext(), exec_state, 0, NULL) | |
| 71 .ToLocal(&cmd_processor_value)) { | |
| 72 Shell::ReportException(isolate, &try_catch); | 64 Shell::ReportException(isolate, &try_catch); |
| 73 return; | 65 return; |
| 74 } | 66 } |
| 75 Local<Object> cmd_processor = Local<Object>::Cast(cmd_processor_value); | |
| 76 | 67 |
| 77 static const int kBufferSize = 256; | 68 static const int kBufferSize = 256; |
| 78 bool running = false; | 69 bool running = false; |
| 79 while (!running) { | 70 while (!running) { |
| 80 char command[kBufferSize]; | 71 char command[kBufferSize]; |
| 81 PrintPrompt(running); | 72 PrintPrompt(running); |
| 82 char* str = fgets(command, kBufferSize, stdin); | 73 char* str = fgets(command, kBufferSize, stdin); |
| 83 if (str == NULL) break; | 74 if (str == NULL) break; |
| 84 | 75 |
| 85 // Ignore empty commands. | 76 // Ignore empty commands. |
| 86 if (strlen(command) == 0) continue; | 77 if (strlen(command) == 0) continue; |
| 87 | 78 |
| 88 TryCatch try_catch(isolate); | 79 TryCatch try_catch(isolate); |
| 89 | 80 |
| 90 // Convert the debugger command to a JSON debugger request. | 81 // Convert the debugger command to a JSON debugger request. |
| 91 Local<Value> request = Shell::DebugCommandToJSONRequest( | 82 Handle<Value> request = Shell::DebugCommandToJSONRequest( |
| 92 isolate, String::NewFromUtf8(isolate, command, NewStringType::kNormal) | 83 isolate, String::NewFromUtf8(isolate, command)); |
| 93 .ToLocalChecked()); | |
| 94 if (try_catch.HasCaught()) { | 84 if (try_catch.HasCaught()) { |
| 95 Shell::ReportException(isolate, &try_catch); | 85 Shell::ReportException(isolate, &try_catch); |
| 96 continue; | 86 continue; |
| 97 } | 87 } |
| 98 | 88 |
| 99 // If undefined is returned the command was handled internally and there is | 89 // If undefined is returned the command was handled internally and there is |
| 100 // no JSON to send. | 90 // no JSON to send. |
| 101 if (request->IsUndefined()) { | 91 if (request->IsUndefined()) { |
| 102 continue; | 92 continue; |
| 103 } | 93 } |
| 104 | 94 |
| 105 Local<String> fun_name; | 95 Handle<String> fun_name; |
| 106 Local<Function> fun; | 96 Handle<Function> fun; |
| 107 // All the functions used below take one argument. | 97 // All the functions used below take one argument. |
| 108 static const int kArgc = 1; | 98 static const int kArgc = 1; |
| 109 Local<Value> args[kArgc]; | 99 Handle<Value> args[kArgc]; |
| 110 | 100 |
| 111 // Invoke the JavaScript to convert the debug command line to a JSON | 101 // Invoke the JavaScript to convert the debug command line to a JSON |
| 112 // request, invoke the JSON request and convert the JSON respose to a text | 102 // request, invoke the JSON request and convert the JSON respose to a text |
| 113 // representation. | 103 // representation. |
| 114 fun_name = String::NewFromUtf8(isolate, "processDebugRequest", | 104 fun_name = String::NewFromUtf8(isolate, "processDebugRequest"); |
| 115 NewStringType::kNormal).ToLocalChecked(); | 105 fun = Handle<Function>::Cast(cmd_processor->Get(fun_name)); |
| 116 fun = Local<Function>::Cast(cmd_processor->Get(isolate->GetCurrentContext(), | |
| 117 fun_name).ToLocalChecked()); | |
| 118 args[0] = request; | 106 args[0] = request; |
| 119 Local<Value> response_val; | 107 Handle<Value> response_val = fun->Call(cmd_processor, kArgc, args); |
| 120 if (!fun->Call(isolate->GetCurrentContext(), cmd_processor, kArgc, args) | 108 if (try_catch.HasCaught()) { |
| 121 .ToLocal(&response_val)) { | |
| 122 Shell::ReportException(isolate, &try_catch); | 109 Shell::ReportException(isolate, &try_catch); |
| 123 continue; | 110 continue; |
| 124 } | 111 } |
| 125 Local<String> response = Local<String>::Cast(response_val); | 112 Handle<String> response = Handle<String>::Cast(response_val); |
| 126 | 113 |
| 127 // Convert the debugger response into text details and the running state. | 114 // Convert the debugger response into text details and the running state. |
| 128 Local<Object> response_details = | 115 Handle<Object> response_details = |
| 129 Shell::DebugMessageDetails(isolate, response); | 116 Shell::DebugMessageDetails(isolate, response); |
| 130 if (try_catch.HasCaught()) { | 117 if (try_catch.HasCaught()) { |
| 131 Shell::ReportException(isolate, &try_catch); | 118 Shell::ReportException(isolate, &try_catch); |
| 132 continue; | 119 continue; |
| 133 } | 120 } |
| 134 String::Utf8Value text_str( | 121 String::Utf8Value text_str( |
| 135 response_details->Get(isolate->GetCurrentContext(), | 122 response_details->Get(String::NewFromUtf8(isolate, "text"))); |
| 136 String::NewFromUtf8(isolate, "text", | |
| 137 NewStringType::kNormal) | |
| 138 .ToLocalChecked()).ToLocalChecked()); | |
| 139 if (text_str.length() > 0) { | 123 if (text_str.length() > 0) { |
| 140 printf("%s\n", *text_str); | 124 printf("%s\n", *text_str); |
| 141 } | 125 } |
| 142 running = response_details->Get(isolate->GetCurrentContext(), | 126 running = response_details->Get(String::NewFromUtf8(isolate, "running")) |
| 143 String::NewFromUtf8(isolate, "running", | 127 ->ToBoolean(isolate) |
| 144 NewStringType::kNormal) | |
| 145 .ToLocalChecked()) | |
| 146 .ToLocalChecked() | |
| 147 ->ToBoolean(isolate->GetCurrentContext()) | |
| 148 .ToLocalChecked() | |
| 149 ->Value(); | 128 ->Value(); |
| 150 } | 129 } |
| 151 } | 130 } |
| 152 | 131 |
| 153 } // namespace v8 | 132 } // namespace v8 |
| OLD | NEW |