Coverage Report

Created: 2025-07-11 06:27

/src/wasm3/source/m3_module.c
Line
Count
Source (jump to first uncovered line)
1
//
2
//  m3_module.c
3
//
4
//  Created by Steven Massey on 5/7/19.
5
//  Copyright © 2019 Steven Massey. All rights reserved.
6
//
7
8
#include "m3_env.h"
9
#include "m3_exception.h"
10
11
12
void Module_FreeFunctions (IM3Module i_module)
13
183
{
14
4.81k
    for (u32 i = 0; i < i_module->numFunctions; ++i)
15
4.63k
    {
16
4.63k
        IM3Function func = & i_module->functions [i];
17
4.63k
        Function_Release (func);
18
4.63k
    }
19
183
}
20
21
22
void  m3_FreeModule  (IM3Module i_module)
23
183
{
24
183
    if (i_module)
25
183
    {
26
183
        m3log (module, "freeing module: %s (funcs: %d; segments: %d)",
27
183
               i_module->name, i_module->numFunctions, i_module->numDataSegments);
28
29
183
        Module_FreeFunctions (i_module);
30
31
183
        m3_Free (i_module->functions);
32
        //m3_Free (i_module->imports);
33
183
        m3_Free (i_module->funcTypes);
34
183
        m3_Free (i_module->dataSegments);
35
183
        m3_Free (i_module->table0);
36
37
672
        for (u32 i = 0; i < i_module->numGlobals; ++i)
38
489
        {
39
489
            m3_Free (i_module->globals[i].name);
40
489
            FreeImportInfo(&(i_module->globals[i].import));
41
489
        }
42
183
        m3_Free (i_module->globals);
43
183
        m3_Free (i_module->memoryExportName);
44
183
        m3_Free (i_module->table0ExportName);
45
46
183
        FreeImportInfo(&i_module->memoryImport);
47
48
183
        m3_Free (i_module);
49
183
    }
50
183
}
51
52
53
M3Result  Module_AddGlobal  (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported)
54
489
{
55
489
_try {
56
489
    u32 index = io_module->numGlobals++;
57
489
    io_module->globals = m3_ReallocArray (M3Global, io_module->globals, io_module->numGlobals, index);
58
489
    _throwifnull (io_module->globals);
59
489
    M3Global * global = & io_module->globals [index];
60
61
489
    global->type = i_type;
62
489
    global->imported = i_isImported;
63
489
    global->isMutable = i_mutable;
64
65
489
    if (o_global)
66
489
        * o_global = global;
67
68
489
} _catch:
69
489
    return result;
70
489
}
71
72
M3Result  Module_PreallocFunctions  (IM3Module io_module, u32 i_totalFunctions)
73
4.70k
{
74
4.70k
_try {
75
4.70k
    if (i_totalFunctions > io_module->allFunctions) {
76
69
        io_module->functions = m3_ReallocArray (M3Function, io_module->functions, i_totalFunctions, io_module->allFunctions);
77
69
        io_module->allFunctions = i_totalFunctions;
78
69
        _throwifnull (io_module->functions);
79
69
    }
80
4.70k
} _catch:
81
4.70k
    return result;
82
4.70k
}
83
84
M3Result  Module_AddFunction  (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
85
4.63k
{
86
4.63k
_try {
87
88
4.63k
    u32 index = io_module->numFunctions++;
89
4.63k
_   (Module_PreallocFunctions(io_module, io_module->numFunctions));
90
91
4.63k
    _throwif ("type sig index out of bounds", i_typeIndex >= io_module->numFuncTypes);
92
93
4.63k
    IM3FuncType ft = io_module->funcTypes [i_typeIndex];
94
95
4.63k
    IM3Function func = Module_GetFunction (io_module, index);
96
4.63k
    func->funcType = ft;
97
98
#   ifdef DEBUG
99
    func->index = index;
100
#   endif
101
102
4.63k
    if (i_importInfo and func->numNames == 0)
103
2.98k
    {
104
2.98k
        func->import = * i_importInfo;
105
2.98k
        func->names[0] = i_importInfo->fieldUtf8;
106
2.98k
        func->numNames = 1;
107
2.98k
    }
108
109
4.63k
    m3log (module, "   added function: %3d; sig: %d", index, i_typeIndex);
110
111
4.63k
} _catch:
112
4.63k
    return result;
113
4.63k
}
114
115
#ifdef DEBUG
116
void  Module_GenerateNames  (IM3Module i_module)
117
{
118
    for (u32 i = 0; i < i_module->numFunctions; ++i)
119
    {
120
        IM3Function func = & i_module->functions [i];
121
122
        if (func->numNames == 0)
123
        {
124
            char* buff = m3_AllocArray(char, 16);
125
            snprintf(buff, 16, "$func%d", i);
126
            func->names[0] = buff;
127
            func->numNames = 1;
128
        }
129
    }
130
    for (u32 i = 0; i < i_module->numGlobals; ++i)
131
    {
132
        IM3Global global = & i_module->globals [i];
133
134
        if (global->name == NULL)
135
        {
136
            char* buff = m3_AllocArray(char, 16);
137
            snprintf(buff, 16, "$global%d", i);
138
            global->name = buff;
139
        }
140
    }
141
}
142
#endif
143
144
IM3Function  Module_GetFunction  (IM3Module i_module, u32 i_functionIndex)
145
4.68k
{
146
4.68k
    IM3Function func = NULL;
147
148
4.68k
    if (i_functionIndex < i_module->numFunctions)
149
4.68k
    {
150
4.68k
        func = & i_module->functions [i_functionIndex];
151
        //func->module = i_module;
152
4.68k
    }
153
154
4.68k
    return func;
155
4.68k
}
156
157
158
const char*  m3_GetModuleName  (IM3Module i_module)
159
0
{
160
0
    if (!i_module || !i_module->name)
161
0
        return ".unnamed";
162
163
0
    return i_module->name;
164
0
}
165
166
void  m3_SetModuleName  (IM3Module i_module, const char* name)
167
0
{
168
0
    if (i_module) i_module->name = name;
169
0
}
170
171
IM3Runtime  m3_GetModuleRuntime  (IM3Module i_module)
172
0
{
173
0
    return i_module ? i_module->runtime : NULL;
174
0
}
175