Skip to content

Truly independent web browser

LibJS: Create yielded results in the realm of the generator

Shannon Boothd86f99ca00e8
diff --git a/Libraries/LibJS/Rust/src/runtime/generator_object.rs b/Libraries/LibJS/Rust/src/runtime/generator_object.rs
index 08fec8b595f..0c8e337d712 100644
--- a/Libraries/LibJS/Rust/src/runtime/generator_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/generator_object.rs
@@ -247,6 +247,14 @@ impl GeneratorObject {
.as_non_null()
}
+ /// The Realm of [[GeneratorContext]], until the generator completes.
+ pub fn realm_of_context(&self) -> Option<Gc<Realm>> {
+ self.execution_context
+ .borrow()
+ .as_ref()
+ .and_then(|context| context.realm.get())
+ }
+
/// Once a generator enters the completed state it never leaves it and its associated execution context is never
/// resumed. Any execution state associated with generator can be discarded at this point.
/// NB: Freeing the context right away, rather than when the generator is finalized, lets the next generator reuse
diff --git a/Libraries/LibJS/Rust/src/runtime/generator_prototype.rs b/Libraries/LibJS/Rust/src/runtime/generator_prototype.rs
index 9638cdffe40..fd8997d408c 100644
--- a/Libraries/LibJS/Rust/src/runtime/generator_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/generator_prototype.rs
@@ -97,7 +97,11 @@ impl GeneratorPrototype {
// 1. Return ? GeneratorResume(this value, value, empty).
let generator_object = typed_this_object::<GeneratorObject>(vm, DISPLAY_NAME)?;
let iteration_result = generator_object.resume(vm, vm.argument(0), None)?;
- Ok(generator_resume_result_to_value(vm, iteration_result))
+ Ok(generator_resume_result_to_value(
+ vm,
+ &generator_object,
+ iteration_result,
+ ))
}
// 27.5.1.3 Generator.prototype.return ( value ), https://tc39.es/ecma262/#sec-generator.prototype.return
@@ -110,7 +114,11 @@ impl GeneratorPrototype {
// 3. Return ? GeneratorResumeAbrupt(g, C, empty).
let iteration_result = generator_object.resume_abrupt(vm, completion, None)?;
- Ok(generator_resume_result_to_value(vm, iteration_result))
+ Ok(generator_resume_result_to_value(
+ vm,
+ &generator_object,
+ iteration_result,
+ ))
}
// 27.5.1.4 Generator.prototype.throw ( exception ), https://tc39.es/ecma262/#sec-generator.prototype.throw
@@ -124,18 +132,31 @@ impl GeneratorPrototype {
// 3. Return ? GeneratorResumeAbrupt(g, C, empty).
let iteration_result = generator_object.resume_abrupt(vm, completion, None)?;
- Ok(generator_resume_result_to_value(vm, iteration_result))
+ Ok(generator_resume_result_to_value(
+ vm,
+ &generator_object,
+ iteration_result,
+ ))
}
}
-fn generator_resume_result_to_value(vm: &Vm, iteration_result: IterationResult) -> Value {
+fn generator_resume_result_to_value(
+ vm: &Vm,
+ generator_object: &GeneratorObject,
+ iteration_result: IterationResult,
+) -> Value {
if iteration_result.value_is_iterator_result {
assert!(iteration_result.value.is_object());
return iteration_result.value;
}
- let realm = vm
- .current_realm()
- .expect("a generator is resumed in an execution context with a realm");
+ // NB: Yield makes its result object in the generator's context. Only the result of a completed generator comes from
+ // the context that resumed it.
+ let realm = if iteration_result.done {
+ vm.current_realm()
+ } else {
+ generator_object.realm_of_context()
+ }
+ .expect("a generator is resumed in an execution context with a realm");
Value::from_object(create_iterator_result_object(
vm,
realm,
diff --git a/Tests/LibWeb/Text/expected/generator-yield-result-realm.txt b/Tests/LibWeb/Text/expected/generator-yield-result-realm.txt
new file mode 100644
index 00000000000..df59b690287
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/generator-yield-result-realm.txt
@@ -0,0 +1,2 @@
+yield result: generator's realm
+final result: next's realm
diff --git a/Tests/LibWeb/Text/input/generator-yield-result-realm.html b/Tests/LibWeb/Text/input/generator-yield-result-realm.html
new file mode 100644
index 00000000000..c01e56d6277
--- /dev/null
+++ b/Tests/LibWeb/Text/input/generator-yield-result-realm.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+ const other = iframe.contentWindow;
+ const otherNext = Object.getPrototypeOf(other.eval("(function* () {})").prototype).next;
+
+ function realmOf(object) {
+ if (Object.getPrototypeOf(object) === Object.prototype) return "generator's realm";
+ if (Object.getPrototypeOf(object) === other.Object.prototype) return "next's realm";
+ return "neither";
+ }
+
+ function* generator() {
+ yield 1;
+ }
+ const iterator = generator();
+ println(`yield result: ${realmOf(otherNext.call(iterator))}`);
+ println(`final result: ${realmOf(otherNext.call(iterator))}`);
+ });
+</script>