Coverage Report

Created: 2026-09-01 06:51

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
33.2k
{
14
33.2k
    *o_functionType = (IM3FuncType) m3_Malloc ("M3FuncType", sizeof (M3FuncType) + i_numTypes * sizeof (m3type_t));
15
33.2k
    return (*o_functionType) ? m3Err_none : m3Err_mallocFailed;
16
33.2k
}
17
18
19
#if d_m3HasTypedRefs
20
21
// The type a (ref $t) / (ref null $t) naming this function type is spelled as
22
m3type_t  RefTypeOfFuncType  (const IM3FuncType i_funcType, bool i_nonNull)
23
{
24
    m3type_t type = d_m3Type_ref | (i_funcType ? i_funcType->canonicalIndex : d_m3Type_heapAbstract);
25
26
    if (i_nonNull)
27
        type |= d_m3Type_refNonNull;
28
29
    return type;
30
}
31
32
#endif // d_m3HasTypedRefs
33
34
35
bool  AreFuncTypesEqual  (const IM3FuncType i_typeA, const IM3FuncType i_typeB)
36
133k
{
37
133k
    if (i_typeA->numRets == i_typeB->numRets && i_typeA->numArgs == i_typeB->numArgs)
38
80.1k
    {
39
80.1k
        return (memcmp (i_typeA->types, i_typeB->types,
40
80.1k
                        (i_typeA->numRets + i_typeA->numArgs) * sizeof (m3type_t)) == 0);
41
80.1k
    }
42
43
53.5k
    return false;
44
133k
}
45
46
u16  GetFuncTypeNumParams  (const IM3FuncType i_funcType)
47
5.19k
{
48
5.19k
    return i_funcType ? i_funcType->numArgs : 0;
49
5.19k
}
50
51
52
m3type_t  GetFuncTypeParamType  (const IM3FuncType i_funcType, u16 i_index)
53
7.89k
{
54
7.89k
    m3type_t type = c_m3Type_unknown;
55
56
7.89k
    if (i_funcType)
57
7.89k
    {
58
7.89k
        if (i_index < i_funcType->numArgs)
59
7.89k
        {
60
7.89k
            type = i_funcType->types [i_funcType->numRets + i_index];
61
7.89k
        }
62
7.89k
    }
63
64
7.89k
    return type;
65
7.89k
}
66
67
68
69
u16  GetFuncTypeNumResults  (const IM3FuncType i_funcType)
70
13.2k
{
71
13.2k
    return i_funcType ? i_funcType->numRets : 0;
72
13.2k
}
73
74
75
m3type_t  GetFuncTypeResultType  (const IM3FuncType i_funcType, u16 i_index)
76
34.9k
{
77
34.9k
    m3type_t type = c_m3Type_unknown;
78
79
34.9k
    if (i_funcType)
80
34.9k
    {
81
34.9k
        if (i_index < i_funcType->numRets)
82
34.9k
        {
83
34.9k
            type = i_funcType->types [i_index];
84
34.9k
        }
85
34.9k
    }
86
87
34.9k
    return type;
88
34.9k
}
89
90
91
//---------------------------------------------------------------------------------------------------------------
92
93
94
void FreeImportInfo (M3ImportInfo * i_info)
95
19.3k
{
96
19.3k
    m3_Free (i_info->moduleUtf8);
97
19.3k
    m3_Free (i_info->fieldUtf8);
98
19.3k
}
99
100
101
void  Function_Release  (IM3Function i_function)
102
7.07k
{
103
7.07k
    m3_Free (i_function->constants);
104
105
13.8k
    for (int i = 0; i < i_function->numNames; i++)
106
6.73k
    {
107
        // name can be an alias of fieldUtf8
108
6.73k
        if (i_function->names[i] != i_function->import.fieldUtf8)
109
3.30k
        {
110
3.30k
            m3_Free (i_function->names[i]);
111
3.30k
        }
112
6.73k
    }
113
114
7.07k
    FreeImportInfo (& i_function->import);
115
116
7.07k
    if (i_function->ownsWasmCode)
117
0
        m3_Free (i_function->wasm);
118
119
    // Function_FreeCompiledCode (func);
120
121
#   if (d_m3EnableCodePageRefCounting)
122
    {
123
        m3_Free (i_function->codePageRefs);
124
        i_function->numCodePageRefs = 0;
125
    }
126
#   endif
127
7.07k
}
128
129
130
void  Function_FreeCompiledCode (IM3Function i_function)
131
0
{
132
#   if (d_m3EnableCodePageRefCounting)
133
    {
134
        i_function->compiled = NULL;
135
136
        while (i_function->numCodePageRefs--)
137
        {
138
            IM3CodePage page = i_function->codePageRefs [i_function->numCodePageRefs];
139
140
            if (--(page->info.usageCount) == 0)
141
            {
142
//                printf ("free %p\n", page);
143
            }
144
        }
145
146
        m3_Free (i_function->codePageRefs);
147
148
        Runtime_ReleaseCodePages (i_function->module->runtime);
149
    }
150
#   endif
151
0
}
152
153
154
cstr_t  m3_GetFunctionName  (IM3Function i_function)
155
1
{
156
1
    u16 numNames = 0;
157
1
    cstr_t *names = GetFunctionNames(i_function, &numNames);
158
1
    if (numNames > 0)
159
1
        return names[0];
160
0
    else
161
0
        return "<unnamed>";
162
1
}
163
164
165
IM3Module  m3_GetFunctionModule  (IM3Function i_function)
166
0
{
167
0
    return i_function ? i_function->module : NULL;
168
0
}
169
170
171
cstr_t *  GetFunctionNames  (IM3Function i_function, u16 * o_numNames)
172
1
{
173
1
    if (!i_function || !o_numNames)
174
0
        return NULL;
175
176
1
    if (i_function->import.fieldUtf8)
177
0
    {
178
0
        *o_numNames = 1;
179
0
        return &i_function->import.fieldUtf8;
180
0
    }
181
1
    else
182
1
    {
183
1
        *o_numNames = i_function->numNames;
184
1
        return i_function->names;
185
1
    }
186
1
}
187
188
189
cstr_t  GetFunctionImportModuleName  (IM3Function i_function)
190
1
{
191
1
    return (i_function->import.moduleUtf8) ? i_function->import.moduleUtf8 : "";
192
1
}
193
194
195
u16  GetFunctionNumArgs  (IM3Function i_function)
196
6.00k
{
197
6.00k
    u16 numArgs = 0;
198
199
6.00k
    if (i_function)
200
6.00k
    {
201
6.00k
        if (i_function->funcType)
202
6.00k
            numArgs = i_function->funcType->numArgs;
203
6.00k
    }
204
205
6.00k
    return numArgs;
206
6.00k
}
207
208
m3type_t  GetFunctionArgType  (IM3Function i_function, u32 i_index)
209
1.75k
{
210
1.75k
    m3type_t type = c_m3Type_none;
211
212
1.75k
    if (i_index < GetFunctionNumArgs (i_function))
213
1.75k
    {
214
1.75k
        u32 numReturns = i_function->funcType->numRets;
215
216
1.75k
        type = i_function->funcType->types [numReturns + i_index];
217
1.75k
    }
218
219
1.75k
    return type;
220
1.75k
}
221
222
223
u16  GetFunctionNumReturns  (IM3Function i_function)
224
1.75k
{
225
1.75k
    u16 numReturns = 0;
226
227
1.75k
    if (i_function)
228
1.75k
    {
229
1.75k
        if (i_function->funcType)
230
1.75k
            numReturns = i_function->funcType->numRets;
231
1.75k
    }
232
233
1.75k
    return numReturns;
234
1.75k
}
235
236
237
u8  GetFunctionReturnType  (const IM3Function i_function, u16 i_index)
238
0
{
239
0
    return i_function ? GetFuncTypeResultType (i_function->funcType, i_index) : c_m3Type_unknown;
240
0
}
241
242
243
u32  GetFunctionNumArgsAndLocals (IM3Function i_function)
244
2.50k
{
245
2.50k
    if (i_function)
246
2.50k
        return i_function->numLocals + GetFunctionNumArgs (i_function);
247
0
    else
248
0
        return 0;
249
2.50k
}
250