-
-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathasync_generator.rs
More file actions
190 lines (176 loc) · 6.19 KB
/
async_generator.rs
File metadata and controls
190 lines (176 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::{
Context, JsValue, Source, TestAction, builtins::promise::PromiseState, object::JsPromise,
run_test_actions,
};
use boa_macros::js_str;
use indoc::indoc;
#[track_caller]
fn assert_promise_iter_value(
promise: &JsValue,
target: &JsValue,
done: bool,
context: &mut Context,
) {
let promise = JsPromise::from_object(promise.as_object().unwrap().clone()).unwrap();
let PromiseState::Fulfilled(v) = promise.state() else {
panic!("promise was not fulfilled");
};
let o = v.as_object().unwrap();
let value = o.get(js_str!("value"), context).unwrap();
let d = o
.get(js_str!("done"), context)
.unwrap()
.as_boolean()
.unwrap();
assert_eq!(&value, target);
assert_eq!(d, done);
}
#[test]
fn return_on_then_infinite_loop() {
// Checks that calling `return` inside `then` only enters an infinite loop without
// crashing the engine.
run_test_actions([
TestAction::run(indoc! {r#"
async function* f() {}
const g = f();
let count = 0;
Object.defineProperty(Object.prototype, "then", {
get: function() {
if (count < 100) {
count++;
g.return();
}
return;
},
});
g.return();
"#}),
TestAction::inspect_context(|ctx| ctx.run_jobs().unwrap()),
TestAction::assert_eq("count", 100),
]);
}
#[test]
fn return_on_then_single() {
// Checks that calling `return` inside `then` once runs without panicking.
run_test_actions([
TestAction::run(indoc! {r#"
async function* f() {}
const g = f();
let first = true;
Object.defineProperty(Object.prototype, "then", {
get: function() {
if (first) {
first = false;
g.return();
}
return;
},
});
let ret = g.return()
"#}),
TestAction::inspect_context(|ctx| ctx.run_jobs().unwrap()),
TestAction::assert_eq("first", false),
TestAction::assert_with_op("ret", |ret, context| {
assert_promise_iter_value(&ret, &JsValue::undefined(), true, context);
true
}),
]);
}
#[test]
fn return_on_then_queue() {
// Checks that calling `return` inside `then` doesn't mess with the request queue.
run_test_actions([
TestAction::run(indoc! {r#"
async function* f() {
yield 1;
yield 2;
}
const g = f();
let count = 0;
Object.defineProperty(Object.prototype, "then", {
get: function() {
if (count < 2) {
count++;
g.return();
}
return;
},
});
let first = g.next();
let second = g.next();
let ret = g.return();
"#}),
TestAction::inspect_context(|ctx| ctx.run_jobs().unwrap()),
TestAction::assert_with_op("first", |first, context| {
assert_promise_iter_value(&first, &JsValue::from(1), false, context);
true
}),
TestAction::assert_with_op("second", |second, context| {
assert_promise_iter_value(&second, &JsValue::from(2), false, context);
true
}),
TestAction::assert_with_op("ret", |ret, context| {
assert_promise_iter_value(&ret, &JsValue::undefined(), true, context);
true
}),
TestAction::assert_eq("count", JsValue::from(2)),
]);
}
#[test]
fn cross_realm_async_generator_yield() {
// Exercises AsyncGeneratorYield spec steps 6-8 (previousRealm handling)
// by creating a generator in one realm and consuming it from another.
// Also verifies that the iter result objects are created in the caller's
// realm by checking their prototype against old_realm's Object.prototype.
let mut context = Context::default();
let generator_realm = context.create_realm().unwrap();
let old_realm = context.enter_realm(generator_realm);
let generator = context
.eval(Source::from_bytes(
b"(async function* g() { yield 42; yield 99; })()",
))
.unwrap();
context.enter_realm(old_realm.clone());
// Grab Object.prototype from the caller's realm (old_realm).
let caller_object_proto = old_realm.intrinsics().constructors().object().prototype();
let next_fn = generator
.as_object()
.unwrap()
.get(js_str!("next"), &mut context)
.unwrap();
let call_next = |ctx: &mut Context| -> JsValue {
let result = next_fn
.as_callable()
.unwrap()
.call(&generator, &[], ctx)
.unwrap();
ctx.run_jobs().unwrap();
result
};
// First yield: value 42
let first = call_next(&mut context);
assert_promise_iter_value(&first, &JsValue::from(42), false, &mut context);
// Verify the iter result was created in old_realm.
let first_promise = JsPromise::from_object(first.as_object().unwrap().clone()).unwrap();
let PromiseState::Fulfilled(first_result) = first_promise.state() else {
panic!("promise was not fulfilled");
};
assert_eq!(
first_result.as_object().unwrap().prototype(),
Some(caller_object_proto.clone()),
"iter result prototype should be old_realm's Object.prototype"
);
// Second yield: value 99
let second = call_next(&mut context);
assert_promise_iter_value(&second, &JsValue::from(99), false, &mut context);
// Verify the iter result was created in old_realm.
let second_promise = JsPromise::from_object(second.as_object().unwrap().clone()).unwrap();
let PromiseState::Fulfilled(second_result) = second_promise.state() else {
panic!("promise was not fulfilled");
};
assert_eq!(
second_result.as_object().unwrap().prototype(),
Some(caller_object_proto),
"iter result prototype should be old_realm's Object.prototype"
);
}