]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Replace all apr_pool_t * usages with CYPool &.
[cycript.git] / ObjectiveC / Library.mm
index 7b33245a534b8b0d3408133d213dff60c2088250..e430c0ceaedaa1bd0f2a520796dc4c84b50c7878 100644 (file)
@@ -1,28 +1,24 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2012  Jay Freeman (saurik)
+ * Copyright (C) 2009-2013  Jay Freeman (saurik)
 */
 
-/* GNU Lesser General Public License, Version 3 {{{ */
+/* GNU General Public License, Version 3 {{{ */
 /*
- * Cycript is free software: you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your
- * option) any later version.
+ * Cycript is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
  *
- * Cycript 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 Lesser General Public
- * License for more details.
+ * Cycript 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 Lesser General Public License
+ * You should have received a copy of the GNU General Public License
  * along with Cycript.  If not, see <http://www.gnu.org/licenses/>.
 **/
 /* }}} */
 
-#ifdef __APPLE__
-#include "Struct.hpp"
-#endif
-
 #include <Foundation/Foundation.h>
 
 #include "ObjectiveC/Internal.hpp"
@@ -163,7 +159,7 @@ enum {
     BLOCK_HAS_SIGNATURE = 1 << 30,
 };
 
-JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
+JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
 
 /* Objective-C Pool Release {{{ */
 apr_status_t CYPoolRelease_(void *data) {
@@ -172,24 +168,24 @@ apr_status_t CYPoolRelease_(void *data) {
     return APR_SUCCESS;
 }
 
-id CYPoolRelease_(apr_pool_t *pool, id object) {
+id CYPoolRelease_(CYPool *pool, id object) {
     if (object == nil)
         return nil;
     else if (pool == NULL)
         return [object autorelease];
     else {
-        apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
+        apr_pool_cleanup_register(*pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
         return object;
     }
 }
 
 template <typename Type_>
-Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
+Type_ CYPoolRelease(CYPool *pool, Type_ object) {
     return (Type_) CYPoolRelease_(pool, (id) object);
 }
 /* }}} */
 /* Objective-C Strings {{{ */
-const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
+const char *CYPoolCString(CYPool &pool, JSContextRef context, NSString *value) {
     if (pool == NULL)
         return [value UTF8String];
     else {
@@ -239,16 +235,16 @@ NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
     return CYCopyNSString(context, CYJSString(context, value));
 }
 
-NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
+NSString *CYCastNSString(CYPool *pool, const CYUTF8String &value) {
     return CYPoolRelease(pool, CYCopyNSString(value));
 }
 
-NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
+NSString *CYCastNSString(CYPool *pool, SEL sel) {
     const char *name(sel_getName(sel));
     return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
 }
 
-NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
+NSString *CYCastNSString(CYPool *pool, JSContextRef context, JSStringRef value) {
     return CYPoolRelease(pool, CYCopyNSString(context, value));
 }
 
@@ -270,7 +266,7 @@ size_t CYGetIndex(NSString *value) {
     return CYGetIndex(CYCastUTF8String(value));
 }
 
-bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
+bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &index) {
     return CYGetOffset(CYPoolCString(pool, context, value), index);
 }
 
@@ -523,7 +519,7 @@ struct PropertyAttributes {
         name = property_getName(property);
         const char *attributes(property_getAttributes(property));
 
-        for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
+        for (char *state, *token(apr_strtok(pool_.strdup(attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
             switch (*token) {
                 case 'R': readonly = true; break;
                 case 'C': copy = true; break;
@@ -548,7 +544,7 @@ struct PropertyAttributes {
 
     const char *Getter() {
         if (getter_ == NULL)
-            getter_ = apr_pstrdup(pool_, name);
+            getter_ = pool_.strdup(name);
         return getter_;
     }
 
@@ -645,12 +641,45 @@ _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSVal
     return is;
 }
 
-NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
+    BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
+
+    struct Descriptor {
+        struct {
+            BlockDescriptor1 one_;
+            BlockDescriptor2 two_;
+            BlockDescriptor3 three_;
+        } d_;
+
+        CYPool pool_;
+    };
+
+    Descriptor *descriptor(new Descriptor);
+    memset(&descriptor->d_, 0, sizeof(descriptor->d_));
+
+    literal->isa = objc_getClass("__NSGlobalBlock__");
+    literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
+    literal->reserved = 0;
+    literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
+    literal->descriptor = descriptor;
+
+    descriptor->d_.one_.size = sizeof(descriptor->d_);
+    descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
+
+    return reinterpret_cast<NSObject *>(literal);
+}
+
+NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) {
     if (CYJSValueIsNSObject(context, object)) {
         Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
         return internal->GetValue();
     }
 
+    if (JSValueIsObjectOfClass(context, object, Functor_)) {
+        cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
+        return CYMakeBlock(internal->GetValue(), internal->signature_);
+    }
+
     bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
     id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
     return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
@@ -666,7 +695,7 @@ NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
 @end
 #endif
 
-id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
+id CYNSObject(CYPool *pool, JSContextRef context, JSValueRef value, bool cast) {
     id object;
     bool copy;
 
@@ -719,12 +748,12 @@ id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cas
         return [object retain];
 }
 
-NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSValueRef value) {
     return CYNSObject(pool, context, value, true);
 }
 
-NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
-    return CYNSObject(pool, context, value, false);
+NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
+    return CYNSObject(&pool, context, value, false);
 }
 
 /* Bridge: NSArray {{{ */
@@ -1153,8 +1182,8 @@ static bool CYIsClass(id self) {
 #endif
 }
 
-Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
-    id self(CYCastNSObject(pool, context, value));
+Class CYCastClass(CYPool &pool, JSContextRef context, JSValueRef value) {
+    id self(CYCastNSObject(&pool, context, value));
     if (CYIsClass(self))
         return (Class) self;
     throw CYJSError(context, "got something that is not a Class");
@@ -1166,7 +1195,7 @@ NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
     size_t size(JSPropertyNameArrayGetCount(names));
     NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
     for (size_t index(0); index != size; ++index)
-        [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
+        [array addObject:CYCastNSString(&pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
     return array;
 }
 
@@ -1267,7 +1296,7 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
     size_t bounds([self count]);
     if (index >= bounds)
-        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
     JSValueRef exception(NULL);
     JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
     CYThrow(context_, exception);
@@ -1281,7 +1310,7 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
     size_t bounds([self count] + 1);
     if (index >= bounds)
-        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
     JSValueRef exception(NULL);
     JSValueRef arguments[3];
     arguments[0] = CYCastJSValue(context_, index);
@@ -1302,7 +1331,7 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
     size_t bounds([self count]);
     if (index >= bounds)
-        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
     JSValueRef exception(NULL);
     JSValueRef arguments[2];
     arguments[0] = CYCastJSValue(context_, index);
@@ -1315,7 +1344,7 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
     size_t bounds([self count]);
     if (index >= bounds)
-        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
+        @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
     CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
 } CYObjectiveCatch }
 
@@ -1437,12 +1466,13 @@ static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (
     ffi_call(cif, function, value, values);
 } CYSadCatch() }
 
-static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
+static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
     switch (type->primitive) {
         // XXX: do something epic about blocks
         case sig::block_P:
         case sig::object_P:
         case sig::typename_P:
+            // XXX: this works for return values, but not for properties and fields
             *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
         break;
 
@@ -1502,7 +1532,7 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid = fa
     return false;
 }
 
-static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
+static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
     if (method != NULL)
         return method_getTypeEncoding(method);
 
@@ -1659,7 +1689,7 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr
         return true;
 
     CYPool pool;
-    NSString *name(CYCastNSString(pool, context, property));
+    NSString *name(CYCastNSString(&pool, context, property));
 
     if (CYInternal *internal = [CYInternal get:self])
         if ([internal hasProperty:property inContext:context])
@@ -1697,7 +1727,7 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object,
         return Internal::Make(context, self, object);
 
     CYPool pool;
-    NSString *name(CYCastNSString(pool, context, property));
+    NSString *name(CYCastNSString(&pool, context, property));
 
     if (CYInternal *internal = [CYInternal get:self])
         if (JSValueRef value = [internal getProperty:property inContext:context])
@@ -1725,7 +1755,7 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object,
                 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
 
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
@@ -1733,13 +1763,13 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
 
     CYPool pool;
 
-    NSString *name(CYCastNSString(pool, context, property));
-    NSObject *data(CYCastNSObject(pool, context, value));
+    NSString *name(CYCastNSString(&pool, context, property));
+    NSObject *data(CYCastNSObject(&pool, context, value));
 
     CYPoolTry {
         if ([self cy$setProperty:name to:data])
             return true;
-    } CYPoolCatch(NULL)
+    } CYPoolCatch(false)
 
     const char *string(CYPoolCString(pool, context, name));
     Class _class(object_getClass(self));
@@ -1785,7 +1815,7 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
     }
 
     return false;
-} CYCatch }
+} CYCatch(false) }
 
 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
