+ if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+ return Internal::Make(context, self, object);
+
+ CYTry {
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+
+ if (CYInternal *internal = CYInternal::Get(self))
+ if (JSValueRef value = internal->GetProperty(context, property))
+ return value;
+
+ CYPoolTry {
+ if (NSObject *data = [self cy$getProperty:name])
+ return CYCastJSValue(context, data);
+ } CYPoolCatch(NULL)
+
+ const char *string(CYPoolCString(pool, name));
+ Class _class(object_getClass(self));
+
+#ifdef __APPLE__
+ 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);
+ }
+#endif
+
+ if (SEL sel = sel_getUid(string))
+ if (CYImplements(self, _class, sel, true))
+ return CYSendMessage(pool, context, self, sel, 0, NULL, false, exception);
+
+ return NULL;
+ } CYCatch
+}
+
+static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ CYPool pool;
+
+ CYTry {
+ NSString *name(CYCastNSString(pool, property));
+ NSObject *data(CYCastNSObject(pool, context, value));
+
+ CYPoolTry {
+ if ([self cy$setProperty:name to:data])
+ return true;
+ } CYPoolCatch(NULL)
+
+ const char *string(CYPoolCString(pool, name));
+ Class _class(object_getClass(self));
+
+#ifdef __APPLE__
+ if (objc_property_t property = class_getProperty(_class, string)) {
+ PropertyAttributes attributes(property);
+ if (const char *setter = attributes.Setter()) {
+ SEL sel(sel_registerName(setter));
+ JSValueRef arguments[1] = {value};
+ CYSendMessage(pool, context, self, sel, 1, arguments, false, exception);
+ return true;
+ }
+ }
+#endif
+
+ 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, false)) {
+ 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;
+ }
+
+ return false;
+ } CYCatch
+}
+
+static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ CYTry {
+ CYPoolTry {
+ NSString *name(CYCastNSString(NULL, property));
+ return [self cy$deleteProperty:name];
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ CYPool pool;
+ Class _class(object_getClass(self));
+
+ {
+ unsigned int size;
+ objc_property_t *data(class_copyPropertyList(_class, &size));
+ for (size_t i(0); i != size; ++i)
+ 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 *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
+ return value;
+ } CYCatch
+}
+
+static bool CYIsClass(id self) {
+ // XXX: this is a lame object_isClass
+ return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
+}
+
+static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
+ Class _class(internal->GetValue());
+ if (!CYIsClass(_class))
+ return false;
+
+ if (JSValueIsObjectOfClass(context, instance, Instance_)) {
+ Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
+ // XXX: this isn't always safe
+ CYTry {
+ return [linternal->GetValue() isKindOfClass:_class];
+ } CYCatch
+ }
+
+ return false;
+}
+
+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_(Class _class, JSPropertyNameAccumulatorRef names) {
+ if (Class super = class_getSuperclass(_class))
+ Internal_getPropertyNames_(super, names);
+
+ unsigned int size;
+ Ivar *data(class_copyIvarList(_class, &size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
+ free(data);
+}
+
+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));
+
+ Internal_getPropertyNames_(_class, names);
+}
+
+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->GetOwner();
+}
+
+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)
+ return false;
+
+ const char *name(CYPoolCString(pool, property));
+ size_t length(strlen(name));
+ double number(CYCastDouble(name, length));
+
+ size_t count(type->data.signature.count);
+
+ if (std::isnan(number)) {
+ if (property == NULL)
+ return false;
+
+ sig::Element *elements(type->data.signature.elements);
+
+ for (size_t local(0); local != count; ++local) {
+ sig::Element *element(&elements[local]);
+ if (element->name != NULL && strcmp(name, element->name) == 0) {
+ index = local;
+ goto base;
+ }
+ }
+
+ return false;
+ } else {
+ index = static_cast<ssize_t>(number);
+ if (index != number || index < 0 || static_cast<size_t>(index) >= count)
+ return false;
+ }
+
+ base:
+ ffi_type **elements(typical->GetFFI()->elements);
+
+ base = reinterpret_cast<uint8_t *>(internal->value_);
+ for (ssize_t local(0); local != index; ++local)
+ base += elements[local]->size;
+
+ return true;
+}
+
+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_);
+
+ ffi_type *ffi(typical->GetFFI());
+
+ uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
+ base += ffi->size * index;
+
+ JSObjectRef owner(internal->GetOwner() ?: object);
+
+ CYTry {
+ return CYFromFFI(context, typical->type_, ffi, base, false, owner);
+ } CYCatch
+}
+
+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_);
+
+ if (typical->type_ == NULL)
+ return NULL;
+
+ ssize_t offset;
+ if (!CYGetOffset(pool, property, offset))
+ return NULL;
+
+ return Pointer_getIndex(context, object, offset, 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_));
+ base += ffi->size * index;
+
+ CYTry {
+ CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
+ return true;
+ } 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 offset;
+ if (!CYGetOffset(pool, property, offset))
+ return NULL;
+
+ return Pointer_setIndex(context, object, offset, 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)));
+ Type_privateData *typical(internal->type_);
+
+ ssize_t index;
+ uint8_t *base;
+
+ if (!Index_(pool, internal, property, index, base))
+ return NULL;
+
+ JSObjectRef owner(internal->GetOwner() ?: object);
+
+ CYTry {
+ return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
+ } CYCatch
+}
+
+static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ ssize_t index;
+ uint8_t *base;
+
+ if (!Index_(pool, internal, property, index, base))
+ return false;
+
+ CYTry {
+ CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value);
+ return true;
+ } CYCatch
+}
+
+static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+ sig::Type *type(typical->type_);
+
+ if (type == NULL)
+ return;
+
+ size_t count(type->data.signature.count);
+ sig::Element *elements(type->data.signature.elements);
+
+ char number[32];
+
+ for (size_t index(0); index != count; ++index) {
+ const char *name;
+ name = elements[index].name;
+
+ if (name == NULL) {
+ sprintf(number, "%lu", index);
+ name = number;
+ }
+
+ JSPropertyNameAccumulatorAddName(names, CYJSString(name));
+ }
+}
+
+JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
+ CYTry {
+ if (setups + count != signature->count - 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil];
+
+ size_t size(setups + count);
+ void *values[size];
+ memcpy(values, setup, sizeof(void *) * setups);
+
+ for (size_t index(setups); index != size; ++index) {
+ sig::Element *element(&signature->elements[index + 1]);
+ ffi_type *ffi(cif->arg_types[index]);
+ // XXX: alignment?
+ values[index] = new(pool) uint8_t[ffi->size];
+ CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]);
+ }
+
+ uint8_t value[cif->rtype->size];
+ ffi_call(cif, function, value, values);
+
+ return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
+ } CYCatch
+}
+
+static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+ if (Class _class = NSClassFromString(name))
+ return CYMakeInstance(context, _class, true);
+ return NULL;
+ } CYCatch
+}
+
+static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ size_t size(objc_getClassList(NULL, 0));
+ Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
+
+ get:
+ size_t writ(objc_getClassList(data, size));
+ if (size < writ) {
+ size = writ;
+ if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
+ data = copy;
+ goto get;
+ } else goto done;
+ }
+
+ for (size_t i(0); i != writ; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
+
+ done:
+ free(data);
+}
+
+static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
+
+ CYTry {
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+ unsigned int size;
+ const char **data(objc_copyClassNamesForImage(internal, &size));
+ JSValueRef value;
+ for (size_t i(0); i != size; ++i)
+ if (strcmp(name, data[i]) == 0) {
+ if (Class _class = objc_getClass(name)) {
+ value = CYMakeInstance(context, _class, true);
+ goto free;
+ } else
+ break;
+ }
+ value = NULL;
+ free:
+ free(data);
+ return value;
+ } CYCatch
+}
+
+static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
+ unsigned int size;
+ const char **data(objc_copyClassNamesForImage(internal, &size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
+ free(data);
+}
+
+static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+ const char *name(CYPoolCString(pool, property));
+ unsigned int size;
+ const char **data(objc_copyImageNames(&size));
+ for (size_t i(0); i != size; ++i)
+ if (strcmp(name, data[i]) == 0) {
+ name = data[i];
+ goto free;
+ }
+ name = NULL;
+ free:
+ free(data);
+ if (name == NULL)
+ return NULL;
+ JSObjectRef value(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
+ return value;
+ } CYCatch
+}
+
+static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ unsigned int size;
+ const char **data(objc_copyImageNames(&size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
+ free(data);
+}
+
+static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ CYTry {
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+ if (Protocol *protocol = NSProtocolFromString(name))
+ return CYMakeInstance(context, protocol, true);
+ return NULL;
+ } CYCatch
+}
+
+static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ unsigned int size;
+ Protocol **data(objc_copyProtocolList(&size));
+ for (size_t i(0); i != size; ++i)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
+ free(data);
+}
+
+static JSObjectRef CYMakeType(JSContextRef context, const char *type) {
+ Type_privateData *internal(new Type_privateData(NULL, type));
+ return JSObjectMake(context, Type_privateData::Class_, internal);
+}
+
+static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) {
+ Type_privateData *internal(new Type_privateData(type));
+ return JSObjectMake(context, Type_privateData::Class_, internal);
+}
+
+static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ if (JSStringIsEqualToUTF8CString(property, "nil"))
+ return Instance::Make(context, nil);
+
+ CYTry {
+ CYPool pool;
+ NSString *name(CYCastNSString(pool, property));
+ if (Class _class = NSClassFromString(name))
+ return CYMakeInstance(context, _class, true);
+ if (NSMutableArray *entry = [[Bridge_ objectAtIndex:0] objectForKey:name])
+ switch ([[entry objectAtIndex:0] intValue]) {
+ case 0:
+ return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
+ case 1:
+ return CYMakeFunctor(context, reinterpret_cast<void (*)()>([name cy$symbol]), CYPoolCString(pool, [entry objectAtIndex:1]));
+ case 2:
+ // XXX: this is horrendously inefficient
+ sig::Signature signature;
+ sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_);
+ ffi_cif cif;
+ 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
+}
+
+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
+ );
+}
+
+extern "C" {
+ int *_NSGetArgc(void);
+ char ***_NSGetArgv(void);
+}
+
+static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count == 0)
+ NSLog(@"");
+ else
+ NSLog(@"%s", CYCastCString(context, arguments[0]));
+ return CYJSUndefined(context);
+ } CYCatch
+}
+
+JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) {
+ const char *type;
+
+ Class _class(object_getClass(self));
+ if (objc_method *method = class_getInstanceMethod(_class, _cmd))
+ type = method_getTypeEncoding(method);
+ else {
+ CYTry {
+ CYPoolTry {
+ NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
+ if (method == nil)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
+ type = CYPoolCString(pool, [method _typeString]);
+ } CYPoolCatch(NULL)
+ } CYCatch
+ }
+
+ void *setup[2];
+ setup[0] = &self;
+ setup[1] = &_cmd;
+
+ sig::Signature signature;
+ sig::Parse(pool, &signature, type, &Structor_);
+
+ ffi_cif cif;
+ sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+
+ void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend);
+ return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
+}
+
+static size_t Nonce_(0);
+
+static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ char name[16];
+ sprintf(name, "%s%zu", CYCastCString(context, arguments[0]), Nonce_++);
+ return CYCastJSValue(context, name);
+}
+
+static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYPool pool;
+
+ bool uninitialized;
+
+ id self;
+ SEL _cmd;
+
+ CYTry {
+ if (count < 2)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
+
+ if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
+ self = internal->GetValue();
+ uninitialized = internal->IsUninitialized();
+ if (uninitialized)
+ internal->value_ = nil;
+ } else {
+ self = CYCastNSObject(pool, context, arguments[0]);
+ uninitialized = false;
+ }
+
+ if (self == nil)
+ return CYJSNull(context);
+
+ _cmd = CYCastSEL(context, arguments[1]);
+ } CYCatch
+
+ return CYSendMessage(pool, context, self, _cmd, count - 2, arguments + 2, uninitialized, exception);
+}
+
+/* Hook: objc_registerClassPair {{{ */
+// XXX: replace this with associated objects
+
+MSHook(void, CYDealloc, id self, SEL sel) {
+ CYInternal *internal;
+ object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
+ if (internal != NULL)
+ delete internal;
+ _CYDealloc(self, sel);
+}
+
+MSHook(void, objc_registerClassPair, Class _class) {
+ Class super(class_getSuperclass(_class));
+ if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
+ class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
+ MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
+ }
+
+ _objc_registerClassPair(_class);
+}
+
+static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count != 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil];
+ CYPool pool;
+ NSObject *value(CYCastNSObject(pool, context, arguments[0]));
+ if (value == NULL || !CYIsClass(value))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to objc_registerClassPair" userInfo:nil];
+ Class _class((Class) value);
+ $objc_registerClassPair(_class);
+ return CYJSUndefined(context);
+ } CYCatch
+}
+/* }}} */
+
+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;
+ setup[1] = object;
+ memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
+ return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
+}
+
+static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYPool pool;
+ Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
+
+ // XXX: handle Instance::Uninitialized?
+ id self(CYCastNSObject(pool, context, _this));
+
+ void *setup[2];
+ setup[0] = &self;
+ setup[1] = &internal->sel_;
+
+ return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
+}
+
+static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYPool pool;
+ Functor_privateData *internal(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
+ return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
+}
+
+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];
+ const char *name(CYCastCString(context, arguments[0]));
+ return CYMakeSelector(context, sel_registerName(name));
+ } CYCatch
+}
+
+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];
+
+ void *value(CYCastPointer<void *>(context, arguments[0]));
+ const char *type(CYCastCString(context, arguments[1]));
+
+ CYPool pool;
+
+ sig::Signature signature;
+ sig::Parse(pool, &signature, type, &Structor_);
+
+ return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL);
+ } CYCatch
+}
+
+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, 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];
+ sig::Type *type(internal->type_);
+ ffi_type *ffi(internal->GetFFI());
+ // XXX: alignment?
+ uint8_t value[ffi->size];
+ CYPool pool;
+ CYPoolFFI(pool, context, type, ffi, value, arguments[0]);
+ return CYFromFFI(context, type, ffi, value);
+ } CYCatch
+}
+
+static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ 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)));
+
+ 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
+}
+
+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];
+ id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
+ return Instance::Make(context, self);
+ } CYCatch
+}
+
+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];
+ const char *type(CYCastCString(context, arguments[1]));
+ return CYMakeFunctor(context, arguments[0], type);
+ } CYCatch
+}
+
+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_));
+}
+
+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 {
+ return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
+ } CYCatch
+}
+
+static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception);
+}
+
+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 {
+ return CYCastJSValue(context, string);
+ } CYCatch
+}
+
+static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ return Instance::Make(context, object_getClass(internal->GetValue()));
+}
+
+static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+ if (!CYIsClass(self))
+ return CYJSUndefined(context);
+ CYTry {
+ return CYGetClassPrototype(context, self);
+ } CYCatch
+}
+
+static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+ if (class_getInstanceMethod(object_getClass(self), @selector(alloc)) == NULL)
+ return CYJSUndefined(context);
+ return Messages::Make(context, self);
+}
+
+static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
+ CYTry {
+ CYPoolTry {
+ 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) {
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
+ CYTry {
+ CYPoolTry {
+ NSString *key(count == 0 ? nil : CYCastNSString(NULL, CYJSString(context, arguments[0])));
+ // XXX: check for support of cy$toJSON?
+ return CYCastJSValue(context, CYJSString([internal->GetValue() cy$toJSON:key]));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
+ return NULL;
+
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+
+ CYTry {
+ CYPoolTry {
+ return CYCastJSValue(context, CYJSString([internal->GetValue() description]));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+
+ CYTry {
+ return CYCastJSValue(context, sel_getName(internal->GetValue()));
+ } CYCatch
+}
+
+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);
+}
+
+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 {
+ CYPoolTry {
+ return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name]));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ if (count != 1)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
+ CYPool pool;
+ Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+ NSObject *value(CYCastNSObject(pool, context, arguments[0]));
+ if (value == NULL) lookup:
+ // XXX: do a lookup of some kind
+ return CYJSNull(context);
+ else if (!CYIsClass(value))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Selector.type takes a Class" userInfo:nil];
+ else {
+ Class _class((Class) value);
+ SEL sel(internal->GetValue());
+ if (objc_method *method = class_getInstanceMethod(_class, sel)) {
+ const char *type(CYPoolTypeEncoding(pool, _class, sel, method));
+ return type == NULL ? CYJSNull(context) : CYCastJSValue(context, CYJSString(type));
+ } else goto lookup;
+ }
+ } CYCatch
+}
+
+static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+ CYPool pool;
+ const char *type(sig::Unparse(pool, internal->type_));
+ CYPoolTry {
+ return CYCastJSValue(context, CYJSString(type));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+ CYPool pool;
+ const char *type(sig::Unparse(pool, internal->type_));
+ CYPoolTry {
+ return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]]));
+ } CYPoolCatch(NULL)
+ } CYCatch
+}
+
+static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
+}
+
+static JSStaticValue CYValue_staticValues[2] = {
+ {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
+ {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},
+ {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {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},
+ {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+static JSStaticValue Instance_staticValues[5] = {
+ {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | 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},
+ {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+static JSStaticFunction Type_staticFunctions[4] = {
+ {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+void CYSetArgs(int argc, const char *argv[]) {
+ JSContextRef context(CYGetJSContext());
+ JSValueRef args[argc];
+ for (int i(0); i != argc; ++i)