Skip to content

Commit

Permalink
js-sys: Add extends to JSON
Browse files Browse the repository at this point in the history
Part of rustwasm#670
  • Loading branch information
fitzgen committed Sep 6, 2018
1 parent cda7175 commit 5df2347
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3048,8 +3048,12 @@ pub mod WebAssembly {
// JSON
#[wasm_bindgen]
extern "C" {

/// The `JSON` object contains methods for parsing [JavaScript Object
/// Notation (JSON)](https://json.org/) and converting values to JSON. It
/// can't be called or constructed, and aside from its two method
/// properties, it has no interesting functionality of its own.
#[derive(Clone, Debug)]
#[wasm_bindgen(extends = Object)]
pub type JSON;

/// The `JSON.parse()` method parses a JSON string, constructing the
Expand Down
14 changes: 13 additions & 1 deletion crates/js-sys/tests/wasm/JSON.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasm_bindgen::JsValue;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use wasm_bindgen::JsCast;
use js_sys::*;
Expand Down Expand Up @@ -82,3 +82,15 @@ fn stringify_error() {
let err_msg: String = From::from(err.message());
assert!(err_msg.contains("rust really rocks"));
}

#[wasm_bindgen_test]
fn json_extends() {
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = JSON)]
static json: JSON;
}

assert!(json.is_instance_of::<Object>());
let _: &Object = json.as_ref();
}

0 comments on commit 5df2347

Please sign in to comment.