Commit Graph

47 Commits

Author SHA1 Message Date
Ivan Enderlin
17dbb32e5a feat(c-api) Do not rename wat2wasm for the moment. 2021-02-12 00:26:53 +01:00
Ivan Enderlin
fd6689c580 chore(c-api) Formalize API prefixes.
The new rule is the following:

* `wasm_` for the standard C API,
* `wasmer_` or `wasi_` for the Wasmer non-standard C API.

For all symbols inside the `unstable` module, the renaming `wasm_` to
`wasmer_` is done without deprecations. It was clear that those API
were unstable.

For all the other symbols, symbols have been renamed to `wasmer_` but
the old symbols have been kept with deprecation warnings.

Special note: The `wasm_named_extern_t` type (and associated
functions) was in `wasi` by mistake. Its place was in the `unstable`
module. This patch also fixes that.

The `wasm_declare_vec_*` macros have been updated to support a default
prefix, or a user-defined prefix. It's now possible to write
`wasm_declare_boxed_vec!(foo);` to get all the API prefixed by `wasm_`
(as previously), or `wasm_declare_boxed_vec!(foo, wasmer);` to prefix
with `wasmer_`.

A user not using symbols from the `unstable` module will continue to
get working code, modulo some deprecations, after this patch.
2021-02-11 23:56:36 +01:00
Ivan Enderlin
698a26c7fe fix(c-api) Restore a bug with wasi_get_imports.
We said that `wasi_get_imports` was taking ownership of
`wasi_env_t`. It wasn't. In 9e63ba9a25,
we have fixed this. But it creates another bug when `wasi_env_t` is
used _after_ for example when calling `wasi_env_read_stdout`.

So this patch reverts the bug fix. And we will discuss about how to
fix that later.
2021-02-02 13:42:29 +01:00
Ivan Enderlin
e5d5303b50 Merge branch 'master' into feat-c-api-wasi-unordered-imports 2021-02-02 12:17:01 +01:00
Ivan Enderlin
995a2d4779 feat(c-api) No longer expand with cbindgen: drop dependency to Rust nightly.
So. Let's explain a dirty hack. `cbindgen` reads the code and collects
symbols. What symbols do we need? None of the one declared in
`wasm.h`, but for non-standard API, we need to collect all of
them. The problem is that `wasm_named_extern_t` is the only
non-standard type where extra symbols are generated by a macro
(`wasm_declare_boxed_vec!`). If we want those macro-generated symbols
to be collected by `cbindgen`, we need to _expand_ the crate
(i.e. running something like `rustc -- -Zunstable-options
--pretty=expanded`). Expanding code is unstable and available only on
nightly compiler. We _don't want_ to use a nightly compiler only for
that. So how can we help `cbindgen` to _see_ those symbols?

First solution: We write the C code directly in a file, which is then
included in the generated header file with the `cbindgen`
API. Problem, it's super easy to get it outdated, and it makes the
build process more complex.

Second solution: We write those symbols in a custom module, that is
just here for `cbindgen`, never used by our Rust code (otherwise it's
duplicated code), with no particular implementation.

And that's why we have the following `cbindgen_hack` module.

But this module must not be compiled by `rustc`. How to force `rustc`
to ignore a module? With conditional compilation. Because `cbindgen`
does not support conditional compilation, it will always _ignore_ the
`#[cfg]` attribute, and will always read the content of the module.

Sorry.
2021-02-02 12:05:47 +01:00
Ivan Enderlin
98bff43f35 chore(c-api) Remove useless unsafe keywords. 2021-02-01 15:49:53 +01:00
Ivan Enderlin
7c63648eb7 Update lib/c-api/src/wasm_c_api/wasi/mod.rs
Co-authored-by: Julien BIANCHI <contact@jubianchi.Fr>
2021-02-01 15:06:26 +01:00
Ivan Enderlin
9e63ba9a25 fix(c-api) Fix memory leak and owernship in WASI.
In `wasi_get_imports` and `wasi_get_unordered_imports`, we said the
ownership of `wasi_env_t` was taken by the function, but it wasn't the
case. This patch changes the type from `&wasi_env_t` to
`Box<wasi_env_t>` to take ownership of it.

