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

[Merged by Bors] - Convert Codeblock variables to Sym #1798

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 44 additions & 78 deletions boa/src/bytecompiler.rs

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions boa/src/syntax/ast/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ pub enum PropertyDefinition {
///
/// [spec]: https://tc39.es/ecma262/#prod-IdentifierReference
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Property_definitions
IdentifierReference(Box<str>),
IdentifierReference(Sym),

/// Binds a property name to a JavaScript value.
///
Expand Down Expand Up @@ -486,11 +486,8 @@ pub enum PropertyDefinition {

impl PropertyDefinition {
/// Creates an `IdentifierReference` property definition.
pub fn identifier_reference<I>(ident: I) -> Self
where
I: Into<Box<str>>,
{
Self::IdentifierReference(ident.into())
pub fn identifier_reference(ident: Sym) -> Self {
Self::IdentifierReference(ident)
}

/// Creates a `Property` definition.
Expand Down
4 changes: 2 additions & 2 deletions boa/src/syntax/ast/node/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Object {
let indentation = " ".repeat(indent + 1);
for property in self.properties().iter() {
buf.push_str(&match property {
PropertyDefinition::IdentifierReference(key) => {
format!("{}{},\n", indentation, key)
PropertyDefinition::IdentifierReference(ident) => {
format!("{}{},\n", indentation, interner.resolve_expect(*ident))
}
PropertyDefinition::Property(key, value) => {
format!(
Expand Down
10 changes: 7 additions & 3 deletions boa/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
property::PropertyDescriptor,
syntax::ast::node::FormalParameter,
vm::{call_frame::FinallyReturn, CallFrame, Opcode},
Context, JsResult, JsString, JsValue,
Context, JsResult, JsValue,
};
use boa_interner::{Interner, Sym, ToInternedString};
use std::{convert::TryInto, mem::size_of};
Expand Down Expand Up @@ -75,7 +75,7 @@ pub struct CodeBlock {
pub(crate) literals: Vec<JsValue>,

/// Variables names
pub(crate) variables: Vec<JsString>,
pub(crate) variables: Vec<Sym>,

/// Functions inside this function
pub(crate) functions: Vec<Gc<CodeBlock>>,
Expand Down Expand Up @@ -344,7 +344,11 @@ impl ToInternedString for CodeBlock {
f.push_str("\nNames:\n");
if !self.variables.is_empty() {
for (i, value) in self.variables.iter().enumerate() {
f.push_str(&format!(" {:04}: {}\n", i, value));
f.push_str(&format!(
" {:04}: {}\n",
i,
interner.resolve_expect(*value)
));
}
} else {
f.push_str(" <empty>");
Expand Down
53 changes: 23 additions & 30 deletions boa/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
function_environment_record::{BindingStatus, FunctionEnvironmentRecord},
lexical_environment::{Environment, VariableScope},
},
property::PropertyDescriptor,
property::{PropertyDescriptor, PropertyKey},
value::Numeric,
vm::{call_frame::CatchAddresses, code_block::Readable},
BoaProfiler, Context, JsBigInt, JsResult, JsString, JsValue,
Expand Down Expand Up @@ -301,8 +301,7 @@ impl Context {
}
Opcode::DefInitArg => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];
let value = self.vm.pop();
let local_env = self.get_current_environment();
local_env
Expand All @@ -312,8 +311,7 @@ impl Context {
}
Opcode::DefVar => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];

if !self.has_binding(name)? {
self.create_mutable_binding(name, false, VariableScope::Function)?;
Expand All @@ -322,8 +320,7 @@ impl Context {
}
Opcode::DefInitVar => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];
let value = self.vm.pop();

if self.has_binding(name)? {
Expand All @@ -335,42 +332,37 @@ impl Context {
}
Opcode::DefLet => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];

self.create_mutable_binding(name, false, VariableScope::Block)?;
self.initialize_binding(name, JsValue::Undefined)?;
}
Opcode::DefInitLet => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];
let value = self.vm.pop();

self.create_mutable_binding(name, false, VariableScope::Block)?;
self.initialize_binding(name, value)?;
}
Opcode::DefInitConst => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];
let value = self.vm.pop();

self.create_immutable_binding(name, true, VariableScope::Block)?;
self.initialize_binding(name, value)?;
}
Opcode::GetName => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];

let value = self.get_binding_value(name)?;
self.vm.push(value);
}
Opcode::GetNameOrUndefined => {
let index = self.vm.read::<u32>();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];

let value = if self.has_binding(name)? {
self.get_binding_value(name)?
Expand All @@ -382,8 +374,7 @@ impl Context {
Opcode::SetName => {
let index = self.vm.read::<u32>();
let value = self.vm.pop();
let name_str = self.vm.frame().code.variables[index as usize].clone();
let name = self.interner_mut().get_or_intern(name_str);
let name = self.vm.frame().code.variables[index as usize];

self.set_mutable_binding(
name,
Expand Down Expand Up @@ -447,7 +438,8 @@ impl Context {
value.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize].clone();
let name = self.vm.frame().code.variables[index as usize];
let name: PropertyKey = self.interner().resolve_expect(name).into();
let result = object.get(name, self)?;

self.vm.push(result)
Expand Down Expand Up @@ -477,7 +469,8 @@ impl Context {
object.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize].clone();
let name = self.vm.frame().code.variables[index as usize];
let name: PropertyKey = self.interner().resolve_expect(name).into();

object.set(
name,
Expand All @@ -497,7 +490,8 @@ impl Context {
object.to_object(self)?
};

let name = self.vm.frame().code.variables[index as usize].clone();
let name = self.vm.frame().code.variables[index as usize];
let name = self.interner().resolve_expect(name);

object.__define_own_property__(
name.into(),
Expand Down Expand Up @@ -557,9 +551,8 @@ impl Context {
let value = self.vm.pop();
let object = object.to_object(self)?;

let name = self.vm.frame().code.variables[index as usize]
.clone()
.into();
let name = self.vm.frame().code.variables[index as usize];
let name = self.interner().resolve_expect(name).into();
let set = object
.__get_own_property__(&name, self)?
.as_ref()
Expand Down Expand Up @@ -603,9 +596,8 @@ impl Context {
let object = self.vm.pop();
let value = self.vm.pop();
let object = object.to_object(self)?;
let name = self.vm.frame().code.variables[index as usize]
.clone()
.into();
let name = self.vm.frame().code.variables[index as usize];
let name = self.interner().resolve_expect(name).into();
let get = object
.__get_own_property__(&name, self)?
.as_ref()
Expand Down Expand Up @@ -646,9 +638,10 @@ impl Context {
}
Opcode::DeletePropertyByName => {
let index = self.vm.read::<u32>();
let key = self.vm.frame().code.variables[index as usize].clone();
let key = self.vm.frame().code.variables[index as usize];
let key = self.interner().resolve_expect(key).into();
let object = self.vm.pop();
let result = object.to_object(self)?.__delete__(&key.into(), self)?;
let result = object.to_object(self)?.__delete__(&key, self)?;
if !result && self.strict() || self.vm.frame().code.strict {
return Err(self.construct_type_error("Cannot delete property"));
}
Expand Down
8 changes: 6 additions & 2 deletions boa_interner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ impl Sym {
/// Symbol for the `"<main>"` string.
pub const MAIN: Self = unsafe { Self::from_raw(NonZeroUsize::new_unchecked(11)) };

/// Symbol for the `"raw"` string.
pub const RAW: Self = unsafe { Self::from_raw(NonZeroUsize::new_unchecked(12)) };

/// Creates a `Sym` from a raw `NonZeroUsize`.
const fn from_raw(value: NonZeroUsize) -> Self {
Self { value }
Expand Down Expand Up @@ -301,15 +304,15 @@ where
T: Display,
{
fn to_interned_string(&self, _interner: &Interner) -> String {
format!("{}", self)
self.to_string()
}
}

impl Interner {
/// List of commonly used static strings.
///
/// Make sure that any string added as a `Sym` constant is also added here.
const STATIC_STRINGS: [&'static str; 11] = [
const STATIC_STRINGS: [&'static str; 12] = [
"",
"arguments",
"await",
Expand All @@ -321,5 +324,6 @@ impl Interner {
"get",
"set",
"<main>",
"raw",
];
}
1 change: 1 addition & 0 deletions boa_interner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn check_constants() {
assert_eq!(Sym::GET, sym_from_usize(9));
assert_eq!(Sym::SET, sym_from_usize(10));
assert_eq!(Sym::MAIN, sym_from_usize(11));
assert_eq!(Sym::RAW, sym_from_usize(12));
}

#[test]
Expand Down