Skip to content

Commit

Permalink
Document CodeBlock (#1691)
Browse files Browse the repository at this point in the history
* Begin adding CodeBlock docs
* Add docs for `CodeBlock` methods and fields
Also fix some typos.
* Add more CodeBlock docs
* Use suggestion from code review
  • Loading branch information
TheDoctor314 committed Dec 5, 2021
1 parent 0724a78 commit e1b2abb
Showing 1 changed file with 17 additions and 3 deletions.
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::{
arguments::Arguments, Captures, ClosureFunctionSignature, Function,
Expand All @@ -21,7 +25,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 @@ -35,12 +39,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 @@ -52,6 +60,7 @@ pub struct CodeBlock {
/// [[ThisMode]]
pub(crate) this_mode: ThisMode,

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

/// Bytecode
Expand All @@ -63,14 +72,15 @@ pub struct CodeBlock {
/// Variables names
pub(crate) variables: Vec<JsString>,

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

/// Indicates if the codeblock contains a lexical name `arguments`
pub(crate) lexical_name_argument: bool,
}

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 @@ -108,6 +118,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

0 comments on commit e1b2abb

Please sign in to comment.