Skip to content

Commit

Permalink
Apply suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed May 11, 2023
1 parent ee3146a commit 06bd977
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
10 changes: 5 additions & 5 deletions boa_engine/src/bytecompiler/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,11 @@ impl ByteCompiler<'_, '_> {
}
}

let site = template.identifier() as u32;
let site = template.identifier();
let count = template.cookeds().len() as u32;

let index = self.next_opcode_location();
self.emit(Opcode::TemplateLookup, &[Self::DUMMY_ADDRESS, site]);
let jump_label = Label { index };
let jump_label = self.emit_opcode_with_operand(Opcode::TemplateLookup);
self.emit_u64(site);

for (cooked, raw) in template.cookeds().iter().zip(template.raws()) {
if let Some(cooked) = cooked {
Expand All @@ -274,7 +273,8 @@ impl ByteCompiler<'_, '_> {
));
}

self.emit(Opcode::TemplateCreate, &[site, count]);
self.emit(Opcode::TemplateCreate, &[count]);
self.emit_u64(site);

self.patch_jump(jump_label);

Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Inner {
environment: Gc<DeclarativeEnvironment>,
global_object: JsObject,
global_this: JsObject,
template_map: GcRefCell<FxHashMap<u32, JsObject>>,
template_map: GcRefCell<FxHashMap<u64, JsObject>>,
}

impl Realm {
Expand Down Expand Up @@ -106,11 +106,11 @@ impl Realm {
}
}

pub(crate) fn push_template(&self, site: u32, template: JsObject) {
pub(crate) fn push_template(&self, site: u64, template: JsObject) {
self.inner.template_map.borrow_mut().insert(site, template);
}

pub(crate) fn lookup_template(&self, site: u32) -> Option<JsObject> {
pub(crate) fn lookup_template(&self, site: u64) -> Option<JsObject> {
self.inner.template_map.borrow().get(&site).cloned()
}
}
11 changes: 8 additions & 3 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,20 @@ impl CodeBlock {
| Opcode::LoopStart
| Opcode::TryStart
| Opcode::AsyncGeneratorNext
| Opcode::GeneratorAsyncDelegateNext
| Opcode::TemplateLookup
| Opcode::TemplateCreate => {
| Opcode::GeneratorAsyncDelegateNext => {
let operand1 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
let operand2 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
format!("{operand1}, {operand2}")
}
Opcode::TemplateLookup | Opcode::TemplateCreate => {
let operand1 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
let operand2 = self.read::<u64>(*pc);
*pc += size_of::<u64>();
format!("{operand1}, {operand2}")
}
Opcode::GeneratorAsyncDelegateResume => {
let operand1 = self.read::<u32>(*pc);
*pc += size_of::<u32>();
Expand Down
12 changes: 11 additions & 1 deletion boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::Red);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::LoopStart | Opcode::TemplateLookup | Opcode::TemplateCreate => {
Opcode::LoopStart => {
let start_address = self.read::<u32>(pc);
pc += size_of::<u32>();
let end_address = self.read::<u32>(pc);
Expand All @@ -148,6 +148,16 @@ impl CodeBlock {
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::Red);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::TemplateLookup | Opcode::TemplateCreate => {
let start_address = self.read::<u32>(pc);
pc += size_of::<u32>();
let end_address = self.read::<u64>(pc);
pc += size_of::<u64>();

let label = format!("{opcode_str} {start_address}, {end_address}");
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::Red);
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::Break => {
let jump_operand = self.read::<u32>(pc);
pc += size_of::<u32>();
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,14 +1607,14 @@ generate_impl! {

/// Lookup if a tagged template object is cached and skip the creation if it is.
///
/// Operands: jump: `u32`, site: `u32`
/// Operands: jump: `u32`, site: `u64`
///
/// Stack: **=>** template (if cached)
TemplateLookup,

/// Create a new tagged template object and cache it.
///
/// Operands: site: `u32`, count: `u32`
/// Operands: count: `u32`, site: `u64`
///
/// Stack: count * (cooked_value, raw_value) **=>** template
TemplateCreate,
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/templates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Operation for TemplateLookup {

fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let jump = context.vm.read::<u32>();
let site = context.vm.read::<u32>();
let site = context.vm.read::<u64>();

if let Some(template) = context.realm().lookup_template(site) {
context.vm.push(template);
Expand All @@ -43,8 +43,8 @@ impl Operation for TemplateCreate {
const INSTRUCTION: &'static str = "INST - TemplateCreate";

fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let site = context.vm.read::<u32>();
let count = context.vm.read::<u32>();
let site = context.vm.read::<u64>();

let template =
Array::array_create(count.into(), None, context).expect("cannot fail per spec");
Expand Down
12 changes: 6 additions & 6 deletions boa_parser/src/parser/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::{
use boa_ast::{Position, Punctuator};
use boa_interner::Interner;
use buffered_lexer::BufferedLexer;
use rustc_hash::FxHasher;
use std::{hash::Hasher, io::Read};
use std::io::Read;

/// The result of a peek for a semicolon.
#[derive(Debug)]
Expand Down Expand Up @@ -188,11 +187,12 @@ where
/// Get the identifier for a tagged template.
#[inline]
pub(super) fn tagged_template_identifier(&mut self) -> u64 {
let mut hasher = FxHasher::default();
hasher.write_u32(self.identifier);
hasher.write_u32(self.tagged_templates_count);
self.tagged_templates_count += 1;
hasher.finish()

let identifier = u64::from(self.identifier);
let count = u64::from(self.tagged_templates_count);

(count << 32) | identifier
}

/// Returns an error if the next token is not of kind `kind`.
Expand Down

0 comments on commit 06bd977

Please sign in to comment.