Implementation:Google deepmind Mujoco WASM Unpack
| Knowledge Sources | |
|---|---|
| Domains | WebAssembly, Memory Management, Type Conversion |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
Provides utility classes and functions for sharing memory between C++ and JavaScript in the MuJoCo WASM environment.
Description
unpack.h defines the WasmBuffer<T> template class for managing typed memory buffers shared between the C++ (Emscripten) heap and JavaScript. The buffer supports creation from JavaScript arrays (FromArray), resizing (Resize), pointer access (GetPointer returning uintptr_t), element count queries (GetElementCount), and TypedArray view creation for zero-copy JavaScript access. It also provides StripWrapperSuffix for cleaning up function names, and handles the fundamental challenge of bridging C++ raw pointer access with JavaScript's typed array system.
Usage
Used by the generated WASM bindings to marshal data between JavaScript and C++, enabling functions that accept or return arrays to work with JavaScript TypedArrays without copying.
Code Reference
Source Location
- Repository: Google_deepmind_Mujoco
- File: wasm/unpack.h
- Lines: 1-351
Key Functions
namespace mujoco::wasm {
std::string StripWrapperSuffix(const char* func);
template <typename T>
class WasmBuffer {
public:
explicit WasmBuffer(int element_count = 0);
static WasmBuffer<T> FromArray(const emscripten::val& array);
void Resize(int element_count);
uintptr_t GetPointer();
int GetElementCount();
// TypedArray view access
};
} // namespace mujoco::wasm
Import
#include "wasm/unpack.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| array | emscripten::val | Yes (for FromArray) | JavaScript array to convert to native buffer |
| element_count | int | Yes (for constructor) | Number of elements to allocate |
Outputs
| Name | Type | Description |
|---|---|---|
| WasmBuffer<T> | WasmBuffer<T> | Typed buffer with shared C++/JavaScript memory access |
| uintptr_t | uintptr_t | Raw pointer to buffer data on the Emscripten heap |
| int | int | Number of elements in the buffer |