]> git.saurik.com Git - cycript.git/blobdiff - Library.mm
Checkpointing pretty-printing implementation so I can quickly refactor CYNoLeader.
[cycript.git] / Library.mm
index e292511a067cac5d381f86d9d7f291c9a0032f5f..45126dceebb44126a6028e6fa94e3b1929de23b4 100644 (file)
@@ -327,6 +327,7 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
     JSObjectRef object(JSObjectMake(context, _class, NULL));
     JSObjectSetPrototype(context, object, prototype);
 
+    JSValueProtect(context, object);
     value = object;
     return object;
 }
@@ -351,6 +352,7 @@ struct Instance :
     virtual ~Instance() {
         if ((flags_ & Transient) == 0)
             // XXX: does this handle background threads correctly?
+            // XXX: this simply does not work on the console because I'm stupid
             [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
     }
 
@@ -395,19 +397,41 @@ struct Messages :
     }
 };
 
-struct Internal :
+struct CYOwned :
     CYValue
 {
+  private:
+    JSContextRef context_;
     JSObjectRef owner_;
 
-    Internal(id value, JSObjectRef owner) :
+  public:
+    CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
         CYValue(value),
+        context_(context),
         owner_(owner)
+    {
+        JSValueProtect(context_, owner_);
+    }
+
+    virtual ~CYOwned() {
+        JSValueUnprotect(context_, owner_);
+    }
+
+    JSObjectRef GetOwner() const {
+        return owner_;
+    }
+};
+
+struct Internal :
+    CYOwned
+{
+    Internal(id value, JSContextRef context, JSObjectRef owner) :
+        CYOwned(value, context, owner)
     {
     }
 
     static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) {
-        return JSObjectMake(context, Internal_, new Internal(object, owner));
+        return JSObjectMake(context, Internal_, new Internal(object, context, owner));
     }
 
     id GetValue() const {
@@ -446,9 +470,14 @@ void Copy(apr_pool_t *pool, Type &lhs, Type &rhs) {
     if (sig::IsAggregate(rhs.primitive))
         Copy(pool, lhs.data.signature, rhs.data.signature);
     else {
-        if (rhs.data.data.type != NULL) {
-            lhs.data.data.type = new(pool) Type;
-            Copy(pool, *lhs.data.data.type, *rhs.data.data.type);
+        sig::Type *&lht(lhs.data.data.type);
+        sig::Type *&rht(rhs.data.data.type);
+
+        if (rht == NULL)
+            lht = NULL;
+        else {
+            lht = new(pool) Type;
+            Copy(pool, *lht, *rht);
         }
 
         lhs.data.data.size = rhs.data.data.size;
@@ -580,27 +609,24 @@ Type_privateData *Selector_privateData::GetType() const {
 }
 
 struct Pointer :
-    CYValue
+    CYOwned
 {
-    JSObjectRef owner_;
     Type_privateData *type_;
 
-    Pointer(void *value, sig::Type *type, JSObjectRef owner) :
-        CYValue(value),
-        owner_(owner),
+    Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) :
+        CYOwned(value, context, owner),
         type_(new(pool_) Type_privateData(type))
     {
     }
 };
 
 struct Struct_privateData :
-    CYValue
+    CYOwned
 {
-    JSObjectRef owner_;
     Type_privateData *type_;
 
-    Struct_privateData(JSObjectRef owner) :
-        owner_(owner)
+    Struct_privateData(JSContextRef context, JSObjectRef owner) :
+        CYOwned(NULL, context, owner)
     {
     }
 };
@@ -609,7 +635,7 @@ typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap;
 static TypeMap Types_;
 
 JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
-    Struct_privateData *internal(new Struct_privateData(owner));
+    Struct_privateData *internal(new Struct_privateData(context, owner));
     apr_pool_t *pool(internal->pool_);
     Type_privateData *typical(new(pool) Type_privateData(type, ffi));
     internal->type_ = typical;
@@ -651,9 +677,16 @@ struct Closure_privateData :
     JSContextRef context_;
     JSObjectRef function_;
 
-    Closure_privateData(const char *type) :
-        Functor_privateData(type, NULL)
+    Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
+        Functor_privateData(type, NULL),
+        context_(context),
+        function_(function)
     {
+        JSValueProtect(context_, function_);
+    }
+
+    virtual ~Closure_privateData() {
+        JSValueUnprotect(context_, function_);
     }
 };
 
@@ -729,10 +762,17 @@ size_t CYGetIndex(const char *value) {
     return _not(size_t);
 }
 
+// XXX: fix this
+static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value);
+
 size_t CYGetIndex(apr_pool_t *pool, NSString *value) {
     return CYGetIndex(CYPoolCString(pool, value));
 }
 
+size_t CYGetIndex(apr_pool_t *pool, JSStringRef value) {
+    return CYGetIndex(CYPoolCString(pool, value));
+}
+
 bool CYGetOffset(const char *value, ssize_t &index) {
     if (value[0] != '0') {
         char *end;
@@ -1448,9 +1488,15 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) {
     if ((self = [super init]) != nil) {
         object_ = object;
         context_ = context;
+        JSValueProtect(context_, object_);
     } return self;
 }
 
+- (void) dealloc {
+    JSValueUnprotect(context_, object_);
+    [super dealloc];
+}
+
 - (NSObject *) cy$toJSON:(NSString *)key {
     JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
     if (!CYIsCallable(context_, toJSON))
@@ -1511,9 +1557,15 @@ bool CYIsCallable(JSContextRef context, JSValueRef value) {
     if ((self = [super init]) != nil) {
         object_ = object;
         context_ = context;
+        JSValueProtect(context_, object_);
     } return self;
 }
 
+- (void) dealloc {
+    JSValueUnprotect(context_, object_);
+    [super dealloc];
+}
+
 - (NSUInteger) count {
     return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
 }
@@ -1691,23 +1743,24 @@ struct CYInternal :
     }
 };
 
-JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
+static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
     Selector_privateData *internal(new Selector_privateData(sel));
     return JSObjectMake(context, Selector_, internal);
 }
 
-JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
-    Pointer *internal(new Pointer(pointer, type, owner));
+static JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
+    Pointer *internal(new Pointer(pointer, context, owner, type));
     return JSObjectMake(context, Pointer_, internal);
 }
 
-JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
+static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
     Functor_privateData *internal(new Functor_privateData(type, function));
     return JSObjectMake(context, Functor_, internal);
 }
 
-const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
+static const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
     if (pool == NULL) {
+        // XXX: this could be much more efficient
         const char *string([CYCastNSString(NULL, value) UTF8String]);
         return string;
     } else {
@@ -1718,11 +1771,11 @@ const char *CYPoolCString(apr_pool_t *pool, JSStringRef value) {
     }
 }
 
-const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+static const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
     return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, CYJSString(context, value));
 }
 
-bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
+static bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
     return CYGetOffset(CYPoolCString(pool, value), index);
 }
 
@@ -1741,7 +1794,7 @@ bool CYGetOffset(apr_pool_t *pool, JSStringRef value, ssize_t &index) {
     utf8; \
 })
 
-void *CYCastPointer_(JSContextRef context, JSValueRef value) {
+static void *CYCastPointer_(JSContextRef context, JSValueRef value) {
     switch (JSValueGetType(context, value)) {
         case kJSTypeNull:
             return NULL;
@@ -1761,11 +1814,11 @@ void *CYCastPointer_(JSContextRef context, JSValueRef value) {
 }
 
 template <typename Type_>
-_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
+static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
     return reinterpret_cast<Type_>(CYCastPointer_(context, value));
 }
 
-SEL CYCastSEL(JSContextRef context, JSValueRef value) {
+static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
     if (JSValueIsObjectOfClass(context, value, Selector_)) {
         Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
         return reinterpret_cast<SEL>(internal->value_);
@@ -1773,7 +1826,7 @@ SEL CYCastSEL(JSContextRef context, JSValueRef value) {
         return CYCastPointer<SEL>(context, value);
 }
 
-void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
+static void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
     switch (type->primitive) {
         case sig::boolean_P:
             *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
@@ -1851,7 +1904,7 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type
     }
 }
 
-JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) {
+static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) {
     JSValueRef value;
 
     switch (type->primitive) {
@@ -1941,7 +1994,7 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
     return false;
 }
 
-const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) {
+static const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method method) {
     if (method != NULL)
         return method_getTypeEncoding(method);
     else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
@@ -1950,7 +2003,7 @@ const char *CYPoolTypeEncoding(apr_pool_t *pool, Class _class, SEL sel, Method m
         return NULL;
 }
 
-void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
     Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
 
     JSContextRef context(internal->context_);
@@ -1965,7 +2018,7 @@ void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
     CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
 }
 
-void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
     Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
 
     JSContextRef context(internal->context_);
@@ -1982,10 +2035,10 @@ void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
     CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
 }
 
-Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) {
+static Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) {
     // XXX: in case of exceptions this will leak
     // XXX: in point of fact, this may /need/ to leak :(
-    Closure_privateData *internal(new Closure_privateData(type));
+    Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type));
 
     ffi_closure *closure((ffi_closure *) _syscall(mmap(
         NULL, sizeof(ffi_closure),
@@ -2000,13 +2053,10 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function,
 
     internal->value_ = closure;
 
-    internal->context_ = CYGetJSContext();
-    internal->function_ = function;
-
     return internal;
 }
 
-JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
+static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
     Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_));
     return JSObjectMake(context, Functor_, internal);
 }
