This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathmodule-foreign.cpp
More file actions
407 lines (329 loc) · 11.2 KB
/
module-foreign.cpp
File metadata and controls
407 lines (329 loc) · 11.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* -*-c++-*-
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
////////////////////////////////////////////////////////////////////////////////
#include <foundation.h>
#include <foundation-auto.h>
////////////////////////////////////////////////////////////////////////////////
MCTypeInfoRef kMCNativeCStringTypeInfo;
MCTypeInfoRef kMCWStringTypeInfo;
MCTypeInfoRef kMCUTF8StringTypeInfo;
extern "C" MC_DLLEXPORT_DEF MCTypeInfoRef MCNativeCStringTypeInfo(void) { return kMCNativeCStringTypeInfo; }
extern "C" MC_DLLEXPORT_DEF MCTypeInfoRef MCWStringTypeInfo(void) { return kMCWStringTypeInfo; }
extern "C" MC_DLLEXPORT_DEF MCTypeInfoRef MCUTF8StringTypeInfo(void) { return kMCUTF8StringTypeInfo; }
MCTypeInfoRef kMCForeignZStringNullErrorTypeInfo;
////////////////////////////////////////////////////////////////////////////////
/* Reject strings that contain nul characters; we can't convert
* them to nul-terminated wide character strings without data
* loss. */
static bool
MCForeignEvalStringNonNull (MCStringRef t_string)
{
/* Reject strings that contain nul characters; we can't convert
* them to nul-terminated wide character strings without data
* loss. */
uindex_t t_ignored;
if (MCStringFirstIndexOfChar (t_string, 0, 0,
kMCStringOptionCompareExact, t_ignored))
{
MCErrorCreateAndThrow (kMCForeignZStringNullErrorTypeInfo, nil);
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool __cbuffer_initialize(void *contents)
{
*(void **)contents = nil;
return true;
}
static void __cbuffer_finalize(void *contents)
{
free(*(void **)contents);
}
static bool __cbuffer_defined(void *contents)
{
return *(void **)contents != nil;
}
static bool __cbuffer_move(const MCForeignTypeDescriptor*, void *from, void *to)
{
*(void **)to = *(void **)from;
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool __cstring_copy(const MCForeignTypeDescriptor*, void *from, void *to)
{
if (*(void **)from == nil)
{
*(void **)to = nil;
return true;
}
const char *t_old_string = *(char **) from;
size_t t_length;
t_length = strlen(t_old_string) + 1;
char *t_new_string;
if (!MCMemoryNewArray(t_length, t_new_string))
return false;
MCMemoryCopy(t_new_string, t_old_string, t_length);
*(char **)to = t_new_string;
return true;
}
static bool __cstring_equal(const MCForeignTypeDescriptor*, void *left, void *right, bool& r_equal)
{
if (*(void **)left == nil || *(void **)right == nil)
r_equal = (left == right);
else
r_equal = strcmp(*(char **)left, *(char **)right) == 0;
return true;
}
static bool __cstring_hash(const MCForeignTypeDescriptor*, void *value, hash_t& r_hash)
{
if (*(void **)value == nil)
{
r_hash = 0;
return true;
}
r_hash = MCHashBytes(value, strlen(*(char **)value));
return true;
}
static bool __nativecstring_import(const MCForeignTypeDescriptor*, void *contents, bool release, MCValueRef& r_value)
{
if (release)
return MCStringCreateWithCStringAndRelease(*(char **)contents, (MCStringRef&)r_value);
return MCStringCreateWithCString(*(const char **)contents, (MCStringRef&)r_value);
}
static bool __nativecstring_export(const MCForeignTypeDescriptor*, MCValueRef value, bool release, void *contents)
{
if (!MCForeignEvalStringNonNull ((MCStringRef) value))
return false;
char *t_cstring_value;
if (!MCStringConvertToCString((MCStringRef)value, t_cstring_value))
return false;
if (release)
MCValueRelease(value);
*(char **)contents = t_cstring_value;
return true;
}
////////////////////////////////////////////////////////////////////////////////
/* Compute length excluding trailing nul */
static size_t
__wstring_len (const unichar_t *value)
{
uindex_t len;
for (len = 0; value[len] != 0; ++len);
return len;
}
static bool
__wstring_copy (const MCForeignTypeDescriptor*,
void *from,
void *to)
{
if (nil == *(void **) from)
{
*(void **)to = nil;
return true;
}
const unichar_t *t_old_string = *(unichar_t **) from;
/* Allocate enough space for string + trailing nul */
size_t t_length;
t_length = sizeof (unichar_t) * (1 + __wstring_len (t_old_string));
unichar_t *t_new_string;
if (!MCMemoryNewArray (t_length, t_new_string))
return false;
MCMemoryCopy (t_new_string, t_old_string, t_length);
*(unichar_t **)to = t_new_string;
return true;
}
static bool
__wstring_equal (const MCForeignTypeDescriptor*,
void *left,
void *right,
bool & r_equal)
{
if (nil == *(void **) left || nil == *(void **) right)
return left == right;
const unichar_t *t_left = *(unichar_t **) left;
const unichar_t *t_right = *(unichar_t **) right;
size_t i = 0;
while (true)
{
if (0 == t_left[i] && 0 == t_right[i])
break;
if (t_left[i] != t_right[i])
return false;
++i;
}
return true;
}
static bool
__wstring_hash (const MCForeignTypeDescriptor*,
void *value,
hash_t & r_hash)
{
if (nil == *(void **)value)
{
r_hash = 0;
return true;
}
const unichar_t *t_value = *(unichar_t **) value;
r_hash = MCHashBytes (t_value, __wstring_len (t_value) * sizeof(unichar_t));
return true;
}
static bool
__wstring_import (const MCForeignTypeDescriptor*,
void *contents,
bool release,
MCValueRef & r_value)
{
if (release)
return MCStringCreateWithWStringAndRelease (*(unichar_t **) contents,
(MCStringRef &) r_value);
else
return MCStringCreateWithWString(*(const unichar_t **) contents,
(MCStringRef &) r_value);
}
static bool
__wstring_export (const MCForeignTypeDescriptor*,
MCValueRef value,
bool release,
void *contents)
{
if (!MCForeignEvalStringNonNull ((MCStringRef) value))
return false;
unichar_t *t_wstring_value;
if (!MCStringConvertToWString ((MCStringRef) value, t_wstring_value))
return false;
if (release)
MCValueRelease (value);
*(unichar_t **)contents = t_wstring_value;
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool
__utf8string_import (const MCForeignTypeDescriptor*,
void *contents,
bool release,
MCValueRef & r_value)
{
char *t_utf8string;
t_utf8string = *(char **) contents;
if (release)
return MCStringCreateWithBytesAndRelease((byte_t*)t_utf8string, t_utf8string?strlen(t_utf8string):0, kMCStringEncodingUTF8, false, (MCStringRef&)r_value);
else
return MCStringCreateWithBytes((byte_t*)t_utf8string, t_utf8string?strlen(t_utf8string):0, kMCStringEncodingUTF8, false, (MCStringRef&)r_value);
}
static bool
__utf8string_export (const MCForeignTypeDescriptor*,
MCValueRef value,
bool release,
void *contents)
{
if (!MCForeignEvalStringNonNull ((MCStringRef) value))
return false;
char *t_utf8string_value;
if (!MCStringConvertToUTF8String((MCStringRef)value, t_utf8string_value))
return false;
if (release)
MCValueRelease (value);
*(char **)contents = t_utf8string_value;
return true;
}
////////////////////////////////////////////////////////////////////////////////
static bool __build_typeinfo(const char *p_name, MCForeignTypeDescriptor *p_desc, MCTypeInfoRef& r_typeinfo)
{
MCAutoStringRef t_name_string;
if (!MCStringCreateWithCString(p_name, &t_name_string))
return false;
MCNewAutoNameRef t_name_name;
if (!MCNameCreate(*t_name_string, &t_name_name))
return false;
if (!MCNamedForeignTypeInfoCreate(*t_name_name, p_desc, r_typeinfo))
return false;
return true;
}
////////////////////////////////////////////////////////////////////////////////
extern "C" bool com_livecode_foreign_Initialize(void)
{
MCForeignPrimitiveType p;
MCForeignTypeDescriptor d;
d . size = sizeof(char *);
d . basetype = kMCNullTypeInfo;
d . bridgetype = kMCStringTypeInfo;
p = kMCForeignPrimitiveTypePointer;
d . layout = &p;
d . layout_size = 1;
d . initialize = __cbuffer_initialize;
d . finalize = __cbuffer_finalize;
d . defined = __cbuffer_defined;
d . move = __cbuffer_move;
d . copy = __cstring_copy;
d . equal = __cstring_equal;
d . hash = __cstring_hash;
d . doimport = __nativecstring_import;
d . doexport = __nativecstring_export;
d . describe = nullptr;
d . promotedtype = kMCNullTypeInfo;
d . promote = nullptr;
if (!__build_typeinfo("com.livecode.foreign.NativeCString", &d, kMCNativeCStringTypeInfo))
return false;
d . size = sizeof(unichar_t *);
d . basetype = kMCNullTypeInfo;
d . bridgetype = kMCStringTypeInfo;
p = kMCForeignPrimitiveTypePointer;
d . layout = &p;
d . layout_size = 1;
d . initialize = __cbuffer_initialize;
d . finalize = __cbuffer_finalize;
d . defined = __cbuffer_defined;
d . move = __cbuffer_move;
d . copy = __wstring_copy;
d . equal = __wstring_equal;
d . hash = __wstring_hash;
d . doimport = __wstring_import;
d . doexport = __wstring_export;
d . describe = nullptr;
d . promotedtype = kMCNullTypeInfo;
d . promote = nullptr;
if (!__build_typeinfo("com.livecode.foreign.WString", &d, kMCWStringTypeInfo))
return false;
d . size = sizeof(char *);
d . basetype = kMCNullTypeInfo;
d . bridgetype = kMCStringTypeInfo;
p = kMCForeignPrimitiveTypePointer;
d . layout = &p;
d . layout_size = 1;
d . initialize = __cbuffer_initialize;
d . finalize = __cbuffer_finalize;
d . defined = __cbuffer_defined;
d . move = __cbuffer_move;
d . copy = __cstring_copy;
d . equal = __cstring_equal;
d . hash = __cstring_hash;
d . doimport = __utf8string_import;
d . doexport = __utf8string_export;
d . describe = nullptr;
d . promotedtype = kMCNullTypeInfo;
d . promote = nullptr;
if (!__build_typeinfo("com.livecode.foreign.UTF8String", &d, kMCUTF8StringTypeInfo))
return false;
/* ---------- */
if (!MCNamedErrorTypeInfoCreate (MCNAME("com.livecode.foreign.NullInZStringError"), MCNAME("foreign"), MCSTR("cannot export char U+0000 in nul-terminated string buffer"), kMCForeignZStringNullErrorTypeInfo))
return false;
return true;
}
extern "C" void com_livecode_foreign_Finalize(void)
{
MCValueRelease(kMCNativeCStringTypeInfo);
MCValueRelease(kMCWStringTypeInfo);
MCValueRelease(kMCForeignZStringNullErrorTypeInfo);
}
////////////////////////////////////////////////////////////////////////////////