]> git.saurik.com Git - cycript.git/blobdiff - Library.mm
Fixed one stupid CYON bug in what was otherwise an awesome release.
[cycript.git] / Library.mm
index a2f5891bd3f1093c9afd59fc45c8ec3b50eed224..2c22bb10598c6ce86467be883642e24807be2aea 100644 (file)
 #include "Pooling.hpp"
 #include "Struct.hpp"
 
-#include <unistd.h>
-
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreFoundation/CFLogUtilities.h>
 
 #include <WebKit/WebScriptObject.h>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <sys/un.h>
-
 #include <sys/mman.h>
 
 #include <iostream>
@@ -73,8 +66,6 @@
 #include "Parser.hpp"
 #include "Cycript.tab.hh"
 
-#include <fcntl.h>
-
 #include <apr-1/apr_thread_proc.h>
 
 #undef _assert
@@ -111,6 +102,7 @@ static JSObjectRef ObjectiveC_;
 
 static JSClassRef Functor_;
 static JSClassRef Instance_;
+static JSClassRef Internal_;
 static JSClassRef Pointer_;
 static JSClassRef Runtime_;
 static JSClassRef Selector_;
@@ -141,32 +133,11 @@ static Class Object_;
 
 static NSArray *Bridge_;
 
-struct CYData {
-    apr_pool_t *pool_;
-
-    virtual ~CYData() {
-    }
-
-    static void *operator new(size_t size, apr_pool_t *pool) {
-        void *data(apr_palloc(pool, size));
-        reinterpret_cast<CYData *>(data)->pool_ = pool;
-        return data;
-    }
-
-    static void *operator new(size_t size) {
-        apr_pool_t *pool;
-        apr_pool_create(&pool, NULL);
-        return operator new(size, pool);
-    }
-
-    static void operator delete(void *data) {
-        apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_);
-    }
+static void Finalize(JSObjectRef object) {
+    delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object));
+}
 
-    static void Finalize(JSObjectRef object) {
-        delete reinterpret_cast<CYData *>(JSObjectGetPrivate(object));
-    }
-};
+class Type_privateData;
 
 struct CYValue :
     CYData
@@ -180,6 +151,15 @@ struct CYValue :
         value_(value)
     {
     }
+
+    CYValue(const CYValue &rhs) :
+        value_(rhs.value_)
+    {
+    }
+
+    virtual Type_privateData *GetType() const {
+        return NULL;
+    }
 };
 
 struct Selector_privateData :
@@ -193,6 +173,8 @@ struct Selector_privateData :
     SEL GetValue() const {
         return reinterpret_cast<SEL>(value_);
     }
+
+    virtual Type_privateData *GetType() const;
 };
 
 struct Instance :
@@ -229,6 +211,28 @@ struct Instance :
     bool IsUninitialized() const {
         return (flags_ & Uninitialized) != 0;
     }
+
+    virtual Type_privateData *GetType() const;
+};
+
+struct Internal :
+    CYValue
+{
+    JSObjectRef owner_;
+
+    Internal(id value, JSObjectRef owner) :
+        CYValue(value),
+        owner_(owner)
+    {
+    }
+
+    static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) {
+        return JSObjectMake(context, Internal_, new Internal(object, owner));
+    }
+
+    id GetValue() const {
+        return reinterpret_cast<id>(value_);
+    }
 };
 
 namespace sig {
@@ -327,6 +331,9 @@ void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type
 struct Type_privateData :
     CYData
 {
+    static Type_privateData *Object;
+    static Type_privateData *Selector;
+
     ffi_type *ffi_;
     sig::Type *type_;
 
@@ -381,6 +388,17 @@ struct Type_privateData :
     }
 };
 
