Skip to content

Commit 8be0a98

Browse files
rail44alexcrichton
authored andcommitted
Add support for methods of Symbol (#437)
1 parent 8abe0f9 commit 8be0a98

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

src/js.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,32 @@ extern "C" {
18341834
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString
18351835
#[wasm_bindgen(static_method_of = Symbol, getter, structural, js_name = toStringTag)]
18361836
pub fn to_string_tag() -> Symbol;
1837+
1838+
/// The Symbol.for(key) method searches for existing symbols in a runtime-wide symbol registry with
1839+
/// the given key and returns it if found.
1840+
/// Otherwise a new symbol gets created in the global symbol registry with this key.
1841+
///
1842+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for
1843+
#[wasm_bindgen(static_method_of = Symbol, js_name = for)]
1844+
pub fn for_(key: &JsString) -> Symbol;
1845+
1846+
/// The Symbol.keyFor(sym) method retrieves a shared symbol key from the global symbol registry for the given symbol.
1847+
///
1848+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/keyFor
1849+
#[wasm_bindgen(static_method_of = Symbol, js_name = keyFor)]
1850+
pub fn key_for(sym: &Symbol) -> JsString;
1851+
1852+
/// The toString() method returns a string representing the specified Symbol object.
1853+
///
1854+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString
1855+
#[wasm_bindgen(method, js_name = toString)]
1856+
pub fn to_string(this: &Symbol) -> JsString;
1857+
1858+
/// The valueOf() method returns the primitive value of a Symbol object.
1859+
///
1860+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/valueOf
1861+
#[wasm_bindgen(method, js_name = valueOf)]
1862+
pub fn value_of(this: &Symbol) -> Symbol;
18371863
}
18381864

18391865
// Intl

tests/all/js_globals/Symbol.rs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,156 @@ fn to_string_tag() {
416416
)
417417
.test();
418418
}
419+
420+
#[test]
421+
fn for_() {
422+
project()
423+
.file(
424+
"src/lib.rs",
425+
r#"
426+
#![feature(proc_macro, wasm_custom_section)]
427+
428+
extern crate wasm_bindgen;
429+
use wasm_bindgen::prelude::*;
430+
use wasm_bindgen::js;
431+
432+
#[wasm_bindgen]
433+
pub fn symbol_for(key: &js::JsString) -> js::Symbol {
434+
js::Symbol::for_(key)
435+
}
436+
437+
"#,
438+
)
439+
.file(
440+
"test.js",
441+
r#"
442+
import * as assert from "assert";
443+
import * as wasm from "./out";
444+
445+
export function test() {
446+
assert.strictEqual(wasm.symbol_for("bar"), wasm.symbol_for("bar"));
447+
assert.strictEqual(wasm.symbol_for("bar"), Symbol.for("bar"));
448+
449+
assert.notStrictEqual(wasm.symbol_for("foo"), Symbol("bar"));
450+
assert.notStrictEqual(wasm.symbol_for("foo"), wasm.symbol_for("bar"));
451+
452+
var sym = wasm.symbol_for("mario");
453+
assert.equal(sym.toString(), "Symbol(mario)");
454+
}
455+
"#,
456+
)
457+
.test();
458+
}
459+
460+
#[test]
461+
fn key_for() {
462+
project()
463+
.file(
464+
"src/lib.rs",
465+
r#"
466+
#![feature(proc_macro, wasm_custom_section)]
467+
468+
extern crate wasm_bindgen;
469+
use wasm_bindgen::prelude::*;
470+
use wasm_bindgen::js;
471+
472+
#[wasm_bindgen]
473+
pub fn symbol_key_for(sym: &js::Symbol) -> js::JsString {
474+
js::Symbol::key_for(sym)
475+
}
476+
477+
"#,
478+
)
479+
.file(
480+
"test.js",
481+
r#"
482+
import * as assert from "assert";
483+
import * as wasm from "./out";
484+
485+
export function test() {
486+
var globalSym = Symbol.for("foo");
487+
assert.strictEqual(wasm.symbol_key_for(globalSym), 'foo');
488+
489+
var localSym = Symbol();
490+
assert.strictEqual(wasm.symbol_key_for(localSym), undefined);
491+
492+
assert.strictEqual(wasm.symbol_key_for(Symbol.iterator), undefined);
493+
}
494+
"#,
495+
)
496+
.test();
497+
}
498+
499+
#[test]
500+
fn to_string() {
501+
project()
502+
.file(
503+
"src/lib.rs",
504+
r#"
505+
#![feature(proc_macro, wasm_custom_section)]
506+
507+
extern crate wasm_bindgen;
508+
use wasm_bindgen::prelude::*;
509+
use wasm_bindgen::js;
510+
511+
#[wasm_bindgen]
512+
pub fn symbol_to_string(this: &js::Symbol) -> js::JsString {
513+
js::Symbol::to_string(this)
514+
}
515+
516+
"#,
517+
)
518+
.file(
519+
"test.js",
520+
r#"
521+
import * as assert from "assert";
522+
import * as wasm from "./out";
523+
524+
export function test() {
525+
assert.strictEqual(wasm.symbol_to_string(Symbol('desc')), 'Symbol(desc)');
526+
527+
assert.strictEqual(wasm.symbol_to_string(Symbol.iterator), 'Symbol(Symbol.iterator)');
528+
529+
assert.strictEqual(wasm.symbol_to_string(Symbol.for('foo')), 'Symbol(foo)');
530+
}
531+
"#,
532+
)
533+
.test();
534+
}
535+
536+
#[test]
537+
fn value_of() {
538+
project()
539+
.file(
540+
"src/lib.rs",
541+
r#"
542+
#![feature(proc_macro, wasm_custom_section)]
543+
544+
extern crate wasm_bindgen;
545+
use wasm_bindgen::prelude::*;
546+
use wasm_bindgen::js;
547+
548+
#[wasm_bindgen]
549+
pub fn symbol_value_of(this: &js::Symbol) -> js::Symbol {
550+
js::Symbol::value_of(this)
551+
}
552+
553+
"#,
554+
)
555+
.file(
556+
"test.js",
557+
r#"
558+
import * as assert from "assert";
559+
import * as wasm from "./out";
560+
561+
export function test() {
562+
var globalSym = Symbol.for("foo");
563+
assert.strictEqual(wasm.symbol_value_of(globalSym), globalSym);
564+
565+
var localSym = Symbol();
566+
assert.strictEqual(wasm.symbol_value_of(localSym), localSym);
567+
}
568+
"#,
569+
)
570+
.test();
571+
}

0 commit comments

Comments
 (0)