OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 15708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15719 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]"))); | 15719 CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]"))); |
15720 | 15720 |
15721 // TODO(1547): Make the following also return "i". | 15721 // TODO(1547): Make the following also return "i". |
15722 // Calling with environment record as base. | 15722 // Calling with environment record as base. |
15723 TestReceiver(o, context->Global(), "func()"); | 15723 TestReceiver(o, context->Global(), "func()"); |
15724 // Calling with no base. | 15724 // Calling with no base. |
15725 TestReceiver(o, context->Global(), "(1,func)()"); | 15725 TestReceiver(o, context->Global(), "(1,func)()"); |
15726 | 15726 |
15727 foreign_context.Dispose(); | 15727 foreign_context.Dispose(); |
15728 } | 15728 } |
| 15729 |
| 15730 |
| 15731 uint8_t callback_fired = 0; |
| 15732 |
| 15733 |
| 15734 void CallCompletedCallback1() { |
| 15735 printf("Firing callback 1.\n"); |
| 15736 callback_fired ^= 1; // Toggle first bit. |
| 15737 } |
| 15738 |
| 15739 |
| 15740 void CallCompletedCallback2() { |
| 15741 printf("Firing callback 2.\n"); |
| 15742 callback_fired ^= 2; // Toggle second bit. |
| 15743 } |
| 15744 |
| 15745 |
| 15746 Handle<Value> RecursiveCall(const Arguments& args) { |
| 15747 uint32_t level = args[0]->Uint32Value(); |
| 15748 if (level < 3) { |
| 15749 level++; |
| 15750 printf("Entering recursion level %d.\n", level); |
| 15751 char script[64]; |
| 15752 snprintf(script, sizeof(script), "recursion(%d)", level); |
| 15753 CompileRun(script); |
| 15754 printf("Leaving recursion level %d.\n", level); |
| 15755 CHECK_EQ(0, callback_fired); |
| 15756 } else { |
| 15757 printf("Recursion ends.\n"); |
| 15758 CHECK_EQ(0, callback_fired); |
| 15759 } |
| 15760 return Undefined(); |
| 15761 } |
| 15762 |
| 15763 |
| 15764 TEST(CallCompletedCallback) { |
| 15765 v8::HandleScope scope; |
| 15766 LocalContext env; |
| 15767 v8::Handle<v8::FunctionTemplate> recursive_runtime = |
| 15768 v8::FunctionTemplate::New(RecursiveCall); |
| 15769 env->Global()->Set(v8_str("recursion"), |
| 15770 recursive_runtime->GetFunction()); |
| 15771 // Adding the same callback a second time has no effect. |
| 15772 v8::V8::AddCallCompletedCallback(CallCompletedCallback1); |
| 15773 v8::V8::AddCallCompletedCallback(CallCompletedCallback1); |
| 15774 v8::V8::AddCallCompletedCallback(CallCompletedCallback2); |
| 15775 printf("--- Script (1) ---\n"); |
| 15776 Local<Script> script = |
| 15777 v8::Script::Compile(v8::String::New("recursion(0)")); |
| 15778 script->Run(); |
| 15779 CHECK_EQ(3, callback_fired); |
| 15780 |
| 15781 printf("\n--- Script (2) ---\n"); |
| 15782 callback_fired = 0; |
| 15783 v8::V8::RemoveCallCompletedCallback(CallCompletedCallback1); |
| 15784 script->Run(); |
| 15785 CHECK_EQ(2, callback_fired); |
| 15786 |
| 15787 printf("\n--- Function ---\n"); |
| 15788 callback_fired = 0; |
| 15789 Local<Function> recursive_function = |
| 15790 Local<Function>::Cast(env->Global()->Get(v8_str("recursion"))); |
| 15791 v8::Handle<Value> args[] = { v8_num(0) }; |
| 15792 recursive_function->Call(env->Global(), 1, args); |
| 15793 CHECK_EQ(2, callback_fired); |
| 15794 } |
| 15795 |
| 15796 |
| 15797 void CallCompletedCallbackNoException() { |
| 15798 v8::HandleScope scope; |
| 15799 CompileRun("1+1;"); |
| 15800 } |
| 15801 |
| 15802 |
| 15803 void CallCompletedCallbackException() { |
| 15804 v8::HandleScope scope; |
| 15805 CompileRun("throw 'second exception';"); |
| 15806 } |
| 15807 |
| 15808 |
| 15809 TEST(CallCompletedCallbackOneException) { |
| 15810 v8::HandleScope scope; |
| 15811 LocalContext env; |
| 15812 v8::V8::AddCallCompletedCallback(CallCompletedCallbackNoException); |
| 15813 CompileRun("throw 'exception';"); |
| 15814 } |
| 15815 |
| 15816 |
| 15817 TEST(CallCompletedCallbackTwoExceptions) { |
| 15818 v8::HandleScope scope; |
| 15819 LocalContext env; |
| 15820 v8::V8::AddCallCompletedCallback(CallCompletedCallbackException); |
| 15821 CompileRun("throw 'first exception';"); |
| 15822 } |
OLD | NEW |