Coverage Report

Created: 2026-01-10 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm3/source/m3_module.c
Line
Count
Source
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
192
{
14
4.91k
    for (u32 i = 0; i < i_module->numFunctions; ++i)
15
4.72k
    {
16
4.72k
        IM3Function func = & i_module->functions [i];
17
4.72k
        Function_Release (func);
18
4.72k
    }
19
192
}
20
21
22
void  m3_FreeModule  (IM3Module i_module)
23
192
{
24
192
    if (i_module)
25
192
    {
26
192
        m3log (module, "freeing module: %s (funcs: %d; segments: %d)",
27
192
               i_module->name, i_module->numFunctions, i_module->numDataSegments);
28
29
192
        Module_FreeFunctions (i_module);
30
31
192
        m3_Free (i_module->functions);
32
        //m3_Free (i_module->imports);
33
192
        m3_Free (i_module->funcTypes);
34
192
        m3_Free (i_module->dataSegments);
35
192
        m3_Free (i_module->table0);
36
37
425
        for (u32 i = 0; i < i_module->numGlobals; ++i)
38
233
        {
39
233
            m3_Free (i_module->globals[i].name);
40
233
            FreeImportInfo(&(i_module->globals[i].import));
41
233
        }
42
192
        m3_Free (i_module->globals);
43
192
        m3_Free (i_module->memoryExportName);
44
192
        m3_Free (i_module->table0ExportName);
45
46
192
        FreeImportInfo(&i_module->memoryImport);
47
48
192
        m3_Free (i_module);
49
192
    }
50
192
}
51
52
53
M3Result  Module_AddGlobal  (IM3Module io_module, IM3Global * o_global, u8 i_type, bool i_mutable, bool i_isImported)
54
233
{
55
233
_try {
56
233
    u32 index = io_module->numGlobals++;
57
233
    io_module->globals = m3_ReallocArray (M3Global, io_module->globals, io_module->numGlobals, index);
58
233
    _throwifnull (io_module->globals);
59
233
    M3Global * global = & io_module->globals [index];
60
61
233
    global->type = i_type;
62
233
    global->imported = i_isImported;
63
233
    global->isMutable = i_mutable;
64
65
233
    if (o_global)
66
233
        * o_global = global;
67
68
233
} _catch:
69
233
    return result;
70
233
}
71
72
M3Result  Module_PreallocFunctions  (IM3Module io_module, u32 i_totalFunctions)
73
4.82k
{
74
4.82k
_try {
75
4.82k
    if (i_totalFunctions > io_module->allFunctions) {
76
101
        io_module->functions = m3_ReallocArray (M3Function, io_module->functions, i_totalFunctions, io_module->allFunctions);
77
101
        io_module->allFunctions = i_totalFunctions;
78
101
        _throwifnull (io_module->functions);
79
101
    }
80
4.82k
} _catch:
81
4.82k
    return result;
82
4.82k
}
83
84
M3Result  Module_AddFunction  (IM3Module io_module, u32 i_typeIndex, IM3ImportInfo i_importInfo)
85
4.72k
{
86
4.72k
_try {
87
88
4.72k
    u32 index = io_module->numFunctions++;
89
4.72k
_   (Module_PreallocFunctions(io_module, io_module->numFunctions));
90
91
4.72k
    _throwif ("type sig index out of bounds", i_typeIndex >= io_module->numFuncTypes);
92
93
4.71k
    IM3FuncType ft = io_module->funcTypes [i_typeIndex];
94
95
4.71k
    IM3Function func = Module_GetFunction (io_module, index);
96
4.71k
    func->funcType = ft;
97
98
#   ifdef DEBUG
99
    func->index = index;
100
#   endif
101
102
4.71k
    if (i_importInfo and func->numNames == 0)
103
2.72k
    {
104
2.72k
        func->import = * i_importInfo;
105
2.72k
        func->names[0] = i_importInfo->fieldUtf8;
106
2.72k
        func->numNames = 1;
107
2.72k
    }
108
109
4.71k
    m3log (module, "   added function: %3d; sig: %d", index, i_typeIndex);
110
111
4.72k
} _catch:
112
4.72k
    return result;
113
4.71k
}
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.88k
{
146
4.88k
    IM3Function func = NULL;
147
148
4.88k
    if (i_functionIndex < i_module->numFunctions)
149
4.88k
    {
150
4.88k
        func = & i_module->functions [i_functionIndex];
151
        //func->module = i_module;
152
4.88k
    }
153
154
4.88k
    return func;
155
4.88k
}
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