Open alexcrichton opened 6 years ago
I find that the most natural way to expose lifetimes to JS is to take a JS callback and say "the data passed to this callback may only be used while the call is active". This is not always super Rust-y, and not always applicable, but it is generally clear how to use the thing correctly from the JS side.
When JS owns something that borrows something else, it gets more difficult.
At the end of the day, FFI'ing with JS is always going to be unsafe, but our goal is to make the Rust side correct and easy. JS doesn't have borrowck, so lifetimes obviously can't improve the JS side, but they can improve the Rust side, and therefore are worthy of pursuit.
I have a similar issue, this surrounding stack-lifetime callbacks inside of an options object. The problem there is, I have a class in JS like
class Foo {
function totalCost(opts) { ... }
}
and inside the opts object is a cost function. What I'd like to do is be able to place a non-'static
closure in the options object, something like
#[wasm_bindgen]
struct Options<'a> {
cost_fn: &'a mut dyn FnMut(String) -> u32
}
but that fails to compile. In the meanwhile I've been using a helper module with a function that takes the partial options object and the callback as a parameter, puts the callback into the options, and calls totalCost
, but I'd like to not exclude Node targets.
This first came up on #419, but one web API looks (sort of) like this:
Here we're creating an
ImageData
while also passing a buffer of memory to its contructor. Presumably anImageData
retains a handle to the array passed in to later get rendered, which means that the lifetime of theImageData
instance produced is actually tied to the&[u8]
passed in.Normally in Rust this would be expressed through lifetimes, something like:
but that's not supported by
#[wasm_bindgen]
today!Should we somehow add support for these lifetimes? Or would this perhaps make web APIs too difficult to use? Should we instead leave-as is where you probably won't get a segfault but you'd get weird results sometimes?