The rest of the patch updates the documentation, and improves null
protections with `Option<T>`.
2021-02-01 14:55:45 +01:00
Ivan Enderlin
37aee2d7ad chore(c-api) Simplify code. 2021-02-01 12:03:38 +01:00
Ivan Enderlin
83c69a5623 feat(c-api) Implement From<String> for wasm_name_t. 2021-02-01 12:03:38 +01:00
Ivan Enderlin
18761cad7d feat(c-api) Implement wasm_extern_copy.
This patch also moves `wasm_extern_delete` into the correct Rust module.
2021-02-01 12:02:39 +01:00
Ivan Enderlin
5abac74618 feat(c-api) Rename wasm_named_extern_extern to wasm_named_extern_unwrap. 2021-02-01 12:02:12 +01:00
Ivan Enderlin
dc0cf7d4a4 feat(c-api) Implement wasi_get_unordered_imports. 2021-02-01 11:59:23 +01:00
Ivan Enderlin
277d85c5ae fix(c-api) Mark wasi_env_set_(instance|memory) as deprecated. 2021-02-01 11:55:38 +01:00
Ivan Enderlin
7f79fcc373 Merge branch 'master' into fix-c-api-wasi-read-captured-stream 2021-01-28 10:45:16 +01:00
Ivan Enderlin
ef1328e1d7 fix(c-api) Don't drain the entire captured stream when reading a small range.
We use `VecDeque::drain` to read the captured stream, zipped with the
given buffer. We could expect that only the yielded items from the
`drain` will be removed, but actually no. Reading [the
documentation](https://doc.rust-lang.org/std/collections/struct.VecDeque.html#method.drain):

> Note 1: The element `range` is removed even if the iterator is not
> consumed until the end.

So by using a range like `..` will drain the entire captured stream,
whatever we read from it. Said differently, if the given buffer length
is smaller than the captured stream, the first read will drain the
entire captured stream.

This patch fixes the problem by specifying a better range:
`..min(inner_buffer.len(), oc.buffer.len())`.

With this new range, it's actually useless to increment
`num_bytes_written`, we already know ahead of time the amount of bytes
we are going to read. Consequently, the patch simplifies this code a
little bit more.
2021-01-28 10:28:49 +01:00
Ivan Enderlin
1828d8ecff Merge branch 'master' into fix-c-api-wasi-version 2021-01-28 09:18:30 +01:00
Ivan Enderlin
7a435ef740 feat(c-api) wasi_version_t has a C representation now.
On Windows, using a `u32` representation for `wasi_version_t` fails if
a C++ compiler is used to treat a C program. So we change our strategy
here. We use a C representation to be FFI-safe, and the
`INVALID_VERSION` variant is now set to -1 instead of `u32::MAX`.

This patch also adds unit tests for `wasi_get_wasi_version` so that we
are sure of the behavior of this `type` on all platforms.
2021-01-28 09:12:10 +01:00
Ivan Enderlin
4688d9b369 chore(c-api) Disable wasi_config_capture_stdin for the moment`. 2021-01-26 16:21:55 +01:00
Ivan Enderlin
70c31068be fix(wasi) Fix the logic behind inherited/captured stdin, stdout and stderr.
First, let's no longer derive from `Default` for `wasi_config_t`. By
default, we want to inherit `stdin`, `stdout` and `stderr`. The
default for `bool` is `false`; we want `true` here.

Second, let's update `wasi_config_new` to correctly set `inherit_*`
fields to `true`.

Third, lets' create `wasi_config_capture_*` functions. By default,
`std*` are inherited, so we need functions to capture them. That's the
new feature this patch introduces. The `wasi_config_inherit_*`
functions are kept for the sake of backward compatibility. Ideally, we
would want an API like `wasi_config_capture_*(capture: bool)`, but it
would duplicate the API somehow.

Fourth, let's fix `wasi_env_new`. We want to capture `stdout` and
`stderr` if and only if the `inherit_*` fields are set to
`false`. There was bug here. That's why everything was working
correctly by the way: `bool::default()` is `false`, and we have this
inverted condition here, so everything was working as expected because
of a double error. The only bug was that it wasn't possible to capture
`std*` before.
2021-01-26 14:12:48 +01:00
Ivan Enderlin
af2b8d5923 doc(c-api) Document wasi_version_t. 2021-01-26 13:19:04 +01:00
Ivan Enderlin
434285c0f9 fix(c-api) Use uppercase enum variants for constants of wasi_version_t. 2021-01-26 13:11:44 +01:00
Mark McCaskey
1cadf9af7f Fix unrelated warnings in Wasm C API 2020-12-01 12:09:21 -08:00
Ivan Enderlin
7973a965fb Merge branch 'master' into fix-vm-leak 2020-12-01 10:08:21 +01:00
Mark McCaskey
40eec3fbd6 Address feedback: misc clean ups 2020-11-25 14:19:29 -08:00
Ivan Enderlin
ca89bd5d75 feat(api) Rename Extern::from_export to …::from_vm_export.
I hope this little change will clarify a little bit that the `Export`
passed to `Extern::from_vm_export` is not a `wasmer::Export` but a
`wasmer_vm::Export`.
2020-11-19 11:06:18 +01:00
Mark McCaskey
46204d6357 Fix up more tests 2020-10-21 12:00:30 -07:00
Ivan Enderlin
d7d40e542f Merge branch 'master' into feat-c-api-update-wasm-h 2020-10-16 10:18:46 +02:00
Ivan Enderlin
fea156defa feat(c-api) Update wasi_get_imports to use a wasm_extern_vec_t
… so that it matches the definition of `wasm_instance_new`, which is
much simpler and consistent.
2020-10-12 11:58:53 +02:00
Mark McCaskey
ba7b24f696 Add --dir and --mapdir support to native executable 2020-10-09 17:38:14 -07:00
Ivan Enderlin
3edcc89698 feat(c-api) Redefine wasm_store_t.
`wasm_store_t` is now a proper struct (rather than an opaque type) of
kind:

```rs
struct wasm_store_t {
    inner: Store
}
```

The rest of the patch updates the code accordingly.
2020-10-05 21:16:43 +02:00
Ivan Enderlin
a2854ebfea fix(c-api) Adjust cbindgen metadata for header files. 2020-10-01 21:20:13 +02:00
Nick Lewycky
9a3c5cf389 Remove extern "C" from a non-pub function that returns non-FFI'able type.
Add `extern "C"` to two `#[no_mangle] pub` functions.
2020-10-01 20:58:09 +02:00
Ivan Enderlin
d55b701e9d feat(c-api) Make wasi_config_t and wasi_env_t opaque structures.
Removing `repr(C)` make them opaque for cbindgen. Which is perfect for what we want.
2020-09-29 16:01:52 +02:00
Ivan Enderlin
174b03f4e9 feat(c-api) Use our fork of cbindgen.
Our fork contains incoming PR. It helps for example to skip struct
fields.
2020-09-24 16:15:04 +02:00
Ivan Enderlin
1a0527af78 feat(c-api) Move all types into their own modules. 2020-09-24 11:41:04 +02:00
Ivan Enderlin
44559a9f04 feat(c-api) Move as_extern APIs into the externals module. 2020-09-24 09:48:56 +02:00
Ivan Enderlin
50fbb9766d feat(c-api) Move instance into its own module. 2020-09-24 09:19:53 +02:00
Ivan Enderlin
bc664b011d feat(c-api) Move memory into its own module. 2020-09-22 17:21:19 +02:00
Mark McCaskey
8a9ac6ce59 Add helper function to get the start function 2020-08-12 18:03:57 -07:00
Mark McCaskey
cba35df3f1 Use wasi_env_set_instance instead of wasi_env_set_memory 2020-08-12 17:32:28 -07:00
Mark McCaskey
892fb4efcf Make caller responsible for allocation array for getting WASI imports 2020-08-12 17:08:58 -07:00
Mark McCaskey
2e36c8e63b Clean up and simplify API: explicit version no longer required 2020-08-12 13:09:58 -07:00
Mark McCaskey
5915cd9d3f Use circular buffer for stdout capture to reduce memory usage 2020-08-12 09:40:44 -07:00
Mark McCaskey
747cdd424a Try replacing wasi_state_builder_t with wasi_config_t
Experimenting to simplify our API.
2020-08-12 09:23:56 -07:00
Mark McCaskey
3919e435c4 Clean up WASI 'Wasm C API' API 2020-08-11 13:08:33 -07:00
Mark McCaskey
1211874f4a Implement WASI for the Wasm C API 2020-08-10 15:53:40 -07:00