Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wasm3/source/m3_function.c
Line
Count
Source
1
//
2
//  m3_function.c
3
//
4
//  Created by Steven Massey on 4/7/21.
5
//  Copyright © 2021 Steven Massey. All rights reserved.
6
//
7
8
#include "m3_function.h"
9
#include "m3_env.h"
10
11
12
M3Result AllocFuncType (IM3FuncType* o_functionType, u32 i_numTypes)
13
421k
{
14
421k
    *o_functionType = (IM3FuncType)m3_Malloc("M3FuncType", sizeof(M3FuncType) + i_numTypes * sizeof(m3type_t));
15
421k
    if (*o_functionType) {
16
421k
        (*o_functionType)->isContinuation = false;
17
421k
        (*o_functionType)->contFuncType   = NULL;
18
421k
        (*o_functionType)->inRecGroup     = false;
19
421k
        return m3Err_none;
20
421k
    }
21
0
    return m3Err_mallocFailed;
22
421k
}
23
24
25
#if d_m3HasTypedRefs
26
27
// The type a (ref $t) / (ref null $t) naming this function type is spelled as
28
m3type_t RefTypeOfFuncType (const IM3FuncType i_funcType, bool i_nonNull)
29
756
{
30
756
    m3type_t type = d_m3Type_ref | (i_funcType ? i_funcType->canonicalIndex : d_m3Type_heapAbstract);
31
32
756
    if (i_funcType and i_funcType->isContinuation) {
33
0
        type |= d_m3Type_refCont;
34
0
    }
35
36
756
    if (i_nonNull) {
37
647
        type |= d_m3Type_refNonNull;
38
647
    }
39
40
756
    return type;
41
756
}
42
43
#endif // d_m3HasTypedRefs
44
45
46
bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB)
47
1.81M
{
48
1.81M
    if (i_typeA->isContinuation != i_typeB->isContinuation) {
49
3.11k
        return false;
50
3.11k
    }
51
1.81M
    if (i_typeA->isContinuation) {
52
1.45k
        return (i_typeA->contFuncType == i_typeB->contFuncType) or
53
695
               (i_typeA->contFuncType and i_typeB->contFuncType and
54
695
                AreFuncTypesEqual(i_typeA->contFuncType, i_typeB->contFuncType));
55
1.45k
    }
56
1.81M
    if (i_typeA->numRets == i_typeB->numRets and i_typeA->numArgs == i_typeB->numArgs) {
57
1.18M
        return (memcmp(i_typeA->types, i_typeB->types,
58
1.18M
                       (i_typeA->numRets + i_typeA->numArgs) * sizeof(m3type_t)) == 0);
59
1.18M
    }
60
61
626k
    return false;
62
1.81M
}
63
64
u16 GetFuncTypeNumParams (const IM3FuncType i_funcType)
65
40.9k
{
66
40.9k
    return i_funcType ? i_funcType->numArgs : 0;
67
40.9k
}
68
69
70
m3type_t GetFuncTypeParamType (const IM3FuncType i_funcType, u16 i_index)
71
20.1k
{
72
20.1k
    m3type_t type = c_m3Type_unknown;
73
74
20.1k
    if (i_funcType) {
75
20.1k
        if (i_index < i_funcType->numArgs) {
76
20.1k
            type = i_funcType->types[i_funcType->numRets + i_index];
77
20.1k
        }
78
20.1k
    }
79
80
20.1k
    return type;
81
20.1k
}
82
83
84
u16 GetFuncTypeNumResults (const IM3FuncType i_funcType)
85
158k
{
86
158k
    return i_funcType ? i_funcType->numRets : 0;
87
158k
}
88
89
90
m3type_t GetFuncTypeResultType (const IM3FuncType i_funcType, u16 i_index)
91
48.4k
{
92
48.4k
    m3type_t type = c_m3Type_unknown;
93
94
48.4k
    if (i_funcType) {
95
48.4k
        if (i_index < i_funcType->numRets) {
96
48.4k
            type = i_funcType->types[i_index];
97
48.4k
        }
98
48.4k
    }
99
100
48.4k
    return type;
101
48.4k
}
102
103
104
//---------------------------------------------------------------------------------------------------------------
105
106
107
void FreeImportInfo (M3ImportInfo* i_info)
108
65.4k
{
109
65.4k
    m3_Free(i_info->moduleUtf8);
110
65.4k
    m3_Free(i_info->fieldUtf8);
111
65.4k
}
112
113
114
#if d_m3HasSnapshots
115
116
void SnapshotMap_Free (M3SnapshotMap* i_map)
117
73.4k
{
118
73.4k
    if (i_map) {
119
0
        m3_Free(i_map->locals);
120
0
        m3_Free(i_map->safePoints);
121
0
        m3_Free(i_map->values);
122
0
        m3_Free(i_map->blocks);
123
0
        m3_Free(i_map);
124
0
    }
125
73.4k
}
126
127
#endif
128
129
void Function_Release (IM3Function i_function)
130
42.3k
{
131
42.3k
    m3_Free(i_function->constants);
132
133
42.3k
#if d_m3HasSnapshots
134
42.3k
    SnapshotMap_Free(i_function->snapshotMap);
135
42.3k
    i_function->snapshotMap = NULL;
136
42.3k
#endif
137
138
81.6k
    for (int i = 0; i < i_function->numNames; i++) {
139
        // name can be an alias of fieldUtf8
140
39.2k
        if (i_function->names[i] != i_function->import.fieldUtf8) {
141
36.5k
            m3_Free(i_function->names[i]);
142
36.5k
        }
143
39.2k
    }
144
145
42.3k
    FreeImportInfo(&i_function->import);
146
147
42.3k
    if (i_function->ownsWasmCode) {
148
0
        m3_Free(i_function->wasm);
149
0
    }
150
151
    // Function_FreeCompiledCode (func);
152
153
#if (d_m3EnableCodePageRefCounting)
154
    {
155
        m3_Free(i_function->codePageRefs);
156
        i_function->numCodePageRefs = 0;
157
    }
158
#endif
159
42.3k
}
160
161
162
void Function_FreeCompiledCode (IM3Function i_function)
163
0
{
164
#if (d_m3EnableCodePageRefCounting)
165
    {
166
        i_function->compiled = NULL;
167
168
        while (i_function->numCodePageRefs--) {
169
            IM3CodePage page = i_function->codePageRefs[i_function->numCodePageRefs];
170
171
            if (--(page->info.usageCount) == 0) {
172
                //                printf ("free %p\n", page);
173
            }
174
        }
175
176
        m3_Free(i_function->codePageRefs);
177
178
        Runtime_ReleaseCodePages(i_function->module->runtime);
179
    }
180
#endif
181
0
}
182
183
184
cstr_t m3_GetFunctionName (IM3Function i_function)
185
2
{
186
2
    u16     numNames = 0;
187
2
    cstr_t* names    = GetFunctionNames(i_function, &numNames);
188
2
    if (numNames > 0) {
189
1
        return names[0];
190
1
    } else {
191
1
        return "<unnamed>";
192
1
    }
193
2
}
194
195
196
IM3Module m3_GetFunctionModule (IM3Function i_function)
197
0
{
198
0
    return i_function ? i_function->module : NULL;
199
0
}
200
201
202
cstr_t* GetFunctionNames (IM3Function i_function, u16* o_numNames)
203
2
{
204
2
    if (!i_function || !o_numNames) {
205
0
        return NULL;
206
0
    }
207
208
2
    if (i_function->import.fieldUtf8) {
209
0
        *o_numNames = 1;
210
0
        return &i_function->import.fieldUtf8;
211
2
    } else {
212
2
        *o_numNames = i_function->numNames;
213
2
        return i_function->names;
214
2
    }
215
2
}
216
217
218
cstr_t GetFunctionImportModuleName (IM3Function i_function)
219
2
{
220
2
    return (i_function->import.moduleUtf8) ? i_function->import.moduleUtf8 : "";
221
2
}
222
223
224
u16 GetFunctionNumArgs (IM3Function i_function)
225
73.9k
{
226
73.9k
    u16 numArgs = 0;
227
228
73.9k
    if (i_function) {
229
73.9k
        if (i_function->funcType) {
230
73.9k
            numArgs = i_function->funcType->numArgs;
231
73.9k
        }
232
73.9k
    }
233
234
73.9k
    return numArgs;
235
73.9k
}
236
237
m3type_t GetFunctionArgType (IM3Function i_function, u32 i_index)
238
3.66k
{
239
3.66k
    m3type_t type = c_m3Type_none;
240
241
3.66k
    if (i_index < GetFunctionNumArgs(i_function)) {
242
3.66k
        u32 numReturns = i_function->funcType->numRets;
243
244
3.66k
        type = i_function->funcType->types[numReturns + i_index];
245
3.66k
    }
246
247
3.66k
    return type;
248
3.66k
}
249
250
251
u16 GetFunctionNumReturns (IM3Function i_function)
252
31.1k
{
253
31.1k
    u16 numReturns = 0;
254
255
31.1k
    if (i_function) {
256
31.1k
        if (i_function->funcType) {
257
31.1k
            numReturns = i_function->funcType->numRets;
258
31.1k
        }
259
31.1k
    }
260
261
31.1k
    return numReturns;
262
31.1k
}
263
264
265
u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index)
266
0
{
267
0
    return i_function ? GetFuncTypeResultType(i_function->funcType, i_index) : c_m3Type_unknown;
268
0
}
269
270
271
u32 GetFunctionNumArgsAndLocals (IM3Function i_function)
272
39.2k
{
273
39.2k
    if (i_function) {
274
39.2k
        return i_function->numLocals + GetFunctionNumArgs(i_function);
275
39.2k
    } else {
276
0
        return 0;
277
0
    }
278
39.2k
}