Browse Source

Change `name` field type in `CodeBlock` to `JsString` (#3107)

* Change name type to `JsString`

* cargo-clippy
pull/3124/head
Haled Odat 1 year ago committed by GitHub
parent
commit
7ee922bd76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      boa_engine/src/bytecompiler/mod.rs
  2. 46
      boa_engine/src/vm/code_block.rs
  3. 19
      boa_engine/src/vm/flowgraph/mod.rs
  4. 2
      boa_runtime/src/console/mod.rs

8
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,

46
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<CodeBlockFlags>,
@ -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::<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
)
}
@ -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
)
}
@ -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 == "<main>" {
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::<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();
@ -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();
@ -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)

19
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>") {
"__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::<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: {})",
@ -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

2
boa_runtime/src/console/mod.rs

@ -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);

Loading…
Cancel
Save