+Type_privateData *Type_privateData::Object;
+Type_privateData *Type_privateData::Selector;
+
+Type_privateData *Instance::GetType() const {
+    return Type_privateData::Object;
+}
+
+Type_privateData *Selector_privateData::GetType() const {
+    return Type_privateData::Selector;
+}
+
 struct Pointer :
     CYValue
 {
@@ -536,6 +554,7 @@ NSString *CYPoolNSCYON(apr_pool_t *pool, id value);
 - (NSString *) cy$toCYON;
 - (NSString *) cy$toKey;
 
+- (bool) cy$hasProperty:(NSString *)name;
 - (NSObject *) cy$getProperty:(NSString *)name;
 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
 - (bool) cy$deleteProperty:(NSString *)name;
@@ -670,19 +689,19 @@ struct PropertyAttributes {
     return [self cy$toCYON];
 }
 
+- (bool) cy$hasProperty:(NSString *)name {
+    return false;
+}
+
 - (NSObject *) cy$getProperty:(NSString *)name {
-    /*if (![name isEqualToString:@"prototype"])
-        NSLog(@"get:%@", name);*/
     return nil;
 }
 
 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
-    //NSLog(@"set:%@", name);
     return false;
 }
 
 - (bool) cy$deleteProperty:(NSString *)name {
-    //NSLog(@"delete:%@", name);
     return false;
 }
 
@@ -740,7 +759,7 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
             [json appendString:@","];
         else
             comma = true;
-        if ([object cy$JSType] != kJSTypeUndefined)
+        if (object == nil || [object cy$JSType] != kJSTypeUndefined)
             [json appendString:CYPoolNSCYON(NULL, object)];
         else {
             [json appendString:@","];
@@ -752,6 +771,17 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
     return json;
 }
 
+- (bool) cy$hasProperty:(NSString *)name {
+    if ([name isEqualToString:@"length"])
+        return true;
+
+    ssize_t index;
+    if (!CYGetIndex(NULL, name, index) || index < 0 || index >= static_cast<ssize_t>([self count]))
+        return [super cy$hasProperty:name];
+    else
+        return true;
+}
+
 - (NSObject *) cy$getProperty:(NSString *)name {
     if ([name isEqualToString:@"length"])
         return [NSNumber numberWithUnsignedInteger:[self count]];
@@ -811,6 +841,10 @@ NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
     return json;
 }
 
+- (bool) cy$hasProperty:(NSString *)name {
+    return [self objectForKey:name] != nil;
+}
+
 - (NSObject *) cy$getProperty:(NSString *)name {
     return [self objectForKey:name];
 }
@@ -1336,38 +1370,47 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) {
 @end
 
 NSString *CYCopyNSCYON(id value) {
-    Class _class(object_getClass(value));
-    SEL sel(@selector(cy$toCYON));
-
     NSString *string;
 
-    if (Method toCYON = class_getInstanceMethod(_class, sel))
-        string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
-    else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
-        if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
-            string = [value cy$toCYON];
-        else goto fail;
-    } else fail: {
-        if (value == NSZombie_)
-            string = @"_NSZombie_";
-        else if (_class == NSZombie_)
-            string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
-        else if (value == NSMessageBuilder_ || value == Object_)
-            string = nil;
-        else
-            string = [NSString stringWithFormat:@"%@", value];
+    if (value == nil)
+        string = @"nil";
+    else {
+        Class _class(object_getClass(value));
+        SEL sel(@selector(cy$toCYON));
+
+        if (Method toCYON = class_getInstanceMethod(_class, sel))
+            string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
+        else if (Method methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
+            if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
+                string = [value cy$toCYON];
+            else goto fail;
+        } else fail: {
+            if (value == NSZombie_)
+                string = @"_NSZombie_";
+            else if (_class == NSZombie_)
+                string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
+            // XXX: frowny /in/ the pants
+            else if (value == NSMessageBuilder_ || value == Object_)
+                string = nil;
+            else
+                string = [NSString stringWithFormat:@"%@", value];
+        }
+
+        // XXX: frowny pants
+        if (string == nil)
+            string = @"undefined";
     }
 
-    // XXX: frowny pants
-    if (string == nil)
-        string = @"undefined";
     return [string retain];
 }
 
 NSString *CYCopyNSCYON(JSContextRef context, JSValueRef value, JSValueRef *exception) {
+    if (JSValueIsNull(context, value))
+        return [@"null" retain];
+
     CYTry {
         CYPoolTry {
-            return CYCopyNSCYON(CYCastNSObject(NULL, context, value) ?: [NSNull null]);
+            return CYCopyNSCYON(CYCastNSObject(NULL, context, value));
         } CYPoolCatch(NULL)
     } CYCatch
 }
@@ -1422,6 +1465,12 @@ struct CYInternal :
         return internal;
     }
 
