OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 3148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3159 v8::Debug::DebugBreak(); | 3159 v8::Debug::DebugBreak(); |
3160 } | 3160 } |
3161 | 3161 |
3162 | 3162 |
3163 TEST(SendCommandToUninitializedVM) { | 3163 TEST(SendCommandToUninitializedVM) { |
3164 const char* dummy_command = "{}"; | 3164 const char* dummy_command = "{}"; |
3165 uint16_t dummy_buffer[80]; | 3165 uint16_t dummy_buffer[80]; |
3166 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer); | 3166 int dummy_length = AsciiToUtf16(dummy_command, dummy_buffer); |
3167 v8::Debug::SendCommand(dummy_buffer, dummy_length); | 3167 v8::Debug::SendCommand(dummy_buffer, dummy_length); |
3168 } | 3168 } |
| 3169 |
| 3170 |
| 3171 // Source for The JavaScript function which returns the number of frames. |
| 3172 static const char* frame_count_source = |
| 3173 "function frame_count(exec_state) {" |
| 3174 " return exec_state.frameCount();" |
| 3175 "}"; |
| 3176 v8::Handle<v8::Function> frame_count; |
| 3177 |
| 3178 |
| 3179 // Source for a JavaScript function which returns the source line for the top |
| 3180 // frame. |
| 3181 static const char* frame_source_line_source = |
| 3182 "function frame_source_line(exec_state) {" |
| 3183 " return exec_state.frame(0).sourceLine();" |
| 3184 "}"; |
| 3185 v8::Handle<v8::Function> frame_source_line; |
| 3186 |
| 3187 |
| 3188 // Source for a JavaScript function which returns the data parameter of a |
| 3189 // function called in the context of the debugger. If no data parameter is |
| 3190 // passed it throws an exception. |
| 3191 static const char* debugger_call_with_data_source = |
| 3192 "function debugger_call_with_data(exec_state, data) {" |
| 3193 " if (data) return data;" |
| 3194 " throw 'No data!'" |
| 3195 "}"; |
| 3196 v8::Handle<v8::Function> debugger_call_with_data; |
| 3197 |
| 3198 |
| 3199 // Source for a JavaScript function which returns the data parameter of a |
| 3200 // function called in the context of the debugger. If no data parameter is |
| 3201 // passed it throws an exception. |
| 3202 static const char* debugger_call_with_closure_source = |
| 3203 "var x = 3;" |
| 3204 "function (exec_state) {" |
| 3205 " if (exec_state.y) return x - 1;" |
| 3206 " exec_state.y = x;" |
| 3207 " return exec_state.y" |
| 3208 "}"; |
| 3209 v8::Handle<v8::Function> debugger_call_with_closure; |
| 3210 |
| 3211 // Function to retrieve the number of JavaScript frames by calling a JavaScript |
| 3212 // in the debugger. |
| 3213 static v8::Handle<v8::Value> CheckFrameCount(const v8::Arguments& args) { |
| 3214 CHECK(v8::Debug::Call(frame_count)->IsNumber()); |
| 3215 CHECK_EQ(args[0]->Int32Value(), |
| 3216 v8::Debug::Call(frame_count)->Int32Value()); |
| 3217 return v8::Undefined(); |
| 3218 } |
| 3219 |
| 3220 |
| 3221 // Function to retrieve the source line of the top JavaScript frame by calling a |
| 3222 // JavaScript function in the debugger. |
| 3223 static v8::Handle<v8::Value> CheckSourceLine(const v8::Arguments& args) { |
| 3224 CHECK(v8::Debug::Call(frame_source_line)->IsNumber()); |
| 3225 CHECK_EQ(args[0]->Int32Value(), |
| 3226 v8::Debug::Call(frame_source_line)->Int32Value()); |
| 3227 return v8::Undefined(); |
| 3228 } |
| 3229 |
| 3230 |
| 3231 // Function to test passing an additional parameter to a JavaScript function |
| 3232 // called in the debugger. It also tests that functions called in the debugger |
| 3233 // can throw exceptions. |
| 3234 static v8::Handle<v8::Value> CheckDataParameter(const v8::Arguments& args) { |
| 3235 v8::Handle<v8::String> data = v8::String::New("Test"); |
| 3236 CHECK(v8::Debug::Call(debugger_call_with_data, data)->IsString()); |
| 3237 |
| 3238 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 3239 CHECK(v8::Debug::Call(debugger_call_with_data).IsEmpty()); |
| 3240 |
| 3241 v8::TryCatch catcher; |
| 3242 v8::Debug::Call(debugger_call_with_data); |
| 3243 CHECK(catcher.HasCaught()); |
| 3244 CHECK(catcher.Exception()->IsString()); |
| 3245 |
| 3246 return v8::Undefined(); |
| 3247 } |
| 3248 |
| 3249 |
| 3250 // Function to test using a JavaScript with closure in the debugger. |
| 3251 static v8::Handle<v8::Value> CheckClosure(const v8::Arguments& args) { |
| 3252 CHECK(v8::Debug::Call(debugger_call_with_closure)->IsNumber()); |
| 3253 CHECK_EQ(3, v8::Debug::Call(debugger_call_with_closure)->Int32Value()); |
| 3254 return v8::Undefined(); |
| 3255 } |
| 3256 |
| 3257 |
| 3258 // Test functions called through the debugger. |
| 3259 TEST(CallFunctionInDebugger) { |
| 3260 // Create and enter a context with the functions CheckFrameCount, |
| 3261 // CheckSourceLine and CheckDataParameter installed. |
| 3262 v8::HandleScope scope; |
| 3263 v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 3264 global_template->Set(v8::String::New("CheckFrameCount"), |
| 3265 v8::FunctionTemplate::New(CheckFrameCount)); |
| 3266 global_template->Set(v8::String::New("CheckSourceLine"), |
| 3267 v8::FunctionTemplate::New(CheckSourceLine)); |
| 3268 global_template->Set(v8::String::New("CheckDataParameter"), |
| 3269 v8::FunctionTemplate::New(CheckDataParameter)); |
| 3270 global_template->Set(v8::String::New("CheckClosure"), |
| 3271 v8::FunctionTemplate::New(CheckClosure)); |
| 3272 v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template); |
| 3273 v8::Context::Scope context_scope(context); |
| 3274 |
| 3275 // Compile a function for checking the number of JavaScript frames. |
| 3276 v8::Script::Compile(v8::String::New(frame_count_source))->Run(); |
| 3277 frame_count = v8::Local<v8::Function>::Cast( |
| 3278 context->Global()->Get(v8::String::New("frame_count"))); |
| 3279 |
| 3280 // Compile a function for returning the source line for the top frame. |
| 3281 v8::Script::Compile(v8::String::New(frame_source_line_source))->Run(); |
| 3282 frame_source_line = v8::Local<v8::Function>::Cast( |
| 3283 context->Global()->Get(v8::String::New("frame_source_line"))); |
| 3284 |
| 3285 // Compile a function returning the data parameter. |
| 3286 v8::Script::Compile(v8::String::New(debugger_call_with_data_source))->Run(); |
| 3287 debugger_call_with_data = v8::Local<v8::Function>::Cast( |
| 3288 context->Global()->Get(v8::String::New("debugger_call_with_data"))); |
| 3289 |
| 3290 // Compile a function capturing closure. |
| 3291 debugger_call_with_closure = v8::Local<v8::Function>::Cast( |
| 3292 v8::Script::Compile( |
| 3293 v8::String::New(debugger_call_with_closure_source))->Run()); |
| 3294 |
| 3295 // Calling a function through the debugger returns undefined if there are no |
| 3296 // JavaScript frames. |
| 3297 CHECK(v8::Debug::Call(frame_count)->IsUndefined()); |
| 3298 CHECK(v8::Debug::Call(frame_source_line)->IsUndefined()); |
| 3299 CHECK(v8::Debug::Call(debugger_call_with_data)->IsUndefined()); |
| 3300 |
| 3301 // Test that the number of frames can be retrieved. |
| 3302 v8::Script::Compile(v8::String::New("CheckFrameCount(1)"))->Run(); |
| 3303 v8::Script::Compile(v8::String::New("function f() {" |
| 3304 " CheckFrameCount(2);" |
| 3305 "}; f()"))->Run(); |
| 3306 |
| 3307 // Test that the source line can be retrieved. |
| 3308 v8::Script::Compile(v8::String::New("CheckSourceLine(0)"))->Run(); |
| 3309 v8::Script::Compile(v8::String::New("function f() {\n" |
| 3310 " CheckSourceLine(1)\n" |
| 3311 " CheckSourceLine(2)\n" |
| 3312 " CheckSourceLine(3)\n" |
| 3313 "}; f()"))->Run(); |
| 3314 |
| 3315 // Test that a parameter can be passed to a function called in the debugger. |
| 3316 v8::Script::Compile(v8::String::New("CheckDataParameter()"))->Run(); |
| 3317 |
| 3318 // Test that a function with closure can be run in the debugger. |
| 3319 v8::Script::Compile(v8::String::New("CheckClosure()"))->Run(); |
| 3320 } |
OLD | NEW |