@@ -2292,7 +2342,7 @@ static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef
     } CYCatch
 }
 
-bool CYIsClass(id self) {
+static bool CYIsClass(id self) {
     // XXX: this is a lame object_isClass
     return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
 }
@@ -2385,10 +2435,10 @@ static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object,
 
 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_;
+    return internal->GetOwner();
 }
 
-bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
+static bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
     Type_privateData *typical(internal->type_);
     sig::Type *type(typical->type_);
     if (type == NULL)
@@ -2440,7 +2490,7 @@ static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, siz
     uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
     base += ffi->size * index;
 
-    JSObjectRef owner(internal->owner_ ?: object);
+    JSObjectRef owner(internal->GetOwner() ?: object);
 
     CYTry {
         return CYFromFFI(context, typical->type_, ffi, base, false, owner);
@@ -2517,7 +2567,7 @@ static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, J
     if (!Index_(pool, internal, property, index, base))
         return NULL;
 
-    JSObjectRef owner(internal->owner_ ?: object);
+    JSObjectRef owner(internal->GetOwner() ?: object);
 
     CYTry {
         return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
@@ -2703,6 +2753,16 @@ static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObject
     free(data);
 }
 
+static JSObjectRef CYMakeType(JSContextRef context, const char *type) {
+    Type_privateData *internal(new Type_privateData(NULL, type));
+    return JSObjectMake(context, Type_, internal);
+}
+
+static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) {
+    Type_privateData *internal(new Type_privateData(type));
+    return JSObjectMake(context, Type_, internal);
+}
+
 static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     if (JSStringIsEqualToUTF8CString(property, "nil"))
         return Instance::Make(context, nil);
@@ -2726,11 +2786,17 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object,
                     sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
                     return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]);
             }
+        if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:name])
+            switch ([[entry objectAtIndex:0] intValue]) {
+                // XXX: implement case 0
+                case 1:
+                    return CYMakeType(context, CYPoolCString(pool, [entry objectAtIndex:1]));
+            }
         return NULL;
     } CYCatch
 }
 
-bool stret(ffi_type *ffi_type) {
+static bool stret(ffi_type *ffi_type) {
     return ffi_type->type == FFI_TYPE_STRUCT && (
         ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
         struct_forward_array[ffi_type->size] != 0
@@ -2740,12 +2806,14 @@ bool stret(ffi_type *ffi_type) {
 extern "C" {
     int *_NSGetArgc(void);
     char ***_NSGetArgv(void);
-    int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
 }
 
 static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
-        NSLog(@"%s", CYCastCString(context, arguments[0]));
+        if (count == 0)
+            NSLog(@"");
+        else
+            NSLog(@"%s", CYCastCString(context, arguments[0]));
         return CYJSUndefined(context);
     } CYCatch
 }
@@ -2854,6 +2922,11 @@ static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef obje
 }
 /* }}} */
 
+static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    JSGarbageCollect(context);
+    return CYJSUndefined(context);
+}
+
 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     JSValueRef setup[count + 2];
     setup[0] = _this;
@@ -2882,7 +2955,7 @@ static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef objec
     return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
 }
 
-JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+static JSObjectRef Selector_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 Selector constructor" userInfo:nil];
@@ -2891,7 +2964,7 @@ JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count,
     } CYCatch
 }
 
-JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
         if (count != 2)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
@@ -2908,25 +2981,47 @@ JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count,
     } CYCatch
 }
 
-JSObjectRef CYMakeType(JSContextRef context, JSObjectRef object, const char *type) {
-    Type_privateData *internal(new Type_privateData(NULL, type));
-    return JSObjectMake(context, Type_, internal);
-}
-
-JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+static JSObjectRef Type_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 Type constructor" userInfo:nil];
         const char *type(CYCastCString(context, arguments[0]));
-        return CYMakeType(context, object, type);
+        return CYMakeType(context, type);
+    } CYCatch
+}
+
+static JSValueRef Type_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+    Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
+    CYTry {
+        sig::Type type;
+
+        if (JSStringIsEqualToUTF8CString(property, "$cyi")) {
+            type.primitive = sig::pointer_P;
+            type.data.data.size = 0;
+        } else {
+            size_t index(CYGetIndex(NULL, property));
+            if (index == _not(size_t))
+                return NULL;
+            type.primitive = sig::array_P;
+            type.data.data.size = index;
+        }
+
+        type.name = NULL;
+        type.flags = 0;
+
+        type.data.data.type = internal->type_;
+
+        return CYMakeType(context, &type);
     } CYCatch
 }
 
 static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
     CYTry {
         if (count != 1)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
-        Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
         sig::Type *type(internal->type_);
         ffi_type *ffi(internal->GetFFI());
         // XXX: alignment?
@@ -2939,17 +3034,26 @@ static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object,
 
 static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
-        if (count > 1)
+        if (count != 0)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
         Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
-        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_, internal->ffi_, NULL);
+
+        sig::Type *type(internal->type_);
+        size_t size;
+
+        if (type->primitive != sig::array_P)
+            size = 0;
+        else {
+            size = type->data.data.size;
+            type = type->data.data.type;
+        }
+
+        void *value(malloc(internal->GetFFI()->size));
+        return CYMakePointer(context, value, type, NULL, NULL);
     } CYCatch
 }
 
-JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+static 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];
@@ -2958,7 +3062,7 @@ JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count,
     } CYCatch
 }
 
-JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
         if (count != 2)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
@@ -2967,7 +3071,7 @@ JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count,
     } CYCatch
 }
 
-JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
     CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
     return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
 }
@@ -3209,8 +3313,9 @@ CYDriver::CYDriver(const std::string &filename) :
     data_(NULL),
     size_(0),
     file_(NULL),
+    strict_(false),
     filename_(filename),
-    source_(NULL)
+    program_(NULL)
 {
     ScannerInit();
 }
@@ -3219,8 +3324,20 @@ CYDriver::~CYDriver() {
     ScannerDestroy();
 }
 
+void CYDriver::Warning(const cy::location &location, const char *message) {
+    if (!strict_)
+        return;
+
+    CYDriver::Error error;
+    error.warning_ = true;
+    error.location_ = location;
+    error.message_ = message;
+    errors_.push_back(error);
+}
+
 void cy::parser::error(const cy::parser::location_type &location, const std::string &message) {
     CYDriver::Error error;
+    error.warning_ = false;
     error.location_ = location;
     error.message_ = message;
     driver.errors_.push_back(error);
@@ -3259,7 +3376,14 @@ const char *CYExecute(apr_pool_t *pool, const char *code) { _pooled
     if (JSValueIsUndefined(context, result))
         return NULL;
 
-    const char *json(CYPoolCCYON(pool, context, result, &exception));
+    const char *json;
+
+    try {
+        json = CYPoolCCYON(pool, context, result, &exception);
+    } catch (const char *error) {
+        return error;
+    }
+
     if (exception != NULL)
         goto error;
 
@@ -3285,7 +3409,7 @@ bool CYSendAll_(int socket, const uint8_t *data, size_t size) {
     return true;
 }
 
-apr_pool_t *Pool_;
+static apr_pool_t *Pool_;
 
 struct CYExecute_ {
     apr_pool_t *pool_;
@@ -3352,7 +3476,8 @@ struct CYClient :
                 size = _not(size_t);
             } else {
                 std::ostringstream str;
-                driver.source_->Show(str);
+                CYOutput out(str);
+                driver.program_->Multiple(out);
                 std::string code(str.str());
                 CYExecute_ execute = {pool, code.c_str()};
                 [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES];
@@ -3493,7 +3618,7 @@ JSGlobalContextRef CYGetJSContext() {
         definition = kJSClassDefinitionEmpty;
         definition.className = "Type";
         definition.staticFunctions = Type_staticFunctions;
-        //definition.getProperty = &Type_getProperty;
+        definition.getProperty = &Type_getProperty;
         definition.callAsFunction = &Type_callAsFunction;
         definition.callAsConstructor = &Type_callAsConstructor;
         definition.finalize = &Finalize;
@@ -3520,7 +3645,6 @@ JSGlobalContextRef CYGetJSContext() {
         definition.className = "ObjectiveC::Image::Classes";
         definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
         definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
-        definition.finalize = &Finalize;
         ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
 
         definition = kJSClassDefinitionEmpty;
@@ -3587,6 +3711,10 @@ JSGlobalContextRef CYGetJSContext() {
 
         class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
 
+        JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
+        CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+        CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction));
+
         CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
         CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
         CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq));
@@ -3599,6 +3727,18 @@ JSGlobalContextRef CYGetJSContext() {
         CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
 
         Result_ = JSStringCreateWithUTF8CString("_");
+
+        JSValueProtect(context, Array_);
+        JSValueProtect(context, Function_);
+        JSValueProtect(context, String_);
+
+        JSValueProtect(context, Instance_prototype_);
+        JSValueProtect(context, Object_prototype_);
+
+        JSValueProtect(context, Array_prototype_);
+        JSValueProtect(context, Array_pop_);
+        JSValueProtect(context, Array_push_);
+        JSValueProtect(context, Array_splice_);
     }
 
     return Context_;