Skip to content

Add String.raw #856

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
merged 1 commit into from
Sep 19, 2018
Merged
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
102 changes: 102 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,108 @@ extern "C" {
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/valueOf)
#[wasm_bindgen(method, js_class = "String", js_name = valueOf)]
pub fn value_of(this: &JsString) -> JsString;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, variadic, static_method_of = JsString, js_class = "String")]
pub fn raw(call_site: &Object, substitutions: &Array) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_0(call_site: &Object) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_1(call_site: &Object, substitutions_1: &str) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_2(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_3(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
substitutions_3: &str,
) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_4(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
substitutions_3: &str,
substitutions_4: &str,
) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_5(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
substitutions_3: &str,
substitutions_4: &str,
substitutions_5: &str,
) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_6(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
substitutions_3: &str,
substitutions_4: &str,
substitutions_5: &str,
substitutions_6: &str,
) -> Result<JsString, JsValue>;

/// The static `raw()` method is a tag function of template literals,
/// similar to the `r` prefix in Python or the `@` prefix in C# for string literals.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw)
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = raw)]
pub fn raw_7(
call_site: &Object,
substitutions_1: &str,
substitutions_2: &str,
substitutions_3: &str,
substitutions_4: &str,
substitutions_5: &str,
substitutions_6: &str,
substitutions_7: &str,
) -> Result<JsString, JsValue>;
}

impl JsString {
Expand Down
11 changes: 11 additions & 0 deletions crates/js-sys/tests/wasm/JsString.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,14 @@ fn value_of() {
let greeting = JsString::from("Hello world!");
assert_eq!(greeting.value_of(), "Hello world!");
}

#[wasm_bindgen_test]
fn raw() {
let call_site = Object::new();
let raw = Array::of3(&"foo".into(), &"bar".into(), &"123".into());
Reflect::set(&call_site.as_ref(), &"raw".into(), &raw.into());
assert_eq!(JsString::raw_2(&call_site, "5", "JavaScript").unwrap(), "foo5barJavaScript123");
let substitutions = Array::of2(&"5".into(), &"JavaScript".into());
assert_eq!(JsString::raw(&call_site, &substitutions).unwrap(), "foo5barJavaScript123");
assert!(JsString::raw_0(&JsValue::null().unchecked_into()).is_err());
}