-
Notifications
You must be signed in to change notification settings - Fork 71
/
typesPrimitives.c
197 lines (157 loc) · 4.07 KB
/
typesPrimitives.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "pThreadedFFI.h"
#include "pharovm/macros.h"
int getTypeByteSize(void* aType);
void fillBasicType(sqInt aOop);
/**
* This primitive takes the receiver that should be a TFBasicType
* Then it initialize the object with a pointer to the corresponding libFFI type.
*/
Primitive(primitiveFillBasicType){
fillBasicType(getReceiver());
}
/**
* This primitive assumes the receiver is a TFBasicType.
* It extract the size of the corresponding libFFI type.
*/
PrimitiveWithDepth(primitiveTypeByteSize, 1){
void* handler;
sqInt receiver;
sqInt size;
receiver = getReceiver();
handler = getHandler(receiver);
checkFailed();
size = getTypeByteSize(handler);
checkFailed();
methodReturnInteger(size);
}
/**
* This primitive frees the struct type ffi_type memory and the array used to hold the members.
*/
PrimitiveWithDepth(primitiveFreeStruct, 1){
sqInt receiver;
ffi_type* structType;
receiver = getReceiver();
checkFailed();
structType = (ffi_type *)getHandler(receiver);
checkFailed();
if(!structType){
primitiveFail();
return;
}
free(structType->elements);
free(structType);
setHandler(receiver, NULL);
//primitiveEnd(); //No need to call it
}
/**
* This primitive returns the size of the struct type.
*/
PrimitiveWithDepth(primitiveStructByteSize, 1){
sqInt receiver;
ffi_type* structType;
receiver = stackValue(0);
checkFailed();
structType = (ffi_type*)getHandler(receiver);
checkFailed();
if(!structType){
primitiveFail();
return;
}
primitiveEndReturnInteger(structType->size);
}
/**
* This primitive initialize a struct type.
* It assumes that the struct type is an external object.
* It should have as slots:
*
* 1) An External Address (it should be a external object)
* 2) An array with the members of the struct. Each member should be a valid type.
* 3) An array with the same size of members that will receive the offsets of the members
*
* This primitive will allocate the required structs and fails if it cannot be allocated
* or the parameters are no good.
*
* Also it calculates the offsets of the struct.
*
*/
PrimitiveWithDepth(primitiveInitializeStructType, 2){
sqInt receiver;
sqInt arrayOfMembers;
sqInt arrayOfOffsets;
sqInt membersSize;
ffi_type* structType;
ffi_type** memberTypes;
size_t* offsets;
receiver = getReceiver();
checkFailed();
// Verify we have a valid handle
getHandler(receiver);
checkFailed();
arrayOfMembers = getAttributeOf(receiver, 1);
checkFailed();
arrayOfOffsets = getAttributeOf(receiver, 2);
checkFailed();
//Validating that they are arrays.
checkIsArray(arrayOfMembers);
checkIsArray(arrayOfOffsets);
membersSize = stSizeOf(arrayOfMembers);
if(membersSize < 1){
primitiveFail();
return;
}
if(membersSize != stSizeOf(arrayOfOffsets)){
primitiveFail();
return;
}
//Validating that the members are of valid size
for(int i=0; i < membersSize; i++){
checkIsPointerSize(stObjectat(arrayOfMembers, i + 1), 1);
}
// Allocating the structure type
structType = malloc(sizeof(ffi_type));
if(!structType){
primitiveFail();
return;
}
memberTypes = malloc(sizeof(ffi_type*) * (membersSize+1));
if(!memberTypes){
free(structType);
primitiveFail();
return;
}
offsets = malloc(sizeof(size_t) * (membersSize));
if(!offsets){
free(memberTypes);
free(structType);
primitiveFail();
return;
}
//The members list is ended by a NULL
memberTypes[membersSize] = NULL;
structType->alignment = 0;
structType->size = 0;
structType->type = FFI_TYPE_STRUCT;
structType->elements = memberTypes;
for(int i=0; i < membersSize; i++){
memberTypes[i] = getHandler(stObjectat(arrayOfMembers, i + 1));
}
setHandler(receiver, structType);
if(failed()){
free(memberTypes);
free(structType);
free(offsets);
return;
}
if(ffi_get_struct_offsets(FFI_DEFAULT_ABI, structType, offsets)!=FFI_OK){
free(memberTypes);
free(structType);
free(offsets);
primitiveFail();
return;
}
for(int i=0; i < membersSize; i++){
stObjectatput(arrayOfOffsets, i+1, integerObjectOf(offsets[i]));
}
free(offsets);
//primitiveEnd(); // No need to call it
}