+    bool HasProperty(JSContextRef context, JSStringRef name) {
+        if (object_ == NULL)
+            return false;
+        return JSObjectHasProperty(context, object_, name);
+    }
+
     JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
         if (object_ == NULL)
             return NULL;
@@ -1440,7 +1489,7 @@ JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
     return JSObjectMake(context, Selector_, data);
 }
 
-JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, JSObjectRef owner) {
+JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
     Pointer *data(new Pointer(pointer, type, owner));
     return JSObjectMake(context, Pointer_, data);
 }
@@ -1641,7 +1690,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void
 
         case sig::pointer_P:
             if (void *pointer = *reinterpret_cast<void **>(data))
-                value = CYMakePointer(context, pointer, type->data.data.type, owner);
+                value = CYMakePointer(context, pointer, type->data.data.type, ffi, owner);
             else goto null;
         break;
 
@@ -1671,11 +1720,52 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void
     return value;
 }
 
-static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+static bool CYImplements(id object, Class _class, SEL selector) {
+    // XXX: possibly use a more "awesome" check?
+    return class_getInstanceMethod(_class, selector) != NULL;
+}
+
+static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+    id self(internal->GetValue());
+
+    if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+        return true;
+
     CYPool pool;
+    NSString *name(CYCastNSString(pool, property));
+
+    if (CYInternal *internal = CYInternal::Get(self))
+        if (internal->HasProperty(context, property))
+            return true;
+
+    CYPoolTry {
+        if ([self cy$hasProperty:name])
+            return true;
+    } CYPoolCatch(false)
+
+    const char *string(CYPoolCString(pool, name));
+    Class _class(object_getClass(self));
+
+    if (class_getProperty(_class, string) != NULL)
+        return true;
+
+    if (SEL sel = sel_getUid(string))
+        if (CYImplements(self, _class, sel))
+            return true;
+
+    return false;
+}
+
+static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+    id self(internal->GetValue());
+
+    if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+        return Internal::Make(context, self, object);
 
     CYTry {
-        NSString *self(CYCastNSObject(pool, context, object));
+        CYPool pool;
         NSString *name(CYCastNSString(pool, property));
 
         if (CYInternal *internal = CYInternal::Get(self))
@@ -1688,17 +1778,17 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object,
         } CYPoolCatch(NULL)
 
         const char *string(CYPoolCString(pool, name));
+        Class _class(object_getClass(self));
 
-        if (objc_property_t property = class_getProperty(object_getClass(self), string)) {
+        if (objc_property_t property = class_getProperty(_class, string)) {
             PropertyAttributes attributes(property);
             SEL sel(sel_registerName(attributes.Getter()));
             return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
         }
 
-        if (Ivar ivar = object_getInstanceVariable(self, string, NULL)) {
-            Type_privateData type(pool, ivar_getTypeEncoding(ivar));
-            return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
-        }
+        if (SEL sel = sel_getUid(string))
+            if (CYImplements(self, _class, sel))
+                return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
 
         return NULL;
     } CYCatch
