mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(codemode): reject Promise combinators whose input cannot be iterated
Promise.all, allSettled, any and race threw synchronously when their
argument was not iterable or iterating it failed; like the reference they
now return a promise rejected with that error, and word the non-iterable
case as the reference does ('Promise.all expects a synchronous iterable,
received a number.').
GetIterator now uses the reference's messages for a Symbol.iterator that is
not a function or returns a non-object, in every place that iterates.
Touches ops.rs (get_iterator, get_async_iterator).
70 test262 files leave rust-skipped.txt.
This commit is contained in:
parent
ff6da3f34b
commit
f304df0a4f
7 changed files with 163 additions and 77 deletions
|
|
@ -0,0 +1,10 @@
|
|||
const out = []
|
||||
for (const b of [{ [Symbol.iterator]: () => 1 }, { [Symbol.iterator]: 5 }, "ab"]) {
|
||||
try { for (const x of b) out.push(x) } catch (e) { out.push(e.message) }
|
||||
try { out.push([...b].length) } catch (e) { out.push(e.message) }
|
||||
try { out.push(Array.from(b).length) } catch (e) { out.push(e.message) }
|
||||
try { const [x] = b; out.push(x) } catch (e) { out.push(e.message) }
|
||||
try { out.push(new Set(b).size) } catch (e) { out.push(e.message) }
|
||||
}
|
||||
out.push(await Promise.all("ab"))
|
||||
return out
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"bindings": {},
|
||||
"ok": true,
|
||||
"value": [
|
||||
"Iterator method result must be an object.",
|
||||
"Iterator method result must be an object.",
|
||||
"Iterator method result must be an object.",
|
||||
"Iterator method result must be an object.",
|
||||
"Iterator method result must be an object.",
|
||||
"Iterator method must be a function.",
|
||||
"Iterator method must be a function.",
|
||||
"Iterator method must be a function.",
|
||||
"Iterator method must be a function.",
|
||||
"Iterator method must be a function.",
|
||||
"a",
|
||||
"b",
|
||||
2,
|
||||
2,
|
||||
"a",
|
||||
2,
|
||||
[
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
],
|
||||
"logs": []
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
const out = []
|
||||
const bad = [3, null, undefined, true, { [Symbol.iterator]: () => 1 }, { [Symbol.iterator]: () => ({ next() { throw new Error("boom") } }) }, { [Symbol.iterator]: 5 }]
|
||||
for (const name of ["all", "allSettled", "any", "race"]) {
|
||||
for (const b of bad) {
|
||||
let sync = "returned"
|
||||
try { const p = Promise[name](b); out.push(p instanceof Promise); await p.then(() => out.push("resolved"), (e) => out.push(name + ": " + e.constructor.name + ": " + e.message)) } catch (e) { sync = "threw " + e.message }
|
||||
if (sync !== "returned") out.push(sync)
|
||||
}
|
||||
}
|
||||
const log = []
|
||||
const it = { [Symbol.iterator]() { let i = 0; return { next() { log.push("next" + i); if (i === 2) throw new Error("stop"); return { value: i++, done: false } } } } }
|
||||
await Promise.all(it).catch((e) => log.push(e.message))
|
||||
out.push(log.join(","))
|
||||
return out
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
{
|
||||
"bindings": {},
|
||||
"ok": true,
|
||||
"value": [
|
||||
true,
|
||||
"all: TypeError: Promise.all expects a synchronous iterable, received a number.",
|
||||
true,
|
||||
"all: TypeError: Promise.all expects a synchronous iterable, received null.",
|
||||
true,
|
||||
"all: TypeError: Promise.all expects a synchronous iterable, received undefined.",
|
||||
true,
|
||||
"all: TypeError: Promise.all expects a synchronous iterable, received a boolean.",
|
||||
true,
|
||||
"all: TypeError: Iterator method result must be an object.",
|
||||
true,
|
||||
"all: Error: boom",
|
||||
true,
|
||||
"all: TypeError: Iterator method must be a function.",
|
||||
true,
|
||||
"allSettled: TypeError: Promise.allSettled expects a synchronous iterable, received a number.",
|
||||
true,
|
||||
"allSettled: TypeError: Promise.allSettled expects a synchronous iterable, received null.",
|
||||
true,
|
||||
"allSettled: TypeError: Promise.allSettled expects a synchronous iterable, received undefined.",
|
||||
true,
|
||||
"allSettled: TypeError: Promise.allSettled expects a synchronous iterable, received a boolean.",
|
||||
true,
|
||||
"allSettled: TypeError: Iterator method result must be an object.",
|
||||
true,
|
||||
"allSettled: Error: boom",
|
||||
true,
|
||||
"allSettled: TypeError: Iterator method must be a function.",
|
||||
true,
|
||||
"any: TypeError: Promise.any expects a synchronous iterable, received a number.",
|
||||
true,
|
||||
"any: TypeError: Promise.any expects a synchronous iterable, received null.",
|
||||
true,
|
||||
"any: TypeError: Promise.any expects a synchronous iterable, received undefined.",
|
||||
true,
|
||||
"any: TypeError: Promise.any expects a synchronous iterable, received a boolean.",
|
||||
true,
|
||||
"any: TypeError: Iterator method result must be an object.",
|
||||
true,
|
||||
"any: Error: boom",
|
||||
true,
|
||||
"any: TypeError: Iterator method must be a function.",
|
||||
true,
|
||||
"race: TypeError: Promise.race expects a synchronous iterable, received a number.",
|
||||
true,
|
||||
"race: TypeError: Promise.race expects a synchronous iterable, received null.",
|
||||
true,
|
||||
"race: TypeError: Promise.race expects a synchronous iterable, received undefined.",
|
||||
true,
|
||||
"race: TypeError: Promise.race expects a synchronous iterable, received a boolean.",
|
||||
true,
|
||||
"race: TypeError: Iterator method result must be an object.",
|
||||
true,
|
||||
"race: Error: boom",
|
||||
true,
|
||||
"race: TypeError: Iterator method must be a function.",
|
||||
"next0,next1,next2,stop"
|
||||
],
|
||||
"logs": []
|
||||
}
|
||||
|
|
@ -305,8 +305,42 @@ fn promise_reject(vm: &mut Vm, _this: Value, args: &[Value], _: Id) -> Res {
|
|||
Ok(Value::Obj(p))
|
||||
}
|
||||
|
||||
/// The members of a combinator's input, or the promise it returns already rejected when iterating it fails.
|
||||
fn combinator_items(
|
||||
vm: &mut Vm,
|
||||
args: &[Value],
|
||||
name: &str,
|
||||
) -> Result<Result<Vec<Value>, Id>, Value> {
|
||||
let items = arg(args, 0);
|
||||
let listed = if vm.is_iterable(&items) {
|
||||
vm.iterate_to_vec(&items)
|
||||
} else {
|
||||
let d = vm.describe_data(&items);
|
||||
Err(vm.type_error(&format!(
|
||||
"Promise.{name} expects a synchronous iterable, received {d}."
|
||||
)))
|
||||
};
|
||||
match listed {
|
||||
Ok(items) => Ok(Ok(items)),
|
||||
Err(e) if vm.fatal.is_some() => Err(e),
|
||||
Err(e) => {
|
||||
let p = vm.new_promise();
|
||||
vm.reject(p, e);
|
||||
Ok(Err(p))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn combinator(vm: &mut Vm, args: &[Value], kind: CombKind) -> Res {
|
||||
let items = vm.iterate_to_vec(&arg(args, 0))?;
|
||||
let name = match kind {
|
||||
CombKind::All => "all",
|
||||
CombKind::AllSettled => "allSettled",
|
||||
CombKind::Any => "any",
|
||||
};
|
||||
let items = match combinator_items(vm, args, name)? {
|
||||
Ok(items) => items,
|
||||
Err(rejected) => return Ok(Value::Obj(rejected)),
|
||||
};
|
||||
let result = vm.new_promise();
|
||||
let n = items.len();
|
||||
if n == 0 {
|
||||
|
|
@ -361,7 +395,10 @@ fn promise_any(vm: &mut Vm, _this: Value, args: &[Value], _: Id) -> Res {
|
|||
}
|
||||
|
||||
fn promise_race(vm: &mut Vm, _this: Value, args: &[Value], _: Id) -> Res {
|
||||
let items = vm.iterate_to_vec(&arg(args, 0))?;
|
||||
let items = match combinator_items(vm, args, "race")? {
|
||||
Ok(items) => items,
|
||||
Err(rejected) => return Ok(Value::Obj(rejected)),
|
||||
};
|
||||
let result = vm.new_promise();
|
||||
if items.is_empty() {
|
||||
let e = vm.type_error(
|
||||
|
|
|
|||
|
|
@ -1201,11 +1201,13 @@ impl<'rt> Vm<'rt> {
|
|||
if self.is_callable(&f) {
|
||||
let it = self.call(&f, *v, &[])?;
|
||||
if !matches!(it, Value::Obj(_)) {
|
||||
return Err(self
|
||||
.type_error("Result of the Symbol.iterator method is not an object"));
|
||||
return Err(self.type_error("Iterator method result must be an object."));
|
||||
}
|
||||
return Ok(it);
|
||||
}
|
||||
if !f.is_nullish() {
|
||||
return Err(self.type_error("Iterator method must be a function."));
|
||||
}
|
||||
}
|
||||
Value::Str(s) => {
|
||||
return Ok(Value::Obj(
|
||||
|
|
@ -1310,17 +1312,19 @@ impl<'rt> Vm<'rt> {
|
|||
if let Value::Obj(id) = v {
|
||||
let f = self.get_obj(*id, SYM_ASYNC_ITERATOR);
|
||||
if !f.is_nullish() {
|
||||
if !self.is_callable(&f) {
|
||||
return Err(self.type_error("Iterator method must be a function."));
|
||||
}
|
||||
let it = self.call(&f, *v, &[])?;
|
||||
if !matches!(it, Value::Obj(_)) {
|
||||
return Err(self
|
||||
.type_error("Result of the Symbol.asyncIterator method is not an object"));
|
||||
return Err(self.type_error("Iterator method result must be an object."));
|
||||
}
|
||||
return Ok(it);
|
||||
}
|
||||
}
|
||||
let sync = self.get_iterator_in(v, crate::bytecode::ITER_FOR_AWAIT)?;
|
||||
let Value::Obj(sync) = sync else {
|
||||
return Err(self.type_error("Result of the Symbol.iterator method is not an object"));
|
||||
return Err(self.type_error("Iterator method result must be an object."));
|
||||
};
|
||||
let next = self.record_next(&Value::Obj(sync))?;
|
||||
let proto = self.realm.async_from_sync_proto;
|
||||
|
|
|
|||
|
|
@ -26,76 +26,6 @@ built-ins/Object/fromEntries/string-entry-primitive-throws.js # error not throw
|
|||
built-ins/Object/isFrozen/15.2.3.12-2-a-11.js # wrong result
|
||||
built-ins/Object/prototype/toLocaleString/S15.2.4.3_A9.js # wrong result
|
||||
built-ins/Object/prototype/valueOf/S15.2.4.4_A9.js # wrong result
|
||||
built-ins/Promise/all/S25.4.4.1_A3.1_T1.js # uncaught TypeError: N is not iterable
|
||||
built-ins/Promise/all/S25.4.4.1_A3.1_T2.js # uncaught TypeError: object is not iterable
|
||||
built-ins/Promise/all/S25.4.4.1_A5.1_T1.js # wrong result
|
||||
built-ins/Promise/all/iter-arg-is-false-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-arg-is-null-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-arg-is-number-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-arg-is-true-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-arg-is-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-false-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-null-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-number-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-string-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-true-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-assigned-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-false-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-null-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-number-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-string-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-true-reject.js # async: wrong result
|
||||
built-ins/Promise/all/iter-returns-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-arg-is-false-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-arg-is-null-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-arg-is-number-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-arg-is-true-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-arg-is-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-false-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-null-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-number-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-string-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-true-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-assigned-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-next-err-reject.js # wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-false-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-null-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-number-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-string-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-true-reject.js # async: wrong result
|
||||
built-ins/Promise/allSettled/iter-returns-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-error-object-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-false-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-null-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-number-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-true-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-arg-is-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/any/iter-returns-false-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/any/iter-returns-null-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/any/iter-returns-number-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/any/iter-returns-string-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/any/iter-returns-true-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/any/iter-returns-undefined-reject.js # uncaught TypeError: Result of the Symbol.iterator method is not an object
|
||||
built-ins/Promise/race/S25.4.4.3_A2.2_T1.js # uncaught TypeError: N is not iterable
|
||||
built-ins/Promise/race/S25.4.4.3_A2.2_T2.js # uncaught TypeError: object is not iterable
|
||||
built-ins/Promise/race/S25.4.4.3_A4.1_T1.js # wrong result
|
||||
built-ins/Promise/race/iter-arg-is-false-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-arg-is-null-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-arg-is-number-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-arg-is-true-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-arg-is-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-false-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-null-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-number-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-string-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-true-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-assigned-undefined-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-false-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-null-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-number-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-string-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-true-reject.js # async: wrong result
|
||||
built-ins/Promise/race/iter-returns-undefined-reject.js # async: wrong result
|
||||
built-ins/RegExp/escape/escaped-surrogates.js # wrong result
|
||||
built-ins/RegExp/prototype/exec/S15.10.6.2_A9.js # wrong result
|
||||
built-ins/RegExp/prototype/test/S15.10.6.3_A9.js # wrong result
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue