Coverage Report

Created: 2025-08-29 06:57

/src/wasm3/source/m3_function.c
Line
Count
Source (jump to first uncovered line)
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.05k
{
14
1.05k
    *o_functionType = (IM3FuncType) m3_Malloc ("M3FuncType", sizeof (M3FuncType) + i_numTypes);
15
1.05k
    return (*o_functionType) ? m3Err_none : m3Err_mallocFailed;
16
1.05k
}
17
18
19
bool  AreFuncTypesEqual  (const IM3FuncType i_typeA, const IM3FuncType i_typeB)
20
2.35k
{
21
2.35k
    if (i_typeA->numRets == i_typeB->numRets && i_typeA->numArgs == i_typeB->numArgs)
22
1.19k
    {
23
1.19k
        return (memcmp (i_typeA->types, i_typeB->types, i_typeA->numRets + i_typeA->numArgs) == 0);
24
1.19k
    }
25
26
1.15k
    return false;
27
2.35k
}
28
29
u16  GetFuncTypeNumParams  (const IM3FuncType i_funcType)
30
1.63k
{
31
1.63k
    return i_funcType ? i_funcType->numArgs : 0;
32
1.63k
}
33
34
35
u8  GetFuncTypeParamType  (const IM3FuncType i_funcType, u16 i_index)
36
30.8k
{
37
30.8k
    u8 type = c_m3Type_unknown;
38
39
30.8k
    if (i_funcType)
40
30.8k
    {
41
30.8k
        if (i_index < i_funcType->numArgs)
42
30.8k
        {
43
30.8k
            type = i_funcType->types [i_funcType->numRets + i_index];
44
30.8k
        }
45
30.8k
    }
46
47
30.8k
    return type;
48
30.8k
}
49
50
51
52
u16  GetFuncTypeNumResults  (const IM3FuncType i_funcType)
53
2.13k
{
54
2.13k
    return i_funcType ? i_funcType->numRets : 0;
55
2.13k
}
56
57
58
u8  GetFuncTypeResultType  (const IM3FuncType i_funcType, u16 i_index)
59
40.5k
{
60
40.5k
    u8 type = c_m3Type_unknown;
61
62
40.5k
    if (i_funcType)
63
40.5k
    {
64
40.5k
        if (i_index < i_funcType->numRets)
65
40.5k
        {
66
40.5k
            type = i_funcType->types [i_index];
67
40.5k
        }
68
40.5k
    }
69
70
40.5k
    return type;
71
40.5k
}
72
73
74
//---------------------------------------------------------------------------------------------------------------
75
76
77
void FreeImportInfo (M3ImportInfo * i_info)
78
5.43k
{
79
5.43k
    m3_Free (i_info->moduleUtf8);
80
5.43k
    m3_Free (i_info->fieldUtf8);
81
5.43k
}
82
83
84
void  Function_Release  (IM3Function i_function)
85
2.25k
{
86
2.25k
    m3_Free (i_function->constants);
87
88
3.32k
    for (int i = 0; i < i_function->numNames; i++)
89
1.07k
    {
90
        // name can be an alias of fieldUtf8
91
1.07k
        if (i_function->names[i] != i_function->import.fieldUtf8)
92
194
        {
93
194
            m3_Free (i_function->names[i]);
94
194
        }
95
1.07k
    }
96
97
2.25k
    FreeImportInfo (& i_function->import);
98
99
2.25k
    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.25k
}
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
1
{
139
1
    u16 numNames = 0;
140
1
    cstr_t *names = GetFunctionNames(i_function, &numNames);
141
1
    if (numNames > 0)
142
0
        return names[0];
143
1
    else
144
1
        return "<unnamed>";
145
1
}
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
1
{
156
1
    if (!i_function || !o_numNames)
157
0
        return NULL;
158
159
1
    if (i_function->import.fieldUtf8)
160
0
    {
161
0
        *o_numNames = 1;
162
0
        return &i_function->import.fieldUtf8;
163
0
    }
164
1
    else
165
1
    {
166
1
        *o_numNames = i_function->numNames;
167
1
        return i_function->names;
168
1
    }
169
1
}
170
171
172
cstr_t  GetFunctionImportModuleName  (IM3Function i_function)
173
1
{
174
1
    return (i_function->import.moduleUtf8) ? i_function->import.moduleUtf8 : "";
175
1
}
176
177
178
u16  GetFunctionNumArgs  (IM3Function i_function)
179
1.32k
{
180
1.32k
    u16 numArgs = 0;
181
182
1.32k
    if (i_function)
183
1.32k
    {
184
1.32k
        if (i_function->funcType)
185
1.32k
            numArgs = i_function->funcType->numArgs;
186
1.32k
    }
187
188
1.32k
    return numArgs;
189
1.32k
}
190
191
u8  GetFunctionArgType  (IM3Function i_function, u32 i_index)
192
704
{
193
704
    u8 type = c_m3Type_none;
194
195
704
    if (i_index < GetFunctionNumArgs (i_function))
196
704
    {
197
704
        u32 numReturns = i_function->funcType->numRets;
198
199
704
        type = i_function->funcType->types [numReturns + i_index];
200
704
    }
201
202
704
    return type;
203
704
}
204
205
206
u16  GetFunctionNumReturns  (IM3Function i_function)
207
53
{
208
53
    u16 numReturns = 0;
209
210
53
    if (i_function)
211
53
    {
212
53
        if (i_function->funcType)
213
53
            numReturns = i_function->funcType->numRets;
214
53
    }
215
216
53
    return numReturns;
217
53
}
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
570
{
228
570
    if (i_function)
229
570
        return i_function->numLocals + GetFunctionNumArgs (i_function);
230
0
    else
231
0
        return 0;
232
570
}
233