}
static JSClassRef Instance_;
+
static JSClassRef ArrayInstance_;
+static JSClassRef FunctionInstance_;
static JSClassRef ObjectInstance_;
static JSClassRef StringInstance_;
-static JSClassRef *Instances_[] = {
- &Instance_,
- &ArrayInstance_,
- &ObjectInstance_,
- &StringInstance_,
-};
-
static JSClassRef Internal_;
static JSClassRef Message_;
static JSClassRef Messages_;
#endif
static Class NSArray_;
+static Class NSBlock_;
static Class NSDictionary_;
static Class NSString_;
static Class Object_;
static Type_privateData *Object_type;
static Type_privateData *Selector_type;
-static bool CYValueIsObjectOfClassInstance(JSContextRef context, JSValueRef value) {
- for (size_t i(0); i != sizeof(Instances_) / sizeof(Instances_[0]); ++i)
- if (JSValueIsObjectOfClass(context, value, *Instances_[i]))
- return true;
- return false;
-}
-
Type_privateData *Instance::GetType() const {
return Object_type;
}
if (self == NSArray_)
prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype"));
+ else if (self == NSBlock_)
+ prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype"));
else if (self == NSDictionary_)
prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype"));
else if (self == NSString_)
@interface NSObject (Cycript)
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
- (JSType) cy$JSType;
- (NSObject *) cy$toJSON:(NSString *)key;
- (NSString *) cy$toCYON;
-- (NSString *) cy$toKey;
- (bool) cy$hasProperty:(NSString *)name;
- (NSObject *) cy$getProperty:(NSString *)name;
@protocol Cycript
- (id) cy$box;
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
@end
NSString *CYCastNSCYON(id value) {
}
NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
- if (!CYValueIsObjectOfClassInstance(context, object))
+ if (!JSValueIsObjectOfClass(context, object, Instance_))
return CYCastNSObject_(pool, context, object);
else {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
@end
/* }}} */
+/* Bridge: NSBlock {{{ */
+#ifdef __APPLE__
+@interface NSBlock
+- (void) invoke;
+@end
+#endif
+/* }}} */
/* Bridge: NSBoolNumber {{{ */
#ifndef __APPLE__
@implementation NSBoolNumber (Cycript)
return [self boolValue] ? @"@true" : @"@false";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return CYCastJSValue(context, (bool) [self boolValue]);
} CYObjectiveCatch }
[json appendString:@","];
else
comma = true;
- [json appendString:[key cy$toKey]];
+ [json appendString:CYCastNSCYON(key)];
[json appendString:@":"];
NSObject *object([self objectForKey:key]);
[json appendString:CYCastNSCYON(object)];
return [self cy$JSType] != kJSTypeBoolean ? [NSString stringWithFormat:@"@%@", self] : [self boolValue] ? @"@true" : @"@false";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
} CYObjectiveCatch }
return self;
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return NULL;
} CYObjectiveCatch }
return [[self cy$toJSON:@""] cy$toCYON];
}
-- (NSString *) cy$toKey {
- return [self cy$toCYON];
-}
-
- (bool) cy$hasProperty:(NSString *)name {
return false;
}
return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
}
-- (NSString *) cy$toKey {
- if (CYIsKey(CYCastUTF8String(self)))
- return self;
- return [self cy$toCYON];
-}
-
- (bool) cy$hasProperty:(NSString *)name {
if ([name isEqualToString:@"length"])
return true;
return false;
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return CYCastJSValue(context, CYJSString(context, self));
} CYObjectiveCatch }
return @"undefined";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
+- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
return CYJSUndefined(context);
} CYObjectiveCatch }
return value;
} CYCatch }
+static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+ id self(internal->GetValue());
+
+ if (![self isKindOfClass:NSBlock_])
+ CYThrow("non-NSBlock object is not a function");
+
+ struct BlockDescriptor1 {
+ unsigned long int reserved;
+ unsigned long int size;
+ };
+
+ struct BlockDescriptor2 {
+ void (*copy_helper)(void *dst, void *src);
+ void (*dispose_helper)(void *src);
+ };
+
+ struct BlockDescriptor3 {
+ const char *signature;
+ const char *layout;
+ };
+
+ struct BlockLiteral {
+ Class isa;
+ int flags;
+ int reserved;
+ void (*invoke)(void *, ...);
+ void *descriptor;
+ } *literal = reinterpret_cast<BlockLiteral *>(self);
+
+ enum {
+ BLOCK_DEALLOCATING = 0x0001,
+ BLOCK_REFCOUNT_MASK = 0xfffe,
+ BLOCK_NEEDS_FREE = 1 << 24,
+ BLOCK_HAS_COPY_DISPOSE = 1 << 25,
+ BLOCK_HAS_CTOR = 1 << 26,
+ BLOCK_IS_GC = 1 << 27,
+ BLOCK_IS_GLOBAL = 1 << 28,
+ BLOCK_HAS_STRET = 1 << 29,
+ BLOCK_HAS_SIGNATURE = 1 << 30,
+ };
+
+ if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
+ uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
+ descriptor += sizeof(BlockDescriptor1);
+ if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
+ descriptor += sizeof(BlockDescriptor2);
+ BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
+
+ if (const char *type = descriptor3->signature) {
+ CYPool pool;
+
+ void *setup[1];
+ setup[0] = &self;
+
+ sig::Signature signature;
+ sig::Parse(pool, &signature, type, &Structor_);
+
+ ffi_cif cif;
+ sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+
+ void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
+ return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function);
+ }
+ }
+
+ if (count != 0)
+ CYThrow("NSBlock without signature field passed arguments");
+
+ CYPoolTry {
+ [self invoke];
+ } CYPoolCatch(NULL);
+
+ return NULL;
+} CYCatch }
+
static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
Class _class(internal->GetValue());
if (!CYIsClass(_class))
return false;
- if (CYValueIsObjectOfClassInstance(context, instance)) {
+ if (JSValueIsObjectOfClass(context, instance, Instance_)) {
Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
// XXX: this isn't always safe
return [linternal->GetValue() isKindOfClass:_class];
self = internal->GetValue();
_class = internal->class_;;
uninitialized = false;
- } else if (CYValueIsObjectOfClassInstance(context, arguments[0])) {
+ } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
self = internal->GetValue();
_class = nil;
}
static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (!CYValueIsObjectOfClassInstance(context, _this))
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
} CYCatch }
static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (!CYValueIsObjectOfClassInstance(context, _this))
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
} CYCatch return /*XXX*/ NULL; }
static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (!CYValueIsObjectOfClassInstance(context, _this))
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
id value(internal->GetValue());
- if (![value respondsToSelector:@selector(cy$JSValueInContext:)])
+ if (![value respondsToSelector:@selector(cy$valueOfInContext:)])
return _this;
- if (JSValueRef result = [value cy$JSValueInContext:context])
+ if (JSValueRef result = [value cy$valueOfInContext:context])
return result;
return _this;
} CYCatch return /*XXX*/ NULL; }
static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (!CYValueIsObjectOfClassInstance(context, _this))
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
} CYCatch return /*XXX*/ NULL; }
static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (!CYValueIsObjectOfClassInstance(context, _this))
+ if (!JSValueIsObjectOfClass(context, _this, Instance_))
return NULL;
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
#endif
NSArray_ = objc_getClass("NSArray");
+ NSBlock_ = objc_getClass("NSBlock");
NSDictionary_ = objc_getClass("NSDictionary");
NSString_ = objc_getClass("NSString");
Object_ = objc_getClass("Object");
definition.deleteProperty = &Instance_deleteProperty;
definition.getPropertyNames = &Instance_getPropertyNames;
definition.callAsConstructor = &Instance_callAsConstructor;
+ definition.callAsFunction = &Instance_callAsFunction;
definition.hasInstance = &Instance_hasInstance;
definition.finalize = &CYFinalize;
Instance_ = JSClassCreate(&definition);
definition.className = "ArrayInstance";
ArrayInstance_ = JSClassCreate(&definition);
+ definition.className = "FunctionInstance";
+ FunctionInstance_ = JSClassCreate(&definition);
+
definition.className = "ObjectInstance";
ObjectInstance_ = JSClassCreate(&definition);
JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype")));
JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype);
+ JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL));
+ JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s)));
+ CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype);
+ JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
+ JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype);
+
JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL));
JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s)));
CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype);
CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
- JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
} CYPoolCatch() }