Skip to content

Commit

Permalink
Integrate UTF-16 interner in the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Feb 27, 2022
1 parent e7786cb commit 70eca88
Show file tree
Hide file tree
Showing 42 changed files with 619 additions and 226 deletions.
48 changes: 32 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion boa_engine/examples/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Person {
// and print a message to stdout.
println!(
"Hello my name is {}, I'm {} years old",
person.name.as_std_string_lossy(),
person.name.to_std_string_escaped(),
person.age // Here we can access the native rust fields of the struct.
);
return Ok(JsValue::undefined());
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/examples/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() -> Result<(), JsValue> {
// We can also mutate the moved data inside the closure.
captures.greeting = js_string!(&captures.greeting, utf16!(" Hello!"));

println!("{}", message.as_std_string_lossy());
println!("{}", message.to_std_string_escaped());
println!();

// We convert `message` into `Jsvalue` to be able to return it.
Expand Down
24 changes: 12 additions & 12 deletions boa_engine/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ pub fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {

match data.len() {
0 => Ok(String::new()),
1 => Ok(target.as_std_string_lossy()),
1 => Ok(target.to_std_string_escaped()),
_ => {
let mut formatted = String::new();
let mut arg_index = 1;
let target = target.as_std_string_lossy();
let target = target.to_std_string_escaped();
let mut chars = target.chars();
while let Some(c) = chars.next() {
if c == '%' {
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {
.cloned()
.unwrap_or_default()
.to_string(context)?
.as_std_string_lossy();
.to_std_string_escaped();
formatted.push_str(&arg);
arg_index += 1;
}
Expand All @@ -123,7 +123,7 @@ pub fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {
for rest in data.iter().skip(arg_index) {
formatted.push_str(&format!(
" {}",
rest.to_string(context)?.as_std_string_lossy()
rest.to_string(context)?.to_std_string_escaped()
));
}

Expand Down Expand Up @@ -315,7 +315,7 @@ impl Console {
context
.interner()
.resolve_expect(frame.code.name)
.to_owned(),
.to_string(),
);
prev_frame = frame.prev.as_ref();
}
Expand Down Expand Up @@ -381,7 +381,7 @@ impl Console {
None => "default".into(),
};

let msg = format!("count {}:", label.as_std_string_lossy());
let msg = format!("count {}:", label.to_std_string_escaped());
let c = context.console_mut().count_map.entry(label).or_insert(0);
*c += 1;

Expand Down Expand Up @@ -412,7 +412,7 @@ impl Console {
context.console_mut().count_map.remove(&label);

logger(
LogMessage::Warn(format!("countReset {}", label.as_std_string_lossy())),
LogMessage::Warn(format!("countReset {}", label.to_std_string_escaped())),
context.console(),
);

Expand Down Expand Up @@ -447,7 +447,7 @@ impl Console {
logger(
LogMessage::Warn(format!(
"Timer '{}' already exist",
label.as_std_string_lossy()
label.to_std_string_escaped()
)),
context.console(),
);
Expand Down Expand Up @@ -481,7 +481,7 @@ impl Console {

if let Some(t) = context.console().timer_map.get(&label) {
let time = Self::system_time_in_ms();
let mut concat = format!("{}: {} ms", label.as_std_string_lossy(), time - t);
let mut concat = format!("{}: {} ms", label.to_std_string_escaped(), time - t);
for msg in args.iter().skip(1) {
concat = concat + " " + &msg.display().to_string();
}
Expand All @@ -490,7 +490,7 @@ impl Console {
logger(
LogMessage::Warn(format!(
"Timer '{}' doesn't exist",
label.as_std_string_lossy()
label.to_std_string_escaped()
)),
context.console(),
);
Expand Down Expand Up @@ -524,7 +524,7 @@ impl Console {
logger(
LogMessage::Info(format!(
"{}: {} ms - timer removed",
label.as_std_string_lossy(),
label.to_std_string_escaped(),
time - t
)),
context.console(),
Expand All @@ -533,7 +533,7 @@ impl Console {
logger(
LogMessage::Warn(format!(
"Timer '{}' doesn't exist",
label.as_std_string_lossy()
label.to_std_string_escaped()
)),
context.console(),
);
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl Date {
Ok(dt) => dt.0,
_ => match value.to_primitive(context, PreferredType::Default)? {
JsValue::String(ref str) => str
.as_std_string()
.to_std_string()
.ok()
.and_then(|s| chrono::DateTime::parse_from_rfc3339(s.as_str()).ok())
.map(|dt| dt.naive_utc()),
Expand Down Expand Up @@ -1860,7 +1860,7 @@ impl Date {
let date = date.to_string(context)?;

Ok(JsValue::new(
date.as_std_string()
date.to_std_string()
.ok()
.and_then(|s| DateTime::parse_from_rfc3339(s.as_str()).ok())
.map_or(f64::NAN, |v| v.naive_utc().timestamp_millis() as f64),
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Json {
.cloned()
.unwrap_or_default()
.to_string(context)?
.as_std_string()
.to_std_string()
.map_err(|e| context.construct_syntax_error(e.to_string()))?;

// 2. Parse ! StringToCodePoints(jsonString) as a JSON text as specified in ECMA-404.
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ impl Number {
) -> JsResult<JsValue> {
if let Some(val) = args.get(0) {
// TODO: parse float with optimal utf16 algorithm
let input_string = val.to_string(context)?.as_std_string_lossy();
let input_string = val.to_string(context)?.to_std_string_escaped();
let s = input_string.trim_start_matches(is_trimmable_whitespace);
let s_prefix_lower = s.chars().take(4).collect::<String>().to_ascii_lowercase();

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Object {
val => {
return ctx.throw_type_error(format!(
"expected an object or null, got {}",
val.type_of().as_std_string_lossy()
val.type_of().to_std_string_escaped()
))
}
};
Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl RegExp {
// 5. If F contains any code unit other than "g", "i", "m", "s", "u", or "y"
// or if it contains the same code unit more than once, throw a SyntaxError exception.
// TODO: Should directly parse the JsString instead of converting to String
let flags = match RegExpFlags::from_str(&f.as_std_string_lossy()) {
let flags = match RegExpFlags::from_str(&f.to_std_string_escaped()) {
Err(msg) => return context.throw_syntax_error(msg),
Ok(result) => result,
};
Expand All @@ -281,8 +281,8 @@ impl RegExp {
// 13. Set obj.[[OriginalFlags]] to F.
// 14. Set obj.[[RegExpMatcher]] to the Abstract Closure that evaluates parseResult by applying the semantics provided in 22.2.2 using patternCharacters as the pattern's List of SourceCharacter values and F as the flag parameters.
// TODO: add support for utf16 regex to remove this conversions.
let ps = p.as_std_string_lossy();
let fs = f.as_std_string_lossy();
let ps = p.to_std_string_escaped();
let fs = f.to_std_string_escaped();
let matcher = match Regex::with_flags(&ps, fs.as_ref()) {
Err(error) => {
return context
Expand Down Expand Up @@ -821,7 +821,7 @@ impl RegExp {
}
};
let r = matcher
.find_from(input.as_std_string_lossy().as_str(), last_byte_index)
.find_from(input.to_std_string_escaped().as_str(), last_byte_index)
.next();

match r {
Expand Down Expand Up @@ -867,7 +867,7 @@ impl RegExp {

// 13. Let e be r's endIndex value.
let mut e = match_value.end();
let lossy_input = input.as_std_string_lossy();
let lossy_input = input.to_std_string_escaped();

// 14. If fullUnicode is true, then
// TODO: disabled for now until we have UTF-16 support
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ fn char_at() {
assert_eq!(forward(&mut context, "'abc'.charAt(9)"), "\"\"");
assert_eq!(forward(&mut context, "'abc'.charAt()"), "\"a\"");
assert_eq!(forward(&mut context, "'abc'.charAt(null)"), "\"a\"");
assert_eq!(forward(&mut context, "'\\uDBFF'.charAt(0)"), "\"\u{FFFD}\"");
assert_eq!(forward(&mut context, "'\\uDBFF'.charAt(0)"), r#""\uDBFF""#);
}

#[test]
Expand Down
Loading

0 comments on commit 70eca88

Please sign in to comment.