Skip to content

Commit

Permalink
Merge da9bff7 into 128f836
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad authored Mar 12, 2022
2 parents 128f836 + da9bff7 commit 4bea602
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 120 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl<'b> ByteCompiler<'b> {
return *index;
}

let index = self.code_block.variables.len() as u32;
self.code_block.variables.push(name);
let index = self.code_block.names.len() as u32;
self.code_block.names.push(name);
self.names_map.insert(name, index);
index
}
Expand Down
26 changes: 13 additions & 13 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct CodeBlock {
pub(crate) literals: Vec<JsValue>,

/// Property field names.
pub(crate) variables: Vec<Sym>,
pub(crate) names: Vec<Sym>,

/// Locators for all bindings in the codeblock.
#[unsafe_ignore_trace]
Expand All @@ -104,7 +104,7 @@ impl CodeBlock {
Self {
code: Vec::new(),
literals: Vec::new(),
variables: Vec::new(),
names: Vec::new(),
bindings: Vec::new(),
num_bindings: 0,
functions: Vec::new(),
Expand Down Expand Up @@ -242,7 +242,7 @@ impl CodeBlock {
*pc += size_of::<u32>();
format!(
"{operand:04}: '{}'",
interner.resolve_expect(self.variables[operand as usize]),
interner.resolve_expect(self.names[operand as usize]),
)
}
Opcode::Pop
Expand Down Expand Up @@ -342,26 +342,26 @@ impl ToInternedString for CodeBlock {
};

f.push_str(&format!(
"{:-^70}\n Location Count Opcode Operands\n\n",
"{:-^70}\nLocation Count Opcode Operands\n\n",
format!("Compiled Output: '{name}'"),
));

let mut pc = 0;
let mut count = 0;
while pc < self.code.len() {
let opcode: Opcode = self.code[pc].try_into().expect("invalid opcode");
let opcode = opcode.as_str();
let operands = self.instruction_operands(&mut pc, interner);
f.push_str(&format!(
" {pc:06} {count:04} {:<27}\n{operands}",
opcode.as_str(),
"{pc:06} {count:04} {opcode:<27}{operands}\n",
));
count += 1;
}

f.push_str("\nLiterals:\n");

if self.literals.is_empty() {
f.push_str(" <empty>");
f.push_str(" <empty>\n");
} else {
for (i, value) in self.literals.iter().enumerate() {
f.push_str(&format!(
Expand All @@ -372,21 +372,21 @@ impl ToInternedString for CodeBlock {
}
}

f.push_str("\nNames:\n");
if self.variables.is_empty() {
f.push_str(" <empty>");
f.push_str("\nBindings:\n");
if self.bindings.is_empty() {
f.push_str(" <empty>\n");
} else {
for (i, value) in self.variables.iter().enumerate() {
for (i, binding_locator) in self.bindings.iter().enumerate() {
f.push_str(&format!(
" {i:04}: {}\n",
interner.resolve_expect(*value)
interner.resolve_expect(binding_locator.name())
));
}
}

f.push_str("\nFunctions:\n");
if self.functions.is_empty() {
f.push_str(" <empty>");
f.push_str(" <empty>\n");
} else {
for (i, code) in self.functions.iter().enumerate() {
f.push_str(&format!(
Expand Down
12 changes: 6 additions & 6 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ impl Context {
value.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize];
let name = self.vm.frame().code.names[index as usize];
let name: PropertyKey = self.interner().resolve_expect(name).into();
let result = object.get(name, self)?;

Expand Down Expand Up @@ -624,7 +624,7 @@ impl Context {
object.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize];
let name = self.vm.frame().code.names[index as usize];
let name: PropertyKey = self.interner().resolve_expect(name).into();

object.set(
Expand All @@ -645,7 +645,7 @@ impl Context {
object.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize];
let name = self.vm.frame().code.names[index as usize];
let name = self.interner().resolve_expect(name);

object.__define_own_property__(
Expand Down Expand Up @@ -706,7 +706,7 @@ impl Context {
let value = self.vm.pop();
let object = object.to_object(self)?;

let name = self.vm.frame().code.variables[index as usize];
let name = self.vm.frame().code.names[index as usize];
let name = self.interner().resolve_expect(name).into();
let set = object
.__get_own_property__(&name, self)?
Expand Down Expand Up @@ -751,7 +751,7 @@ impl Context {
let object = self.vm.pop();
let value = self.vm.pop();
let object = object.to_object(self)?;
let name = self.vm.frame().code.variables[index as usize];
let name = self.vm.frame().code.names[index as usize];
let name = self.interner().resolve_expect(name).into();
let get = object
.__get_own_property__(&name, self)?
Expand Down Expand Up @@ -793,7 +793,7 @@ impl Context {
}
Opcode::DeletePropertyByName => {
let index = self.vm.read::<u32>();
let key = self.vm.frame().code.variables[index as usize];
let key = self.vm.frame().code.names[index as usize];
let key = self.interner().resolve_expect(key).into();
let object = self.vm.pop();
let result = object.to_object(self)?.__delete__(&key, self)?;
Expand Down
57 changes: 13 additions & 44 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,14 @@ arguments to start a shell to execute JS.

These are added in order of how the code is read:

## Tokens
## Tokens and AST nodes

The first thing boa will do is generate tokens from source code. If the token
generation is wrong the rest of the operation will be wrong, this is usually
a good starting place.
The first thing boa will do is to generate tokens from the source code.
These tokens are then parsed into an abstract syntax tree (AST).
Any syntax errors should be thrown while the AST is generated.

To print the tokens to stdout, you can use the `boa_cli` command-line flag
`--dump-tokens` or `-t`, which can optionally take a format type. Supports
these formats: `Debug`, `Json`, `JsonPretty`. By default it is the `Debug`
format.

```bash
cargo run -- test.js --dump-tokens # token dump format is Debug by default.
```

or with interactive mode (REPL):

```bash
cargo run -- --dump-tokens # token dump format is Debug by default.
```

Seeing the order of tokens can be a big help to understanding what the parser
is working with.

**Note:** flags `--dump-tokens` and `--dump-ast` are mutually exclusive. When
using the flag `--dump-tokens`, the code will not be executed.

## AST nodes

Assuming the tokens looks fine, the next step is to see the AST. You can use
the `boa_cli` command-line flag `--dump-ast`, which can optionally take a
format type. Supports these formats: `Debug`, `Json`, `JsonPretty`. By default
You can use the `boa_cli` command-line flag `--dump-ast` to print the AST.
The flag supports these formats: `Debug`, `Json`, `JsonPretty`. By default
it is the `Debug` format.

Dumping the AST of a file:
Expand All @@ -59,23 +35,20 @@ or with interactive mode (REPL):
cargo run -- --dump-ast # AST dump format is Debug by default.
```

These methods will print out the entire parse tree.
## Bytecode generation and Execution

Once the AST has been generated boa will compile it into bytecode.
The bytecode is then executed by the vm.
You can print the bytecode and the executed instructions with the
flag command-line flag `--trace`.

**Note:** flags `--dump-tokens` and `--dump-ast` are mutually exclusive. When
using the flag `--dump-ast`, the code will not be executed.
For more detailed information about the vm and the trace output look [here](./vm.md).

## Compiler panics

In the case of a compiler panic, to get a full backtrace you will need to set
the environment variable `RUST_BACKTRACE=1`.

## Execution

Once the tree has been generated [exec](../boa/src/lib.rs#L92) will begin to
run through each node. If the tokens and tree looks fine, you can start looking
here. We usually just add `dbg!()` in the relevent places to see what the
output is at the time.

## Debugger

### VS Code Debugger
Expand All @@ -94,7 +67,3 @@ rust-lldb ./target/debug/boa [arguments]

[remote_containers]: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
[blog_debugging]: https://jason-williams.co.uk/debugging-rust-in-vscode

## VM

For debugging the new VM see [here](./vm.md)
Binary file added docs/img/boa_architecture.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4bea602

Please sign in to comment.