tabs->spaces

extensions
Steven Massey 3 years ago
parent a1c75493ad
commit 2fa4a11556

@ -14,97 +14,97 @@
IM3Module m3_NewModule (IM3Environment i_environment)
{
IM3Module module = m3_AllocStruct (M3Module);
if (module)
{
module->name = ".unnamed";
module->startFunction = -1;
module->environment = i_environment;
module->wasmStart = NULL;
module->wasmEnd = NULL;
}
return module;
IM3Module module = m3_AllocStruct (M3Module);
if (module)
{
module->name = ".unnamed";
module->startFunction = -1;
module->environment = i_environment;
module->wasmStart = NULL;
module->wasmEnd = NULL;
}
return module;
}
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 m3_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);
M3Result result = m3Err_none; d_m3Assert (io_functionIndex);
IM3Function function = NULL;
IM3FuncType ftype = NULL;
IM3Function function = NULL;
IM3FuncType ftype = NULL;
_ (SignatureToFuncType (& ftype, i_signature));
i32 index = * io_functionIndex;
bytes_t bytes = i_wasmBytes;
bytes_t end = i_wasmBytes + 5;
u32 size;
_ (ReadLEB_u32 (& size, & bytes, end));
end = bytes + size;
if (index >= 0)
{
_throwif ("function index out of bounds", index >= i_module->numFunctions);
function = & i_module->functions [index];
if (not AreFuncTypesEqual (ftype, function->funcType))
_throw ("function type mismatch");
}
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
index = (i32) i_module->numFunctions;
i32 index = * io_functionIndex;
bytes_t bytes = i_wasmBytes;
bytes_t end = i_wasmBytes + 5;
u32 size;
_ (ReadLEB_u32 (& size, & bytes, end));
end = bytes + size;
if (index >= 0)
{
_throwif ("function index out of bounds", index >= i_module->numFunctions);
function = & i_module->functions [index];
if (not AreFuncTypesEqual (ftype, function->funcType))
_throw ("function type mismatch");
}
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
index = (i32) i_module->numFunctions;
_ (Module_AddFunction (i_module, funcTypeIndex, NULL));
function = Module_GetFunction (i_module, index);
* io_functionIndex = index;
}
function = Module_GetFunction (i_module, index);
* io_functionIndex = index;
}
function->compiled = NULL;
function->compiled = NULL;
if (function->ownsWasmCode)
m3_Free (function->wasm);
size_t numBytes = end - i_wasmBytes;
if (function->ownsWasmCode)
m3_Free (function->wasm);
size_t numBytes = end - i_wasmBytes;
function->wasm = m3_CopyMem (i_wasmBytes, numBytes);
_throwifnull (function->wasm);
function->wasmEnd = function->wasm + numBytes;
function->ownsWasmCode = true;
if (i_doCompilation and not i_module->runtime)
_throw ("module must be loaded into runtime to compile function");
_throwifnull (function->wasm);
function->wasmEnd = function->wasm + numBytes;
function->ownsWasmCode = true;
if (i_doCompilation and not i_module->runtime)
_throw ("module must be loaded into runtime to compile function");
_ (Compile_Function (function));
_catch:
m3_Free (ftype);
return result;
_catch:
m3_Free (ftype);
return result;
}
IM3Function m3_GetFunctionByIndex (IM3Module i_module, uint32_t i_index)
{
return Module_GetFunction (i_module, i_index);
return Module_GetFunction (i_module, i_index);
}

@ -22,23 +22,23 @@ extern "C" {
// API extensions
//-------------------------------------------------------------------------------------------------------------------------------
// Creates an empty module.
IM3Module m3_NewModule (IM3Environment i_environment);
// Creates an empty module.
IM3Module m3_NewModule (IM3Environment i_environment);
// 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.
// ** InjectFunction invalidates any existing IM3Function pointers
M3Result m3_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex,
const char * const i_signature,
const uint8_t * const i_wasmBytes, // i_wasmBytes is copied
bool i_doCompilation);
// 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.
// ** InjectFunction invalidates any existing IM3Function pointers
M3Result m3_InjectFunction (IM3Module i_module,
int32_t * io_functionIndex,
const char * const i_signature,
const uint8_t * const i_wasmBytes, // i_wasmBytes is copied
bool i_doCompilation);
IM3Function m3_GetFunctionByIndex (IM3Module i_module,
uint32_t i_index);
IM3Function m3_GetFunctionByIndex (IM3Module i_module,
uint32_t i_index);
#if defined(__cplusplus)
}

