From a89097e56bb1fa8ca86d5cf1f9cf7944cff0666c Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 12 Feb 2021 12:51:50 +0100 Subject: [PATCH] fix(wasi) Use `realloc` + `memcpy` instead of `open_memstream`. While `open_memstream` is part of POSIX, it's not available on Windows. So we need to allocate and grow the buffer manually, old school way. --- wasmer/wasi.go | 17 ++++++----------- wasmer/wasi_test.go | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/wasmer/wasi.go b/wasmer/wasi.go index 230fe5c9..727bc957 100644 --- a/wasmer/wasi.go +++ b/wasmer/wasi.go @@ -1,7 +1,7 @@ package wasmer // #include -// #include +// #include // #include // // // Buffer size for `wasi_env_read_inner`. @@ -17,28 +17,23 @@ package wasmer // // // Common function to read a WASI environment captured stream. // size_t to_wasi_env_read_inner(wasi_env_t *wasi_env, char** buffer, wasi_env_reader reader) { -// FILE *memory_stream; +// char* buffer_ptr = (char*) malloc(1); // size_t buffer_size = 0; // -// memory_stream = open_memstream(buffer, &buffer_size); -// -// if (NULL == memory_stream) { -// return 0; -// } -// // char temp_buffer[WASI_ENV_READER_BUFFER_SIZE] = { 0 }; -// size_t data_read_size = WASI_ENV_READER_BUFFER_SIZE; +// size_t data_read_size = 0; // // do { // data_read_size = reader(wasi_env, temp_buffer, WASI_ENV_READER_BUFFER_SIZE); // // if (data_read_size > 0) { +// buffer_ptr = realloc(buffer_ptr, buffer_size + data_read_size); +// memcpy(buffer_ptr + buffer_size, temp_buffer, data_read_size); // buffer_size += data_read_size; -// fwrite(temp_buffer, sizeof(char), data_read_size, memory_stream); // } // } while (WASI_ENV_READER_BUFFER_SIZE == data_read_size); // -// fclose(memory_stream); +// *buffer = buffer_ptr; // // return buffer_size; // } diff --git a/wasmer/wasi_test.go b/wasmer/wasi_test.go index 62ab76b2..40dd2523 100644 --- a/wasmer/wasi_test.go +++ b/wasmer/wasi_test.go @@ -50,10 +50,10 @@ func TestWasiWithCapturedStdout(t *testing.T) { assert.Equal( t, - stdout, "Found program name: `test-program`\n"+ "Found 1 arguments: --foo\n"+ "Found 2 environment variables: ABC=DEF, X=ZY\n"+ "Found 1 preopened directories: DirEntry(\"/the_host_current_directory\")\n", + stdout, ) }