Coverage Report

Created: 2023-06-07 06:30

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