Skip to content

Commit

Permalink
Implement From<&str> for JsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Nov 4, 2023
1 parent 78e7d32 commit 7829b91
Show file tree
Hide file tree
Showing 73 changed files with 598 additions and 799 deletions.
4 changes: 2 additions & 2 deletions boa_cli/src/debug/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn r#type(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValu
let shape = object.shape();

Ok(if shape.is_shared() {
js_string!("shared")
"shared"
} else {
js_string!("unique")
"unique"
}
.into())
}
Expand Down
5 changes: 2 additions & 3 deletions boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
},
context::intrinsics::Intrinsics,
error::JsNativeError,
js_string,
object::{JsObject, ObjectData},
property::{Attribute, PropertyNameKind},
realm::Realm,
Expand Down Expand Up @@ -48,10 +47,10 @@ impl IntrinsicObject for ArrayIterator {
.iterator_prototypes()
.iterator(),
)
.static_method(Self::next, js_string!("next"), 0)
.static_method(Self::next, "next", 0)
.static_property(
JsSymbol::to_string_tag(),
js_string!("Array Iterator"),
"Array Iterator",
Attribute::CONFIGURABLE,
)
.build();
Expand Down
16 changes: 8 additions & 8 deletions boa_engine/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn of() {
TestAction::assert("arrayEquals(Array.of(), [])"),
TestAction::run("let a = Array.of.call(Date, 'a', undefined, 3);"),
TestAction::assert("a instanceof Date"),
TestAction::assert_eq("a[0]", js_string!("a")),
TestAction::assert_eq("a[0]", "a"),
TestAction::assert_eq("a[1]", JsValue::undefined()),
TestAction::assert_eq("a[2]", 3),
TestAction::assert_eq("a.length", 3),
Expand Down Expand Up @@ -75,18 +75,18 @@ fn copy_within() {
fn join() {
run_test_actions([
TestAction::assert_eq("[].join('.')", js_string!()),
TestAction::assert_eq("['a'].join('.')", js_string!("a")),
TestAction::assert_eq("['a', 'b', 'c'].join('.')", js_string!("a.b.c")),
TestAction::assert_eq("let a=[];a[0]=a;a[1]=a;a[2]=a;a.join()", js_string!(",,")),
TestAction::assert_eq("['a'].join('.')", "a"),
TestAction::assert_eq("['a', 'b', 'c'].join('.')", "a.b.c"),
TestAction::assert_eq("let a=[];a[0]=a;a[1]=a;a[2]=a;a.join()", ",,"),
]);
}

#[test]
fn to_string() {
run_test_actions([
TestAction::assert_eq("[].toString()", js_string!()),
TestAction::assert_eq("['a'].toString()", js_string!("a")),
TestAction::assert_eq("['a', 'b', 'c'].toString()", js_string!("a,b,c")),
TestAction::assert_eq("['a'].toString()", "a"),
TestAction::assert_eq("['a', 'b', 'c'].toString()", "a,b,c"),
]);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ fn every() {
fn find() {
run_test_actions([TestAction::assert_eq(
"['a', 'b', 'c'].find(e => e == 'a')",
js_string!("a"),
"a",
)]);
}

Expand Down Expand Up @@ -350,7 +350,7 @@ fn fill_obj_ref() {
let a = new Array(3).fill(obj);
obj.hi = 'hi'
"#}),
TestAction::assert_eq("a[2].hi", js_string!("hi")),
TestAction::assert_eq("a[2].hi", "hi"),
]);
}

Expand Down
7 changes: 3 additions & 4 deletions boa_engine/src/builtins/atomics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::sync::atomic::Ordering;
use crate::{
builtins::BuiltInObject,
context::intrinsics::Intrinsics,
js_string,
object::{JsObject, Object},
property::Attribute,
realm::Realm,
Expand Down Expand Up @@ -472,9 +471,9 @@ impl Atomics {
};

Ok(match result {
futex::AtomicsWaitResult::NotEqual => js_string!("not-equal"),
futex::AtomicsWaitResult::TimedOut => js_string!("timed-out"),
futex::AtomicsWaitResult::Ok => js_string!("ok"),
futex::AtomicsWaitResult::NotEqual => "not-equal",
futex::AtomicsWaitResult::TimedOut => "timed-out",
futex::AtomicsWaitResult::Ok => "ok",
}
.into())
}
Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/builtins/bigint/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{js_string, run_test_actions, JsBigInt, JsNativeErrorKind, TestAction};
use crate::{run_test_actions, JsBigInt, JsNativeErrorKind, TestAction};

#[test]
fn equality() {
Expand Down Expand Up @@ -147,10 +147,10 @@ fn operations() {
#[test]
fn to_string() {
run_test_actions([
TestAction::assert_eq("1000n.toString()", js_string!("1000")),
TestAction::assert_eq("1000n.toString(2)", js_string!("1111101000")),
TestAction::assert_eq("255n.toString(16)", js_string!("ff")),
TestAction::assert_eq("1000n.toString(36)", js_string!("rs")),
TestAction::assert_eq("1000n.toString()", "1000"),
TestAction::assert_eq("1000n.toString(2)", "1111101000"),
TestAction::assert_eq("255n.toString(16)", "ff"),
TestAction::assert_eq("1000n.toString(36)", "rs"),
]);
}

Expand Down
8 changes: 4 additions & 4 deletions boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ impl Date {
// 2. Let tv be ? thisTimeValue(O).
let Some(tv) = this_time_value(this)?.and_then(NaiveDateTime::from_timestamp_millis) else {
// 3. If tv is NaN, return "Invalid Date".
return Ok(js_string!("Invalid Date").into());
return Ok("Invalid Date".into());
};

// 4. Let t be LocalTime(tv).
Expand Down Expand Up @@ -1393,7 +1393,7 @@ impl Date {
// 1. Let tv be ? thisTimeValue(this value).
// 2. Return ToDateString(tv).
let Some(tv) = this_time_value(this)?.and_then(NaiveDateTime::from_timestamp_millis) else {
return Ok(js_string!("Invalid Date").into());
return Ok("Invalid Date".into());
};
Ok(js_string!(context
.host_hooks()
Expand Down Expand Up @@ -1422,7 +1422,7 @@ impl Date {
// 2. Let tv be ? thisTimeValue(O).
let Some(tv) = this_time_value(this)?.and_then(NaiveDateTime::from_timestamp_millis) else {
// 3. If tv is NaN, return "Invalid Date".
return Ok(js_string!("Invalid Date").into());
return Ok("Invalid Date".into());
};

// 4. Let t be LocalTime(tv).
Expand Down Expand Up @@ -1452,7 +1452,7 @@ impl Date {
// 1. Let O be this Date object.
let Some(t) = this_time_value(this)?.and_then(NaiveDateTime::from_timestamp_millis) else {
// 3. If tv is NaN, return "Invalid Date".
return Ok(js_string!("Invalid Date").into());
return Ok("Invalid Date".into());
};

// 2. Let tv be ? thisTimeValue(O).
Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/builtins/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,31 +762,31 @@ fn date_proto_set_utc_seconds() {
fn date_proto_to_date_string() {
run_test_actions([TestAction::assert_eq(
"new Date(2020, 6, 8, 9, 16, 15, 779).toDateString()",
js_string!("Wed Jul 08 2020"),
"Wed Jul 08 2020",
)]);
}

#[test]
fn date_proto_to_gmt_string() {
run_test_actions([TestAction::assert_eq(
"new Date(Date.UTC(2020, 6, 8, 9, 16, 15, 779)).toGMTString()",
js_string!("Wed, 08 Jul 2020 09:16:15 GMT"),
"Wed, 08 Jul 2020 09:16:15 GMT",
)]);
}

#[test]
fn date_proto_to_iso_string() {
run_test_actions([TestAction::assert_eq(
"new Date(Date.UTC(2020, 6, 8, 9, 16, 15, 779)).toISOString()",
js_string!("2020-07-08T09:16:15.779Z"),
"2020-07-08T09:16:15.779Z",
)]);
}

#[test]
fn date_proto_to_json() {
run_test_actions([TestAction::assert_eq(
"new Date(Date.UTC(2020, 6, 8, 9, 16, 15, 779)).toJSON()",
js_string!("2020-07-08T09:16:15.779Z"),
"2020-07-08T09:16:15.779Z",
)]);
}

Expand Down Expand Up @@ -826,7 +826,7 @@ fn date_proto_to_time_string() {
fn date_proto_to_utc_string() {
run_test_actions([TestAction::assert_eq(
"new Date(Date.UTC(2020, 6, 8, 9, 16, 15, 779)).toUTCString()",
js_string!("Wed, 08 Jul 2020 09:16:15 GMT"),
"Wed, 08 Jul 2020 09:16:15 GMT",
)]);
}

Expand Down
66 changes: 24 additions & 42 deletions boa_engine/src/builtins/error/tests.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,47 @@
use crate::{js_string, run_test_actions, TestAction};
use crate::{run_test_actions, TestAction};
use indoc::indoc;

#[test]
fn error_to_string() {
run_test_actions([
TestAction::assert_eq("(new Error('1')).toString()", js_string!("Error: 1")),
TestAction::assert_eq(
"(new RangeError('2')).toString()",
js_string!("RangeError: 2"),
),
TestAction::assert_eq(
"(new ReferenceError('3')).toString()",
js_string!("ReferenceError: 3"),
),
TestAction::assert_eq(
"(new SyntaxError('4')).toString()",
js_string!("SyntaxError: 4"),
),
TestAction::assert_eq(
"(new TypeError('5')).toString()",
js_string!("TypeError: 5"),
),
TestAction::assert_eq(
"(new EvalError('6')).toString()",
js_string!("EvalError: 6"),
),
TestAction::assert_eq("(new URIError('7')).toString()", js_string!("URIError: 7")),
TestAction::assert_eq("(new Error('1')).toString()", "Error: 1"),
TestAction::assert_eq("(new RangeError('2')).toString()", "RangeError: 2"),
TestAction::assert_eq("(new ReferenceError('3')).toString()", "ReferenceError: 3"),
TestAction::assert_eq("(new SyntaxError('4')).toString()", "SyntaxError: 4"),
TestAction::assert_eq("(new TypeError('5')).toString()", "TypeError: 5"),
TestAction::assert_eq("(new EvalError('6')).toString()", "EvalError: 6"),
TestAction::assert_eq("(new URIError('7')).toString()", "URIError: 7"),
// no message
TestAction::assert_eq("(new Error()).toString()", js_string!("Error")),
TestAction::assert_eq("(new RangeError()).toString()", js_string!("RangeError")),
TestAction::assert_eq(
"(new ReferenceError()).toString()",
js_string!("ReferenceError"),
),
TestAction::assert_eq("(new SyntaxError()).toString()", js_string!("SyntaxError")),
TestAction::assert_eq("(new TypeError()).toString()", js_string!("TypeError")),
TestAction::assert_eq("(new EvalError()).toString()", js_string!("EvalError")),
TestAction::assert_eq("(new URIError()).toString()", js_string!("URIError")),
TestAction::assert_eq("(new Error()).toString()", "Error"),
TestAction::assert_eq("(new RangeError()).toString()", "RangeError"),
TestAction::assert_eq("(new ReferenceError()).toString()", "ReferenceError"),
TestAction::assert_eq("(new SyntaxError()).toString()", "SyntaxError"),
TestAction::assert_eq("(new TypeError()).toString()", "TypeError"),
TestAction::assert_eq("(new EvalError()).toString()", "EvalError"),
TestAction::assert_eq("(new URIError()).toString()", "URIError"),
// no name
TestAction::assert_eq(
indoc! {r#"
let message = new Error('message');
message.name = '';
message.toString()
"#},
js_string!("message"),
"message",
),
]);
}

#[test]
fn error_names() {
run_test_actions([
TestAction::assert_eq("Error.name", js_string!("Error")),
TestAction::assert_eq("EvalError.name", js_string!("EvalError")),
TestAction::assert_eq("RangeError.name", js_string!("RangeError")),
TestAction::assert_eq("ReferenceError.name", js_string!("ReferenceError")),
TestAction::assert_eq("SyntaxError.name", js_string!("SyntaxError")),
TestAction::assert_eq("URIError.name", js_string!("URIError")),
TestAction::assert_eq("TypeError.name", js_string!("TypeError")),
TestAction::assert_eq("AggregateError.name", js_string!("AggregateError")),
TestAction::assert_eq("Error.name", "Error"),
TestAction::assert_eq("EvalError.name", "EvalError"),
TestAction::assert_eq("RangeError.name", "RangeError"),
TestAction::assert_eq("ReferenceError.name", "ReferenceError"),
TestAction::assert_eq("SyntaxError.name", "SyntaxError"),
TestAction::assert_eq("URIError.name", "URIError"),
TestAction::assert_eq("TypeError.name", "TypeError"),
TestAction::assert_eq("AggregateError.name", "AggregateError"),
]);
}

Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/builtins/function/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn function_prototype() {
fn function_prototype_call() {
run_test_actions([TestAction::assert_eq(
"Object.prototype.toString.call(new Error())",
js_string!("[object Error]"),
"[object Error]",
)]);
}

Expand Down Expand Up @@ -134,9 +134,9 @@ fn closure_capture_clone() {
let object = JsObject::with_object_proto(ctx.intrinsics());
object
.define_property_or_throw(
js_string!("key"),
"key",
PropertyDescriptor::builder()
.value(js_string!(" world!"))
.value(" world!")
.writable(false)
.enumerable(false)
.configurable(false),
Expand Down Expand Up @@ -168,10 +168,10 @@ fn closure_capture_clone() {
.name("closure")
.build();

ctx.register_global_property(js_string!("closure"), func, Attribute::default())
ctx.register_global_property("closure", func, Attribute::default())
.unwrap();
}),
TestAction::assert_eq("closure()", js_string!("Hello world!")),
TestAction::assert_eq("closure()", "Hello world!"),
]);
}

Expand Down
26 changes: 13 additions & 13 deletions boa_engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl IntrinsicObject for Collator {
.static_method(Self::supported_locales_of, "supportedLocalesOf", 1)
.property(
JsSymbol::to_string_tag(),
js_string!("Intl.Collator"),
"Intl.Collator",
Attribute::CONFIGURABLE,
)
.accessor("compare", Some(compare), None, Attribute::CONFIGURABLE)
Expand Down Expand Up @@ -520,8 +520,8 @@ impl Collator {
.create_data_property_or_throw(
"usage",
match collator.usage {
Usage::Search => js_string!("search"),
Usage::Sort => js_string!("sort"),
Usage::Search => "search",
Usage::Sort => "sort",
},
context,
)
Expand All @@ -530,24 +530,24 @@ impl Collator {
.create_data_property_or_throw(
"sensitivity",
match collator.sensitivity {
Sensitivity::Base => js_string!("base"),
Sensitivity::Accent => js_string!("accent"),
Sensitivity::Case => js_string!("case"),
Sensitivity::Variant => js_string!("variant"),
Sensitivity::Base => "base",
Sensitivity::Accent => "accent",
Sensitivity::Case => "case",
Sensitivity::Variant => "variant",
},
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
js_string!("ignorePunctuation"),
"ignorePunctuation",
collator.ignore_punctuation,
context,
)
.expect("operation must not fail per the spec");
options
.create_data_property_or_throw(
js_string!("collation"),
"collation",
js_string!(collator.collation.to_string()),
context,
)
Expand All @@ -558,11 +558,11 @@ impl Collator {
if let Some(kf) = collator.case_first {
options
.create_data_property_or_throw(
js_string!("caseFirst"),
"caseFirst",
match kf {
CaseFirst::Off => js_string!("false"),
CaseFirst::LowerFirst => js_string!("lower"),
CaseFirst::UpperFirst => js_string!("upper"),
CaseFirst::Off => "false",
CaseFirst::LowerFirst => "lower",
CaseFirst::UpperFirst => "upper",
_ => unreachable!(),
},
context,
Expand Down
Loading

0 comments on commit 7829b91

Please sign in to comment.