@@ -1794,8 +1824,8 @@ static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JS
     CYPoolTry {
         NSString *name(CYCastNSString(NULL, context, property));
         return [self cy$deleteProperty:name];
-    } CYPoolCatch(NULL)
-} CYCatch return /*XXX*/ NULL; }
+    } CYPoolCatch(false)
+} CYCatch(false) return /*XXX*/ false; }
 
 static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
     const char *name(sel_getName(method_getName(method)));
@@ -1852,7 +1882,7 @@ static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
     JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
     return value;
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
@@ -1900,7 +1930,7 @@ static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef obje
     } CYPoolCatch(NULL);
 
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
@@ -1915,17 +1945,17 @@ static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor,
     }
 
     return false;
-} CYCatch }
+} CYCatch(false) }
 
 static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count == 0)
         throw CYJSError(context, "incorrect number of arguments to Instance");
     CYPool pool;
-    id value(CYCastNSObject(pool, context, arguments[0]));
+    id value(CYCastNSObject(&pool, context, arguments[0]));
     if (value == nil)
         value = [NSNull null];
     return CYCastJSValue(context, [value cy$box]);
-} CYCatch }
+} CYCatch(NULL) }
 
 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
     Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
@@ -1954,7 +1984,7 @@ static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object,
     }
 
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
     Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
