|
|
|
@ -12,30 +12,52 @@
|
|
|
|
|
#include "m3_exception.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IM3Module m3_NewModule (IM3Environment i_environment)
|
|
|
|
|
{
|
|
|
|
|
IM3Module module = m3_AllocStruct (M3Module);
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
|
|
|
|
module->name = ".unnamed";
|
|
|
|
|
module->startFunction = -1;
|
|
|
|
|
module->environment = i_environment;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module->wasmStart = NULL;
|
|
|
|
|
module->wasmEnd = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return module;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M3Result m3_InjectFunction (IM3Module i_module,
|
|
|
|
|
int32_t * io_functionIndex,
|
|
|
|
|
const char * const i_signature,
|
|
|
|
|
const uint8_t * const i_wasmBytes,
|
|
|
|
|
bool i_doCompilation)
|
|
|
|
|
M3Result m3Ext_InjectFunction (IM3Module i_module,
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
@ -63,15 +85,19 @@ _ (ReadLEB_u32 (& size, & bytes, end));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// add slot to function type table in the module
|
|
|
|
|
u32 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));
|
|
|
|
@ -106,7 +132,7 @@ _ (CompileFunction (function));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M3Result m3_AddFunctionToTable (IM3Function i_function,
|
|
|
|
|
M3Result m3Ext_AddFunctionToTable (IM3Function i_function,
|
|
|
|
|
uint32_t * o_elementIndex,
|
|
|
|
|
uint32_t i_tableIndex)
|
|
|
|
|
{
|
|
|
|
@ -118,12 +144,13 @@ M3Result m3_AddFunctionToTable (IM3Function i_function,
|
|
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
|
{
|
|
|
|
|
u32 newTableSize = module->table0Size + 1;
|
|
|
|
|
module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, module->table0Size);
|
|
|
|
|
u32 previousSize = module->table0Size;
|
|
|
|
|
u32 newTableSize = previousSize + 1;
|
|
|
|
|
module->table0 = m3_ReallocArray (IM3Function, module->table0, newTableSize, previousSize);
|
|
|
|
|
_throwifnull (module->table0);
|
|
|
|
|
|
|
|
|
|
* o_elementIndex = module->table0Size;
|
|
|
|
|
module->table0 [* o_elementIndex] = i_function;
|
|
|
|
|
* o_elementIndex = previousSize;
|
|
|
|
|
module->table0 [previousSize] = i_function;
|
|
|
|
|
module->table0Size = newTableSize;
|
|
|
|
|
}
|
|
|
|
|
else _throw ("null module");
|
|
|
|
@ -140,6 +167,7 @@ IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index)
|
|
|
|
|
return Module_GetFunction (i_module, i_index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M3Result m3_GetFunctionIndex (IM3Function i_function,
|
|
|
|
|
uint32_t * o_index)
|
|
|
|
|
{
|
|
|
|
@ -167,3 +195,20 @@ M3Result m3_GetDataSegmentOffset (IM3Module i_module,
|
|
|
|
|
_catch: return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|