Skip to content

Schema Manipulation API

Erik McClure edited this page Nov 30, 2019 · 1 revision

In addition to the standard External API, INExports includes functions for safely manipulating a module that has been loaded into memory. This can be used to inject functions, change exports, adjust function signatures, serialize modules, load source maps, or other modifications. Examples of these functions can be found in test_manual.cpp.

Schema Manipulation Functions

int SerializeModule(Environment* env, size_t m, const char* out, size_t* len);
  • env The environment that contains the module that will be serialized.
  • m The index of the in the given environment that will be serialized.
  • out If 'len' is zero, the path to an output file. Otherwise, a pointer to an in-memory buffer that will contain the serialized result. If null, defaults to a file called '<module_name>.wat' in the current working directory
  • len If 'len' is nonzero and out is not null, then this should be the length of the buffer pointed to by out
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Serializes the 'm'th module in the given environment into the provided output file.


int LoadSourceMap(Environment* env, unsigned int m, const char* path, size_t len);
  • env The environment that contains the module the sourcemap will be attached to.
  • m the index of the module to attache the sourcemap to.
  • path if len is zero, a null-terminated UTF8 string containing the path of the sourcemap. Otherwise, a pointer to a memory location.
  • len if zero, path points to a string. Otherwise, this contains the size of the memory location holding the sourcemap.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Loads a source map from the given path or memory location into the module at index 'm'.


enum IN_ERROR SerializeSourceMap(const SourceMap* map, const char* path);
  • map A pointer to the source map that should be serialized.
  • path A null-terminated UTF8 string pointing to the file location where the source map should be saved
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Serializes the given source map to JSON and saves it at path.


int InsertModuleSection(Environment* env, Module* m, enum WASM_MODULE_SECTIONS section, varuint32 index);
  • env The environment associated with the given module.
  • m A pointer to a module associated with the given environment that the section should be inserted into.
  • section A enumeration value signifying what section of the module to insert a new element into.
  • index The index, relative to the section being modified, where the new element should be inserted. This index cannot be greater than the size of the section being modified, but it can be equal to the current size of the section, which will simply append the new item to the end.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Inserts a new, zero'd element into the given module section at the specified index. It is up to the caller to initialize the new element with a valid state.


int DeleteModuleSection(Environment* env, Module* m, enum WASM_MODULE_SECTIONS section, varuint32 index);
  • env The environment associated with the given module.
  • m A pointer to a module associated with the given environment that the section should be removed from.
  • section A enumeration value signifying what section of the module to remove an element from.
  • index The index, relative to the section being modified, of the element that will be removed.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Deletes an element from the given module section at the specified index and moves the other elements in the array.


int SetByteArray(Environment* env, ByteArray* bytearray, const void* data, varuint32 size);
  • env The environment associated with the given bytearray.
  • bytearray The ByteArray object whose internal buffer will recieve the data.
  • data A pointer to a location in memory which will be copied to the ByteArray's internal buffer.
  • size The size of the memory location to copy into the ByteArray's internal buffer.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Resizes a ByteArray to the size of the provided memory buffer and copies the entire memory section into it's internal buffer.


int SetIdentifier(Environment* env, Identifier* identifier, const char* str);
  • env The environment associated with the given identifier.
  • identifier The identifier whose buffer will be set to the given string.
  • str A null-terminated string to be copied into the identifier.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Copies the given null-terminated string into the internal buffer of the target identifier, preserving the null-terminator.


int InsertModuleLocal(Environment* env, FunctionType* func, FunctionBody* body, varuint32 index, varsint7 local, DebugInfo* info);
  • env The environment associated with the given function.
  • func The FunctionBody object to insert the local definition into.
  • index The index where the new local will be inserted.
  • local The local that will be inserted.
  • info An optional pointer to debug information describing the local.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Inserts a local into the given function body at the provided index and initializes it with a type, and an optional debuginfo value.


int RemoveModuleLocal(Environment* env, FunctionType* func, FunctionBody* body, varuint32 index);
  • env The environment associated with the given function.
  • func The FunctionBody object to remove the local definition from.
  • index The index of the local that will be removed.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Removes a local definition from the given function body at the provided index.


int InsertModuleInstruction(Environment* env, FunctionBody* body, varuint32 index, Instruction* ins);

Inserts an instruction into the given function body at the provided index and initializes it with an initial value.

  • env The environment associated with the given function.
  • func The FunctionBody object to insert the instruction into.
  • index The index where the new instruction will be inserted.
  • ins The instruction that will be inserted.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

int RemoveModuleInstruction(Environment* env, FunctionBody* body, varuint32 index);

Removes an instruction from the given function body at the provided index.

  • env The environment associated with the given function.
  • func The FunctionBody object to remove the instruction from.
  • index The index of the instruction that will be removed.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

int InsertModuleParam(Environment* env, FunctionType* func, FunctionBody* body, varuint32 index, varsint7 param, DebugInfo* info);
  • env The environment associated with the given function.
  • func The FunctionType object to insert the parameter into.
  • param The parameter that will be inserted.
  • info An optional pointer to debug information describing the parameter.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Inserts a parameter into the given function type at the provided index and initializes it. If both the corresponding function body and a DebugInfo object are provided, also inserts and initializes the appropriate debug information.


int RemoveModuleParam(Environment* env, FunctionType* func, FunctionBody* body, varuint32 index);
  • env The environment associated with the given function.
  • func The FunctionType object to remove the parameter from.
  • index The index of the parameter that will be removed.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Removes a parameter from the given function type at the provided index. If the corresponding function body is provided, also removes the corresponding DebugInfo object from the body, if it exists.


int InsertModuleReturn(Environment* env, FunctionType* func, varuint32 index, varsint7 result);
  • env The environment associated with the given function.
  • func The FunctionType object to insert the result value into.
  • result The result that will be inserted.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Inserts and initializes a return value into the given function type at the provided index.


int RemoveModuleReturn(Environment* env, FunctionType* func, varuint32 index);
  • env The environment associated with the given function.
  • func The FunctionType object to remove the result value from.
  • index The index of the result value that will be removed.
  • Returns a standard IN_ERROR error code, or ERR_SUCCESS if it succeeds.

Removes a return value from the given function type at the provided index.