diff --git a/source/extensions/m3_extensions.c b/source/extensions/m3_extensions.c index df632cb..22962fe 100644 --- a/source/extensions/m3_extensions.c +++ b/source/extensions/m3_extensions.c @@ -13,51 +13,51 @@ -M3Result m3Ext_ReserveFunctions (IM3Module i_module, - uint32_t i_numFunctions) +M3Result m3Ext_ReserveFunctions (IM3Module i_module, + uint32_t i_numFunctions) { - M3Result result = m3Err_none; - - if (i_module) - { d_m3Assert (i_module->table0Size == 0); - if (i_module->table0Size == 0) - { - i_module->numReservedFunctions = i_numFunctions; - - u32 totalFunctions = i_module->numFunctions + i_numFunctions; - - i_module->functions = m3_ReallocArray (M3Function, i_module->functions, totalFunctions, i_module->numFunctions); - _throwifnull (i_module->functions); - } - else _throw ("ReserveFunctions must come before LoadModule"); - } - else _throw (m3Err_nullArgument); - - _catch: - return result; + M3Result result = m3Err_none; + + if (i_module) + { d_m3Assert (i_module->table0Size == 0); + if (i_module->table0Size == 0) + { + i_module->numReservedFunctions = i_numFunctions; + + u32 totalFunctions = i_module->numFunctions + i_numFunctions; + + i_module->functions = m3_ReallocArray (M3Function, i_module->functions, totalFunctions, i_module->numFunctions); + _throwifnull (i_module->functions); + } + else _throw ("ReserveFunctions must come before LoadModule"); + } + else _throw (m3Err_nullArgument); + + _catch: + return result; } i32 Module_HasFuncType (IM3Module i_module, IM3FuncType i_funcType) -{ - if (i_module->funcTypes) - { - for (u32 i = 0; i < i_module->numFuncTypes; ++i) - { - if (AreFuncTypesEqual (i_module->funcTypes [i], i_funcType)) - return i; - } - } - - return -1; +{ + if (i_module->funcTypes) + { + for (u32 i = 0; i < i_module->numFuncTypes; ++i) + { + if (AreFuncTypesEqual (i_module->funcTypes [i], i_funcType)) + return i; + } + } + + return -1; } M3Result m3Ext_InjectFunction (IM3Module i_module, - int32_t * io_functionIndex, - const char * const i_signature, - const uint8_t * const i_wasmBytes, - bool i_doCompilation) + int32_t * io_functionIndex, + const char * const i_signature, + const uint8_t * const i_wasmBytes, + bool i_doCompilation) { M3Result result = m3Err_none; d_m3Assert (io_functionIndex); @@ -85,19 +85,19 @@ _ (ReadLEB_u32 (& size, & bytes, end)); } else { - i32 funcTypeIndex = Module_HasFuncType (i_module, ftype); - if (funcTypeIndex < 0) - { - // add slot to function type table in the module - funcTypeIndex = i_module->numFuncTypes++; - i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex); - _throwifnull (i_module->funcTypes); - - // add functype object to the environment - Environment_AddFuncType (i_module->environment, & ftype); - i_module->funcTypes [funcTypeIndex] = ftype; - ftype = NULL; // prevent freeing below - } + i32 funcTypeIndex = Module_HasFuncType (i_module, ftype); + if (funcTypeIndex < 0) + { + // add slot to function type table in the module + funcTypeIndex = i_module->numFuncTypes++; + i_module->funcTypes = m3_ReallocArray (IM3FuncType, i_module->funcTypes, i_module->numFuncTypes, funcTypeIndex); + _throwifnull (i_module->funcTypes); + + // add functype object to the environment + Environment_AddFuncType (i_module->environment, & ftype); + i_module->funcTypes [funcTypeIndex] = ftype; + ftype = NULL; // prevent freeing below + } index = (i32) i_module->numFunctions; _ (Module_AddFunction (i_module, funcTypeIndex, NULL)); @@ -119,7 +119,7 @@ _ (Module_AddFunction (i_module, funcTypeIndex, NULL)); function->ownsWasmCode = true; function->module = i_module; - + if (i_doCompilation and not i_module->runtime) _throw ("module must be loaded into runtime to compile function"); @@ -132,33 +132,33 @@ _ (CompileFunction (function)); } -M3Result m3Ext_AddFunctionToTable (IM3Function i_function, - uint32_t * o_elementIndex, - uint32_t i_tableIndex) +M3Result m3Ext_AddFunctionToTable (IM3Function i_function, + uint32_t * o_elementIndex, + uint32_t i_tableIndex) { - M3Result result = m3Err_none; - - if (i_function and o_elementIndex) - { - IM3Module module = i_function->module; - - if (module) - { - u32 previousSize = module->table0Size; - u32 newTableSize = previousSize + 1; - module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, previousSize); - _throwifnull (module->table0); - - * o_elementIndex = previousSize; - module->table0 [previousSize] = i_function; - module->table0Size = newTableSize; - } - else _throw ("null module"); - - } - else _throw (m3Err_nullArgument); - - _catch: return result; + M3Result result = m3Err_none; + + if (i_function and o_elementIndex) + { + IM3Module module = i_function->module; + + if (module) + { + u32 previousSize = module->table0Size; + u32 newTableSize = previousSize + 1; + module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, previousSize); + _throwifnull (module->table0); + + * o_elementIndex = previousSize; + module->table0 [previousSize] = i_function; + module->table0Size = newTableSize; + } + else _throw ("null module"); + + } + else _throw (m3Err_nullArgument); + + _catch: return result; } @@ -168,47 +168,47 @@ IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index) } -M3Result m3_GetFunctionIndex (IM3Function i_function, - uint32_t * o_index) +M3Result m3_GetFunctionIndex (IM3Function i_function, + uint32_t * o_index) { - if (i_function and o_index) - { - * o_index = i_function->index; - - return m3Err_none; - } - else return m3Err_functionLookupFailed; + if (i_function and o_index) + { + * o_index = i_function->index; + + return m3Err_none; + } + else return m3Err_functionLookupFailed; } -M3Result m3_GetDataSegmentOffset (IM3Module i_module, - uint32_t i_index) +M3Result m3_GetDataSegmentOffset (IM3Module i_module, + uint32_t i_index) { - M3Result result = m3Err_none; d_m3Assert (i_module); - - if (i_module) - { - d_m3Assert (false); // TODO: finish - } - else _throw (m3Err_nullArgument); - - _catch: return result; + M3Result result = m3Err_none; d_m3Assert (i_module); + + if (i_module) + { + d_m3Assert (false); // TODO: finish + } + else _throw (m3Err_nullArgument); + + _catch: return result; } -M3Result m3_RegisterCustomOpcode (IM3Module i_module, - uint16_t i_opcode, - uint8_t i_numImmediates, - IM3Operation i_operation) +M3Result m3_RegisterCustomOpcode (IM3Module i_module, + uint16_t i_opcode, + uint8_t i_numImmediates, + IM3Operation i_operation) { - M3Result result = m3Err_none; d_m3Assert (i_module); - - if (i_module) - { - - } - else _throw (m3Err_nullArgument); - - _catch: return result; + M3Result result = m3Err_none; d_m3Assert (i_module); + + if (i_module) + { + + } + else _throw (m3Err_nullArgument); + + _catch: return result; } diff --git a/source/extensions/wasm3_ext.h b/source/extensions/wasm3_ext.h index 822f91b..bdb7167 100644 --- a/source/extensions/wasm3_ext.h +++ b/source/extensions/wasm3_ext.h @@ -22,63 +22,63 @@ extern "C" { // API extensions //------------------------------------------------------------------------------------------------------------------------------- /* - These extensions allow for unconventional uses of Wasm3 -- mainly dynamic modification of modules to inject new Wasm - functions during runtime. + These extensions allow for unconventional uses of Wasm3 -- mainly dynamic modification of modules to inject new Wasm + functions during runtime. */ //------------------------------------------------------------------------------------------------------------------------------- // Creates an empty module. IM3Module m3Ext_NewModule (IM3Environment i_environment); - // ReserveFunctions must be called prior to LoadModule. This reserves extra empty function slots that InjectFunction - // can rely upon. - M3Result m3Ext_ReserveFunctions (IM3Module i_module, - uint32_t i_numFunctions); + // ReserveFunctions must be called prior to LoadModule. This reserves extra empty function slots that InjectFunction + // can rely upon. + M3Result m3Ext_ReserveFunctions (IM3Module i_module, + uint32_t i_numFunctions); // 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 // function signature. TODO: failure result is? M3Result m3Ext_InjectFunction (IM3Module i_module, int32_t * io_functionIndex, -// const char * const i_name, +// const char * const i_name, const char * const i_signature, const uint8_t * const i_wasmBytes, // i_wasmBytes is copied bool i_doCompilation); - M3Result m3Ext_AddFunctionToTable (IM3Function i_function, - uint32_t * o_elementIndex, - uint32_t i_tableIndex); // i_tableIndex must be zero + M3Result m3Ext_AddFunctionToTable (IM3Function i_function, + uint32_t * o_elementIndex, + uint32_t i_tableIndex); // i_tableIndex must be zero IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index); - M3Result m3_GetFunctionIndex (IM3Function i_function, - uint32_t * o_index); + M3Result m3_GetFunctionIndex (IM3Function i_function, + uint32_t * o_index); - M3Result m3_GetDataSegmentOffset (IM3Module i_module, - uint32_t i_index); + M3Result m3_GetDataSegmentOffset (IM3Module i_module, + uint32_t i_index); //------------------------------------------------------------------------------------------------------------------------------- - M3Result m3_RegisterCustomOpcode (IM3Module i_module, - uint16_t i_opcode, - uint8_t i_numImmediates, - IM3Operation i_operation); + M3Result m3_RegisterCustomOpcode (IM3Module i_module, + uint16_t i_opcode, + uint8_t i_numImmediates, + IM3Operation i_operation); //------------------------------------------------------------------------------------------------------------------------------- #if 0 - M3Result m3_SetStackGlobalIndex (IM3Module io_module, - uint32_t i_index); + M3Result m3_SetStackGlobalIndex (IM3Module io_module, + uint32_t i_index); - M3Result m3_StackAllocate (IM3Module io_module, - uint32_t * o_location, - const uint8_t * const i_bytes, - uint32_t i_size); + M3Result m3_StackAllocate (IM3Module io_module, + uint32_t * o_location, + const uint8_t * const i_bytes, + uint32_t i_size); - M3Result m3_PopStack (IM3Module io_module, - uint32_t i_size); + M3Result m3_PopStack (IM3Module io_module, + uint32_t i_size); #endif #if defined(__cplusplus)