Skip to content

Commit

Permalink
Make CompletionType::Yield use the return_value
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Sep 28, 2023
1 parent cbc9dad commit 4d2a14b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<'host> Context<'host> {
length: usize,
body: NativeFunction,
) -> JsResult<()> {
let function = FunctionObjectBuilder::new(&self.realm, body)
let function = FunctionObjectBuilder::new(self.realm(), body)
.name(name)
.length(length)
.constructor(true)
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<'host> Context<'host> {
length: usize,
body: NativeFunction,
) -> JsResult<()> {
let function = FunctionObjectBuilder::new(&self.realm, body)
let function = FunctionObjectBuilder::new(self.realm(), body)
.name(name)
.length(length)
.constructor(false)
Expand Down
13 changes: 9 additions & 4 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ impl Vm {
pub(crate) fn set_return_value(&mut self, value: JsValue) {
self.return_value = value;
}

pub(crate) fn take_return_value(&mut self) -> JsValue {
std::mem::take(&mut self.return_value)
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -355,12 +359,12 @@ impl Context<'_> {
Ok(CompletionType::Return) => {
self.vm.stack.truncate(self.vm.frame().fp as usize);

let execution_result = std::mem::take(&mut self.vm.return_value);
let result = self.vm.take_return_value();
if self.vm.frame().exit_early {
return CompletionRecord::Normal(execution_result);
return CompletionRecord::Normal(result);
}

self.vm.push(execution_result);
self.vm.push(result);
self.vm.pop_frame();
}
Ok(CompletionType::Throw) => {
Expand Down Expand Up @@ -401,11 +405,12 @@ impl Context<'_> {
}
// Early return immediately.
Ok(CompletionType::Yield) => {
let result = self.vm.take_return_value();
if self.vm.frame().exit_early {
let result = self.vm.pop();
return CompletionRecord::Return(result);
}

self.vm.push(result);
self.vm.pop_frame();
}
Err(err) => {
Expand Down
18 changes: 12 additions & 6 deletions boa_engine/src/vm/opcode/await/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use boa_gc::{Gc, GcRefCell};

use crate::{
builtins::{generator::GeneratorContext, Promise},
builtins::{generator::GeneratorContext, promise::PromiseCapability, Promise},
native_function::NativeFunction,
object::FunctionObjectBuilder,
vm::{opcode::Operation, CompletionType, GeneratorResumeKind},
Expand Down Expand Up @@ -125,11 +125,17 @@ impl Operation for Await {
context,
);

if let Some(promise_capabality) = context.vm.frame().promise_capability.clone() {
context.vm.push(promise_capabality.promise().clone());
} else {
context.vm.push(JsValue::undefined());
}
let return_value = context
.vm
.frame()
.promise_capability
.as_ref()
.map(PromiseCapability::promise)
.cloned()
.map(JsValue::from)
.unwrap_or_default();

context.vm.set_return_value(return_value);
Ok(CompletionType::Yield)
}
}
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/vm/opcode/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Operation for Generator {
.async_generator = Some(gen_clone);
}

context.vm.push(generator);
context.vm.set_return_value(generator.into());
Ok(CompletionType::Yield)
}
}
Expand Down
14 changes: 8 additions & 6 deletions boa_engine/src/vm/opcode/generator/yield_stm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ impl Operation for GeneratorYield {
const NAME: &'static str = "GeneratorYield";
const INSTRUCTION: &'static str = "INST - GeneratorYield";

fn execute(_context: &mut Context<'_>) -> JsResult<CompletionType> {
fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let value = context.vm.pop();
context.vm.set_return_value(value);
Ok(CompletionType::Yield)
}
}
Expand Down Expand Up @@ -76,11 +78,11 @@ impl Operation for AsyncGeneratorYield {

context.vm.push(resume_kind);

Ok(CompletionType::Normal)
} else {
gen.state = AsyncGeneratorState::SuspendedYield;
context.vm.push(JsValue::undefined());
GeneratorYield::execute(context)
return Ok(CompletionType::Normal);
}

gen.state = AsyncGeneratorState::SuspendedYield;
context.vm.set_return_value(JsValue::undefined());
Ok(CompletionType::Yield)
}
}
2 changes: 1 addition & 1 deletion boa_engine/src/vm/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ generate_opcodes! {
///
/// Operands:
///
/// Stack: **=>** resume_kind, received
/// Stack: value **=>** resume_kind, received
GeneratorYield,

/// Resumes the current generator function.
Expand Down

0 comments on commit 4d2a14b

Please sign in to comment.