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
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.
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.
33 namespace i = v8::internal;
36 class HarmonyIsolate {
39 i::FLAG_harmony_promises = true;
40 isolate_ = Isolate::New();
49 Isolate* GetIsolate() const { return isolate_; }
57 TEST(MicrotaskDeliverySimple) {
58 HarmonyIsolate isolate;
59 HandleScope scope(isolate.GetIsolate());
60 LocalContext context(isolate.GetIsolate());
64 "function handler(resolve) { resolver.resolve = resolve; }"
66 "var observeOrders = [1, 4];"
67 "function observer() {"
68 "ordering.push(observeOrders.shift());"
71 "var p = new Promise(handler);"
74 "}).then(function() {"
77 "return new Promise(handler);"
78 "}).then(function() {"
80 "}).then(function() {"
83 "Object.observe(obj, observer);"
85 CHECK_EQ(6, CompileRun("ordering.length")->Int32Value());
86 CHECK_EQ(1, CompileRun("ordering[0]")->Int32Value());
87 CHECK_EQ(2, CompileRun("ordering[1]")->Int32Value());
88 CHECK_EQ(3, CompileRun("ordering[2]")->Int32Value());
89 CHECK_EQ(4, CompileRun("ordering[3]")->Int32Value());
90 CHECK_EQ(5, CompileRun("ordering[4]")->Int32Value());
91 CHECK_EQ(6, CompileRun("ordering[5]")->Int32Value());
95 TEST(MicrotaskPerIsolateState) {
96 HarmonyIsolate isolate;
97 HandleScope scope(isolate.GetIsolate());
98 LocalContext context1(isolate.GetIsolate());
99 isolate.GetIsolate()->SetAutorunMicrotasks(false);
101 "var obj = { calls: 0 };");
102 Handle<Value> obj = CompileRun("obj");
104 LocalContext context2(isolate.GetIsolate());
105 context2->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
109 "new Promise(function(resolve) {"
110 "resolver.resolve = resolve;"
111 "}).then(function() {"
115 "resolver.resolve();"
119 LocalContext context3(isolate.GetIsolate());
120 context3->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
123 "var foo = { id: 1 };"
124 "Object.observe(foo, function() {"
130 LocalContext context4(isolate.GetIsolate());
131 context4->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
133 isolate.GetIsolate()->RunMicrotasks();
134 CHECK_EQ(2, CompileRun("obj.calls")->Int32Value());