*/
/* }}} */
-#define _GNU_SOURCE
-
#include <substrate.h>
#include "cycript.hpp"
#include "Pooling.hpp"
#include "Struct.hpp"
+#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFLogUtilities.h>
-
#include <JavaScriptCore/JSStringRefCF.h>
+#endif
+
+#include <Foundation/Foundation.h>
+
#include <WebKit/WebScriptObject.h>
#include <sys/mman.h>
}
/* }}} */
/* JavaScript Strings {{{ */
-JSStringRef CYCopyJSString(id value) {
- // XXX: this definition scares me; is anyone using this?!
- return value == NULL ? NULL : JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description]));
-}
-
JSStringRef CYCopyJSString(const char *value) {
return value == NULL ? NULL : JSStringCreateWithUTF8CString(value);
}
return string_;
}
};
+/* }}} */
+/* Objective-C Strings {{{ */
+JSStringRef CYCopyJSString_(NSString *value) {
+#ifdef __APPLE__
+ return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(string));
+#else
+ return CYCopyJSString([value UTF8String]);
+#endif
+}
+
+JSStringRef CYCopyJSString(id value) {
+ if (value == nil)
+ return NULL;
+ // XXX: this definition scares me; is anyone using this?!
+ NSString *string([value description]);
+ return CYCopyJSString_(string);
+}
+#ifdef __APPLE__
CFStringRef CYCopyCFString(JSStringRef value) {
return JSStringCopyCFString(kCFAllocatorDefault, value);
}
CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
return CYCopyCFString(CYJSString(context, value));
}
-
+#endif
/* }}} */
static JSGlobalContextRef Context_;
static JSClassRef Runtime_;
static JSClassRef Selector_;
static JSClassRef Struct_;
-static JSClassRef Type_;
static JSClassRef ObjectiveC_Classes_;
static JSClassRef ObjectiveC_Image_Classes_;
CYValue() {
}
- CYValue(void *value) :
- value_(value)
+ CYValue(const void *value) :
+ value_(const_cast<void *>(value))
{
}
return Instance_prototype_;
// XXX: I need to think through multi-context
- typedef std::map<Class, JSValueRef> CacheMap;
+ typedef std::map<id, JSValueRef> CacheMap;
static CacheMap cache_;
JSValueRef &value(cache_[self]);
static Type_privateData *Object;
static Type_privateData *Selector;
+ static JSClassRef Class;
+
ffi_type *ffi_;
sig::Type *type_;
- (void *) cy$symbol;
@end
+#ifdef __APPLE__
struct PropertyAttributes {
CYPool pool_;
}
};
+#endif
+#ifdef __APPLE__
NSString *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
}
+#endif
/* Bridge: NSArray {{{ */
@implementation NSArray (Cycript)
[json appendString:@"["];
bool comma(false);
+#ifdef __APPLE__
for (id object in self) {
+#else
+ id object;
+ for (size_t index(0), count([self count]); index != count; ++index) {
+ object = [self objectAtIndex:index];
+#endif
if (comma)
[json appendString:@","];
else
}
- (NSObject *) cy$getProperty:(NSString *)name {
- if ([name isEqualToString:@"length"])
- return [NSNumber numberWithUnsignedInteger:[self count]];
+ if ([name isEqualToString:@"length"]) {
+ NSUInteger count([self count]);
+#ifdef __APPLE__
+ return [NSNumber numberWithUnsignedInteger:count];
+#else
+ return [NSNumber numberWithUnsignedInt:count];
+#endif
+ }
size_t index(CYGetIndex(NULL, name));
if (index == _not(size_t) || index >= [self count])
[json appendString:@"{"];
bool comma(false);
+#ifdef __APPLE__
for (id key in self) {
+#else
+ NSEnumerator *keys([self keyEnumerator]);
+ while (id key = [keys nextObject]) {
+#endif
if (comma)
[json appendString:@","];
else
- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
if ([name isEqualToString:@"length"]) {
// XXX: is this not intelligent?
- NSUInteger size([(NSNumber *)value unsignedIntegerValue]);
+ NSNumber *number(reinterpret_cast<NSNumber *>(value));
+#ifdef __APPLE__
+ NSUInteger size([number unsignedIntegerValue]);
+#else
+ NSUInteger size([number unsignedIntValue]);
+#endif
NSUInteger count([self count]);
if (size < count)
[self removeObjectsInRange:NSMakeRange(size, count - size)];
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))
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()) {
return true;
}
}
+#endif
size_t length(strlen(string));
definition.callAsFunction = &Type_callAsFunction;
definition.callAsConstructor = &Type_callAsConstructor;
definition.finalize = &Finalize;
- Type_ = JSClassCreate(&definition);
+ Type_privateData::Class = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
definition.className = "Runtime";
MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
+#ifdef __APPLE__
class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
+#endif
JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
CYSetProperty(context, global, CYJSString("Cycript"), cycript);