Skip to content

Commit 803adf4

Browse files
committed
[[ FFI ]] Add Obj-C FFI
This patch adds a collection of types to use for binding to Obj-C methods as well as the binding string support for hooking up to Obj-C object instance and class methods.
1 parent 1d272cd commit 803adf4

17 files changed

+1571
-10
lines changed

libfoundation/include/foundation-objc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
#include <foundation.h>
2222
#endif
2323

24+
#ifdef __OBJC__
2425
#import <Foundation/NSString.h>
2526
#import <Foundation/NSData.h>
2627

2728
NSString *MCStringConvertToAutoreleasedNSString(MCStringRef string);
2829
NSString *MCNameConvertToAutoreleasedNSString(MCNameRef name);
2930
NSData *MCDataConvertToAutoreleasedNSData(MCDataRef data);
31+
#endif
3032

3133
#endif

libfoundation/include/foundation.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ typedef struct __MCStream *MCStreamRef;
677677
typedef struct __MCProperList *MCProperListRef;
678678
typedef struct __MCForeignValue *MCForeignValueRef;
679679
typedef struct __MCJavaObject *MCJavaObjectRef;
680+
typedef struct __MCObjcObject *MCObjcObjectRef;
680681

681682
// Forward declaration
682683
typedef struct __MCLocale* MCLocaleRef;
@@ -3415,6 +3416,58 @@ MC_DLLEXPORT bool MCProperListEndsWithList(MCProperListRef list, MCProperListRef
34153416
MC_DLLEXPORT bool MCProperListIsListOfType(MCProperListRef list, MCValueTypeCode p_type);
34163417
MC_DLLEXPORT bool MCProperListIsHomogeneous(MCProperListRef list, MCValueTypeCode& r_type);
34173418

3419+
////////////////////////////////////////////////////////////////////////////////
3420+
//
3421+
// OBJC DEFINITIONS
3422+
//
3423+
3424+
/* The ObjcObject type manages the lifetime of the obj-c object it contains.
3425+
* Specifcally, it sends 'release' to the object when the ObjcObject is dropped */
3426+
MC_DLLEXPORT extern MCTypeInfoRef kMCObjcObjectTypeInfo;
3427+
MC_DLLEXPORT MCTypeInfoRef MCObjcObjectTypeInfo(void) ATTRIBUTE_PURE;
3428+
3429+
/* The ObjcId type describes an id which is passed into, or out of an obj-c
3430+
* method with no implicit action on its reference count. */
3431+
MC_DLLEXPORT extern MCTypeInfoRef kMCObjcIdTypeInfo;
3432+
MC_DLLEXPORT MCTypeInfoRef MCObjcIdTypeInfo(void) ATTRIBUTE_PURE;
3433+
3434+
/* The ObjcRetainedId type describes an id which is passed into, or out of an
3435+
* obj-c method and is expected to already have been retained. (i.e. the
3436+
* caller or callee expects to receive it with +1 ref count). */
3437+
MC_DLLEXPORT extern MCTypeInfoRef kMCObjcRetainedIdTypeInfo;
3438+
MC_DLLEXPORT MCTypeInfoRef MCObjcRetainedIdTypeInfo(void) ATTRIBUTE_PURE;
3439+
3440+
/* The ObjcAutoreleasedId type describes an id which has been placed in the
3441+
* innermost autorelease pool before being returned to the caller. */
3442+
MC_DLLEXPORT extern MCTypeInfoRef kMCObjcAutoreleasedIdTypeInfo;
3443+
MC_DLLEXPORT MCTypeInfoRef MCObjcAutoreleasedIdTypeInfo(void) ATTRIBUTE_PURE;
3444+
3445+
/* The ObjcObjectCreateWithId function creates an ObjcObject out of a raw id
3446+
* value, retaining it to make sure it owns a reference to it. */
3447+
MC_DLLEXPORT bool MCObjcObjectCreateWithId(void *value, MCObjcObjectRef& r_obj);
3448+
3449+
/* The ObjcObjectCreateWithId function creates an ObjcObject out of a raw id
3450+
* value, taking a +1 reference count from it (i.e. it assumes the value has
3451+
* already been retained before being called). */
3452+
MC_DLLEXPORT bool MCObjcObjectCreateWithRetainedId(void *value, MCObjcObjectRef& r_obj);
3453+
3454+
/* The ObjcObjectCreateWithAutoreleasedId function creates an ObjcObject out of
3455+
* a raw id value which is in the innermost autorelease pool. Currently this
3456+
* means that it retains it. */
3457+
MC_DLLEXPORT bool MCObjcObjectCreateWithAutoreleasedId(void *value, MCObjcObjectRef& r_obj);
3458+
3459+
/* The ObjcObjectGetId function returns the raw id value contained within
3460+
* an ObjcObject. The retain count of the id remains unchanged. */
3461+
MC_DLLEXPORT void *MCObjcObjectGetId(MCObjcObjectRef obj);
3462+
3463+
/* The ObjcObjectGetRetainedId function returns the raw id value contained within
3464+
* an ObjcObject. The id is retained before being returned. */
3465+
MC_DLLEXPORT void *MCObjcObjectGetRetainedId(MCObjcObjectRef obj);
3466+
3467+
/* The ObjcObjectGetAutoreleasedId function returns the raw id value contained within
3468+
* an ObjcObject. The id is autoreleased before being returned. */
3469+
MC_DLLEXPORT void *MCObjcObjectGetAutoreleasedId(MCObjcObjectRef obj);
3470+
34183471
////////////////////////////////////////////////////////////////////////////////
34193472

34203473
enum MCPickleFieldType

libfoundation/libfoundation.gyp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,15 @@
159159
[
160160
'OS != "mac" and OS != "ios"',
161161
{
162+
'sources':
163+
[
164+
'src/foundation-objc-dummy.cpp',
165+
],
166+
162167
'sources!':
163168
[
164169
'src/foundation-string-cf.cpp',
170+
'src/foundation-objc.mm',
165171
],
166172
},
167173
],

libfoundation/src/foundation-core.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ bool MCInitialize(void)
8787
if (!__MCJavaInitialize())
8888
return false;
8989

90+
if (!__MCObjcInitialize())
91+
return false;
92+
9093
return true;
9194
}
9295

@@ -108,7 +111,8 @@ void MCFinalize(void)
108111
__MCStringFinalize();
109112
__MCUnicodeFinalize();
110113
__MCJavaFinalize();
111-
114+
__MCObjcFinalize();
115+
112116
// Finalize values last
113117
__MCValueFinalize();
114118
}

libfoundation/src/foundation-foreign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCUIntTypeInfo() { return kMCCUIntTypeInf
121121
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCSIntTypeInfo() { return kMCCSIntTypeInfo; }
122122
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCULongTypeInfo() { return kMCCULongTypeInfo; }
123123
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCSLongTypeInfo() { return kMCCSLongTypeInfo; }
124-
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCULongLongTypeInfo() { return kMCCULongTypeInfo; }
125-
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCSLongLongTypeInfo() { return kMCCSLongTypeInfo; }
124+
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCULongLongTypeInfo() { return kMCCULongLongTypeInfo; }
125+
MC_DLLEXPORT_DEF MCTypeInfoRef MCForeignCSLongLongTypeInfo() { return kMCCSLongLongTypeInfo; }
126126

127127
/**/
128128

0 commit comments

Comments
 (0)