@@ -1708,7 +1798,7 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
     CYPool pool;
 
     CYTry {
-        NSString *self(CYCastNSObject(pool, context, object));
+        id self(CYCastNSObject(pool, context, object));
         NSString *name(CYCastNSString(pool, property));
         NSString *data(CYCastNSObject(pool, context, value));
 
@@ -1718,8 +1808,9 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
         } CYPoolCatch(NULL)
 
         const char *string(CYPoolCString(pool, name));
+        Class _class(object_getClass(self));
 
-        if (objc_property_t property = class_getProperty(object_getClass(self), string)) {
+        if (objc_property_t property = class_getProperty(_class, string)) {
             PropertyAttributes attributes(property);
             if (const char *setter = attributes.Setter()) {
                 SEL sel(sel_registerName(setter));
@@ -1729,12 +1820,28 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
             }
         }
 
-        if (Ivar ivar = object_getInstanceVariable(self, string, NULL)) {
-            Type_privateData type(pool, ivar_getTypeEncoding(ivar));
-            CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
-            return true;
+        size_t length(strlen(string));
+
+        char set[length + 5];
+
+        set[0] = 's';
+        set[1] = 'e';
+        set[2] = 't';
+
+        if (string[0] != '\0') {
+            set[3] = toupper(string[0]);
+            memcpy(set + 4, string + 1, length - 1);
         }
 
+        set[length + 3] = ':';
+        set[length + 4] = '\0';
+
+        if (SEL sel = sel_getUid(set))
+            if (CYImplements(self, _class, sel)) {
+                JSValueRef arguments[1] = {value};
+                CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
+            }
+
         if (CYInternal *internal = CYInternal::Set(self)) {
             internal->SetProperty(context, property, value);
             return true;
@@ -1747,7 +1854,7 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr
 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     CYTry {
         CYPoolTry {
-            NSString *self(CYCastNSObject(NULL, context, object));
+            id self(CYCastNSObject(NULL, context, object));
             NSString *name(CYCastNSString(NULL, property));
             return [self cy$deleteProperty:name];
         } CYPoolCatch(NULL)
@@ -1766,6 +1873,70 @@ static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object,
             JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
         free(data);
     }
+}
+
+static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYTry {
+        Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+        JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized));
+        return value;
+    } CYCatch
+}
+
+static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
+    Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+    CYPool pool;
+
+    id self(internal->GetValue());
+    const char *name(CYPoolCString(pool, property));
+
+    if (object_getInstanceVariable(self, name, NULL) != NULL)
+        return true;
+
+    return false;
+}
+
+static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+    Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+    CYPool pool;
+
+    CYTry {
+        id self(internal->GetValue());
+        const char *name(CYPoolCString(pool, property));
+
+        if (Ivar ivar = object_getInstanceVariable(self, name, NULL)) {
+            Type_privateData type(pool, ivar_getTypeEncoding(ivar));
+            return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
+        }
+
+        return NULL;
+    } CYCatch
+}
+
+static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+    Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+    CYPool pool;
+
+    CYTry {
+        id self(internal->GetValue());
+        const char *name(CYPoolCString(pool, property));
+
+        if (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);
+            return true;
+        }
+
+        return false;
+    } CYCatch
+}
+
+static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+    Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+    CYPool pool;
+
+    id self(internal->GetValue());
+    Class _class(object_getClass(self));
 
     for (Class super(_class); super != NULL; super = class_getSuperclass(super)) {
         unsigned int size;
@@ -1776,12 +1947,9 @@ static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object,
     }
 }
 
-static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
-    CYTry {
-        Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
-        JSObjectRef value(Instance::Make(context, [data->GetValue() alloc], Instance::Uninitialized));
-        return value;
-    } CYCatch
+static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
+    return internal->owner_;
 }
 
 bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
@@ -1827,18 +1995,10 @@ bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property
     return true;
 }
 
-static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
-    CYPool pool;
+static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) {
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
     Type_privateData *typical(internal->type_);
 
-    if (typical->type_ == NULL)
-        return NULL;
-
-    ssize_t index;
-    if (!CYGetIndex(pool, property, index))
-        return NULL;
-
     ffi_type *ffi(typical->GetFFI());
 
     uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
@@ -1851,7 +2011,7 @@ static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object,
     } CYCatch
 }
 
