Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document CodeBlock #1691

Merged
merged 4 commits into from
Dec 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions boa/src/vm/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! CodeBlock
//!
//! This module is for the CodeBlock which implements a function representation in the VM

use crate::{
builtins::function::{
Captures, ClosureFunctionSignature, Function, NativeFunctionSignature, ThisMode,
Expand All @@ -20,7 +24,7 @@ use std::{convert::TryInto, fmt::Write, mem::size_of};

use super::CallFrame;

/// This represents wether a value can be read from [`CodeBlock`] code.
/// This represents whether a value can be read from [`CodeBlock`] code.
pub unsafe trait Readable {}

unsafe impl Readable for u8 {}
Expand All @@ -34,12 +38,16 @@ unsafe impl Readable for i64 {}
unsafe impl Readable for f32 {}
unsafe impl Readable for f64 {}

/// The internal representation of a JavaScript function.
///
/// A CodeBlock is generated for each function compiled by the [ByteCompiler](crate::bytecompiler::ByteCompiler).
/// It stores the bytecode and the other attributes of the function.
#[derive(Debug, Trace, Finalize)]
pub struct CodeBlock {
/// Name of this function
pub(crate) name: JsString,

// The length of this function.
/// The number of arguments expected.
pub(crate) length: u32,

/// Is this function in strict mode.
Expand All @@ -51,6 +59,7 @@ pub struct CodeBlock {
/// [[ThisMode]]
pub(crate) this_mode: ThisMode,

/// Parameters passed to this function.
pub(crate) params: Box<[FormalParameter]>,

/// Bytecode
Expand All @@ -62,11 +71,12 @@ pub struct CodeBlock {
/// Variables names
pub(crate) variables: Vec<JsString>,

// Functions inside this function
/// Functions inside this function
pub(crate) functions: Vec<Gc<CodeBlock>>,
}

impl CodeBlock {
/// Constructs a new `CodeBlock`.
pub fn new(name: JsString, length: u32, strict: bool, constructor: bool) -> Self {
Self {
code: Vec::new(),
Expand Down Expand Up @@ -103,6 +113,10 @@ impl CodeBlock {
unsafe { self.read_unchecked(offset) }
}

/// Get the operands after the `Opcode` pointed to by `pc` as a `String`.
/// Modifies the `pc` to point to the next instruction.
///
/// Returns an empty `String` if no operands are present.
pub(crate) fn instruction_operands(&self, pc: &mut usize) -> String {
let opcode: Opcode = self.code[*pc].try_into().unwrap();
*pc += size_of::<Opcode>();
Expand Down