Skip to content

Commit

Permalink
fix(wasi) Use realloc + memcpy instead of open_memstream.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hywan committed Feb 12, 2021
1 parent e7164e1 commit a89097e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
17 changes: 6 additions & 11 deletions wasmer/wasi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package wasmer

// #include <stdlib.h>
// #include <stdio.h>
// #include <string.h>
// #include <wasmer_wasm.h>
//
// // Buffer size for `wasi_env_read_inner`.
Expand All @@ -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;
// }
Expand Down
2 changes: 1 addition & 1 deletion wasmer/wasi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

0 comments on commit a89097e

Please sign in to comment.