Commit Graph

396 Commits

Author SHA1 Message Date
Ivan Enderlin
2bc502bd47 fix(c-api) Move the middleware API into the unstable module. 2021-03-01 15:27:56 +01:00
Ivan Enderlin
d660943b45 feat(c-api) Simplify &Box<T> to &T. 2021-03-01 15:13:40 +01:00
Nick Lewycky
197d0f7a49 Initial commit of C API for metering and middleware. 2021-02-09 01:20:34 -08:00
Ivan Enderlin
f85ebe1b21 fix(c-api) Fix how string's length is computed.
Obviously, it's just `feature.size`. Why did I do that…?
2021-02-05 10:09:43 +01:00
Ivan Enderlin
f0f1915c86 chore(c-api) Rename or add variables for more clarity. 2021-02-04 14:00:00 +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
4725a5d623 Merge branch 'master' into feat-c-api-cross-compilation-2 2021-02-02 11:07:21 +01:00
bors[bot]
c4c7b2bb9b Merge #2083
2083: doc(c-api) Remove inline documentation in `wasmer_wasm.h`, clarification about stability… r=Hywan a=Hywan

# Description

As suggested in https://github.com/wasmerio/wasmer/pull/2053#discussion_r566692725, this PR removes the automatically generated documentation when building `wasmer_wasm.h`.

It also clarifies our position regarding stability, and add a section about the documentation.

This PR takes also the opportunity to mark `wasi_env_set_(instance|memory)` as deprecated functions.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2021-02-02 09:24:37 +00:00
Ivan Enderlin
0826d3d722 Merge branch 'master' into feat-c-api-cross-compilation-2 2021-02-02 09:51:01 +01:00
Ivan Enderlin
7eb08399f2 fix(c-api) Include feedbacks from @MarkMcCaskey! 2021-02-02 09:49:39 +01:00
Ivan Enderlin
98bff43f35 chore(c-api) Remove useless unsafe keywords. 2021-02-01 15:49:53 +01:00
Ivan Enderlin
b954429ca6 fix(c-api) Avoid more than one call to the function environment finalizer.
A function environment `WrapperEnv` can be cloned. In case the
original and the cloned environments are being dropped, the
`self.finalizer` function —if any— is called twice. That's a bug. It
must be called only once.

This patch updates the code to apply the finalizer only once by taking
it —if any— the first time it's called.
2021-02-01 15:42:28 +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
9a835a4fdc doc(c-api) Remove incorrect link. 2021-02-01 15:00:13 +01:00
Ivan Enderlin
8ab82cf589 doc(c-api) Fix documentation. 2021-02-01 14:58:42 +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
b1dc0416dd chore(c-api) Make type explicit for the sake of simplicity. 2021-02-01 13:42:06 +01:00
Ivan Enderlin
bbbbe53602 test(c-api) Move a doctest into a test. 2021-02-01 13:38:51 +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
00bf74c488 chore(c-api) Reorder field for the sake of consistency. 2021-02-01 11:59:23 +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
7e757ded6b test(c-api) Simplify test by using wasm_name_new_from_string. 2021-01-29 15:04:40 +01:00
Ivan Enderlin
9fb174e961 doc(c-api) Add more documentation. 2021-01-29 14:41:41 +01:00
Ivan Enderlin
3c5692ca2f doc(c-api) Mark some types as unstable. 2021-01-29 14:39:59 +01:00
Ivan Enderlin
e4057469bc chore(c-api) get_default_compiler_config can stay private. 2021-01-29 14:29:50 +01:00
Ivan Enderlin
9fda912f31 feat(c-api) wasm_config_t and wasm_engine_new_with_config handle the target. 2021-01-29 14:27:07 +01:00
Ivan Enderlin
14b8fb0a9a feat(c-api) Implement wasm_config_set_target. 2021-01-29 14:26:52 +01:00
Ivan Enderlin
bc5bbfdeef feat(c-api) Implement Debug for wasm_target_t. 2021-01-29 14:26:38 +01:00
Ivan Enderlin
1eec258550 chore(c-api) Remove warning. 2021-01-29 14:25:47 +01:00
Ivan Enderlin
169fe16f31 doc+test(c-api) Add more tests and more documentation. 2021-01-29 12:34:14 +01:00
Ivan Enderlin
8f42485a49 Merge branch 'master' into feat-c-api-cross-compilation-2 2021-01-29 11:55:14 +01:00
bors[bot]
a0cadcf361 Merge #2071
2071: feat(c-api) Rename the `wasmer` module to `unstable` r=Hywan a=Hywan

# Description

This patch is the first step to prepare more unstable API (like cross-compilation etc.). Nothing changes for the moment from the user perspective, it's not a breaking change. It's just a reorganisation of the code, and better documentation. Basically the `wasmer_c_api::wasm_c_api::wasmer` module has been renamed `unstable`, so that it's super clear :-).

# Review

- [ ] ~Add a short description of the the change to the CHANGELOG.md file~ not necessary


Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2021-01-28 17:54:24 +00:00
Ivan Enderlin
b201f12494 feat(c-api) Start implementing wasm_target_t, wasm_triple_t and wasm_cpu_features_t. 2021-01-28 17:26:15 +01:00
Ivan Enderlin
2f82d5536f feat(c-api) Rename the wasmer module to unstable. 2021-01-28 15:35:51 +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
c03d61b78a test(c-api) Simplify wat2wasm test cases. 2021-01-28 09:11:50 +01:00
bors[bot]
2d57f31037 Merge #2054
2054: Add `wasm_config_delete` to the Wasm C API r=MarkMcCaskey a=MarkMcCaskey

Fixes 1 missing import reported in #2052 

This function is relatively new to the Wasm C API and its implementation is trivial

Co-authored-by: Mark McCaskey <mark@wasmer.io>
2021-01-26 16:38:47 +00: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