#include <mach/mach.h>
#endif
+#include "Code.hpp"
#include "Error.hpp"
#include "JavaScript.hpp"
#include "String.hpp"
static id (*$objc_getAssociatedObject)(id object, void *key);
static void (*$objc_removeAssociatedObjects)(id object);
+@class NSBlock;
+
struct BlockLiteral {
Class isa;
int flags;
CYExecuteClosure(cif, result, arguments, arg, &BlockAdapter_);
}
-NSObject *CYMakeBlock(JSContextRef context, JSObjectRef function, sig::Signature &signature) {
+NSBlock *CYMakeBlock(JSContextRef context, JSObjectRef function, sig::Signature &signature) {
_assert(__NSMallocBlock__ != Nil);
BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
descriptor->d_.two_.dispose_helper = &CYDisposeBlock;
descriptor->d_.three_.signature = sig::Unparse(*descriptor->internal_->pool_, &signature);
- return reinterpret_cast<NSObject *>(literal);
+ return reinterpret_cast<NSBlock *>(literal);
}
NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) {
}
- (NSString *) cy$toCYON:(bool)objective {
- return [@"#" stringByAppendingString:[[self description] cy$toCYON:true]];
+ return [@"^" stringByAppendingString:[[self description] cy$toCYON:true]];
}
- (bool) cy$hasProperty:(NSString *)name {
ffi_call(cif, function, value, values);
} CYSadCatch() }
-static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
- // XXX: assigning to an indirect id * works for return values, but not for properties and fields
+static NSBlock *CYCastNSBlock(CYPool &pool, JSContextRef context, JSValueRef value, sig::Signature *signature) {
+ if (JSValueIsNull(context, value))
+ return nil;
+ JSObjectRef object(CYCastJSObject(context, value));
- switch (type->primitive) {
- case sig::block_P: {
- _assert(type->data.signature.count != 0);
- sig::Signature signature;
- sig::Copy(*pool, signature, type->data.signature);
+ if (JSValueIsObjectOfClass(context, object, FunctionInstance_))
+ return reinterpret_cast<Instance *>(JSObjectGetPrivate(object))->GetValue();
+
+ if (JSValueIsObjectOfClass(context, object, Instance_)) {
+ _assert(reinterpret_cast<Instance *>(JSObjectGetPrivate(object))->GetValue() == nil);
+ return nil;
+ }
+
+ _assert(JSObjectIsFunction(context, object));
- sig::Element *elements(new(*pool) sig::Element[++signature.count]);
- elements[0] = signature.elements[0];
- memcpy(elements + 2, signature.elements + 1, sizeof(sig::Element) * (signature.count - 2));
- signature.elements = elements;
+ _assert(signature != NULL);
+ _assert(signature->count != 0);
- elements[1].name = NULL;
- elements[1].type = new(*pool) sig::Type();
- elements[1].offset = _not(size_t);
+ sig::Signature modified;
+ modified.count = signature->count + 1;
+ modified.elements = new(pool) sig::Element[modified.count];
- memset(elements[1].type, 0, sizeof(sig::Type));
- elements[1].type->primitive = sig::object_P;
+ modified.elements[0] = signature->elements[0];
+ memcpy(modified.elements + 2, signature->elements + 1, sizeof(sig::Element) * (signature->count - 1));
- JSObjectRef function(CYCastJSObject(context, value));
- *reinterpret_cast<id *>(data) = CYMakeBlock(context, function, signature);
- } break;
+ modified.elements[1].name = NULL;
+ modified.elements[1].type = new(pool) sig::Type();
+ modified.elements[1].offset = _not(size_t);
+
+ memset(modified.elements[1].type, 0, sizeof(sig::Type));
+ modified.elements[1].type->primitive = sig::object_P;
+
+ return CYMakeBlock(context, object, modified);
+}
+
+static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
+ // XXX: assigning to an indirect id * works for return values, but not for properties and fields
+
+ switch (type->primitive) {
+ case sig::block_P:
+ // XXX: this function might not handle the idea of a null pool
+ *reinterpret_cast<id *>(data) = CYCastNSBlock(*pool, context, value, &type->data.signature);
+ break;
case sig::object_P:
case sig::typename_P:
return false;
}
-static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
- if (method != NULL)
- return method_getTypeEncoding(method);
-
- const char *name(sel_getName(sel));
- size_t length(strlen(name));
-
- char keyed[length + 2];
- keyed[0] = '6';
- keyed[length + 1] = '\0';
- memcpy(keyed + 1, name, length);
-
- if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
- return entry->value_;
-
- return NULL;
-}
-
static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
JSObjectRef _this(CYCastJSObject(context, values[0]));
return CYCallAsFunction(context, function, _this, count - 2, values + 2);
Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
type = sig::Unparse(pool, &message->signature_);
imp = reinterpret_cast<IMP>(message->GetValue());
- } else {
- objc_method *method(class_getInstanceMethod(_class, sel));
- type = CYPoolTypeEncoding(pool, context, sel, method);
+ } else if (objc_method *method = class_getInstanceMethod(_class, sel)) {
+ type = method_getTypeEncoding(method);
imp = CYMakeMessage(context, value, type);
- }
+ } else _assert(false);
objc_method *method(NULL);
#if OBJC_API_VERSION >= 2
Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
SEL sel(internal->GetValue());
- objc_method *method;
- if (Class _class = CYCastClass(pool, context, arguments[0]))
- method = class_getInstanceMethod(_class, sel);
- else
- method = NULL;
-
- const char *encoding(CYPoolTypeEncoding(pool, context, sel, method));
- if (encoding == NULL)
- return CYJSNull(context);
+ Class _class(_require(CYCastClass(pool, context, arguments[0])));
+ objc_method *method(_require(class_getInstanceMethod(_class, sel)));
+ const char *encoding(method_getTypeEncoding(method));
sig::Signature signature;
sig::Parse(pool, &signature, encoding, &Structor_);
CYPool pool;
CYUTF8String utf8(CYPoolUTF8String(pool, CYUTF16String(*data, *size)));
- utf8 = CYPoolCode(pool, utf8);
+ CYStream stream(utf8.data, utf8.data + utf8.size);
+ utf8 = CYPoolCode(pool, stream);
CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(utf8.data, utf8.size)));
size_t bytes(utf16.size * sizeof(uint16_t));