@ -30,7 +30,7 @@ M3Result SignatureToFuncType (IM3FuncType * o_functionType, ccstr_t i_signatur
M3Result result = m3Err_none;
IM3FuncType funcType = NULL;
_try {
if (not o_functionType)
_throw ("null function type");

@ -2341,9 +2341,9 @@ M3Result Compile_Function (IM3Function io_function)
{
IM3FuncType ft = io_function->funcType;
M3Result result = m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s",
M3Result result = m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s",
m3_GetFunctionName(io_function), (u32) (io_function->wasmEnd - io_function->wasm), GetFunctionNumArgs (io_function),
c_waTypes [GetSingleRetType(ft)]);
c_waTypes [GetSingleRetType(ft)]);
IM3Runtime runtime = io_function->module->runtime;
IM3Compilation o = & runtime->compilation;
@ -2356,10 +2356,10 @@ M3Result Compile_Function (IM3Function io_function)
o->wasmEnd = io_function->wasmEnd;
_try {
// skip over code size. the end was already calculated during parse phase
u32 size;
_ (ReadLEB_u32 (& size, & o->wasm, o->wasmEnd)); d_m3Assert (size == (o->wasmEnd - o->wasm))
// skip over code size. the end was already calculated during parse phase
u32 size;
_ (ReadLEB_u32 (& size, & o->wasm, o->wasmEnd)); d_m3Assert (size == (o->wasmEnd - o->wasm))
_ (AcquireCompilationCodePage (o, & o->page));
pc_t pc = GetPagePC (o->page);

@ -177,38 +177,38 @@ void FreeImportInfo (M3ImportInfo * i_info)
IM3Environment m3_NewEnvironment ()
{
M3Result result = m3Err_none;
M3Result result = m3Err_none;
IM3Environment env = m3_AllocStruct (M3Environment);
if (env)
{
_try
{
// create FuncTypes for all simple block return ValueTypes
for (u8 t = c_m3Type_none; t <= c_m3Type_f64; t++)
{
IM3FuncType ftype;
_ (AllocFuncType (& ftype, 1));
ftype->numArgs = 0;
ftype->numRets = (t == c_m3Type_none) ? 0 : 1;
ftype->types [0] = t;
Environment_AddFuncType (env, & ftype);
d_m3Assert (t < 5);
env->retFuncTypes [t] = ftype;
}
}
_catch:
if (result)
{
m3_FreeEnvironment (env);
env = NULL;
}
}
if (env)
{
_try
{
// create FuncTypes for all simple block return ValueTypes
for (u8 t = c_m3Type_none; t <= c_m3Type_f64; t++)
{
IM3FuncType ftype;
_ (AllocFuncType (& ftype, 1));
ftype->numArgs = 0;
ftype->numRets = (t == c_m3Type_none) ? 0 : 1;
ftype->types [0] = t;
Environment_AddFuncType (env, & ftype);
d_m3Assert (t < 5);
env->retFuncTypes [t] = ftype;
}
}
_catch:
if (result)
{
m3_FreeEnvironment (env);
env = NULL;
}
}
return env;
}
@ -224,7 +224,7 @@ void Environment_Release (IM3Environment i_environment)
m3_Free (ftype);
ftype = next;
}
m3log (runtime, "freeing %d pages from environment", CountCodePages (i_environment->pagesReleased));
FreeCodePages (& i_environment->pagesReleased);
}

@ -149,10 +149,10 @@ typedef struct M3Module
cstr_t name;
u32 numFuncTypes;
IM3FuncType * funcTypes; // array of pointers to list of FuncTypes
IM3FuncType * funcTypes; // array of pointers to list of FuncTypes
u32 numImports;
//IM3Function * imports; b // notice: "I" prefix. imports are pointers to functions in another module.
//IM3Function * imports; b // notice: "I" prefix. imports are pointers to functions in another module.
u32 numFunctions;
M3Function * functions;
@ -193,10 +193,10 @@ typedef struct M3Environment
{
// struct M3Runtime * runtimes;
IM3FuncType funcTypes; // linked list
IM3FuncType funcTypes; // linked list
IM3FuncType retFuncTypes [c_m3Type_unknown]; // these 'point' to elements in the linked list above.
// the number of elements must match the basic types as per M3ValueType
IM3FuncType retFuncTypes [c_m3Type_unknown]; // these 'point' to elements in the linked list above.
// the number of elements must match the basic types as per M3ValueType
M3CodePage * pagesReleased;
}
M3Environment;

@ -66,7 +66,7 @@ _try {
M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
{
M3Result result = m3Err_none;
_try {
u32 index = io_module->numFunctions++;
io_module->functions = m3_ReallocArray (M3Function, io_module->functions, io_module->numFunctions, index);
@ -87,8 +87,8 @@ _try {
func->numNames = 1;
func->names[0] = i_importInfo->fieldUtf8;
}
m3log (module, " added function: %3d; sig: %d", index, i_typeIndex);
m3log (module, " added function: %3d; sig: %d", index, i_typeIndex);
} _catch:
return result;
@ -100,10 +100,10 @@ IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex)
IM3Function func = NULL;
if (i_functionIndex < i_module->numFunctions)
{
{
func = & i_module->functions [i_functionIndex];
func->module = i_module;
}
func->module = i_module;
}
return func;
}