-static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     CYPool pool;
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
     Type_privateData *typical(internal->type_);
@@ -1863,6 +2023,17 @@ static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStri
     if (!CYGetIndex(pool, property, index))
         return NULL;
 
+    return Pointer_getIndex(context, object, index, exception);
+}
+
+static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+    return Pointer_getIndex(context, object, 0, exception);
+}
+
+static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) {
+    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+    Type_privateData *typical(internal->type_);
+
     ffi_type *ffi(typical->GetFFI());
 
     uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
@@ -1874,6 +2045,31 @@ static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStri
     } CYCatch
 }
 
+static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+    CYPool pool;
+    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+    Type_privateData *typical(internal->type_);
+
+    if (typical->type_ == NULL)
+        return NULL;
+
+    ssize_t index;
+    if (!CYGetIndex(pool, property, index))
+        return NULL;
+
+    return Pointer_setIndex(context, object, 0, value, exception);
+}
+
+static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+    return Pointer_setIndex(context, object, 0, value, exception);
+}
+
+static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this)));
+    Type_privateData *typical(internal->type_);
+    return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this);
+}
+
 static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     CYPool pool;
     Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
@@ -2110,6 +2306,9 @@ static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObject
 }
 
 static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+    if (JSStringIsEqualToUTF8CString(property, "nil"))
+        return Instance::Make(context, nil, Instance::None);
+
     CYTry {
         CYPool pool;
         NSString *name(CYCastNSString(pool, property));
@@ -2281,7 +2480,7 @@ JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count,
         sig::Signature signature;
         sig::Parse(pool, &signature, type, &Structor_);
 
-        return CYMakePointer(context, value, signature.elements[0].type, NULL);
+        return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL);
     } CYCatch
 }
 
@@ -2322,7 +2521,16 @@ static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef obje
         size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0]));
         // XXX: alignment?
         void *value(malloc(internal->GetFFI()->size * size));
-        return CYMakePointer(context, value, internal->type_, NULL);
+        return CYMakePointer(context, value, internal->type_, internal->ffi_, NULL);
+    } CYCatch
+}
+
+JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYTry {
+        if (count > 1)
+            @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Instance constructor" userInfo:nil];
+        id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
+        return Instance::Make(context, self, Instance::None);
     } CYCatch
 }
 
@@ -2353,9 +2561,28 @@ JSValueRef Selector_getProperty_prototype(JSContextRef context, JSObjectRef obje
     return Function_;
 }
 
+static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+    Type_privateData *typical(internal->GetType());
+
+    sig::Type *type;
+    ffi_type *ffi;
+
+    if (typical == NULL) {
+        type = NULL;
+        ffi = NULL;
+    } else {
+        type = typical->type_;
+        ffi = typical->ffi_;
+    }
+
+    return CYMakePointer(context, &internal->value_, type, ffi, object);
+}
+
 static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+
     CYTry {
-        CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
         return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
     } CYCatch
 }
@@ -2365,26 +2592,29 @@ static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRe
 }
 
 static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+    char string[32];
+    sprintf(string, "%p", internal->value_);
+
     CYTry {
-        CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
-        char string[32];
-        sprintf(string, "%p", internal->value_);
         return CYCastJSValue(context, string);
     } CYCatch
 }
 
 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
     CYTry {
-        Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
         CYPoolTry {
-            return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toCYON]));
+            return CYCastJSValue(context, CYJSString(CYPoolNSCYON(NULL, internal->GetValue())));
         } CYPoolCatch(NULL)
     } CYCatch
 }
 
 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
     CYTry {
-        Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
         CYPoolTry {
             NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0])));
             return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key]));
