Support caching of next method in generators. (#3937)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var successCount = 0
|
||||
|
||||
function check_fulfilled(p, value, done)
|
||||
{
|
||||
assert(p instanceof Promise)
|
||||
|
||||
p.then(function(v) {
|
||||
assert(v.value === value)
|
||||
assert(v.done === done)
|
||||
successCount++
|
||||
}, function() {
|
||||
assert(false)
|
||||
})
|
||||
}
|
||||
|
||||
function check_rejected(p, value)
|
||||
{
|
||||
assert(p instanceof Promise)
|
||||
|
||||
p.then(function(v) {
|
||||
assert(false)
|
||||
}, function(v) {
|
||||
assert(v === value)
|
||||
successCount++
|
||||
})
|
||||
}
|
||||
|
||||
// Test 1
|
||||
|
||||
var o1 = {}
|
||||
var state1 = 0
|
||||
var async1 = {
|
||||
[Symbol.asyncIterator]() {
|
||||
return {
|
||||
get next() {
|
||||
assert(++state1 === 2)
|
||||
return function () {
|
||||
return { value:"Res", done:false }
|
||||
}
|
||||
},
|
||||
get throw() {
|
||||
++state1
|
||||
assert(state1 === 5 || state1 === 7 || state1 == 9)
|
||||
return function (v) {
|
||||
assert(v === "Input")
|
||||
return { value:o1, done:false }
|
||||
}
|
||||
},
|
||||
get return() {
|
||||
++state1
|
||||
assert(state1 === 6 || state1 === 8 || state1 == 10)
|
||||
return function (v) {
|
||||
assert(v === o1)
|
||||
return { value:4.5, done:false }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function *f1() {
|
||||
assert(++state1 === 1)
|
||||
yield *async1
|
||||
assert(false)
|
||||
}
|
||||
|
||||
var gen = f1()
|
||||
check_fulfilled(gen.next(), "Res", false)
|
||||
assert(++state1 === 3)
|
||||
check_fulfilled(gen.throw("Input"), o1, false)
|
||||
check_fulfilled(gen.return(o1), 4.5, false)
|
||||
|
||||
check_fulfilled(gen.next(), "Res", false)
|
||||
check_fulfilled(gen.throw("Input"), o1, false)
|
||||
check_fulfilled(gen.return(o1), 4.5, false)
|
||||
|
||||
check_fulfilled(gen.next(), "Res", false)
|
||||
check_fulfilled(gen.throw("Input"), o1, false)
|
||||
check_fulfilled(gen.return(o1), 4.5, false)
|
||||
assert(++state1 === 4)
|
||||
|
||||
// Test 2
|
||||
|
||||
var state2 = 0
|
||||
var async2 = {
|
||||
[Symbol.asyncIterator]() {
|
||||
return {
|
||||
get next() {
|
||||
assert(++state2 === 2)
|
||||
return "Not callable"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function *f2() {
|
||||
assert(++state2 === 1)
|
||||
try {
|
||||
yield *async2
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
return "End"
|
||||
}
|
||||
|
||||
gen = f2()
|
||||
check_fulfilled(gen.next(), "End", true)
|
||||
|
||||
// END
|
||||
|
||||
function __checkAsync() {
|
||||
assert(state1 == 10)
|
||||
assert(state2 == 2)
|
||||
assert(successCount == 10)
|
||||
}
|
||||
@@ -119,3 +119,88 @@ function *gen5() {
|
||||
g = gen5()
|
||||
check_result(g.next(), 1, false)
|
||||
check_result(g.throw(10), 3, false)
|
||||
|
||||
var o = {}
|
||||
var state = 0
|
||||
var iter6 = {
|
||||
[Symbol.iterator]() {
|
||||
return {
|
||||
get next() {
|
||||
assert(++state === 2)
|
||||
return function () {
|
||||
return { value:"Res", done:false }
|
||||
}
|
||||
},
|
||||
get throw() {
|
||||
++state
|
||||
assert(state === 4 || state === 9 || state == 14)
|
||||
return function (v) {
|
||||
assert(v === "Input")
|
||||
return { value:o, done:false }
|
||||
}
|
||||
},
|
||||
get return() {
|
||||
++state
|
||||
assert(state === 6 || state === 11 || state == 16)
|
||||
return function (v) {
|
||||
assert(v === o)
|
||||
return { value:4.5, done:false }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function *gen6() {
|
||||
assert(++state === 1)
|
||||
yield *iter6
|
||||
assert(false)
|
||||
}
|
||||
|
||||
g = gen6()
|
||||
check_result(g.next(), "Res", false)
|
||||
assert(++state === 3)
|
||||
check_result(g.throw("Input"), o, false)
|
||||
assert(++state === 5)
|
||||
check_result(g.return(o), 4.5, false)
|
||||
assert(++state === 7)
|
||||
|
||||
check_result(g.next(), "Res", false)
|
||||
assert(++state === 8)
|
||||
check_result(g.throw("Input"), o, false)
|
||||
assert(++state === 10)
|
||||
check_result(g.return(o), 4.5, false)
|
||||
assert(++state === 12)
|
||||
|
||||
check_result(g.next(), "Res", false)
|
||||
assert(++state === 13)
|
||||
check_result(g.throw("Input"), o, false)
|
||||
assert(++state === 15)
|
||||
check_result(g.return(o), 4.5, false)
|
||||
assert(++state === 17)
|
||||
|
||||
state = 0
|
||||
var iter7 = {
|
||||
[Symbol.iterator]() {
|
||||
return {
|
||||
get next() {
|
||||
assert(++state === 2)
|
||||
return "Not callable"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function *gen7() {
|
||||
assert(++state === 1)
|
||||
yield *iter7
|
||||
assert(false)
|
||||
}
|
||||
|
||||
g = gen7()
|
||||
try {
|
||||
g.next()
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof TypeError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user