mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-21 08:41:35 +00:00
Consistently use extern "C"
This is what rustfmt favors, so let's favor it too! Closes #1042
This commit is contained in:
@ -39,7 +39,7 @@ In addition to the shims we talked about above which JS generates the macro
|
||||
|
||||
```
|
||||
#[no_mangle]
|
||||
pub extern fn __wbindgen_describe_greet() {
|
||||
pub extern "C" fn __wbindgen_describe_greet() {
|
||||
<Fn(&str)>::describe();
|
||||
}
|
||||
```
|
||||
|
@ -95,13 +95,13 @@ let's take a look at that.
|
||||
// original input to `#[wasm_bindgen]` omitted ...
|
||||
|
||||
#[export_name = "foo_new"]
|
||||
pub extern fn __wasm_bindgen_generated_Foo_new(arg0: i32) -> u32
|
||||
pub extern "C" fn __wasm_bindgen_generated_Foo_new(arg0: i32) -> u32
|
||||
let ret = Foo::new(arg0);
|
||||
Box::into_raw(Box::new(WasmRefCell::new(ret))) as u32
|
||||
}
|
||||
|
||||
#[export_name = "foo_get"]
|
||||
pub extern fn __wasm_bindgen_generated_Foo_get(me: u32) -> i32 {
|
||||
pub extern "C" fn __wasm_bindgen_generated_Foo_get(me: u32) -> i32 {
|
||||
let me = me as *mut WasmRefCell<Foo>;
|
||||
wasm_bindgen::__rt::assert_not_null(me);
|
||||
let me = unsafe { &*me };
|
||||
@ -109,7 +109,7 @@ pub extern fn __wasm_bindgen_generated_Foo_get(me: u32) -> i32 {
|
||||
}
|
||||
|
||||
#[export_name = "foo_set"]
|
||||
pub extern fn __wasm_bindgen_generated_Foo_set(me: u32, arg1: i32) {
|
||||
pub extern "C" fn __wasm_bindgen_generated_Foo_set(me: u32, arg1: i32) {
|
||||
let me = me as *mut WasmRefCell<Foo>;
|
||||
::wasm_bindgen::__rt::assert_not_null(me);
|
||||
let me = unsafe { &*me };
|
||||
@ -117,7 +117,7 @@ pub extern fn __wasm_bindgen_generated_Foo_set(me: u32, arg1: i32) {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern fn __wbindgen_foo_free(me: u32) {
|
||||
pub unsafe extern "C" fn __wbindgen_foo_free(me: u32) {
|
||||
let me = me as *mut WasmRefCell<Foo>;
|
||||
wasm_bindgen::__rt::assert_not_null(me);
|
||||
(*me).borrow_mut(); // ensure no active borrows
|
||||
|
@ -87,12 +87,12 @@ at a mostly abbreviated and/or "simplified" in the sense of this is what it
|
||||
compiles down to:
|
||||
|
||||
```rust
|
||||
pub extern fn greet(a: &str) -> String {
|
||||
pub extern "C" fn greet(a: &str) -> String {
|
||||
format!("Hello, {}!", a)
|
||||
}
|
||||
|
||||
#[export_name = "greet"]
|
||||
pub extern fn __wasm_bindgen_generated_greet(
|
||||
pub extern "C" fn __wasm_bindgen_generated_greet(
|
||||
arg0_ptr: *const u8,
|
||||
arg0_len: usize,
|
||||
) -> *mut String {
|
||||
|
@ -10,7 +10,7 @@ As usual though, let's dive into an example!
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen(module = "./bar")]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Bar;
|
||||
|
||||
#[wasm_bindgen(constructor)]
|
||||
@ -125,7 +125,7 @@ pub struct Bar {
|
||||
|
||||
impl Bar {
|
||||
fn new() -> Bar {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_new() -> u32;
|
||||
}
|
||||
unsafe {
|
||||
@ -135,7 +135,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
fn another_function() -> i32 {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_another_function() -> i32;
|
||||
}
|
||||
unsafe {
|
||||
@ -144,7 +144,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
fn get(&self) -> i32 {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_get(ptr: u32) -> i32;
|
||||
}
|
||||
unsafe {
|
||||
@ -155,7 +155,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
fn set(&self, val: i32) {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_set(ptr: u32, val: i32);
|
||||
}
|
||||
unsafe {
|
||||
@ -165,7 +165,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
fn property(&self) -> i32 {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_property(ptr: u32) -> i32;
|
||||
}
|
||||
unsafe {
|
||||
@ -176,7 +176,7 @@ impl Bar {
|
||||
}
|
||||
|
||||
fn set_property(&self, val: i32) {
|
||||
extern {
|
||||
extern "C" {
|
||||
fn __wbg_s_Bar_set_property(ptr: u32, val: i32);
|
||||
}
|
||||
unsafe {
|
||||
|
@ -9,7 +9,7 @@ greetings in JS but call it from Rust. We might have, for example:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen(module = "./greet")]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn greet(a: &str) -> String;
|
||||
}
|
||||
|
||||
@ -51,8 +51,8 @@ generated Rust as well. Like before this is simplified from what's actually
|
||||
generated.
|
||||
|
||||
```rust
|
||||
extern fn greet(a: &str) -> String {
|
||||
extern {
|
||||
extern "C" fn greet(a: &str) -> String {
|
||||
extern "C" {
|
||||
fn __wbg_f_greet(a_ptr: *const u8, a_len: usize, ret_len: *mut usize) -> *mut u8;
|
||||
}
|
||||
unsafe {
|
||||
|
@ -86,7 +86,7 @@ pub fn foo(a: &JsValue) {
|
||||
}
|
||||
|
||||
#[export_name = "foo"]
|
||||
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||
pub extern "C" fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||
let arg0 = unsafe {
|
||||
ManuallyDrop::new(JsValue::__from_idx(arg0))
|
||||
};
|
||||
@ -189,7 +189,7 @@ pub fn foo(a: JsValue) {
|
||||
}
|
||||
|
||||
#[export_name = "foo"]
|
||||
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||
pub extern "C" fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||
let arg0 = unsafe {
|
||||
JsValue::__from_idx(arg0)
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
[online]: https://rustwasm.github.io/wasm-bindgen/exbuild/import_js/
|
||||
[code]: https://github.com/rustwasm/wasm-bindgen/tree/master/examples/import_js
|
||||
|
||||
The `#[wasm_bindgen]` attribute can be used on `extern { .. }` blocks to import
|
||||
The `#[wasm_bindgen]` attribute can be used on `extern "C" { .. }` blocks to import
|
||||
functionality from JS. This is how the `js-sys` and the `web-sys` crates are
|
||||
built, but you can also use it in your own crate!
|
||||
|
||||
|
@ -6,7 +6,7 @@ attached to any imported function or method, and the function must return a
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
// `catch` on a standalone function.
|
||||
#[wasm_bindgen(catch)]
|
||||
fn foo() -> Result<(), JsValue>;
|
||||
|
@ -7,7 +7,7 @@ used in the generated glue:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Shoes;
|
||||
|
||||
#[wasm_bindgen(constructor)]
|
||||
|
@ -7,7 +7,7 @@ the inheritance hierarchy:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Foo;
|
||||
|
||||
#[wasm_bindgen(extends = Foo)]
|
||||
@ -33,7 +33,7 @@ the types.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Foo;
|
||||
|
||||
#[wasm_bindgen(extends = Foo)]
|
||||
|
@ -21,7 +21,7 @@ today:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Foo;
|
||||
#[wasm_bindgen(method)]
|
||||
fn bar(this: &Foo, argument: &str) -> JsValue;
|
||||
@ -47,7 +47,7 @@ If we instead, however, write this:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Foo;
|
||||
#[wasm_bindgen(method, final)] // note the change here
|
||||
fn bar(this: &Foo, argument: &str) -> JsValue;
|
||||
|
@ -24,7 +24,7 @@ We would import this with the following `#[wasm_bindgen]` attributes:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type TheDude;
|
||||
|
||||
#[wasm_bindgen(method, getter)]
|
||||
@ -48,7 +48,7 @@ example the below is equivalent to the above:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type TheDude;
|
||||
|
||||
#[wasm_bindgen(method, getter = white_russians)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
# `#[wasm_bindgen]` on JavaScript Imports
|
||||
|
||||
This section enumerates the attributes available for customizing bindings for
|
||||
JavaScript functions and classes imported into Rust within an `extern { ... }`
|
||||
JavaScript functions and classes imported into Rust within an `extern "C" { ... }`
|
||||
block.
|
||||
|
@ -58,7 +58,7 @@ on methods:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Foo;
|
||||
static foo: Foo;
|
||||
|
||||
|
@ -6,7 +6,7 @@ Rust side.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
// We don't want to import JS strings as `String`, since Rust already has a
|
||||
// `String` type in its prelude, so rename it as `JsString`.
|
||||
#[wasm_bindgen(js_name = String)]
|
||||
|
@ -8,7 +8,7 @@ snake-cased Rust identifier:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_name = jsOftenUsesCamelCase)]
|
||||
fn js_often_uses_camel_case() -> u32;
|
||||
}
|
||||
@ -20,7 +20,7 @@ identifiers, in which case `js_name = "some string"` is used instead of `js_name
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_name = "$$$")]
|
||||
fn cash_money() -> u32;
|
||||
}
|
||||
@ -30,7 +30,7 @@ polymorphic JavaScript functions:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
fn console_log_str(s: &str);
|
||||
|
||||
@ -50,7 +50,7 @@ Note that if you use `js_name` when importing a type you'll also need to use the
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_name = String)]
|
||||
type JsString;
|
||||
#[wasm_bindgen(method, getter, js_class = "String")]
|
||||
|
@ -8,7 +8,7 @@ name (like a class or function name) it'll be accessed through this namespace.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ which is a shared reference to an imported JavaScript type.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Set;
|
||||
|
||||
#[wasm_bindgen(method)]
|
||||
|
@ -5,7 +5,7 @@ example,
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen(module = "wu/tang/clan")]
|
||||
extern {
|
||||
extern "C" {
|
||||
type ThirtySixChambers;
|
||||
}
|
||||
```
|
||||
@ -21,7 +21,7 @@ instead. For example,
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn illmatic() -> u32;
|
||||
}
|
||||
```
|
||||
|
@ -6,7 +6,7 @@ to JavaScript's `Date.now()` static method, one would use this attribute:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Date;
|
||||
|
||||
#[wasm_bindgen(static_method_of = Date)]
|
||||
|
@ -18,7 +18,7 @@ dynamically walked on every access.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
type Duck;
|
||||
|
||||
#[wasm_bindgen(method, structural)]
|
||||
|
@ -28,7 +28,7 @@ function as variadic:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(variadic)]
|
||||
fn sum(args: &[i32]) -> i32;
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ For example to use `AudioContext` you might do:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(vendor_prefix = webkit)]
|
||||
type AudioContext;
|
||||
|
||||
|
@ -13,7 +13,7 @@ also [follow along in the repository][repo].
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn alert(msg: &str);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ FnMut` trait objects:
|
||||
// Import JS functions that take closures
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn takes_immutable_closure(f: &Fn());
|
||||
|
||||
fn takes_mutable_closure(f: &mut FnMut());
|
||||
@ -44,7 +44,7 @@ Closures also support arguments and return values like exports do, for example:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn takes_closure_that_takes_int_and_returns_string(x: &Fn(u32) -> String);
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ as arguments and returns.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn setInterval(closure: &Closure<FnMut()>, millis: u32) -> f64;
|
||||
fn cancelInterval(token: f64);
|
||||
|
||||
|
@ -32,7 +32,7 @@ will unconditionally panic on non-wasm targets. For example:
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn alert(s: &str);
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ pub struct Bar {
|
||||
}
|
||||
|
||||
#[wasm_bindgen(module = "./index")] // what ES6 module to import from
|
||||
extern {
|
||||
extern "C" {
|
||||
fn bar_on_reset(to: &str, opaque: &JsValue);
|
||||
|
||||
// We can import classes and annotate functionality on those classes as well
|
||||
|
@ -15,7 +15,7 @@ the attribute also serializes some information to the output artifact which
|
||||
|
||||
There's a more thorough explanation below of the various bits and pieces of the
|
||||
attribute, but it suffices for now to say that you can attach it to free
|
||||
functions, structs, impl blocks for those structs and `extern { ... }` blocks.
|
||||
functions, structs, impl blocks for those structs and `extern "C" { ... }` blocks.
|
||||
Some Rust features like generics, lifetime parameters, etc, aren't supported on
|
||||
functions tagged with `#[wasm_bindgen]` right now.
|
||||
|
||||
|
Reference in New Issue
Block a user