Skip to content

Commit

Permalink
Change name field type in CodeBlock to JsString (#3107)
Browse files Browse the repository at this point in the history
* Change name type to `JsString`

* cargo-clippy
  • Loading branch information
HalidOdat committed Jul 9, 2023
1 parent 49f0059 commit 7ee922b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
8 changes: 7 additions & 1 deletion boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 17 additions & 29 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CodeBlockFlags>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -360,7 +359,9 @@ impl CodeBlock {
*pc += size_of::<u32>() + size_of::<u8>();
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
)
}
Expand All @@ -369,7 +370,9 @@ impl CodeBlock {
*pc += size_of::<u32>();
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
)
}
Expand Down Expand Up @@ -623,16 +626,16 @@ 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 == "<main>" {
String::new()
} else {
"\n".to_owned()
};

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;
Expand Down Expand Up @@ -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
));
}
Expand All @@ -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::<JsString>(false)
.into();

let name: JsValue = code.name().clone().into();
let length: JsValue = code.length.into();

let script_or_module = context.vm.active_runnable.clone();
Expand Down Expand Up @@ -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::<JsString>(false)
.into();

let name: JsValue = code.name().clone().into();
let length: JsValue = code.length.into();

let script_or_module = context.vm.active_runnable.clone();
Expand Down Expand Up @@ -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::<JsString>(false),
)
.value(code.name().clone())
.writable(false)
.enumerable(false)
.configurable(true)
Expand Down
19 changes: 10 additions & 9 deletions boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>") {
"__main__".to_string()
} else {
interner.resolve_expect(self.name).to_string()
self.name().to_std_string_escaped()
};

graph.set_label(name);
Expand Down Expand Up @@ -406,9 +407,9 @@ impl CodeBlock {
| Opcode::GetFunction
| Opcode::GetFunctionAsync => {
let operand = self.read::<u32>(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::<u32>() + size_of::<u8>();
let label = format!(
"{opcode_str} '{fn_name}' (length: {})",
Expand All @@ -419,9 +420,9 @@ impl CodeBlock {
}
Opcode::GetGenerator | Opcode::GetGeneratorAsync => {
let operand = self.read::<u32>(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
Expand Down
2 changes: 1 addition & 1 deletion boa_runtime/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl Console {
.map(|frame| frame.code_block().name())
.collect::<Vec<_>>()
.into_iter()
.map(|s| context.interner().resolve_expect(s).to_string())
.map(JsString::to_std_string_escaped)
.collect::<Vec<_>>()
.join("\n");
logger(LogMessage::Log(stack_trace_dump), console);
Expand Down

0 comments on commit 7ee922b

Please sign in to comment.