You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wasm3/source/m3_module.c

118 lines
2.7 KiB
C

5 years ago
//
// m3_module.c
//
// Created by Steven Massey on 5/7/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
5 years ago
#include "m3_env.h"
4 years ago
#include "m3_exception.h"
4 years ago
4 years ago
void Module_FreeFunctions (IM3Module i_module)
{
for (u32 i = 0; i < i_module->numFunctions; ++i)
{
IM3Function func = & i_module->functions [i];
Function_Release (func);
}
}
5 years ago
void m3_FreeModule (IM3Module i_module)
{
if (i_module)
{
m3log (module, "freeing module: %s (funcs: %d; segments: %d)",
i_module->name, i_module->numFunctions, i_module->numDataSegments);
Module_FreeFunctions (i_module);
m3Free (i_module->functions);
4 years ago
//m3Free (i_module->imports);
m3Free (i_module->funcTypes);
m3Free (i_module->dataSegments);
m3Free (i_module->table0);
// TODO: free importinfo
m3Free (i_module->globals);
m3Free (i_module);
}
5 years ago
}
M3Result Module_AddGlobal (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported)
{
M3Result result = m3Err_none;
4 years ago
_try {
u32 index = io_module->numGlobals++;
4 years ago
_ (m3ReallocArray (& io_module->globals, M3Global, io_module->numGlobals, index));
4 years ago
M3Global * global = & io_module->globals [index];
4 years ago
global->type = i_type;
global->imported = i_isImported;
global->isMutable = i_mutable;
4 years ago
if (o_global)
* o_global = global;
4 years ago
} _catch:
return result;
5 years ago
}
M3Result Module_AddFunction (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
{
M3Result result = m3Err_none;
4 years ago
_try {
u32 index = io_module->numFunctions++;
4 years ago
_ (m3ReallocArray (& io_module->functions, M3Function, io_module->numFunctions, index));
4 years ago
_throwif("type sig index out of bounds", i_typeIndex >= io_module->numFuncTypes);
4 years ago
IM3FuncType ft = io_module->funcTypes [i_typeIndex];
4 years ago
IM3Function func = Module_GetFunction (io_module, index);
func->funcType = ft;
4 years ago
if (i_importInfo and func->numNames == 0)
4 years ago
{
func->import = * i_importInfo;
func->numNames = 1;
func->names[0] = i_importInfo->fieldUtf8;
}
4 years ago
// m3log (module, " added function: %3d; sig: %d", index, i_typeIndex);
4 years ago
} _catch:
return result;
5 years ago
}
IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex)
{
IM3Function func = NULL;
if (i_functionIndex < i_module->numFunctions)
func = & i_module->functions [i_functionIndex];
return func;
5 years ago
}
const char* m3_GetModuleName (IM3Module i_module)
{
if (!i_module || !i_module->name)
return "<unknown>";
return i_module->name;
}
IM3Runtime m3_GetModuleRuntime (IM3Module i_module)
{
return i_module ? i_module->runtime : NULL;
}