forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
63 lines (57 loc) · 2.48 KB
/
function.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "balrt.h"
bool _bal_function_subtype_contains(UniformSubtypePtr stp, TaggedPtr p) {
if ((getTag(p) & UT_MASK) != TAG_FUNCTION) {
return false;
}
FunctionPtr fp = taggedToPtr(p);
FunctionDescPtr fdp = fp->desc;
EasyFunctionSubtypePtr fstp = (EasyFunctionSubtypePtr)stp;
int64_t maxRequiredParams = (fstp->nRequiredParams > fdp->nRequiredParams) ? fstp->nRequiredParams : fdp->nRequiredParams;
if (!memberTypeIsSubtypeSimple(fdp->returnType, fstp->returnBitSet) ||
(maxRequiredParams > (int64_t)fstp->nRequiredParams)) {
return false;
}
for (int64_t i = 0; i < maxRequiredParams; i++) {
MemberType paramType = (i < fdp->nRequiredParams) ? fdp->paramTypes[i] : fdp->restType;
uint32_t paramBitSet = fstp->paramBitSets[i];
if (!memberTypeIsSupertypeSimple(paramType, paramBitSet)) {
return false;
}
}
return true;
}
// nArgs = requiredArgCount + restArgCount
// We are using uint64_t to avoid overflow in case of restArgCount close to INT64_MAX. This means indexing uniform arg array
// must also be done using uint64_t
GC TaggedPtr *_bal_function_alloc_uniform_args(uint64_t nArgs) {
GC TaggedPtr *arr = _bal_alloc(sizeof(TaggedPtr) * nArgs);
return arr;
}
void _bal_function_add_to_uniform_args(TaggedPtr *uniformArgArray, const TaggedPtr restArgArray, int64_t startingOffset) {
ListPtr lp = taggedToPtr(restArgArray);
int64_t len = lp->tpArray.length;
for (int64_t i = 0; i < len; i++) {
uint64_t index = startingOffset + i;
uniformArgArray[index] = lp->desc->get(restArgArray, i);
}
}
void _bal_function_add_to_rest_args(TaggedPtr restArgArray, const TaggedPtr *uniformArgArray, int64_t startingOffset, int64_t restArgCount) {
ListPtr lp = taggedToPtr(restArgArray);
for (int64_t i = 0; i < restArgCount; i++) {
uint64_t index = startingOffset + i;
TaggedPtr val = uniformArgArray[index];
PanicCode err = lp->desc->set(restArgArray, lp->gArray.length, val);
if (err != 0) {
// This should never happen since we preallocate the array and the values are of the expected type
_bal_panic_internal(err);
}
}
}
ClosurePtr _bal_function_alloc_closure_val(uint32_t nValues) {
ClosurePtr closure = _bal_alloc(sizeof(Closure) + sizeof(TaggedPtr) * nValues);
return closure;
}
GC TaggedPtr *_bal_function_alloc_heap_capture() {
GC TaggedPtr *ptr = _bal_alloc(sizeof(TaggedPtr));
return ptr;
}