OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "v8.h" |
| 29 |
| 30 #include "cctest.h" |
| 31 |
| 32 using namespace v8; |
| 33 namespace i = v8::internal; |
| 34 |
| 35 namespace { |
| 36 class HarmonyIsolate { |
| 37 public: |
| 38 HarmonyIsolate() { |
| 39 i::FLAG_harmony_observation = true; |
| 40 i::FLAG_harmony_promises = true; |
| 41 isolate_ = Isolate::New(); |
| 42 isolate_->Enter(); |
| 43 } |
| 44 |
| 45 ~HarmonyIsolate() { |
| 46 isolate_->Exit(); |
| 47 isolate_->Dispose(); |
| 48 } |
| 49 |
| 50 Isolate* GetIsolate() const { return isolate_; } |
| 51 |
| 52 private: |
| 53 Isolate* isolate_; |
| 54 }; |
| 55 } |
| 56 |
| 57 |
| 58 TEST(MicrotaskDeliverySimple) { |
| 59 HarmonyIsolate isolate; |
| 60 HandleScope scope(isolate.GetIsolate()); |
| 61 LocalContext context(isolate.GetIsolate()); |
| 62 CompileRun( |
| 63 "var ordering = [];" |
| 64 "var resolver = {};" |
| 65 "function handler(resolve) { resolver.resolve = resolve; }" |
| 66 "var obj = {};" |
| 67 "var observeOrders = [1, 4];" |
| 68 "function observer() {" |
| 69 "ordering.push(observeOrders.shift());" |
| 70 "resolver.resolve();" |
| 71 "}" |
| 72 "var p = new Promise(handler);" |
| 73 "p.then(function() {" |
| 74 "ordering.push(2);" |
| 75 "}).then(function() {" |
| 76 "ordering.push(3);" |
| 77 "obj.id++;" |
| 78 "return new Promise(handler);" |
| 79 "}).then(function() {" |
| 80 "ordering.push(5);" |
| 81 "}).then(function() {" |
| 82 "ordering.push(6);" |
| 83 "});" |
| 84 "Object.observe(obj, observer);" |
| 85 "obj.id = 1;"); |
| 86 CHECK_EQ(6, CompileRun("ordering.length")->Int32Value()); |
| 87 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value()); |
| 88 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value()); |
| 89 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value()); |
| 90 CHECK_EQ(4, CompileRun("ordering[3]")->Int32Value()); |
| 91 CHECK_EQ(5, CompileRun("ordering[4]")->Int32Value()); |
| 92 CHECK_EQ(6, CompileRun("ordering[5]")->Int32Value()); |
| 93 } |
| 94 |
| 95 |
| 96 TEST(MicrotaskPerIsolateState) { |
| 97 HarmonyIsolate isolate; |
| 98 HandleScope scope(isolate.GetIsolate()); |
| 99 LocalContext context1(isolate.GetIsolate()); |
| 100 V8::SetAutorunMicrotasks(isolate.GetIsolate(), false); |
| 101 CompileRun( |
| 102 "var obj = { calls: 0 };"); |
| 103 Handle<Value> obj = CompileRun("obj"); |
| 104 { |
| 105 LocalContext context2(isolate.GetIsolate()); |
| 106 context2->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"), |
| 107 obj); |
| 108 CompileRun( |
| 109 "var resolver = {};" |
| 110 "new Promise(function(resolve) {" |
| 111 "resolver.resolve = resolve;" |
| 112 "}).then(function() {" |
| 113 "obj.calls++;" |
| 114 "});" |
| 115 "(function() {" |
| 116 "resolver.resolve();" |
| 117 "})();"); |
| 118 } |
| 119 { |
| 120 LocalContext context3(isolate.GetIsolate()); |
| 121 context3->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"), |
| 122 obj); |
| 123 CompileRun( |
| 124 "var foo = { id: 1 };" |
| 125 "Object.observe(foo, function() {" |
| 126 "obj.calls++;" |
| 127 "});" |
| 128 "foo.id++;"); |
| 129 } |
| 130 { |
| 131 LocalContext context4(isolate.GetIsolate()); |
| 132 context4->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"), |
| 133 obj); |
| 134 V8::RunMicrotasks(isolate.GetIsolate()); |
| 135 CHECK_EQ(2, CompileRun("obj.calls")->Int32Value()); |
| 136 } |
| 137 } |
OLD | NEW |