@ -314,8 +314,8 @@ _ (ReadLEB_u32 (& numFunctions, & i_bytes, i_end));
for (u32 f = 0; f < numFunctions; ++f)
{
const u8 * start = i_bytes;
const u8 * start = i_bytes;
u32 size;
_ (ReadLEB_u32 (& size, & i_bytes, i_end));
@ -348,7 +348,7 @@ _ (NormalizeType (& normalType, wasmType));
func->module = io_module;
func->wasm = start;
func->wasmEnd = i_bytes;
func->wasmEnd = i_bytes;
//func->ownsWasmCode = io_module->hasWasmCodeCopy;
func->numLocals = numLocals;
}

@ -178,7 +178,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
void m3_FreeRuntime (IM3Runtime i_runtime);
// Wasm currently only supports one memory region. i_memoryIndex should be zero.
// Wasm currently only supports one memory region. i_memoryIndex should be zero.
uint8_t * m3_GetMemory (IM3Runtime i_runtime,
uint32_t * o_memorySizeInBytes,
uint32_t i_memoryIndex);
@ -190,19 +190,19 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
// modules
//-------------------------------------------------------------------------------------------------------------------------------
// i_wasmBytes data must be persistent during the lifetime of the module
// i_wasmBytes data must be persistent during the lifetime of the module
M3Result m3_ParseModule (IM3Environment i_environment,
IM3Module * o_module,
const uint8_t * const i_wasmBytes,
uint32_t i_numWasmBytes);
// Only unloaded modules need to be freed
// Only unloaded modules need to be freed
void m3_FreeModule (IM3Module i_module);
// LoadModule transfers ownership of a module to the runtime. Do not free modules once successfully imported into the runtime
// LoadModule transfers ownership of a module to the runtime. Do not free modules once successfully imported into the runtime
M3Result m3_LoadModule (IM3Runtime io_runtime, IM3Module io_module);
// Calling m3_RunStart is optional
// Calling m3_RunStart is optional
M3Result m3_RunStart (IM3Module i_module);
typedef const void * (* M3RawCall) (IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem);
@ -228,7 +228,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
//-------------------------------------------------------------------------------------------------------------------------------
M3Result m3_Yield (void);
// o_function is valid during the lifetime of the originating runtime
// o_function is valid during the lifetime of the originating runtime
M3Result m3_FindFunction (IM3Function * o_function,
IM3Runtime i_runtime,
const char * const i_functionName);
@ -262,7 +262,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow")
void m3_PrintM3Info (void);
void m3_PrintProfilerInfo (void);
// The runtime owns the backtrace, do not free the backtrace you obtain. Returns NULL if there's no backtrace.
// The runtime owns the backtrace, do not free the backtrace you obtain. Returns NULL if there's no backtrace.
IM3BacktraceInfo m3_GetBacktrace (IM3Runtime i_runtime);
#if defined(__cplusplus)

@ -75,7 +75,7 @@ int main (int i_argc, const char * i_argv [])
}
DisabledTest (codepages.b)
DisabledTest (codepages.b)
{
const u32 c_numPages = 2000;
IM3CodePage pages [2000] = { NULL };
@ -126,41 +126,41 @@ int main (int i_argc, const char * i_argv [])
Test (extensions)
{
M3Result result;
M3Result result;
IM3Environment env = m3_NewEnvironment ();
IM3Runtime runtime = m3_NewRuntime (env, 1024, NULL);
IM3Module module = m3_NewModule (env);
i32 functionIndex = -1;
u8 wasm [5] = { 0x04, // size
0x00, // num local defs
0x41, 0x37, // i32.const= 55
0x0b // end block
};
// will partially fail (compilation) because module isn't attached to a runtime yet.
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result != m3Err_none)
expect (functionIndex >= 0)
i32 functionIndex = -1;
u8 wasm [5] = { 0x04, // size
0x00, // num local defs
0x41, 0x37, // i32.const= 55
0x0b // end block
};
// will partially fail (compilation) because module isn't attached to a runtime yet.
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result != m3Err_none)
expect (functionIndex >= 0)
result = m3_LoadModule (runtime, module); expect (result == m3Err_none)
result = m3_LoadModule (runtime, module); expect (result == m3Err_none)
// try again
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result == m3Err_none)
// try again
result = m3_InjectFunction (module, & functionIndex, "i()", wasm, true); expect (result == m3Err_none)
IM3Function function = m3_GetFunctionByIndex (module, functionIndex); expect (function)
if (function)
{
result = m3_CallV (function); expect (result == m3Err_none)
u32 ret = 0;
m3_GetResultsV (function, & ret); expect (ret == 55);
}
IM3Function function = m3_GetFunctionByIndex (module, functionIndex); expect (function)
if (function)
{
result = m3_CallV (function); expect (result == m3Err_none)
u32 ret = 0;
m3_GetResultsV (function, & ret); expect (ret == 55);
}
m3_FreeRuntime (runtime);
}

Loading…
Cancel
Save