Skip to content

Commit

Permalink
Custom JavaScript string rust type (#1380)
Browse files Browse the repository at this point in the history
- Moved src/value/rcstring.rs => src/string.rs
  • Loading branch information
HalidOdat authored Jul 17, 2021
1 parent ecd56ca commit be4a872
Show file tree
Hide file tree
Showing 18 changed files with 534 additions and 204 deletions.
8 changes: 4 additions & 4 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
builtins::BuiltIn,
object::ObjectInitializer,
property::Attribute,
value::{display::display_obj, RcString, Value},
BoaProfiler, Context, Result,
value::{display::display_obj, Value},
BoaProfiler, Context, JsString, Result,
};
use rustc_hash::FxHashMap;
use std::time::SystemTime;
Expand Down Expand Up @@ -137,8 +137,8 @@ pub fn formatter(data: &[Value], context: &mut Context) -> Result<String> {
/// This is the internal console object state.
#[derive(Debug, Default)]
pub(crate) struct Console {
count_map: FxHashMap<RcString, u32>,
timer_map: FxHashMap<RcString, u128>,
count_map: FxHashMap<JsString, u32>,
timer_map: FxHashMap<JsString, u128>,
groups: Vec<String>,
}

Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
use crate::{
builtins::BuiltIn,
object::Object,
object::ObjectInitializer,
property::{Attribute, DataDescriptor, PropertyKey},
symbol::WellKnownSymbols,
value::IntegerOrInfinity,
BoaProfiler, Context, Result, Value,
};
use crate::{object::Object, symbol::WellKnownSymbols};
use serde::Serialize;
use serde_json::{self, ser::PrettyFormatter, Serializer, Value as JSONValue};

Expand Down
10 changes: 5 additions & 5 deletions boa/src/builtins/object/for_in_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::value::RcString;
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object},
gc::{Finalize, Trace},
object::{GcObject, ObjectData},
property::PropertyKey,
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result, Value,
symbol::WellKnownSymbols,
BoaProfiler, Context, JsString, Result, Value,
};
use crate::{property::PropertyKey, symbol::WellKnownSymbols};
use rustc_hash::FxHashSet;
use std::collections::VecDeque;

Expand All @@ -20,8 +20,8 @@ use std::collections::VecDeque;
#[derive(Debug, Clone, Finalize, Trace)]
pub struct ForInIterator {
object: Value,
visited_keys: FxHashSet<RcString>,
remaining_keys: VecDeque<RcString>,
visited_keys: FxHashSet<JsString>,
remaining_keys: VecDeque<JsString>,
object_was_visited: bool,
}

