Tabs -> Spaces

extensions
Volodymyr Shymanskyy 3 years ago
parent 433e7d71a3
commit 3ad8b27955

@ -13,51 +13,51 @@
M3Result m3Ext_ReserveFunctions (IM3Module i_module, M3Result m3Ext_ReserveFunctions (IM3Module i_module,
uint32_t i_numFunctions) uint32_t i_numFunctions)
{ {
M3Result result = m3Err_none; M3Result result = m3Err_none;
if (i_module) if (i_module)
{ d_m3Assert (i_module->table0Size == 0); { d_m3Assert (i_module->table0Size == 0);
if (i_module->table0Size == 0) if (i_module->table0Size == 0)
{ {
i_module->numReservedFunctions = i_numFunctions; i_module->numReservedFunctions = i_numFunctions;
u32 totalFunctions = i_module->numFunctions + i_numFunctions; u32 totalFunctions = i_module->numFunctions + i_numFunctions;
i_module->functions = m3_ReallocArray (M3Function, i_module->functions, totalFunctions, i_module->numFunctions); i_module->functions = m3_ReallocArray (M3Function, i_module->functions, totalFunctions, i_module->numFunctions);
_throwifnull (i_module->functions); _throwifnull (i_module->functions);
} }
else _throw ("ReserveFunctions must come before LoadModule"); else _throw ("ReserveFunctions must come before LoadModule");
} }
else _throw (m3Err_nullArgument); else _throw (m3Err_nullArgument);
_catch: _catch:
return result; return result;
} }
i32 Module_HasFuncType (IM3Module i_module, IM3FuncType i_funcType) i32 Module_HasFuncType (IM3Module i_module, IM3FuncType i_funcType)
{ {
if (i_module->funcTypes) if (i_module->funcTypes)
{ {
for (u32 i = 0; i < i_module->numFuncTypes; ++i) for (u32 i = 0; i < i_module->numFuncTypes; ++i)
{ {
if (AreFuncTypesEqual (i_module->funcTypes [i], i_funcType)) if (AreFuncTypesEqual (i_module->funcTypes [i], i_funcType))
return i; return i;
} }
} }
return -1; return -1;
} }
M3Result m3Ext_InjectFunction (IM3Module i_module, M3Result m3Ext_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex, int32_t * io_functionIndex,
const char * const i_signature, const char * const i_signature,
const uint8_t * const i_wasmBytes, const uint8_t * const i_wasmBytes,
bool i_doCompilation) bool i_doCompilation)
{ {
M3Result result = m3Err_none; d_m3Assert (io_functionIndex); M3Result result = m3Err_none; d_m3Assert (io_functionIndex);
@ -85,19 +85,19 @@ _ (ReadLEB_u32 (& size, & bytes, end));
} }
else else
{ {
i32 funcTypeIndex = Module_HasFuncType (i_module, ftype); i32 funcTypeIndex = Module_HasFuncType (i_module, ftype);
if (funcTypeIndex < 0) if (funcTypeIndex < 0)
{ {
// add slot to function type table in the module // add slot to function type table in the module
funcTypeIndex = i_module->numFuncTypes++; funcTypeIndex = i_module->numFuncTypes++;
i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex); i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex);
_throwifnull (i_module->funcTypes); _throwifnull (i_module->funcTypes);
// add functype object to the environment // add functype object to the environment
Environment_AddFuncType (i_module->environment, & ftype); Environment_AddFuncType (i_module->environment, & ftype);
i_module->funcTypes [funcTypeIndex] = ftype; i_module->funcTypes [funcTypeIndex] = ftype;
ftype = NULL; // prevent freeing below ftype = NULL; // prevent freeing below
} }
index = (i32) i_module->numFunctions; index = (i32) i_module->numFunctions;
_ (Module_AddFunction (i_module, funcTypeIndex, NULL)); _ (Module_AddFunction (i_module, funcTypeIndex, NULL));
@ -132,33 +132,33 @@ _ (CompileFunction (function));
} }
M3Result m3Ext_AddFunctionToTable (IM3Function i_function, M3Result m3Ext_AddFunctionToTable (IM3Function i_function,
uint32_t * o_elementIndex, uint32_t * o_elementIndex,
uint32_t i_tableIndex) uint32_t i_tableIndex)
{ {
M3Result result = m3Err_none; M3Result result = m3Err_none;
if (i_function and o_elementIndex) if (i_function and o_elementIndex)
{ {
IM3Module module = i_function->module; IM3Module module = i_function->module;
if (module) if (module)
{ {
u32 previousSize = module->table0Size; u32 previousSize = module->table0Size;
u32 newTableSize = previousSize + 1; u32 newTableSize = previousSize + 1;
module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, previousSize); module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, previousSize);
_throwifnull (module->table0); _throwifnull (module->table0);
* o_elementIndex = previousSize; * o_elementIndex = previousSize;
module->table0 [previousSize] = i_function; module->table0 [previousSize] = i_function;
module->table0Size = newTableSize; module->table0Size = newTableSize;
} }
else _throw ("null module"); else _throw ("null module");
} }
else _throw (m3Err_nullArgument); else _throw (m3Err_nullArgument);
_catch: return result; _catch: return result;
} }
@ -168,47 +168,47 @@ IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index)
} }
M3Result m3_GetFunctionIndex (IM3Function i_function, M3Result m3_GetFunctionIndex (IM3Function i_function,
uint32_t * o_index) uint32_t * o_index)
{ {
if (i_function and o_index) if (i_function and o_index)
{ {
* o_index = i_function->index; * o_index = i_function->index;
return m3Err_none; return m3Err_none;
} }
else return m3Err_functionLookupFailed; else return m3Err_functionLookupFailed;
} }
M3Result m3_GetDataSegmentOffset (IM3Module i_module, M3Result m3_GetDataSegmentOffset (IM3Module i_module,
uint32_t i_index) uint32_t i_index)
{ {
M3Result result = m3Err_none; d_m3Assert (i_module); M3Result result = m3Err_none; d_m3Assert (i_module);
if (i_module) if (i_module)
{ {
d_m3Assert (false); // TODO: finish d_m3Assert (false); // TODO: finish
} }
else _throw (m3Err_nullArgument); else _throw (m3Err_nullArgument);
_catch: return result; _catch: return result;
} }
M3Result m3_RegisterCustomOpcode (IM3Module i_module, M3Result m3_RegisterCustomOpcode (IM3Module i_module,
uint16_t i_opcode, uint16_t i_opcode,
uint8_t i_numImmediates, uint8_t i_numImmediates,
IM3Operation i_operation) IM3Operation i_operation)
{ {
M3Result result = m3Err_none; d_m3Assert (i_module); M3Result result = m3Err_none; d_m3Assert (i_module);
if (i_module) if (i_module)
{ {
} }
else _throw (m3Err_nullArgument); else _throw (m3Err_nullArgument);
_catch: return result; _catch: return result;
} }

