Skip to content

Commit b7f75ab

Browse files
dcodeIOkripken
authored andcommitted
Add Emscripten memory helpers for using the C-API (from Wasm) (#2476)
We already have exports for _malloc and _free in the Emscripten build, but there is no way yet to initialize the data without resorting to JS. Hence this PR adds a few additional memory helpers to the Emscripten build so it becomes possible to manipulate Binaryen memory without the need for extra glue code, for example when Binaryen is a WebAssembly import, and one is allocating strings to be used by / reading strings returned by Binaryen. I expect this to be a bit controversial because the use case is relatively specific, but it makes sense for us because we are consuming the C-API directly (from JS and eventually Wasm) and don't rely on binaryen.js-post.js.
1 parent 31eac76 commit b7f75ab

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

src/binaryen-c.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4695,6 +4695,60 @@ size_t BinaryenSizeofAllocateAndWriteResult(void) {
46954695
return sizeof(BinaryenModuleAllocateAndWriteResult);
46964696
}
46974697

4698-
#endif
4698+
// Helpers for accessing Binaryen's memory from another module without the
4699+
// need to round-trip through JS, e.g. when allocating and initializing
4700+
// strings passed to / reading strings returned by the C-API.
4701+
4702+
// TODO: Remove these once Wasm supports multiple memories.
4703+
4704+
// Stores an 8-bit integer to Binaryen memory.
4705+
EMSCRIPTEN_KEEPALIVE
4706+
void _i32_store8(int8_t* ptr, int8_t value) { *ptr = value; }
4707+
4708+
// Stores a 16-bit integer to Binaryen memory.
4709+
EMSCRIPTEN_KEEPALIVE
4710+
void _i32_store16(int16_t* ptr, int16_t value) { *ptr = value; }
4711+
4712+
// Stores a 32-bit integer to Binaryen memory.
4713+
EMSCRIPTEN_KEEPALIVE
4714+
void _i32_store(int32_t* ptr, int32_t value) { *ptr = value; }
4715+
4716+
// Stores a 32-bit float to Binaryen memory.
4717+
EMSCRIPTEN_KEEPALIVE
4718+
void _f32_store(float* ptr, float value) { *ptr = value; }
4719+
4720+
// Stores a 64-bit float to Binaryen memory.
4721+
EMSCRIPTEN_KEEPALIVE
4722+
void _f64_store(double* ptr, double value) { *ptr = value; }
4723+
4724+
// Loads an 8-bit signed integer from Binaryen memory.
4725+
EMSCRIPTEN_KEEPALIVE
4726+
int8_t _i32_load8_s(int8_t* ptr) { return *ptr; }
4727+
4728+
// Loads an 8-bit unsigned integer from Binaryen memory.
4729+
EMSCRIPTEN_KEEPALIVE
4730+
uint8_t _i32_load8_u(uint8_t* ptr) { return *ptr; }
4731+
4732+
// Loads a 16-bit signed integer from Binaryen memory.
4733+
EMSCRIPTEN_KEEPALIVE
4734+
int16_t _i32_load16_s(int16_t* ptr) { return *ptr; }
4735+
4736+
// Loads a 16-bit unsigned integer from Binaryen memory.
4737+
EMSCRIPTEN_KEEPALIVE
4738+
uint16_t _i32_load16_u(uint16_t* ptr) { return *ptr; }
4739+
4740+
// Loads a 32-bit integer from Binaryen memory.
4741+
EMSCRIPTEN_KEEPALIVE
4742+
int32_t _i32_load(int32_t* ptr) { return *ptr; }
4743+
4744+
// Loads a 32-bit float from Binaryen memory.
4745+
EMSCRIPTEN_KEEPALIVE
4746+
float _f32_load(float* ptr) { return *ptr; }
4747+
4748+
// Loads a 64-bit float from Binaryen memory.
4749+
EMSCRIPTEN_KEEPALIVE
4750+
double _f64_load(double* ptr) { return *ptr; }
4751+
4752+
#endif // __EMSCRIPTEN__
46994753

47004754
} // extern "C"

0 commit comments

Comments
 (0)