Expand Down
14 changes: 7 additions & 7 deletions boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{
object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
value::{IntegerOrInfinity, RcString, Value},
BoaProfiler, Context, Result,
value::{IntegerOrInfinity, Value},
BoaProfiler, Context, JsString, Result,
};
use regexp_string_iterator::RegExpStringIterator;
use regress::Regex;
Expand Down Expand Up @@ -650,7 +650,7 @@ impl RegExp {
/// [spec]: https://tc39.es/ecma262/#sec-regexpexec
pub(crate) fn abstract_exec(
this: &Value,
input: RcString,
input: JsString,
context: &mut Context,
) -> Result<Value> {
// 1. Assert: Type(R) is Object.
Expand Down Expand Up @@ -694,7 +694,7 @@ impl RegExp {
/// [spec]: https://tc39.es/ecma262/#sec-regexpbuiltinexec
pub(crate) fn abstract_builtin_exec(
this: &Value,
input: RcString,
input: JsString,
context: &mut Context,
) -> Result<Value> {
// 1. Assert: R is an initialized RegExp instance.
Expand Down Expand Up @@ -1160,7 +1160,7 @@ impl RegExp {
}

// 12. Let accumulatedResult be the empty String.
let mut accumulated_result = RcString::from("");
let mut accumulated_result = JsString::new("");

// 13. Let nextSourcePosition be 0.
let mut next_source_position = 0;
Expand Down Expand Up @@ -1227,7 +1227,7 @@ impl RegExp {

// k. If functionalReplace is true, then
// l. Else,
let replacement: RcString;
let replacement: JsString;
if functional_replace {
// i. Let replacerArgs be « matched ».
let mut replacer_args = vec![Value::from(matched)];
Expand Down Expand Up @@ -1553,7 +1553,7 @@ impl RegExp {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-advancestringindex
fn advance_string_index(s: RcString, index: usize, unicode: bool) -> usize {
fn advance_string_index(s: JsString, index: usize, unicode: bool) -> usize {
// Regress only works with utf8, so this function differs from the spec.

// 1. Assert: index ≤ 2^53 - 1.
Expand Down
9 changes: 4 additions & 5 deletions boa/src/builtins/regexp/regexp_string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ use crate::{
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
symbol::WellKnownSymbols,
value::RcString,
BoaProfiler, Context, Result, Value,
BoaProfiler, Context, JsString, Result, Value,
};

// TODO: See todos in create_regexp_string_iterator and next.
#[derive(Debug, Clone, Finalize, Trace)]
pub struct RegExpStringIterator {
matcher: Value,
string: RcString,
string: JsString,
global: bool,
unicode: bool,
completed: bool,
}

// TODO: See todos in create_regexp_string_iterator and next.
impl RegExpStringIterator {
fn new(matcher: Value, string: RcString, global: bool, unicode: bool) -> Self {
fn new(matcher: Value, string: JsString, global: bool, unicode: bool) -> Self {
Self {
matcher,
string,
Expand All @@ -51,7 +50,7 @@ impl RegExpStringIterator {
/// [spec]: https://tc39.es/ecma262/#sec-createregexpstringiterator
pub(crate) fn create_regexp_string_iterator(
matcher: &Value,
string: RcString,
string: JsString,
global: bool,
unicode: bool,
context: &mut Context,
Expand Down
28 changes: 13 additions & 15 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use crate::{
object::{ConstructorBuilder, Object, ObjectData},
property::Attribute,
symbol::WellKnownSymbols,
value::{RcString, Value},
BoaProfiler, Context, Result,
BoaProfiler, Context, JsString, Result, Value,
};
use std::{
char::{decode_utf16, from_u32},
Expand All @@ -31,7 +30,7 @@ use std::{
};
use unicode_normalization::UnicodeNormalization;

pub(crate) fn code_point_at(string: RcString, position: i32) -> Option<(u32, u8, bool)> {
pub(crate) fn code_point_at(string: JsString, position: i32) -> Option<(u32, u8, bool)> {
let size = string.encode_utf16().count() as i32;
if position < 0 || position >= size {
return None;
Expand Down Expand Up @@ -170,7 +169,7 @@ impl String {
.clone()
}
Some(ref value) => value.to_string(context)?,
None => RcString::default(),
None => JsString::default(),
};

if new_target.is_undefined() {
Expand Down Expand Up @@ -202,7 +201,7 @@ impl String {
Ok(this)
}

fn this_string_value(this: &Value, context: &mut Context) -> Result<RcString> {
fn this_string_value(this: &Value, context: &mut Context) -> Result<JsString> {
match this {
Value::String(ref string) => return Ok(string.clone()),
Value::Object(ref object) => {
Expand Down Expand Up @@ -685,10 +684,9 @@ impl String {

// 11. If functionalReplace is true, then
// 12. Else,
let replacement: RcString;
if functional_replace {
let replacement = if functional_replace {
// a. Let replacement be ? ToString(? Call(replaceValue, undefined, « searchString, 𝔽(position), string »)).
replacement = context
context
.call(
&replace_value,
&Value::Undefined,
Expand All @@ -698,22 +696,22 @@ impl String {
this_str.clone().into(),
],
)?
.to_string(context)?;
.to_string(context)?
} else {
// a. Assert: Type(replaceValue) is String.
// b. Let captures be a new empty List.
let captures = Vec::new();

// c. Let replacement be ! GetSubstitution(searchString, string, position, captures, undefined, replaceValue).
replacement = get_substitution(
get_substitution(
search_str.to_string(),
this_str.to_string(),
position.unwrap(),
captures,
Value::undefined(),
replace_value.to_string(context)?.to_string(),
)?;
}
)?
};

// 13. Return the string-concatenation of preserved, replacement, and the substring of string from position + searchLength.
Ok(format!(
Expand Down Expand Up @@ -862,9 +860,9 @@ impl String {
/// Performs the actual string padding for padStart/End.
/// <https://tc39.es/ecma262/#sec-stringpad/>
fn string_pad(
primitive: RcString,
primitive: JsString,
max_length: i32,
fill_string: Option<RcString>,
fill_string: Option<JsString>,
at_start: bool,
) -> Value {
let primitive_length = primitive.len() as i32;
Expand Down Expand Up @@ -1480,7 +1478,7 @@ pub(crate) fn get_substitution(
captures: Vec<Value>,
_named_captures: Value,
replacement: StdString,
) -> Result<RcString> {
) -> Result<JsString> {
// 1. Assert: Type(matched) is String.

// 2. Let matchLength be the number of code units in matched.
Expand Down
10 changes: 5 additions & 5 deletions boa/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
op::{AssignOp, BinOp, BitOp, CompOp, LogOp, NumOp, UnaryOp},
Const, Node,
},
value::{RcBigInt, RcString},
value::RcBigInt,
vm::{CodeBlock, Opcode},
Value,
JsString, Value,
};

use std::collections::HashMap;
Expand Down Expand Up @@ -34,7 +34,7 @@ fn u64_to_array(value: u64) -> [u8; 8] {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Literal {
String(RcString),
String(JsString),
BigInt(RcBigInt),
}

Expand Down Expand Up @@ -64,7 +64,7 @@ enum Access<'a> {
pub struct ByteCompiler {
code_block: CodeBlock,
literals_map: HashMap<Literal, u32>,
names_map: HashMap<RcString, u32>,
names_map: HashMap<JsString, u32>,
loops: Vec<LoopControlInfo>,
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl ByteCompiler {
return *index;
}

let name: RcString = name.into();
let name = JsString::new(name);
let index = self.code_block.names.len() as u32;
self.code_block.names.push(name.clone());
self.names_map.insert(name, index);
Expand Down
3 changes: 2 additions & 1 deletion boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub mod object;
pub mod profiler;
pub mod property;
pub mod realm;
pub mod string;
pub mod symbol;
// syntax module has a lot of acronyms
#[allow(clippy::upper_case_acronyms)]
Expand All @@ -71,7 +72,7 @@ pub(crate) use crate::{exec::Executable, profiler::BoaProfiler};

// Export things to root level
#[doc(inline)]
pub use crate::{context::Context, symbol::JsSymbol, value::Value};
pub use crate::{context::Context, string::JsString, symbol::JsSymbol, value::Value};

use crate::syntax::{
ast::node::StatementList,
Expand Down
14 changes: 7 additions & 7 deletions boa/src/object/iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Object, PropertyDescriptor, PropertyKey};
use crate::{value::RcString, JsSymbol};
use crate::{JsString, JsSymbol};
use std::{collections::hash_map, iter::FusedIterator};

impl Object {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Object {
#[derive(Debug, Clone)]
pub struct Iter<'a> {
indexed_properties: hash_map::Iter<'a, u32, PropertyDescriptor>,
string_properties: hash_map::Iter<'a, RcString, PropertyDescriptor>,
string_properties: hash_map::Iter<'a, JsString, PropertyDescriptor>,
symbol_properties: hash_map::Iter<'a, JsSymbol, PropertyDescriptor>,
}

Expand Down Expand Up @@ -342,10 +342,10 @@ impl FusedIterator for IndexPropertyValues<'_> {}

/// An iterator over the `String` property entries of an `Object`
#[derive(Debug, Clone)]
pub struct StringProperties<'a>(hash_map::Iter<'a, RcString, PropertyDescriptor>);
pub struct StringProperties<'a>(hash_map::Iter<'a, JsString, PropertyDescriptor>);

impl<'a> Iterator for StringProperties<'a> {
type Item = (&'a RcString, &'a PropertyDescriptor);
type Item = (&'a JsString, &'a PropertyDescriptor);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -369,10 +369,10 @@ impl FusedIterator for StringProperties<'_> {}

/// An iterator over the string keys (`RcString`) of an `Object`.
#[derive(Debug, Clone)]
pub struct StringPropertyKeys<'a>(hash_map::Keys<'a, RcString, PropertyDescriptor>);
pub struct StringPropertyKeys<'a>(hash_map::Keys<'a, JsString, PropertyDescriptor>);

impl<'a> Iterator for StringPropertyKeys<'a> {
type Item = &'a RcString;
type Item = &'a JsString;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -396,7 +396,7 @@ impl FusedIterator for StringPropertyKeys<'_> {}

/// An iterator over the string values (`Property`) of an `Object`.
#[derive(Debug, Clone)]
pub struct StringPropertyValues<'a>(hash_map::Values<'a, RcString, PropertyDescriptor>);
pub struct StringPropertyValues<'a>(hash_map::Values<'a, JsString, PropertyDescriptor>);

impl<'a> Iterator for StringPropertyValues<'a> {
type Item = &'a PropertyDescriptor;
Expand Down
Loading

0 comments on commit be4a872

Please sign in to comment.