-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
213 lines (167 loc) · 4.23 KB
/
Copy pathcommon.cpp
File metadata and controls
213 lines (167 loc) · 4.23 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#define export(T) extern "C" T EMSCRIPTEN_KEEPALIVE
#else
#define export(T) extern "C" __declspec(dllexport) T
#endif
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
struct TestStruct {
int I;
float F;
};
struct CallbackStruct {
unsigned short us;
void(*callback)(int i);
};
struct FlagStruct {
uint32_t flags;
};
struct AlignmentStruct {
unsigned short us;
uint32_t choices;
};
export(void) WriteInt (const int value, int * result) {
*result = value;
}
export(int) ReadInt (const int * source) {
return *source;
}
export(void) WriteStruct (const int i, const float f, TestStruct * result) {
result->I = i;
result->F = f;
}
union TestUnion {
int I;
float F;
};
export(void)WriteUnionInt(const int i, TestUnion * result) {
result->I = i;
}
export(void)WriteUnionFloat(const float f, TestUnion * result) {
result->F = f;
}
union TestComplexUnion {
int I;
TestStruct S;
};
export(void)WriteComplexUnionInt(const int i, TestComplexUnion *result) {
result->I = i;
}
export(void)WriteComplexUnionStruct(const int i, const float f, TestComplexUnion *result) {
result->S.I = i;
result->S.F = f;
}
export(TestStruct) ReturnStruct (const int i, const float f) {
TestStruct result;
result.I = i;
result.F = f;
return result;
}
export(TestStruct) ReturnStructArgument (const TestStruct arg) {
return arg;
}
export(const char *) ReturnString(const char *s) {
return s;
}
export(int) ReturnInt(int i) {
return i;
}
export(const unsigned char *) ReturnStaticString() {
return (const unsigned char *)"fuzzy pickles";
}
export(void) MutateStringArgument (char * buf, const int capacity) {
// #%(*#@%OJIJ#LW% i hate clang
// strcat_s(buf, capacity, " world");
strcat(buf, " world");
}
export(int) CopyStringArgument (char * dst, const int capacity, const char * src) {
int length = strlen(src);
memset(dst, 0, capacity);
strcpy(dst, src);
return length;
}
export(int) CopySecondStringFromArray(char * dst, const int capacity, const char ** src) {
int length = strlen(src[1]);
memset(dst, 0, capacity);
strcpy(dst, src[1]);
return length;
}
export(int) WriteStringIntoBuffer (unsigned char * dst, const int capacity) {
const char * str = "hello world";
memset(dst, 0, capacity);
strcpy((char *)dst, str);
return strlen(str);
}
export(void) FillStructBuffer(TestStruct *buffer, const int capacity) {
memset(buffer, 0, capacity*sizeof(TestStruct));
for (int i = 0; i < capacity; i++) {
buffer[i].I = 1;
buffer[i].F = 2.0;
}
}
export(int)Add(int a, int b) {
return a + b;
}
export(void *)Alloc(int size) {
return malloc(size);
}
export(void)Free(void * ptr) {
return free(ptr);
}
export(void *) ReturnNullPtr() {
return NULL;
}
export(float) AddFloat (float a, float b) {
return a + b;
}
typedef int (*TPWriteStringIntoBuffer) (unsigned char *, const int);
typedef TestStruct (*TPReturnStructArgument) (const TestStruct);
typedef int (*TPBinaryOperator) (int, int);
typedef const char * (*TPReturnString) (const char *);
typedef float(*TPAddFloat) (float, float);
export(TPWriteStringIntoBuffer) ReturnWriteStringIntoBuffer () {
return WriteStringIntoBuffer;
};
export(TPReturnStructArgument) ReturnReturnStructArgument () {
return ReturnStructArgument;
};
export(TPBinaryOperator) ReturnAdd () {
return Add;
};
export(TPReturnString) ReturnReturnString () {
return ReturnString;
};
export(TPAddFloat) ReturnAddFloat () {
return AddFloat;
}
export(int) CallBinaryOperator (TPBinaryOperator op, int a, int b) {
return op(a, b);
}
export(TestStruct) CallReturnStructArgument (TPReturnStructArgument rsa, TestStruct arg) {
return rsa(arg);
}
export(void) CallFunctionInStruct(CallbackStruct s, int i) {
return s.callback(i);
}
export(FlagStruct) PassFlagInStruct(FlagStruct s) {
FlagStruct s2;
s2.flags = s.flags;
return s2;
}
export(void) FillUshortArray(uint16_t *ramp) {
ramp[0] = 192;
}
export(uint16_t) FirstElementOfUshortArray(uint16_t *arr) {
return arr[0];
}
export(AlignmentStruct) TestAlignment(const AlignmentStruct arg) {
return arg;
}
export(int) ReadStringLength(const char **s) {
return strlen(*s);
}
export(int) ReturnSecondIntFromArray(int *ints) {
return ints[1];
}