@ -22,63 +22,63 @@ extern "C" {
// API extensions // API extensions
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
These extensions allow for unconventional uses of Wasm3 -- mainly dynamic modification of modules to inject new Wasm These extensions allow for unconventional uses of Wasm3 -- mainly dynamic modification of modules to inject new Wasm
functions during runtime. functions during runtime.
*/ */
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
// Creates an empty module. // Creates an empty module.
IM3Module m3Ext_NewModule (IM3Environment i_environment); IM3Module m3Ext_NewModule (IM3Environment i_environment);
// ReserveFunctions must be called prior to LoadModule. This reserves extra empty function slots that InjectFunction // ReserveFunctions must be called prior to LoadModule. This reserves extra empty function slots that InjectFunction
// can rely upon. // can rely upon.
M3Result m3Ext_ReserveFunctions (IM3Module i_module, M3Result m3Ext_ReserveFunctions (IM3Module i_module,
uint32_t i_numFunctions); uint32_t i_numFunctions);
// To append a new function, set io_functionIndex to negative. On return, the new function index will be set. // To append a new function, set io_functionIndex to negative. On return, the new function index will be set.
// To overwrite an existing function, set io_functionIndex to the desired element. i_signature must match the existing // To overwrite an existing function, set io_functionIndex to the desired element. i_signature must match the existing
// function signature. TODO: failure result is? // function signature. TODO: failure result is?
M3Result m3Ext_InjectFunction (IM3Module i_module, M3Result m3Ext_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex, int32_t * io_functionIndex,
// const char * const i_name, // const char * const i_name,
const char * const i_signature, const char * const i_signature,
const uint8_t * const i_wasmBytes, // i_wasmBytes is copied const uint8_t * const i_wasmBytes, // i_wasmBytes is copied
bool i_doCompilation); bool i_doCompilation);
M3Result m3Ext_AddFunctionToTable (IM3Function i_function, M3Result m3Ext_AddFunctionToTable (IM3Function i_function,
uint32_t * o_elementIndex, uint32_t * o_elementIndex,
uint32_t i_tableIndex); // i_tableIndex must be zero uint32_t i_tableIndex); // i_tableIndex must be zero
IM3Function m3_GetFunctionByIndex (IM3Module i_module, IM3Function m3_GetFunctionByIndex (IM3Module i_module,
uint32_t i_index); uint32_t i_index);
M3Result m3_GetFunctionIndex (IM3Function i_function, M3Result m3_GetFunctionIndex (IM3Function i_function,
uint32_t * o_index); uint32_t * o_index);
M3Result m3_GetDataSegmentOffset (IM3Module i_module, M3Result m3_GetDataSegmentOffset (IM3Module i_module,
uint32_t i_index); uint32_t i_index);
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
M3Result m3_RegisterCustomOpcode (IM3Module i_module, M3Result m3_RegisterCustomOpcode (IM3Module i_module,
uint16_t i_opcode, uint16_t i_opcode,
uint8_t i_numImmediates, uint8_t i_numImmediates,
IM3Operation i_operation); IM3Operation i_operation);
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
#if 0 #if 0
M3Result m3_SetStackGlobalIndex (IM3Module io_module, M3Result m3_SetStackGlobalIndex (IM3Module io_module,
uint32_t i_index); uint32_t i_index);
M3Result m3_StackAllocate (IM3Module io_module, M3Result m3_StackAllocate (IM3Module io_module,
uint32_t * o_location, uint32_t * o_location,
const uint8_t * const i_bytes, const uint8_t * const i_bytes,
uint32_t i_size); uint32_t i_size);
M3Result m3_PopStack (IM3Module io_module, M3Result m3_PopStack (IM3Module io_module,
uint32_t i_size); uint32_t i_size);
#endif #endif
#if defined(__cplusplus) #if defined(__cplusplus)

Loading…
Cancel
Save