Coverage Report

Created: 2025-11-15 06:04

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