@@ -1965,12 +1995,12 @@ static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStr
 
     if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
         Type_privateData type(pool, ivar_getTypeEncoding(ivar));
-        CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
+        CYPoolFFI(&pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
         return true;
     }
 
     return false;
-} CYCatch }
+} CYCatch(false) }
 
 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
     if (Class super = class_getSuperclass(_class))
@@ -2006,11 +2036,11 @@ static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef
 
 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
     CYPool pool;
-    NSString *name(CYCastNSString(pool, context, property));
+    NSString *name(CYCastNSString(&pool, context, property));
     if (Class _class = NSClassFromString(name))
         return CYMakeInstance(context, _class, true);
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
 #ifdef __APPLE__
@@ -2060,7 +2090,7 @@ static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSO
   free:
     free(data);
     return value;
-} CYCatch }
+} CYCatch(NULL) }
 
 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
     const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
@@ -2089,7 +2119,7 @@ static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRe
     JSObjectRef value(JSObjectMake(context, NULL, NULL));
     CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
     return value;
-} CYCatch }
+} CYCatch(NULL) }
 
 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
     unsigned int size;
@@ -2106,7 +2136,7 @@ static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjec
     if (Protocol *protocol = objc_getProtocol(name))
         return CYMakeInstance(context, protocol, true);
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
 #if OBJC_API_VERSION >= 2
@@ -2126,13 +2156,25 @@ static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjec
     if (name == "nil")
         return Instance::Make(context, nil);
     return NULL;
-} CYCatch }
+} CYCatch(NULL) }
 
 static void ObjectiveC_Constants_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
     JSPropertyNameAccumulatorAddName(names, CYJSString("nil"));
 }
 
 #ifdef __APPLE__
