Browse Source

Document CodeBlock (#1691)

* Begin adding CodeBlock docs
* Add docs for `CodeBlock` methods and fields
Also fix some typos.
* Add more CodeBlock docs
* Use suggestion from code review
pull/1730/head
TheDoctor314 3 years ago committed by GitHub
parent
commit
e1b2abbc86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      boa/src/vm/code_block.rs

20
boa/src/vm/code_block.rs

@ -1,3 +1,7 @@
//! CodeBlock
//!
//! This module is for the CodeBlock which implements a function representation in the VM
use crate::{ use crate::{
builtins::function::{ builtins::function::{
arguments::Arguments, Captures, ClosureFunctionSignature, Function, arguments::Arguments, Captures, ClosureFunctionSignature, Function,
@ -21,7 +25,7 @@ use std::{convert::TryInto, fmt::Write, mem::size_of};
use super::CallFrame; 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 {} pub unsafe trait Readable {}
unsafe impl Readable for u8 {} unsafe impl Readable for u8 {}
@ -35,12 +39,16 @@ unsafe impl Readable for i64 {}
unsafe impl Readable for f32 {} unsafe impl Readable for f32 {}
unsafe impl Readable for f64 {} 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)] #[derive(Debug, Trace, Finalize)]
pub struct CodeBlock { pub struct CodeBlock {
/// Name of this function /// Name of this function
pub(crate) name: JsString, pub(crate) name: JsString,
// The length of this function. /// The number of arguments expected.
pub(crate) length: u32, pub(crate) length: u32,
/// Is this function in strict mode. /// Is this function in strict mode.
@ -52,6 +60,7 @@ pub struct CodeBlock {
/// [[ThisMode]] /// [[ThisMode]]
pub(crate) this_mode: ThisMode, pub(crate) this_mode: ThisMode,
/// Parameters passed to this function.
pub(crate) params: Box<[FormalParameter]>, pub(crate) params: Box<[FormalParameter]>,
/// Bytecode /// Bytecode
@ -63,7 +72,7 @@ pub struct CodeBlock {
/// Variables names /// Variables names
pub(crate) variables: Vec<JsString>, pub(crate) variables: Vec<JsString>,
// Functions inside this function /// Functions inside this function
pub(crate) functions: Vec<Gc<CodeBlock>>, pub(crate) functions: Vec<Gc<CodeBlock>>,
/// Indicates if the codeblock contains a lexical name `arguments` /// Indicates if the codeblock contains a lexical name `arguments`
@ -71,6 +80,7 @@ pub struct CodeBlock {
} }
impl CodeBlock { impl CodeBlock {
/// Constructs a new `CodeBlock`.
pub fn new(name: JsString, length: u32, strict: bool, constructor: bool) -> Self { pub fn new(name: JsString, length: u32, strict: bool, constructor: bool) -> Self {
Self { Self {
code: Vec::new(), code: Vec::new(),
@ -108,6 +118,10 @@ impl CodeBlock {
unsafe { self.read_unchecked(offset) } 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 { pub(crate) fn instruction_operands(&self, pc: &mut usize) -> String {
let opcode: Opcode = self.code[*pc].try_into().unwrap(); let opcode: Opcode = self.code[*pc].try_into().unwrap();
*pc += size_of::<Opcode>(); *pc += size_of::<Opcode>();

Loading…
Cancel
Save