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 12, 2023
1 parent e2a2f6b commit 52d192a
Show file tree
Hide file tree
Showing 74 changed files with 604 additions and 809 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<JsValue> {
let shape = object.shape();

Ok(if shape.is_shared() {
js_string!("shared")
"shared"
} else {
js_string!("unique")
"unique"
}
.into())
}
Expand Down
8 changes: 4 additions & 4 deletions boa_cli/src/debug/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use boa_engine::{
JsNativeError, JsObject, JsResult, JsValue, NativeFunction,
};

fn storage(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn storage(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected string argument")
Expand All @@ -20,7 +20,7 @@ fn storage(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsVal
Ok(js_string!(storage).into())
}

fn encoding(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsValue> {
fn encoding(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected string argument")
Expand All @@ -41,7 +41,7 @@ fn encoding(_: &JsValue, args: &[JsValue], _: &mut Context<'_>) -> JsResult<JsVa
Ok(js_string!(encoding).into())
}

fn summary(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult<JsValue> {
fn summary(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let Some(value) = args.get(0) else {
return Err(JsNativeError::typ()
.with_message("expected string argument")
Expand All @@ -68,7 +68,7 @@ fn summary(_: &JsValue, args: &[JsValue], context: &mut Context<'_>) -> JsResult
Ok(summary.into())
}

pub(super) fn create_string(context: &mut Context<'_>) -> JsObject {
pub(super) fn create_string(context: &mut Context) -> JsObject {
ObjectInitializer::new(context)
.function(NativeFunction::from_fn_ptr(storage), "storage", 1)
.function(NativeFunction::from_fn_ptr(encoding), "encoding", 1)
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 @@ -468,9 +467,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 @@ -1238,7 +1238,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 @@ -1385,7 +1385,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 @@ -1414,7 +1414,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 @@ -1444,7 +1444,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
Loading

0 comments on commit 52d192a

Please sign in to comment.