+#if defined(__i386__) || defined(__x86_64__)
+#define OBJC_MAX_STRUCT_BY_VALUE 8
+static int struct_forward_array[] = {
+    0, 0, 0, 1, 0, 1, 1, 1, 0 };
+#elif defined(__arm__)
+#define OBJC_MAX_STRUCT_BY_VALUE 1
+static int struct_forward_array[] = {
+    0, 0 };
+#else
+#error missing objc-runtime-info
+#endif
+
 static bool stret(ffi_type *ffi_type) {
     return ffi_type->type == FFI_TYPE_STRUCT && (
         ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
@@ -2141,7 +2183,7 @@ static bool stret(ffi_type *ffi_type) {
 }
 #endif
 
-JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
+JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
     const char *type;
 
     if (_class == NULL)
@@ -2207,7 +2249,7 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class
 
     void (*function)() = reinterpret_cast<void (*)()>(imp);
     return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count < 2)
@@ -2234,7 +2276,7 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje
         if (uninitialized)
             internal->value_ = nil;
     } else {
-        self = CYCastNSObject(pool, context, arguments[0]);
+        self = CYCastNSObject(&pool, context, arguments[0]);
         _class = nil;
         uninitialized = false;
     }
@@ -2245,7 +2287,7 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje
     _cmd = CYCastSEL(context, arguments[1]);
 
     return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     JSValueRef setup[count + 2];
@@ -2260,7 +2302,7 @@ static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef objec
     Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
 
     // XXX: handle Instance::Uninitialized?
-    id self(CYCastNSObject(pool, context, _this));
+    id self(CYCastNSObject(&pool, context, _this));
 
     void *setup[2];
     setup[0] = &self;
@@ -2273,10 +2315,10 @@ static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t co
     if (count != 2)
         throw CYJSError(context, "incorrect number of arguments to Super constructor");
     CYPool pool;
-    id self(CYCastNSObject(pool, context, arguments[0]));
+    id self(CYCastNSObject(&pool, context, arguments[0]));
     Class _class(CYCastClass(pool, context, arguments[1]));
     return cy::Super::Make(context, self, _class);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count != 1)
@@ -2284,14 +2326,14 @@ static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t
     CYPool pool;
     const char *name(CYPoolCString(pool, context, arguments[0]));
     return CYMakeSelector(context, sel_registerName(name));
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count > 1)
         throw CYJSError(context, "incorrect number of arguments to Instance constructor");
     id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
     return CYMakeInstance(context, self, false);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
@@ -2327,7 +2369,7 @@ static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectR
     if (!CYIsClass(self))
         return CYJSUndefined(context);
     return CYGetClassPrototype(context, self);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
@@ -2343,7 +2385,7 @@ static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectR
 
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
     return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!CYJSValueIsNSObject(context, _this))
@@ -2366,7 +2408,7 @@ static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectR
         else
             return CYCastJSValue(context, CYJSString(context, [value description]));
     } CYPoolCatch(NULL)
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!CYJSValueIsNSObject(context, _this))
@@ -2382,7 +2424,7 @@ static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObject
         return result;
 
     return _this;
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!CYJSValueIsNSObject(context, _this))
@@ -2391,7 +2433,7 @@ static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObje
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
     // XXX: but... but... THIS ISN'T A POINTER! :(
     return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!CYJSValueIsNSObject(context, _this))
@@ -2404,7 +2446,7 @@ static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjec
         // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
         return CYCastJSValue(context, CYJSString(context, [value description]));
     } CYPoolCatch(NULL)
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!CYJSValueIsNSObject(context, _this))
@@ -2426,12 +2468,12 @@ static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectR
     CYPoolTry {
         return CYMakeType(context, type.str().c_str());
     } CYPoolCatch(NULL)
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
     return CYCastJSValue(context, sel_getName(internal->GetValue()));
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
@@ -2445,7 +2487,7 @@ static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectR
         NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
         return CYCastJSValue(context, CYJSString(context, string));
     } CYPoolCatch(NULL)
-} CYCatch return /*XXX*/ NULL; }
+} CYCatch(NULL) return /*XXX*/ NULL; }
 
 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count != 1)
@@ -2465,7 +2507,7 @@ static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef
         return CYCastJSValue(context, CYJSString(type));
 
     return CYJSNull(context);
-} CYCatch }
+} CYCatch(NULL) }
 
 static JSStaticValue Selector_staticValues[2] = {
     {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
@@ -2519,7 +2561,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     $objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
     $objc_removeAssociatedObjects = reinterpret_cast<void (*)(id)>(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects"));
 
-    apr_pool_t *pool(CYGetGlobalPool());
+    CYPool &pool(CYGetGlobalPool());
 
     Object_type = new(pool) Type_privateData("@");
     Selector_type = new(pool) Type_privateData(":");