From 1a257b52ce2cfeffecb42236ace7654b79f56b83 Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:39:13 +0100 Subject: [PATCH] VM Tidy Up (#1610) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * - add trace to VSCode launch - comment out pushUndefined and Return - Move call frame to its own file - heading for code blocks needed to be on its own line - Show the difference between the VM starting up and a new Call frame being ran * add back in return opCodes * Update boa/src/vm/mod.rs Co-authored-by: João Borges --- .vscode/launch.json | 2 +- boa/src/bytecompiler.rs | 1 + boa/src/vm/call_frame.rs | 17 +++++++++++++++++ boa/src/vm/code_block.rs | 2 +- boa/src/vm/mod.rs | 24 +++++++++--------------- 5 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 boa/src/vm/call_frame.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 891c5839b0..ce5770f294 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -28,7 +28,7 @@ "vm" ] }, - "args": ["${workspaceFolder}/tests/js/test.js"], + "args": ["-t", "${workspaceFolder}/tests/js/test.js"], "sourceLanguages": ["rust"] } ] diff --git a/boa/src/bytecompiler.rs b/boa/src/bytecompiler.rs index 1e3dffb8c7..fa06b30e1b 100644 --- a/boa/src/bytecompiler.rs +++ b/boa/src/bytecompiler.rs @@ -868,6 +868,7 @@ impl ByteCompiler { compiler.code_block.params = paramaters.to_owned().into_boxed_slice(); + // TODO These are redundant if a function returns so may need to check if a function returns and adding these if it doesn't compiler.emit(Opcode::PushUndefined, &[]); compiler.emit(Opcode::Return, &[]); diff --git a/boa/src/vm/call_frame.rs b/boa/src/vm/call_frame.rs new file mode 100644 index 0000000000..9c327cdc8a --- /dev/null +++ b/boa/src/vm/call_frame.rs @@ -0,0 +1,17 @@ +//! CallFrame +//! This module will provides everything needed to implement the CallFrame + +use super::CodeBlock; +use crate::{environment::lexical_environment::Environment, JsValue}; +use gc::Gc; + +#[derive(Debug)] +pub struct CallFrame { + pub(crate) prev: Option>, + pub(crate) code: Gc, + pub(crate) pc: usize, + pub(crate) fp: usize, + pub(crate) exit_on_return: bool, + pub(crate) this: JsValue, + pub(crate) environment: Environment, +} diff --git a/boa/src/vm/code_block.rs b/boa/src/vm/code_block.rs index b2f1999ccd..a6f97cc89e 100644 --- a/boa/src/vm/code_block.rs +++ b/boa/src/vm/code_block.rs @@ -218,7 +218,7 @@ impl CodeBlock { impl std::fmt::Display for CodeBlock { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( + writeln!( f, "----------------- name '{}' (length: {}) ------------------", self.name, self.length diff --git a/boa/src/vm/mod.rs b/boa/src/vm/mod.rs index a8e6561ee2..4e6864e275 100644 --- a/boa/src/vm/mod.rs +++ b/boa/src/vm/mod.rs @@ -2,18 +2,18 @@ //! This module will provide an instruction set for the AST to use, various traits, //! plus an interpreter to execute those instructions -use crate::environment::lexical_environment::Environment; use crate::{ builtins::Array, environment::lexical_environment::VariableScope, symbol::WellKnownSymbols, BoaProfiler, Context, JsResult, JsValue, }; +mod call_frame; mod code_block; mod opcode; +pub use call_frame::CallFrame; pub use code_block::CodeBlock; pub use code_block::JsVmFunction; -use gc::Gc; pub use opcode::Opcode; use std::{convert::TryInto, mem::size_of, time::Instant}; @@ -22,18 +22,6 @@ use self::code_block::Readable; #[cfg(test)] mod tests; - -#[derive(Debug)] -pub struct CallFrame { - pub(crate) prev: Option>, - pub(crate) code: Gc, - pub(crate) pc: usize, - pub(crate) fp: usize, - pub(crate) exit_on_return: bool, - pub(crate) this: JsValue, - pub(crate) environment: Environment, -} - /// Virtual Machine. #[derive(Debug)] pub struct Vm { @@ -515,11 +503,17 @@ impl Context { const OPERAND_COLUMN_WIDTH: usize = COLUMN_WIDTH; const NUMBER_OF_COLUMNS: usize = 4; + let msg = if self.vm.frame().exit_on_return { + " VM Start" + } else { + " Call Frame " + }; + if self.vm.trace { println!("{}\n", self.vm.frame().code); println!( "{:-^width$}", - " Vm Start ", + msg, width = COLUMN_WIDTH * NUMBER_OF_COLUMNS - 10 ); println!(