Skip to content

Commit

Permalink
wasi: fast calls
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jul 6, 2022
1 parent 7233b18 commit fb6e1e4
Show file tree
Hide file tree
Showing 28 changed files with 326 additions and 292 deletions.
558 changes: 281 additions & 277 deletions src/node_wasi.cc

Large diffs are not rendered by default.

54 changes: 41 additions & 13 deletions src/node_wasi.h
Expand Up @@ -10,6 +10,10 @@
namespace node {
namespace wasi {

struct WasmMemory {
char* data;
size_t size;
};

class WASI : public BaseObject,
public mem::NgLibMemoryManager<WASI, uvwasi_mem_t> {
Expand All @@ -23,16 +27,17 @@ class WASI : public BaseObject,
SET_MEMORY_INFO_NAME(WASI)
SET_SELF_SIZE(WASI)

static void ArgsGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ArgsSizesGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ClockResGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ClockTimeGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void EnvironGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void EnvironSizesGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdAdvise(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdAllocate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdClose(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdDatasync(const v8::FunctionCallbackInfo<v8::Value>& args);
static uint32_t ArgsGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t ArgsSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t ClockResGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t ClockTimeGet(WASI&, WasmMemory, uint32_t, uint64_t, uint32_t);
static uint32_t EnvironGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t EnvironSizesGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t FdAdvise(
WASI&, WasmMemory, uint32_t, uint64_t, uint64_t, uint32_t);
static uint32_t FdAllocate(WASI&, WasmMemory, uint32_t, uint64_t, uint64_t);
static uint32_t FdClose(WASI&, WasmMemory, uint32_t);
static uint32_t FdDatasync(WASI&, WasmMemory, uint32_t);
static void FdFdstatGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdFdstatSetFlags(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdFdstatSetRights(
Expand All @@ -49,7 +54,8 @@ class WASI : public BaseObject,
static void FdRead(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdReaddir(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdRenumber(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdSeek(const v8::FunctionCallbackInfo<v8::Value>& args);
static uint32_t FdSeek(
WASI&, WasmMemory, uint32_t, int64_t, uint32_t, uint32_t);
static void FdSync(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdTell(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FdWrite(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -69,8 +75,8 @@ class WASI : public BaseObject,
static void PollOneoff(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ProcExit(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ProcRaise(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RandomGet(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SchedYield(const v8::FunctionCallbackInfo<v8::Value>& args);
static uint32_t RandomGet(WASI&, WasmMemory, uint32_t, uint32_t);
static uint32_t SchedYield(WASI&, WasmMemory);
static void SockRecv(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockSend(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SockShutdown(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -82,6 +88,28 @@ class WASI : public BaseObject,
void IncreaseAllocatedSize(size_t size);
void DecreaseAllocatedSize(size_t size);

// <typename FT, FT F> as a C++14 desugaring of `<auto F>`
template <typename FT, FT F, typename R, typename... Args>
class WasiFunction {
public:
static void SetFunction(Environment*,
const char*,
v8::Local<v8::FunctionTemplate>);

private:
static R FastCallback(v8::Local<v8::Object> receiver,
Args...,
v8::FastApiCallbackOptions&);

static void SlowCallback(const v8::FunctionCallbackInfo<v8::Value>&);

// Another hack, SlowCallback is just a dumb wrapper that expands
// `InnerSlowCallback<Indices...>` from `Args...` :(
template <size_t... Indices>
static void InnerSlowCallback(std::index_sequence<Indices...>,
const v8::FunctionCallbackInfo<v8::Value>&);
};

private:
~WASI() override;
inline void readUInt8(char* memory, uint8_t* value, uint32_t offset);
Expand Down
5 changes: 3 additions & 2 deletions test/wasi/Makefile
@@ -1,12 +1,13 @@
CC = /opt/wasi-sdk/bin/clang
TARGET = wasm32-unknown-wasi
TARGET = wasm32-wasi
SYSROOT =
CFLAGS = -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks

OBJ = $(patsubst c/%.c, wasm/%.wasm, $(wildcard c/*.c))
all: $(OBJ)

wasm/%.wasm : c/%.c
$(CC) $< --target=$(TARGET) --sysroot=$(SYSROOT) -s -o $@
$(CC) $< $(CFLAGS) --target=$(TARGET) --sysroot=$(SYSROOT) -s -o $@

.PHONY clean:
rm -f $(OBJ)
1 change: 1 addition & 0 deletions test/wasi/test-wasi.js
Expand Up @@ -50,6 +50,7 @@ if (process.argv[2] === 'wasi-child') {
opts.input = options.stdin;

const child = cp.spawnSync(process.execPath, [
...process.argv.slice(1, -1),
'--experimental-wasi-unstable-preview1',
__filename,
'wasi-child',
Expand Down
Binary file modified test/wasi/wasm/cant_dotdot.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/clock_getres.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/create_symlink.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/exitcode.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/fd_prestat_get_refresh.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/follow_symlink.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/freopen.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/ftruncate.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/getentropy.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/getrusage.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/gettimeofday.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/link.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/main_args.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/notdir.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/poll.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/preopen_populates.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/read_file.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/read_file_twice.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/readdir.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/stat.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/stdin.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/symlink_escape.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/symlink_loop.wasm
Binary file not shown.
Binary file modified test/wasi/wasm/write_file.wasm
Binary file not shown.

0 comments on commit fb6e1e4

Please sign in to comment.