From 7ee922bd76805a3470562110589b0c72300800d2 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:37:23 +0200 Subject: [PATCH] Change `name` field type in `CodeBlock` to `JsString` (#3107) * Change name type to `JsString` * cargo-clippy --- boa_engine/src/bytecompiler/mod.rs | 8 +++++- boa_engine/src/vm/code_block.rs | 46 +++++++++++------------------- boa_engine/src/vm/flowgraph/mod.rs | 19 ++++++------ boa_runtime/src/console/mod.rs | 2 +- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/boa_engine/src/bytecompiler/mod.rs b/boa_engine/src/bytecompiler/mod.rs index 2b6f6215d3..927a940738 100644 --- a/boa_engine/src/bytecompiler/mod.rs +++ b/boa_engine/src/bytecompiler/mod.rs @@ -1387,8 +1387,14 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> { #[must_use] #[allow(clippy::missing_const_for_fn)] pub fn finish(self) -> CodeBlock { + let name = self + .context + .interner() + .resolve_expect(self.function_name) + .utf16() + .into(); CodeBlock { - name: self.function_name, + name, length: self.length, this_mode: self.this_mode, params: self.params, diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index da9bd0ca8b..b72b234a03 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -21,7 +21,6 @@ use crate::{ use bitflags::bitflags; use boa_ast::function::FormalParameterList; use boa_gc::{empty_trace, Finalize, Gc, Trace}; -use boa_interner::Sym; use boa_profiler::Profiler; use std::{cell::Cell, collections::VecDeque, mem::size_of, rc::Rc}; use thin_vec::ThinVec; @@ -97,7 +96,7 @@ unsafe impl Trace for CodeBlockFlags { pub struct CodeBlock { /// Name of this function #[unsafe_ignore_trace] - pub(crate) name: Sym, + pub(crate) name: JsString, #[unsafe_ignore_trace] pub(crate) flags: Cell, @@ -141,7 +140,7 @@ pub struct CodeBlock { impl CodeBlock { /// Creates a new `CodeBlock`. #[must_use] - pub fn new(name: Sym, length: u32, strict: bool) -> Self { + pub fn new(name: JsString, length: u32, strict: bool) -> Self { let mut flags = CodeBlockFlags::empty(); flags.set(CodeBlockFlags::STRICT, strict); Self { @@ -161,8 +160,8 @@ impl CodeBlock { /// Retrieves the name associated with this code block. #[must_use] - pub const fn name(&self) -> Sym { - self.name + pub const fn name(&self) -> &JsString { + &self.name } /// Check if the function is traced. @@ -360,7 +359,9 @@ impl CodeBlock { *pc += size_of::() + size_of::(); format!( "{operand:04}: '{}' (length: {})", - interner.resolve_expect(self.functions[operand as usize].name), + self.functions[operand as usize] + .name() + .to_std_string_escaped(), self.functions[operand as usize].length ) } @@ -369,7 +370,9 @@ impl CodeBlock { *pc += size_of::(); format!( "{operand:04}: '{}' (length: {})", - interner.resolve_expect(self.functions[operand as usize].name), + self.functions[operand as usize] + .name() + .to_std_string_escaped(), self.functions[operand as usize].length ) } @@ -623,8 +626,8 @@ impl CodeBlock { #[cfg(any(feature = "trace", feature = "flowgraph"))] impl ToInternedString for CodeBlock { fn to_interned_string(&self, interner: &Interner) -> String { - let name = interner.resolve_expect(self.name); - let mut f = if self.name == Sym::MAIN { + let name = self.name(); + let mut f = if name == "
" { String::new() } else { "\n".to_owned() @@ -632,7 +635,7 @@ impl ToInternedString for CodeBlock { f.push_str(&format!( "{:-^70}\nLocation Count Opcode Operands\n\n", - format!("Compiled Output: '{name}'"), + format!("Compiled Output: '{}'", name.to_std_string_escaped()), )); let mut pc = 0; @@ -681,7 +684,7 @@ impl ToInternedString for CodeBlock { for (i, code) in self.functions.iter().enumerate() { f.push_str(&format!( " {i:04}: name: '{}' (length: {})\n", - interner.resolve_expect(code.name), + code.name().to_std_string_escaped(), code.length )); } @@ -708,12 +711,7 @@ pub(crate) fn create_function_object( ) -> JsObject { let _timer = Profiler::global().start_event("create_function_object", "vm"); - let name: JsValue = context - .interner() - .resolve_expect(code.name) - .into_common::(false) - .into(); - + let name: JsValue = code.name().clone().into(); let length: JsValue = code.length.into(); let script_or_module = context.vm.active_runnable.clone(); @@ -793,12 +791,7 @@ pub(crate) fn create_function_object_fast( ) -> JsObject { let _timer = Profiler::global().start_event("create_function_object_fast", "vm"); - let name: JsValue = context - .interner() - .resolve_expect(code.name) - .into_common::(false) - .into(); - + let name: JsValue = code.name().clone().into(); let length: JsValue = code.length.into(); let script_or_module = context.vm.active_runnable.clone(); @@ -883,12 +876,7 @@ pub(crate) fn create_generator_function_object( }; let name_property = PropertyDescriptor::builder() - .value( - context - .interner() - .resolve_expect(code.name) - .into_common::(false), - ) + .value(code.name().clone()) .writable(false) .enumerable(false) .configurable(true) diff --git a/boa_engine/src/vm/flowgraph/mod.rs b/boa_engine/src/vm/flowgraph/mod.rs index 6e632d66bb..9b281cfbae 100644 --- a/boa_engine/src/vm/flowgraph/mod.rs +++ b/boa_engine/src/vm/flowgraph/mod.rs @@ -1,7 +1,8 @@ //! This module is responsible for generating the vm instruction flowgraph. use crate::vm::{CodeBlock, Opcode}; -use boa_interner::{Interner, Sym}; +use boa_interner::Interner; +use boa_macros::utf16; use std::mem::size_of; mod color; @@ -18,10 +19,10 @@ impl CodeBlock { /// Output the [`CodeBlock`] VM instructions into a [`Graph`]. pub fn to_graph(&self, interner: &Interner, graph: &mut SubGraph) { // Have to remove any invalid graph chars like `<` or `>`. - let name = if self.name == Sym::MAIN { + let name = if self.name() == utf16!("
") { "__main__".to_string() } else { - interner.resolve_expect(self.name).to_string() + self.name().to_std_string_escaped() }; graph.set_label(name); @@ -406,9 +407,9 @@ impl CodeBlock { | Opcode::GetFunction | Opcode::GetFunctionAsync => { let operand = self.read::(pc); - let fn_name = interner - .resolve_expect(self.functions[operand as usize].name) - .to_string(); + let fn_name = self.functions[operand as usize] + .name() + .to_std_string_escaped(); pc += size_of::() + size_of::(); let label = format!( "{opcode_str} '{fn_name}' (length: {})", @@ -419,9 +420,9 @@ impl CodeBlock { } Opcode::GetGenerator | Opcode::GetGeneratorAsync => { let operand = self.read::(pc); - let fn_name = interner - .resolve_expect(self.functions[operand as usize].name) - .to_string(); + let fn_name = self.functions[operand as usize] + .name() + .to_std_string_escaped(); let label = format!( "{opcode_str} '{fn_name}' (length: {})", self.functions[operand as usize].length diff --git a/boa_runtime/src/console/mod.rs b/boa_runtime/src/console/mod.rs index dac9a2e8db..dbba6da283 100644 --- a/boa_runtime/src/console/mod.rs +++ b/boa_runtime/src/console/mod.rs @@ -360,7 +360,7 @@ impl Console { .map(|frame| frame.code_block().name()) .collect::>() .into_iter() - .map(|s| context.interner().resolve_expect(s).to_string()) + .map(JsString::to_std_string_escaped) .collect::>() .join("\n"); logger(LogMessage::Log(stack_trace_dump), console);