mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-28 05:52:21 +03:00
Merge pull request #55 from sendilkumarn/fix-no-mangle
Remove no_mangle and extern wherever applicable
This commit is contained in:
commit
ddf27f0ab1
@ -49,8 +49,7 @@ pub extern fn only_integers(a: i32) -> u32 {
|
||||
// is equivalent to...
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn only_integers_with_wasm_bindgen(a: i32) -> u32 {
|
||||
pub fn only_integers_with_wasm_bindgen(a: i32) -> u32 {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@ -88,8 +87,7 @@ Let's take a look at an example.
|
||||
```rust
|
||||
// foo.rs
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn foo(a: &JsValue) {
|
||||
pub fn foo(a: &JsValue) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@ -318,8 +316,7 @@ at that.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn greet(a: &str) -> String {
|
||||
pub fn greet(a: &str) -> String {
|
||||
format!("Hello, {}!", a)
|
||||
}
|
||||
```
|
||||
|
10
README.md
10
README.md
@ -91,8 +91,7 @@ extern {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn greet(name: &str) {
|
||||
pub fn greet(name: &str) {
|
||||
alert(&format!("Hello, {}!", name));
|
||||
}
|
||||
```
|
||||
@ -279,8 +278,7 @@ use wasm_bindgen::prelude::*;
|
||||
|
||||
// Strings can both be passed in and received
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn concat(a: &str, b: &str) -> String {
|
||||
pub fn concat(a: &str, b: &str) -> String {
|
||||
let mut a = a.to_string();
|
||||
a.push_str(b);
|
||||
return a
|
||||
@ -427,8 +425,8 @@ impls, and foreign modules. Impls can only contain functions, and the attribute
|
||||
cannot be attached to functions in an impl block or functions in a foreign
|
||||
module. No lifetime parameters or type parameters are allowed on any of these
|
||||
types. Foreign modules must have the `"C"` abi (or none listed). Free functions
|
||||
with `#[wasm_bindgen]` must also have the `"C"` abi or none listed and also be
|
||||
annotated with the `#[no_mangle]` attribute.
|
||||
with `#[wasm_bindgen]` might no have the `"C"` abi or none listed and also not
|
||||
needed to annotate with the `#[no_mangle]` attribute.
|
||||
|
||||
All structs referenced through arguments to functions should be defined in the
|
||||
macro itself. Arguments allowed implement the `WasmBoundary` trait, and examples
|
||||
|
@ -90,10 +90,7 @@ impl Program {
|
||||
.find(|&(_, ref m)| m.name() == "no_mangle");
|
||||
match no_mangle {
|
||||
Some((i, _)) => { f.attrs.remove(i); }
|
||||
None => {
|
||||
panic!("#[wasm_bindgen] can only be applied to #[no_mangle] \
|
||||
functions, or those that would otherwise be exported")
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
f.to_tokens(tokens);
|
||||
self.exports.push(Export {
|
||||
@ -380,10 +377,6 @@ impl Function {
|
||||
if input.unsafety.is_some() {
|
||||
panic!("can only bindgen safe functions");
|
||||
}
|
||||
if input.abi.is_none() {
|
||||
panic!("can only bindgen `extern` ABI functions, or those that \
|
||||
would otherwise be exported")
|
||||
}
|
||||
|
||||
Function::from_decl(input.ident,
|
||||
input.decl,
|
||||
|
45
tests/api.rs
45
tests/api.rs
@ -11,45 +11,38 @@ fn works() {
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn foo() -> JsValue {
|
||||
pub fn foo() -> JsValue {
|
||||
JsValue::from("foo")
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: &str) -> JsValue {
|
||||
pub fn bar(s: &str) -> JsValue {
|
||||
JsValue::from(s)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn baz() -> JsValue {
|
||||
pub fn baz() -> JsValue {
|
||||
JsValue::from(1.0)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn baz2(a: &JsValue, b: &JsValue) {
|
||||
pub fn baz2(a: &JsValue, b: &JsValue) {
|
||||
assert_eq!(a.as_f64(), Some(2.0));
|
||||
assert_eq!(b.as_f64(), None);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn js_null() -> JsValue {
|
||||
pub fn js_null() -> JsValue {
|
||||
JsValue::null()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn js_undefined() -> JsValue {
|
||||
pub fn js_undefined() -> JsValue {
|
||||
JsValue::undefined()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn test_is_null_undefined(
|
||||
pub fn test_is_null_undefined(
|
||||
a: &JsValue,
|
||||
b: &JsValue,
|
||||
c: &JsValue,
|
||||
@ -65,20 +58,17 @@ fn works() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn get_true() -> JsValue {
|
||||
pub fn get_true() -> JsValue {
|
||||
JsValue::from(true)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn get_false() -> JsValue {
|
||||
pub fn get_false() -> JsValue {
|
||||
JsValue::from(false)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn test_bool(
|
||||
pub fn test_bool(
|
||||
a: &JsValue,
|
||||
b: &JsValue,
|
||||
c: &JsValue,
|
||||
@ -89,38 +79,33 @@ fn works() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn mk_symbol() -> JsValue {
|
||||
pub fn mk_symbol() -> JsValue {
|
||||
let a = JsValue::symbol(None);
|
||||
assert!(a.is_symbol());
|
||||
return a
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn mk_symbol2(s: &str) -> JsValue {
|
||||
pub fn mk_symbol2(s: &str) -> JsValue {
|
||||
let a = JsValue::symbol(Some(s));
|
||||
assert!(a.is_symbol());
|
||||
return a
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn assert_symbols(a: &JsValue, b: &JsValue) {
|
||||
pub fn assert_symbols(a: &JsValue, b: &JsValue) {
|
||||
assert!(a.is_symbol());
|
||||
assert!(!b.is_symbol());
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn acquire_string(a: &JsValue, b: &JsValue) {
|
||||
pub fn acquire_string(a: &JsValue, b: &JsValue) {
|
||||
assert_eq!(a.as_string().unwrap(), "foo");
|
||||
assert_eq!(b.as_string(), None);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn acquire_string2(a: &JsValue) -> String {
|
||||
pub fn acquire_string2(a: &JsValue) -> String {
|
||||
a.as_string().unwrap_or("wrong".to_string())
|
||||
}
|
||||
"#)
|
||||
|
@ -254,8 +254,7 @@ fn issue_27() {
|
||||
pub struct Expr {}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn context() -> Context {
|
||||
pub fn context() -> Context {
|
||||
Context {}
|
||||
}
|
||||
"#)
|
||||
|
@ -17,9 +17,8 @@ fn c_style_enum() {
|
||||
Red,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn cycle(color: Color) -> Color {
|
||||
pub fn cycle(color: Color) -> Color {
|
||||
match color {
|
||||
Color::Green => Color::Yellow,
|
||||
Color::Yellow => Color::Red,
|
||||
@ -60,9 +59,8 @@ fn c_style_enum_with_custom_values() {
|
||||
Red,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn cycle(color: Color) -> Color {
|
||||
pub fn cycle(color: Color) -> Color {
|
||||
match color {
|
||||
Color::Green => Color::Yellow,
|
||||
Color::Yellow => Color::Red,
|
||||
|
@ -11,14 +11,12 @@ fn simple() {
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn get_random() -> f64 {
|
||||
pub fn get_random() -> f64 {
|
||||
Math::random()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn do_log(a: f64) -> f64 {
|
||||
pub fn do_log(a: f64) -> f64 {
|
||||
Math::log(a)
|
||||
}
|
||||
|
||||
@ -61,8 +59,7 @@ fn import_class() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar() {
|
||||
pub fn bar() {
|
||||
Foo::bar();
|
||||
}
|
||||
"#)
|
||||
@ -112,8 +109,7 @@ fn construct() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() {
|
||||
pub fn run() {
|
||||
let f = Foo::create();
|
||||
assert_eq!(f.get_internal_string(), "this");
|
||||
f.append_to_internal_string(" foo");
|
||||
@ -181,8 +177,7 @@ fn new_constructors() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() {
|
||||
pub fn run() {
|
||||
let f = Foo::new(1);
|
||||
assert_eq!(f.get(), 2);
|
||||
}
|
||||
@ -232,14 +227,12 @@ fn switch_methods() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn a() {
|
||||
pub fn a() {
|
||||
Foo::a();
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn b() {
|
||||
pub fn b() {
|
||||
Foo::new().b();
|
||||
}
|
||||
"#)
|
||||
@ -313,8 +306,7 @@ fn properties() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() {
|
||||
pub fn run() {
|
||||
let a = Foo::new();
|
||||
assert_eq!(a.a(), 1);
|
||||
a.set_a(2);
|
||||
|
@ -19,26 +19,22 @@ fn simple() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: &str) {
|
||||
pub fn bar(s: &str) {
|
||||
foo(s);
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn another_thunk(a: u32) -> i32 {
|
||||
pub fn another_thunk(a: u32) -> i32 {
|
||||
another(a)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bool_thunk(a: bool) -> bool {
|
||||
pub fn bool_thunk(a: bool) -> bool {
|
||||
take_and_return_bool(a)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn get_the_object() -> JsValue {
|
||||
pub fn get_the_object() -> JsValue {
|
||||
return_object()
|
||||
}
|
||||
"#)
|
||||
@ -102,8 +98,7 @@ fn unused() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar() {}
|
||||
pub fn bar() {}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as wasm from "./out";
|
||||
@ -133,14 +128,12 @@ fn strings() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(a: &str) -> String {
|
||||
pub fn bar(a: &str) -> String {
|
||||
foo(a.to_string())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar2(a: String) -> String {
|
||||
pub fn bar2(a: String) -> String {
|
||||
foo(a)
|
||||
}
|
||||
"#)
|
||||
@ -179,15 +172,13 @@ fn exceptions() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() {
|
||||
pub fn run() {
|
||||
foo();
|
||||
bar();
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run2() {
|
||||
pub fn run2() {
|
||||
assert!(baz().is_err());
|
||||
bar();
|
||||
}
|
||||
@ -237,8 +228,7 @@ fn exn_caught() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() -> JsValue {
|
||||
pub fn run() -> JsValue {
|
||||
foo().unwrap_err()
|
||||
}
|
||||
"#)
|
||||
@ -275,8 +265,7 @@ fn free_imports() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn run() {
|
||||
pub fn run() {
|
||||
assert_eq!(parseInt("3"), 3);
|
||||
}
|
||||
"#)
|
||||
|
@ -16,8 +16,7 @@ fn simple() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: &JsValue) {
|
||||
pub fn bar(s: &JsValue) {
|
||||
foo(s);
|
||||
}
|
||||
"#)
|
||||
@ -58,8 +57,7 @@ fn owned() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: JsValue) {
|
||||
pub fn bar(s: JsValue) {
|
||||
foo(s);
|
||||
}
|
||||
"#)
|
||||
@ -104,8 +102,7 @@ fn clone() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: JsValue) {
|
||||
pub fn bar(s: JsValue) {
|
||||
foo1(s.clone());
|
||||
foo2(&s);
|
||||
foo3(s.clone());
|
||||
@ -151,8 +148,7 @@ fn promote() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar(s: &JsValue) {
|
||||
pub fn bar(s: &JsValue) {
|
||||
foo1(s);
|
||||
foo2(s.clone());
|
||||
foo3(s);
|
||||
@ -193,8 +189,7 @@ fn returning_vector() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn bar() -> Vec<JsValue> {
|
||||
pub fn bar() -> Vec<JsValue> {
|
||||
let mut res = Vec::new();
|
||||
for _ in 0..10 {
|
||||
res.push(foo())
|
||||
|
@ -11,8 +11,7 @@ fn auto_bind_math() {
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn math(a: f32, b: f64) -> f64 {
|
||||
pub fn math(a: f32, b: f64) -> f64 {
|
||||
b.acos() +
|
||||
b.asin() +
|
||||
b.atan() +
|
||||
|
@ -22,8 +22,7 @@ fn works() {
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[no_mangle]
|
||||
pub extern fn clone(a: &JsValue) -> JsValue {
|
||||
pub fn clone(a: &JsValue) -> JsValue {
|
||||
drop(a.clone());
|
||||
a.clone()
|
||||
}
|
||||
|
@ -10,33 +10,28 @@ fn add() {
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn add(a: u32, b: u32) -> u32 {
|
||||
pub fn add(a: u32, b: u32) -> u32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn add3(a: u32) -> u32 {
|
||||
pub fn add3(a: u32) -> u32 {
|
||||
a + 3
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn get2(_b: bool) -> u32 {
|
||||
pub fn get2(_b: bool) -> u32 {
|
||||
2
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn return_and_take_bool(a: bool, b: bool) -> bool {
|
||||
pub fn return_and_take_bool(a: bool, b: bool) -> bool {
|
||||
a && b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn raw_pointers_work(a: *mut u32, b: *const u8) -> *const u32 {
|
||||
pub fn raw_pointers_work(a: *mut u32, b: *const u8) -> *const u32 {
|
||||
unsafe {
|
||||
(*a) = (*b) as u32;
|
||||
return a
|
||||
@ -68,16 +63,14 @@ fn string_arguments() {
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn assert_foo_and_bar(a: &str, b: &str) {
|
||||
pub fn assert_foo_and_bar(a: &str, b: &str) {
|
||||
assert_eq!(a, "foo2");
|
||||
assert_eq!(b, "bar");
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn assert_foo(a: &str) {
|
||||
pub fn assert_foo(a: &str) {
|
||||
assert_eq!(a, "foo");
|
||||
}
|
||||
"#)
|
||||
@ -102,15 +95,13 @@ fn return_a_string() {
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn clone(a: &str) -> String {
|
||||
pub fn clone(a: &str) -> String {
|
||||
a.to_string()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn concat(a: &str, b: &str, c: i8) -> String {
|
||||
pub fn concat(a: &str, b: &str, c: i8) -> String {
|
||||
format!("{} {} {}", a, b, c)
|
||||
}
|
||||
"#)
|
||||
@ -138,13 +129,11 @@ fn exceptions() {
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn foo(_a: u32) {}
|
||||
pub fn foo(_a: u32) {}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn bar(_a: &str) {}
|
||||
pub fn bar(_a: &str) {}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
|
@ -12,9 +12,8 @@ fn export() {
|
||||
|
||||
macro_rules! doit {
|
||||
($($i:ident)*) => ($(
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn $i(a: &[$i]) -> Vec<$i> {
|
||||
pub fn $i(a: &[$i]) -> Vec<$i> {
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(a[0], 1 as $i);
|
||||
assert_eq!(a[1], 2 as $i);
|
||||
@ -98,9 +97,8 @@ fn import() {
|
||||
fn $js(a: &[$i]) -> Vec<$i>;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[wasm_bindgen]
|
||||
pub extern fn $rust(a: &[$i]) -> Vec<$i> {
|
||||
pub fn $rust(a: &[$i]) -> Vec<$i> {
|
||||
assert_eq!(a.len(), 2);
|
||||
assert_eq!(a[0], 1 as $i);
|
||||
assert_eq!(a[1], 2 as $i);
|
||||
|
Loading…
Reference in New Issue
Block a user