@@ -2393,8 +2623,9 @@ static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectR
 }
 
 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
     CYTry {
-        Instance *data(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
         CYPoolTry {
             return CYCastJSValue(context, CYJSString([data->GetValue() description]));
         } CYPoolCatch(NULL)
@@ -2402,8 +2633,9 @@ static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjec
 }
 
 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+
     CYTry {
-        Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
         return CYCastJSValue(context, sel_getName(data->GetValue()));
     } CYCatch
 }
@@ -2413,9 +2645,10 @@ static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectR
 }
 
 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+    const char *name(sel_getName(internal->GetValue()));
+
     CYTry {
-        Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
-        const char *name(sel_getName(internal->GetValue()));
         CYPoolTry {
             return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name]));
         } CYPoolCatch(NULL)
@@ -2470,6 +2703,11 @@ static JSStaticValue CYValue_staticValues[2] = {
     {NULL, NULL, NULL, 0}
 };
 
+static JSStaticValue Pointer_staticValues[2] = {
+    {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, NULL, 0}
+};
+
 static JSStaticFunction Pointer_staticFunctions[4] = {
     {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2477,6 +2715,11 @@ static JSStaticFunction Pointer_staticFunctions[4] = {
     {NULL, NULL, 0}
 };
 
+static JSStaticFunction Struct_staticFunctions[2] = {
+    {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, 0}
+};
+
 static JSStaticFunction Functor_staticFunctions[4] = {
     {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2489,13 +2732,24 @@ static JSStaticFunction Functor_staticFunctions[4] = {
     {NULL, NULL, NULL, 0}
 };*/
 
-static JSStaticFunction Instance_staticFunctions[4] = {
+static JSStaticValue Instance_staticValues[2] = {
+    {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, NULL, 0}
+};
+
+static JSStaticFunction Instance_staticFunctions[5] = {
+    {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
 };
 
+static JSStaticFunction Internal_staticFunctions[2] = {
+    {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, 0}
+};
+
 static JSStaticFunction Selector_staticFunctions[5] = {
     {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2504,7 +2758,7 @@ static JSStaticFunction Selector_staticFunctions[5] = {
     {NULL, NULL, 0}
 };
 
-static JSStaticFunction Type_staticFunctions[5] = {
+static JSStaticFunction Type_staticFunctions[4] = {
     {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2591,7 +2845,6 @@ bool CYSendAll_(int socket, const uint8_t *data, size_t size) {
     return true;
 }
 
-static int Socket_;
 apr_pool_t *Pool_;
 
 struct CYExecute_ {
@@ -2683,29 +2936,20 @@ static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
     return NULL;
 }
 
-static void * APR_THREAD_FUNC Cyrver(apr_thread_t *thread, void *data) {
-    for (;;) {
-        int socket(_syscall(accept(Socket_, NULL, NULL)));
-        CYClient *client(new CYClient(socket));
-        apr_threadattr_t *attr;
-        _aprcall(apr_threadattr_create(&attr, Pool_));
-        _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
-    }
-
-    return NULL;
-}
-
-void Unlink() {
-    pid_t pid(getpid());
-    char path[104];
-    sprintf(path, "/tmp/.s.cy.%u", pid);
-    unlink(path);
+extern "C" void CYHandleClient(apr_pool_t *pool, int socket) {
+    CYClient *client(new(pool) CYClient(socket));
+    apr_threadattr_t *attr;
+    _aprcall(apr_threadattr_create(&attr, client->pool_));
+    _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
 }
 
 MSInitialize { _pooled
     _aprcall(apr_initialize());
     _aprcall(apr_pool_create(&Pool_, NULL));
 
+    Type_privateData::Object = new(Pool_) Type_privateData(Pool_, "@");
+    Type_privateData::Selector = new(Pool_) Type_privateData(Pool_, ":");
+
     Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
 
     NSCFBoolean_ = objc_getClass("NSCFBoolean");
@@ -2713,29 +2957,6 @@ MSInitialize { _pooled
     NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
     NSZombie_ = objc_getClass("_NSZombie_");
     Object_ = objc_getClass("Object");
-
-    Socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));
-
-    struct sockaddr_un address;
-    memset(&address, 0, sizeof(address));
-    address.sun_family = AF_UNIX;
-
-    pid_t pid(getpid());
-    sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
-
-    try {
-        _syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
-        atexit(&Unlink);
-        _syscall(listen(Socket_, 0));
-
-        apr_threadattr_t *attr;
-        _aprcall(apr_threadattr_create(&attr, Pool_));
-
-        apr_thread_t *thread;
-        _aprcall(apr_thread_create(&thread, attr, &Cyrver, NULL, Pool_));
-    } catch (...) {
-        NSLog(@"failed to setup Cyrver");
-    }
 }
 
 JSGlobalContextRef CYGetJSContext() {
@@ -2746,27 +2967,39 @@ JSGlobalContextRef CYGetJSContext() {
         definition.className = "Functor";
         definition.staticFunctions = Functor_staticFunctions;
         definition.callAsFunction = &Functor_callAsFunction;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Functor_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
         definition.className = "Instance";
-        definition.staticValues = CYValue_staticValues;
+        definition.staticValues = Instance_staticValues;
         definition.staticFunctions = Instance_staticFunctions;
+        definition.hasProperty = &Instance_hasProperty;
         definition.getProperty = &Instance_getProperty;
         definition.setProperty = &Instance_setProperty;
         definition.deleteProperty = &Instance_deleteProperty;
         definition.getPropertyNames = &Instance_getPropertyNames;
         definition.callAsConstructor = &Instance_callAsConstructor;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Instance_ = JSClassCreate(&definition);
 
+        definition = kJSClassDefinitionEmpty;
+        definition.className = "Internal";
+        definition.staticFunctions = Internal_staticFunctions;
+        definition.hasProperty = &Internal_hasProperty;
+        definition.getProperty = &Internal_getProperty;
+        definition.setProperty = &Internal_setProperty;
+        definition.getPropertyNames = &Internal_getPropertyNames;
+        definition.finalize = &Finalize;
+        Internal_ = JSClassCreate(&definition);
+
         definition = kJSClassDefinitionEmpty;
         definition.className = "Pointer";
+        definition.staticValues = Pointer_staticValues;
         definition.staticFunctions = Pointer_staticFunctions;
         definition.getProperty = &Pointer_getProperty;
         definition.setProperty = &Pointer_setProperty;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Pointer_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
@@ -2775,15 +3008,16 @@ JSGlobalContextRef CYGetJSContext() {
         //definition.staticValues = Selector_staticValues;
         definition.staticFunctions = Selector_staticFunctions;
         definition.callAsFunction = &Selector_callAsFunction;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Selector_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
         definition.className = "Struct";
+        definition.staticFunctions = Struct_staticFunctions;
         definition.getProperty = &Struct_getProperty;
         definition.setProperty = &Struct_setProperty;
         definition.getPropertyNames = &Struct_getPropertyNames;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Struct_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
@@ -2792,7 +3026,7 @@ JSGlobalContextRef CYGetJSContext() {
         //definition.getProperty = &Type_getProperty;
         definition.callAsFunction = &Type_callAsFunction;
         definition.callAsConstructor = &Type_callAsConstructor;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         Type_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
@@ -2816,7 +3050,7 @@ JSGlobalContextRef CYGetJSContext() {
         definition.className = "ObjectiveC::Image::Classes";
         definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
         definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
-        definition.finalize = &CYData::Finalize;
+        definition.finalize = &Finalize;
         ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
@@ -2843,7 +3077,7 @@ JSGlobalContextRef CYGetJSContext() {
         CYSetProperty(context, ObjectiveC_, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
 
         CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
-        CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, NULL));
+        CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, &Instance_new));
         CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
         CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
         CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new));