Skip to content

Commit bd2a751

Browse files
authored
Merge pull request livecode#5290 from runrevmark/bugfix-19335
[[ Bug 19335 ]] Add sized integer types to LCB
2 parents 792ca31 + a8c896b commit bd2a751

File tree

17 files changed

+1343
-590
lines changed

17 files changed

+1343
-590
lines changed

docs/guides/LiveCode Builder Language Reference.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -430,31 +430,50 @@ statement blocks.
430430
ForeignHandlerDefinition
431431
: 'foreign' 'handler' <Name: Identifier> '(' [ ParameterList ] ')' [ 'returns' <ReturnType: Type> ) ] 'binds' 'to' <Binding: String>
432432

433-
ForeignType
434-
: Type
435-
| 'CBool'
436-
| 'CInt'
437-
| 'CUInt'
438-
| 'CFloat'
439-
| 'CDouble'
440-
441433
A foreign handler definition binds an identifier to a handler defined in
442434
foreign code.
443435

444-
Foreign handler definitions can contain more types in their parameters
445-
than elsewhere - those specified in the ForeignType clause. These allow
446-
low-level types to be specified making it easier to interoperate.
436+
There are a number of types defined in the foreign module which map to
437+
the appropriate foreign type when used in foreign handler signatures.
438+
439+
There are the standard machine types:
440+
441+
- Bool maps to an 8-bit boolean
442+
- Int8/SInt8 and UInt8 map to 8-bit integers
443+
- Int16/SInt16 and UInt16 map to 16-bit integers
444+
- Int32/SInt32 and UInt32 map to 32-bit integers
445+
- Int64/SInt64 and UInt64 map to 64-bit integers
446+
- IntSize/SIntSize and UIntSize map to the integer size needed to hold a memory size
447+
- IntPtr/SIntPtr and UIntPtr map to the integer size needed to hold a pointer
448+
449+
There are the standard C primitive types:
447450

448-
Foreign types map to high-level types as follows:
451+
- CBool maps to 'bool'
452+
- CChar, CSChar and CUChar map to 'char', 'signed char' and 'unsigned char'
453+
- CShort/CSShort and CUShort map to 'signed short' and 'unsigned short'
454+
- CInt/CSInt and CUInt map to 'signed int' and 'unsigned int'
455+
- CLong/CSLong and CULong map to 'signed long' and 'unsigned long'
456+
- CLongLong/CSLongLong and CULongLong map to 'signed long long' and 'unsigned long long'
457+
- CFloat maps to 'float'
458+
- CDouble maps to 'double'
449459

450-
- bool maps to boolean
451-
- int and uint map to integer (number)
452-
- float and double map to real (number)
460+
There are aliases for the Java primitive types:
453461

454-
This mapping means that a foreign handler with a bool parameter say,
455-
will accept a boolean from LiveCode Builder code when called.
462+
- JBoolean maps to Bool
463+
- JByte maps to Int8
464+
- JShort maps to Int16
465+
- JInt maps to Int32
466+
- JLong maps to Int64
467+
- JFloat maps to Float32
468+
- JDouble maps to Float64
456469

457-
At present, only C and Java binding is allowed and follow these rules:
470+
All the primitive types above will implicitly bridge between corresponding
471+
high level types:
472+
473+
- CBool and Bool bridge to and from Boolean
474+
- All integer and real types bridge to and from Number
475+
476+
Other LCB types pass as follows into foreign handlers:
458477

459478
- any type passes an MCValueRef
460479
- nothing type passes as the null pointer
@@ -466,12 +485,11 @@ At present, only C and Java binding is allowed and follow these rules:
466485
- Data type passes an MCDataRef
467486
- Array type passes an MCArrayRef
468487
- List type passes an MCProperListRef
469-
- Pointer type passes a void *
470-
- CBool type passes a bool (i.e. an int - pre-C99).
471-
- CInt type passes an int
472-
- CUInt type passes an unsigned int
473-
- CFloat type passes a float
474-
- CDouble type passes a double
488+
489+
Finally, the Pointer type passes as void * to foreign handlers. If you want
490+
a pointer which can be null, then use optional Pointer - LCB will throw an
491+
error if there is an attempt to map from the null pointer value to a slot
492+
with a non-optional Pointer type.
475493

