Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change name field type in CodeBlock to JsString #3107

Merged
merged 2 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,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, RefCell},
Expand Down Expand Up @@ -102,7 +101,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 @@ -146,7 +145,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 @@ -166,8 +165,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 @@ -365,7 +364,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 @@ -374,7 +375,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 @@ -628,16 +631,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 @@ -686,7 +689,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 @@ -713,12 +716,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 @@ -798,12 +796,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 @@ -888,12 +881,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