Skip to content

Commit

Permalink
Renames bind_wasm_callable to bind_wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Dheatly23 committed Nov 6, 2024
1 parent 743916a commit b8e6e53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion doc/WasmInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Config is too complex to be put here, read at [WasmConfig](./WasmConfig.md).

Calls WASM exported function with given arguments. Returns null if it errors.

### `Callable bind_wasm_callable(StringName name)`
### `Callable bind_wasm(StringName name)`

Creates a callable that calls WASM exported function.

Expand Down
2 changes: 1 addition & 1 deletion src/godot_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use godot::prelude::*;
/// WARNING: Incredibly unsafe.
/// It's just used as workaround to pass Godot objects across closure.
/// (At least until it supports multi-threading)
#[derive(Clone)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub(crate) struct SendSyncWrapper<T: ?Sized>(T);

Expand Down
51 changes: 26 additions & 25 deletions src/wasm_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::hash_map::{Entry, HashMap};
use std::hash::{Hash, Hasher};
#[cfg(feature = "wasi")]
use std::sync::Arc;
use std::{fmt, mem, ptr};
use std::{ffi, fmt, mem, ptr};

use anyhow::{bail, Result as AnyResult};
use cfg_if::cfg_if;
Expand Down Expand Up @@ -737,41 +737,35 @@ impl WasmInstance {
}
}

#[derive(Debug)]
struct WasmCallable {
name: StringName,
ty: FuncType,
f: Func,
ptr: *const ffi::c_void,
this: SendSyncWrapper<Gd<WasmInstance>>,
}

unsafe impl Send for WasmCallable {}
unsafe impl Sync for WasmCallable {}

impl PartialEq for WasmCallable {
fn eq(&self, other: &Self) -> bool {
(self.name == other.name)
&& (*self.this == *other.this)
&& (self.this == other.this)
&& FuncType::eq(&self.ty, &other.ty)
&& (self.ptr == other.ptr)
}
}

impl Eq for WasmCallable {}

impl Hash for WasmCallable {
fn hash<H: Hasher>(&self, state: &mut H) {
<StringName as Hash>::hash(&self.name, state);
Hash::hash(&self.name, state);
self.ty.hash(state);
self.this.hash(state);
}
}

impl fmt::Debug for WasmCallable {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { this, name, ty, f } = self;

fmt.debug_struct("WasmCallable")
.field("object", &**this)
.field("name", name)
.field("type", ty)
.field("func", f)
.finish()
self.ptr.hash(state);
}
}

Expand Down Expand Up @@ -926,19 +920,26 @@ impl WasmInstance {
///
/// Returns a `Callable` that can be used to call into WASM.
#[func]
fn bind_wasm_callable(&self, name: StringName) -> Callable {
fn bind_wasm(&self, name: StringName) -> Callable {
self.unwrap_data(|m| {
m.acquire_store(|m, mut store| {
let n = name.to_string();
let f = match site_context!(m.instance.get_core())?.get_export(&mut store, &n) {
Some(Extern::Func(f)) => f,
Some(_) => bail_with_site!("Export {n} is not a function"),
None => bail_with_site!("Export {n} does not exists"),
let f = {
let name = name.to_string();
match site_context!(m.instance.get_core())?.get_export(&mut store, &name) {
Some(Extern::Func(f)) => f,
Some(_) => bail_with_site!("Export {name} is not a function"),
None => bail_with_site!("Export {name} does not exists"),
}
};
let ty = f.ty(&store);

let this = SendSyncWrapper::new(self.to_gd());
Ok(Callable::from_custom(WasmCallable { name, ty, f, this }))
Ok(Callable::from_custom(WasmCallable {
name,
ty: f.ty(&store),
// SAFETY: Pointer is only used for comparison.
ptr: unsafe { f.to_raw(store) },
f,
this: SendSyncWrapper::new(self.to_gd()),
}))
})
})
.unwrap_or_else(Callable::invalid)
Expand Down

0 comments on commit b8e6e53

Please sign in to comment.