476494
Modes map as follows:
477495

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
version: 9.0.0-dp-7
3+
---
4+
# LiveCode Builder Standard Library
5+
## Foreign function interface
6+
7+
* The machine types Bool, SIntSize, UIntSize, SIntPtr, UIntPtr, SInt8, UInt8,
8+
SInt16, UInt16, SInt32, UInt32, SInt64 and UInt64 have been added. These all
9+
map to their corresponding foreign counterparts.
10+
11+
* The C types CBool, CChar, CSChar, CUChar, CSShort, CUShort, CSInt,
12+
CUInt, CSLong, CULong, CSLongLong and CULongLong have been added.
13+
These all map to their corresponding C counterparts.
14+
15+
* The Java integer primtive types JBoolean, JByte, JShort, JInt, JLong,
16+
JFloat and JDouble have been added. These map to int8_t,
17+
int16_t, int32_t, int64_t, float and double.

libfoundation/include/foundation.h

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,11 +1286,16 @@ extern "C" {
12861286
// the same from version to version. In particular, never serialize a hash
12871287
// value - recompute on unserialization of the object.
12881288

1289+
// Return a hash for the given bool.
1290+
MC_DLLEXPORT hash_t MCHashBool(bool);
1291+
12891292
// Return a hash for the given integer.
12901293
MC_DLLEXPORT hash_t MCHashInteger(integer_t);
12911294
MC_DLLEXPORT hash_t MCHashUInteger(uinteger_t);
12921295
MC_DLLEXPORT hash_t MCHashSize(ssize_t);
12931296
MC_DLLEXPORT hash_t MCHashUSize(size_t);
1297+
MC_DLLEXPORT hash_t MCHashInt64(int64_t);
1298+
MC_DLLEXPORT hash_t MCHashUInt64(uint64_t);
12941299

12951300
// Return a hash value for the given double - note that (hopefully!) hashing
12961301
// an integer stored as a double will be the same as hashing the integer.
@@ -1546,24 +1551,73 @@ MC_DLLEXPORT MCTypeInfoRef MCListTypeInfo(void) ATTRIBUTE_PURE;
15461551
MC_DLLEXPORT MCTypeInfoRef MCProperListTypeInfo(void) ATTRIBUTE_PURE;
15471552

15481553
MC_DLLEXPORT extern MCTypeInfoRef kMCBoolTypeInfo;
1549-
MC_DLLEXPORT extern MCTypeInfoRef kMCIntTypeInfo;
1550-
MC_DLLEXPORT extern MCTypeInfoRef kMCUIntTypeInfo;
1554+
MC_DLLEXPORT MCTypeInfoRef MCForeignBoolTypeInfo(void) ATTRIBUTE_PURE;
1555+
1556+
MC_DLLEXPORT extern MCTypeInfoRef kMCUInt8TypeInfo;
1557+
MC_DLLEXPORT extern MCTypeInfoRef kMCSInt8TypeInfo;
1558+
MC_DLLEXPORT extern MCTypeInfoRef kMCUInt16TypeInfo;
1559+
MC_DLLEXPORT extern MCTypeInfoRef kMCSInt16TypeInfo;
1560+
MC_DLLEXPORT extern MCTypeInfoRef kMCUInt32TypeInfo;
1561+
MC_DLLEXPORT extern MCTypeInfoRef kMCSInt32TypeInfo;
1562+
MC_DLLEXPORT extern MCTypeInfoRef kMCUInt64TypeInfo;
1563+
MC_DLLEXPORT extern MCTypeInfoRef kMCSInt64TypeInfo;
1564+
MC_DLLEXPORT MCTypeInfoRef MCForeignUInt8TypeInfo(void) ATTRIBUTE_PURE;
1565+
MC_DLLEXPORT MCTypeInfoRef MCForeignSInt8TypeInfo(void) ATTRIBUTE_PURE;
1566+
MC_DLLEXPORT MCTypeInfoRef MCForeignUInt16TypeInfo(void) ATTRIBUTE_PURE;
1567+
MC_DLLEXPORT MCTypeInfoRef MCForeignSInt16TypeInfo(void) ATTRIBUTE_PURE;
1568+
MC_DLLEXPORT MCTypeInfoRef MCForeignUInt32TypeInfo(void) ATTRIBUTE_PURE;
1569+
MC_DLLEXPORT MCTypeInfoRef MCForeignSInt32TypeInfo(void) ATTRIBUTE_PURE;
1570+
MC_DLLEXPORT MCTypeInfoRef MCForeignUInt64TypeInfo(void) ATTRIBUTE_PURE;
1571+
MC_DLLEXPORT MCTypeInfoRef MCForeignSInt64TypeInfo(void) ATTRIBUTE_PURE;
1572+
15511573
MC_DLLEXPORT extern MCTypeInfoRef kMCFloatTypeInfo;
15521574
MC_DLLEXPORT extern MCTypeInfoRef kMCDoubleTypeInfo;
1553-
MC_DLLEXPORT extern MCTypeInfoRef kMCPointerTypeInfo;
1554-
1555-
MC_DLLEXPORT MCTypeInfoRef MCForeignBoolTypeInfo(void) ATTRIBUTE_PURE;
1556-
MC_DLLEXPORT MCTypeInfoRef MCForeignUIntTypeInfo(void) ATTRIBUTE_PURE;
1557-
MC_DLLEXPORT MCTypeInfoRef MCForeignIntTypeInfo(void) ATTRIBUTE_PURE;
15581575
MC_DLLEXPORT MCTypeInfoRef MCForeignFloatTypeInfo(void) ATTRIBUTE_PURE;
15591576
MC_DLLEXPORT MCTypeInfoRef MCForeignDoubleTypeInfo(void) ATTRIBUTE_PURE;
1577+
1578+
MC_DLLEXPORT extern MCTypeInfoRef kMCPointerTypeInfo;
15601579
MC_DLLEXPORT MCTypeInfoRef MCForeignPointerTypeInfo(void) ATTRIBUTE_PURE;
15611580

1562-
MC_DLLEXPORT extern MCTypeInfoRef kMCSizeTypeInfo;
1563-
MC_DLLEXPORT extern MCTypeInfoRef kMCSSizeTypeInfo;
1581+
MC_DLLEXPORT extern MCTypeInfoRef kMCUIntSizeTypeInfo;
1582+
MC_DLLEXPORT extern MCTypeInfoRef kMCSIntSizeTypeInfo;
1583+
MC_DLLEXPORT MCTypeInfoRef MCForeignUIntSizeTypeInfo(void) ATTRIBUTE_PURE;
1584+
MC_DLLEXPORT MCTypeInfoRef MCForeignSIntSizeTypeInfo(void) ATTRIBUTE_PURE;
1585+
1586+
MC_DLLEXPORT extern MCTypeInfoRef kMCUIntPtrTypeInfo;
1587+
MC_DLLEXPORT extern MCTypeInfoRef kMCSIntPtrTypeInfo;
1588+
MC_DLLEXPORT MCTypeInfoRef MCForeignUIntPtrTypeInfo(void) ATTRIBUTE_PURE;
1589+
MC_DLLEXPORT MCTypeInfoRef MCForeignSIntPtrTypeInfo(void) ATTRIBUTE_PURE;
1590+
1591+
MC_DLLEXPORT extern MCTypeInfoRef kMCCBoolTypeInfo;
1592+
MC_DLLEXPORT MCTypeInfoRef MCForeignCBoolTypeInfo(void) ATTRIBUTE_PURE;
1593+
1594+
MC_DLLEXPORT extern MCTypeInfoRef kMCCCharTypeInfo;
1595+
MC_DLLEXPORT extern MCTypeInfoRef kMCCUCharTypeInfo;
1596+
MC_DLLEXPORT extern MCTypeInfoRef kMCCSCharTypeInfo;
1597+
MC_DLLEXPORT extern MCTypeInfoRef kMCCUShortTypeInfo;
1598+
MC_DLLEXPORT extern MCTypeInfoRef kMCCSShortTypeInfo;
1599+
MC_DLLEXPORT extern MCTypeInfoRef kMCCUIntTypeInfo;
1600+
MC_DLLEXPORT extern MCTypeInfoRef kMCCSIntTypeInfo;
1601+
MC_DLLEXPORT extern MCTypeInfoRef kMCCULongTypeInfo;
1602+
MC_DLLEXPORT extern MCTypeInfoRef kMCCSLongTypeInfo;
1603+
MC_DLLEXPORT extern MCTypeInfoRef kMCCULongLongTypeInfo;
1604+
MC_DLLEXPORT extern MCTypeInfoRef kMCCSLongLongTypeInfo;
1605+
MC_DLLEXPORT MCTypeInfoRef MCForeignCCharTypeInfo(void) ATTRIBUTE_PURE;
1606+
MC_DLLEXPORT MCTypeInfoRef MCForeignCUCharTypeInfo(void) ATTRIBUTE_PURE;
1607+
MC_DLLEXPORT MCTypeInfoRef MCForeignCSCharTypeInfo(void) ATTRIBUTE_PURE;
1608+
MC_DLLEXPORT MCTypeInfoRef MCForeignCUShortTypeInfo(void) ATTRIBUTE_PURE;
1609+
MC_DLLEXPORT MCTypeInfoRef MCForeignCSShortTypeInfo(void) ATTRIBUTE_PURE;
1610+
MC_DLLEXPORT MCTypeInfoRef MCForeignCUIntTypeInfo(void) ATTRIBUTE_PURE;
1611+
MC_DLLEXPORT MCTypeInfoRef MCForeignCSIntTypeInfo(void) ATTRIBUTE_PURE;
1612+
MC_DLLEXPORT MCTypeInfoRef MCForeignCULongTypeInfo(void) ATTRIBUTE_PURE;
1613+
MC_DLLEXPORT MCTypeInfoRef MCForeignCSLongTypeInfo(void) ATTRIBUTE_PURE;
1614+
MC_DLLEXPORT MCTypeInfoRef MCForeignCULongLongTypeInfo(void) ATTRIBUTE_PURE;
1615+
MC_DLLEXPORT MCTypeInfoRef MCForeignCSLongLongTypeInfo(void) ATTRIBUTE_PURE;
15641616

1565-
MC_DLLEXPORT MCTypeInfoRef MCForeignSizeTypeInfo(void) ATTRIBUTE_PURE;
1566-
MC_DLLEXPORT MCTypeInfoRef MCForeignSSizeTypeInfo(void) ATTRIBUTE_PURE;
1617+
MC_DLLEXPORT extern MCTypeInfoRef kMCUIntTypeInfo;
1618+
MC_DLLEXPORT extern MCTypeInfoRef kMCSIntTypeInfo;
1619+
MC_DLLEXPORT MCTypeInfoRef MCForeignUIntTypeInfo(void) ATTRIBUTE_PURE;
1620+
MC_DLLEXPORT MCTypeInfoRef MCForeignSIntTypeInfo(void) ATTRIBUTE_PURE;
15671621

15681622
//////////
15691623

@@ -2966,6 +3020,9 @@ MC_DLLEXPORT bool MCErrorThrowGenericWithMessage(MCStringRef message, ...);
29663020
// FOREIGN DEFINITIONS
29673021
//
29683022

3023+
MC_DLLEXPORT extern MCTypeInfoRef kMCForeignImportErrorTypeInfo;
3024+
MC_DLLEXPORT extern MCTypeInfoRef kMCForeignExportErrorTypeInfo;
3025+
29693026
MC_DLLEXPORT bool MCForeignValueCreate(MCTypeInfoRef typeinfo, void *contents, MCForeignValueRef& r_value);
29703027
MC_DLLEXPORT bool MCForeignValueCreateAndRelease(MCTypeInfoRef typeinfo, void *contents, MCForeignValueRef& r_value);
29713028

libfoundation/libfoundation.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'module_test_sources':
1010
[
1111
'test/environment.cpp',
12+
'test/test_foreign.cpp',
1213
'test/test_hash.cpp',
1314
'test/test_proper-list.cpp',
1415
'test/test_string.cpp',

libfoundation/src/foundation-core.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ void MCMemoryDeleteArray(void *p_array)
215215

216216
////////////////////////////////////////////////////////////////////////////////
217217

218+
MC_DLLEXPORT_DEF
219+
hash_t MCHashBool(bool b)
220+
{
221+
return hash_t(b);
222+
}
223+
218224
MC_DLLEXPORT_DEF
219225
hash_t MCHashInteger(integer_t i)
220226
{
@@ -235,12 +241,25 @@ MCHashSize (ssize_t i)
235241
return MCHashInt(i);
236242
}
237243

244+
MC_DLLEXPORT_DEF
238245
hash_t
239246
MCHashUSize (size_t i)
240247
{
241248
return MCHashInt(i);
242249
}
243250

251+
MC_DLLEXPORT_DEF
252+
hash_t
253+
MCHashInt64(int64_t i)
254+
{
255+
return MCHashInt(i);
256+
}
257+
258+
hash_t
259+
MCHashUInt64(uint64_t i)
260+
{
261+
return MCHashInt(i);
262+
}
244263
MC_DLLEXPORT_DEF
245264
hash_t MCHashPointer(const void *p)
246265
{

0 commit comments

Comments
 (0)