Coverage Report

Created: 2025-07-18 06:29

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