From 06bd97756388230ba3625849e5e274643461edda Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Thu, 11 May 2023 04:36:56 +0200 Subject: [PATCH] Apply suggestion --- boa_engine/src/bytecompiler/expression/mod.rs | 10 +++++----- boa_engine/src/realm.rs | 6 +++--- boa_engine/src/vm/code_block.rs | 11 ++++++++--- boa_engine/src/vm/flowgraph/mod.rs | 12 +++++++++++- boa_engine/src/vm/opcode/mod.rs | 4 ++-- boa_engine/src/vm/opcode/templates/mod.rs | 4 ++-- boa_parser/src/parser/cursor/mod.rs | 12 ++++++------ 7 files changed, 37 insertions(+), 22 deletions(-) diff --git a/boa_engine/src/bytecompiler/expression/mod.rs b/boa_engine/src/bytecompiler/expression/mod.rs index 2131dad8c13..2203c95d4c9 100644 --- a/boa_engine/src/bytecompiler/expression/mod.rs +++ b/boa_engine/src/bytecompiler/expression/mod.rs @@ -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 { @@ -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); diff --git a/boa_engine/src/realm.rs b/boa_engine/src/realm.rs index bf62830a8c3..37d48d59eda 100644 --- a/boa_engine/src/realm.rs +++ b/boa_engine/src/realm.rs @@ -49,7 +49,7 @@ struct Inner { environment: Gc, global_object: JsObject, global_this: JsObject, - template_map: GcRefCell>, + template_map: GcRefCell>, } impl Realm { @@ -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 { + pub(crate) fn lookup_template(&self, site: u64) -> Option { self.inner.template_map.borrow().get(&site).cloned() } } diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index a1fe40d9c7f..3de7dfed4fe 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -293,15 +293,20 @@ impl CodeBlock { | Opcode::LoopStart | Opcode::TryStart | Opcode::AsyncGeneratorNext - | Opcode::GeneratorAsyncDelegateNext - | Opcode::TemplateLookup - | Opcode::TemplateCreate => { + | Opcode::GeneratorAsyncDelegateNext => { let operand1 = self.read::(*pc); *pc += size_of::(); let operand2 = self.read::(*pc); *pc += size_of::(); format!("{operand1}, {operand2}") } + Opcode::TemplateLookup | Opcode::TemplateCreate => { + let operand1 = self.read::(*pc); + *pc += size_of::(); + let operand2 = self.read::(*pc); + *pc += size_of::(); + format!("{operand1}, {operand2}") + } Opcode::GeneratorAsyncDelegateResume => { let operand1 = self.read::(*pc); *pc += size_of::(); diff --git a/boa_engine/src/vm/flowgraph/mod.rs b/boa_engine/src/vm/flowgraph/mod.rs index b29e0990fcf..eb70abc6ca0 100644 --- a/boa_engine/src/vm/flowgraph/mod.rs +++ b/boa_engine/src/vm/flowgraph/mod.rs @@ -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::(pc); pc += size_of::(); let end_address = self.read::(pc); @@ -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::(pc); + pc += size_of::(); + let end_address = self.read::(pc); + pc += size_of::(); + + 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::(pc); pc += size_of::(); diff --git a/boa_engine/src/vm/opcode/mod.rs b/boa_engine/src/vm/opcode/mod.rs index f0ec9383321..0a57ec89852 100644 --- a/boa_engine/src/vm/opcode/mod.rs +++ b/boa_engine/src/vm/opcode/mod.rs @@ -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, diff --git a/boa_engine/src/vm/opcode/templates/mod.rs b/boa_engine/src/vm/opcode/templates/mod.rs index aae538a8bcd..4e193d4ffa2 100644 --- a/boa_engine/src/vm/opcode/templates/mod.rs +++ b/boa_engine/src/vm/opcode/templates/mod.rs @@ -20,7 +20,7 @@ impl Operation for TemplateLookup { fn execute(context: &mut Context<'_>) -> JsResult { let jump = context.vm.read::(); - let site = context.vm.read::(); + let site = context.vm.read::(); if let Some(template) = context.realm().lookup_template(site) { context.vm.push(template); @@ -43,8 +43,8 @@ impl Operation for TemplateCreate { const INSTRUCTION: &'static str = "INST - TemplateCreate"; fn execute(context: &mut Context<'_>) -> JsResult { - let site = context.vm.read::(); let count = context.vm.read::(); + let site = context.vm.read::(); let template = Array::array_create(count.into(), None, context).expect("cannot fail per spec"); diff --git a/boa_parser/src/parser/cursor/mod.rs b/boa_parser/src/parser/cursor/mod.rs index 22eeb5805ad..0b91a3cb060 100644 --- a/boa_parser/src/parser/cursor/mod.rs +++ b/boa_parser/src/parser/cursor/mod.rs @@ -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)] @@ -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`.