}
Instance::Instance(id value, Flags flags) :
- CYValue(value),
+ value_(value),
flags_(flags)
{
- if ((flags & Instance::Permanent) == 0)
+ if (IsPermanent());
+ /*else if ([value retainCount] == NSUInteger(-1))
+ flags_ |= Instance::Permanent;*/
+ else
value_ = [value_ retain];
}
Instance::~Instance() {
- if ((flags_ & Permanent) == 0)
+ if (!IsPermanent())
[value_ release];
}
if (initialize) {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
- if ((internal->flags_ & Instance::Uninitialized) != 0) {
- internal->flags_ = static_cast<Instance::Flags>(internal->flags_ & ~Instance::Uninitialized);
- _assert(internal->value_ == nil);
- internal->value_ = value;
+ if (internal->IsUninitialized()) {
+ internal->flags_ &= ~Instance::Uninitialized;
+ if (internal->value_ == nil)
+ internal->value_ = value;
+ else
+ _assert(internal->value_ == value);
}
[value release];
objc_method *method(NULL);
unsigned int size;
objc_method **methods(class_copyMethodList(_class, &size));
+ pool.atexit(free, methods);
+
for (size_t i(0); i != size; ++i)
if (sel_isEqual(method_getName(methods[i]), sel)) {
method = methods[i];
break;
}
- free(methods);
if (method != NULL)
method_setImplementation(method, imp);
static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
+ CYPool pool;
Class _class(internal->value_);
unsigned int size;
objc_method **data(class_copyMethodList(_class, &size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
free(data);
}
-static bool CYHasImplicitProperties(Class _class) {
+static bool CYHasImplicitProperties(JSContextRef context, Class _class) {
+ if (!CYCastBool(context, CYGetCachedValue(context, CYJSString("cydget"))))
+ return false;
// XXX: this is an evil hack to deal with NSProxy; fix elsewhere
if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties)))
return true;
return [_class cy$hasImplicitProperties];
}
+static objc_property_t CYFindProperty(CYPool &pool, Class _class, const char *name) {
+ if (_class == Nil)
+ return NULL;
+ if (objc_property_t property = class_getProperty(_class, name))
+ return property;
+ return NULL;
+
+ /* // XXX: I don't think any of this is required
+ unsigned int count;
+ Protocol **protocols(class_copyProtocolList(_class, &count));
+ // XXX: just implement a scope guard already :/
+ pool.atexit(free, protocols);
+
+ for (unsigned int i(0); i != count; ++i)
+ if (objc_property_t property = protocol_getProperty(protocols[i], name, true, true))
+ return property;
+
+ return CYFindProperty(pool, class_getSuperclass(_class), name); */
+}
+
static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
id self(internal->value_);
const char *string(CYPoolCString(pool, context, name));
- if (class_getProperty(_class, string) != NULL)
+ if (CYFindProperty(pool, _class, string) != NULL)
return true;
- if (CYHasImplicitProperties(_class))
+ if (CYHasImplicitProperties(context, _class))
if (SEL sel = sel_getUid(string))
if (CYImplements(self, _class, sel, true))
return true;
const char *string(CYPoolCString(pool, context, name));
Class _class(object_getClass(self));
- if (objc_property_t property = class_getProperty(_class, string)) {
+ if (objc_property_t property = CYFindProperty(pool, _class, string)) {
PropertyAttributes attributes(property);
SEL sel(sel_registerName(attributes.Getter()));
return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false);
}
- if (CYHasImplicitProperties(_class))
+ if (CYHasImplicitProperties(context, _class))
if (SEL sel = sel_getUid(string))
if (CYImplements(self, _class, sel, true))
return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false);
const char *string(CYPoolCString(pool, context, name));
Class _class(object_getClass(self));
- if (objc_property_t property = class_getProperty(_class, string)) {
+ if (objc_property_t property = CYFindProperty(pool, _class, string)) {
PropertyAttributes attributes(property);
if (const char *setter = attributes.Setter()) {
SEL sel(sel_registerName(setter));
{
unsigned int size;
objc_property_t *data(class_copyPropertyList(_class, &size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
- free(data);
}
- if (CYHasImplicitProperties(_class))
+ if (CYHasImplicitProperties(context, _class))
for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
unsigned int size;
objc_method **data(class_copyMethodList(current, &size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
Instance_getPropertyNames_message(names, data[i]);
- free(data);
}
CYPoolTry {
return false;
}
-static void CYBitField(unsigned &length, unsigned &shift, id self, Ivar ivar, const char *encoding, unsigned offset) {
+static void CYBitField(CYPool &pool, unsigned &length, unsigned &shift, id self, Ivar ivar, const char *encoding, unsigned offset) {
length = CYCastDouble(encoding + 1);
shift = 0;
unsigned int size;
objc_ivar **ivars(class_copyIvarList(object_getClass(self), &size));
+ pool.atexit(free, ivars);
+
for (size_t i(0); i != size; ++i)
if (ivars[i] == ivar)
break;
_assert(encoding[0] == 'b');
shift += CYCastDouble(encoding + 1);
}
- free(ivars);
}
static JSValueRef Interior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
_assert(encoding[0] != '\0');
if (encoding[0] == 'b') {
unsigned length, shift;
- CYBitField(length, shift, self, ivar, encoding, offset);
+ CYBitField(pool, length, shift, self, ivar, encoding, offset);
_assert(shift + length <= sizeof(uintptr_t) * 8);
uintptr_t &field(*reinterpret_cast<uintptr_t *>(data));
uintptr_t mask((1 << length) - 1);
_assert(encoding != NULL);
if (encoding[0] == 'b') {
unsigned length, shift;
- CYBitField(length, shift, self, ivar, encoding, offset);
+ CYBitField(pool, length, shift, self, ivar, encoding, offset);
_assert(shift + length <= sizeof(uintptr_t) * 8);
uintptr_t &field(*reinterpret_cast<uintptr_t *>(data));
uintptr_t mask((1 << length) - 1);
return false;
} CYCatch(false) }
-static void Interior_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
+static void Interior_getPropertyNames_(CYPool &pool, Class _class, JSPropertyNameAccumulatorRef names) {
if (Class super = class_getSuperclass(_class))
- Interior_getPropertyNames_(super, names);
+ Interior_getPropertyNames_(pool, super, names);
unsigned int size;
objc_ivar **data(class_copyIvarList(_class, &size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
- free(data);
}
static void Interior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
id self(internal->value_);
Class _class(object_getClass(self));
- Interior_getPropertyNames_(_class, names);
+ Interior_getPropertyNames_(pool, _class, names);
}
static JSValueRef Interior_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
}
static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ CYPool pool;
+
size_t size;
if (Class *data = CYCopyClassList(size)) {
+ pool.atexit(free, data);
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
- free(data);
}
}
CYPool pool;
const char *name(CYPoolCString(pool, context, property));
+
unsigned int size;
const char **data(objc_copyClassNamesForImage(internal, &size));
+ pool.atexit(free, data);
+
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, Instance::Permanent);
- goto free;
- } else
- break;
+ if (Class _class = objc_getClass(name))
+ return CYMakeInstance(context, _class, Instance::Permanent);
+ else
+ return NULL;
}
- value = NULL;
- free:
- free(data);
- return value;
+
+ return NULL;
} CYCatch(NULL) }
static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
+ CYPool pool;
+
unsigned int size;
const char **data(objc_copyClassNamesForImage(internal, &size));
+ pool.atexit(free, data);
+
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 {
} CYCatch(NULL) }
static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ CYPool pool;
+
unsigned int size;
const char **data(objc_copyImageNames(&size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
- free(data);
}
#endif
} CYCatch(NULL) }
static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ CYPool pool;
+
unsigned int size;
Protocol **data(objc_copyProtocolList(&size));
+ pool.atexit(free, data);
+
for (size_t i(0); i != size; ++i)
JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
- free(data);
}
static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
size_t number;
Class *classes(CYCopyClassList(number));
_assert(classes != NULL);
+ pool.atexit(free, classes);
for (size_t i(0); i != number; ++i)
for (Class current(classes[i]); current != Nil; current = class_getSuperclass(current))
break;
}
- free(classes);
-
for (unsigned i(0); i != size; ++i) {
const malloc_zone_t *zone(reinterpret_cast<const malloc_zone_t *>(zones[i]));
if (zone == NULL || zone->introspect == NULL)
self = internal->value_;
_class = nil;
uninitialized = internal->IsUninitialized();
- if (uninitialized)
+ if (uninitialized && [internal->value_ retainCount] != NSUInteger(-1))
internal->value_ = nil;
} else {
self = CYCastNSObject(&pool, context, arguments[0]);
CYSetProperty(context, cache, CYJSString("id"), CYMakeType(context, sig::Object()), kJSPropertyAttributeDontEnum);
CYSetProperty(context, cache, CYJSString("Class"), CYMakeType(context, sig::Meta()), kJSPropertyAttributeDontEnum);
CYSetProperty(context, cache, CYJSString("SEL"), CYMakeType(context, sig::Selector()), kJSPropertyAttributeDontEnum);
+
+ CYSetProperty(context, cy, CYJSString("cydget"), CYCastJSValue(context, false));
} CYPoolCatch() }
static void *CYObjectiveC_CastSymbol(const char *name) {
_extern void CydgetSetupContext(JSGlobalContextRef context) { CYObjectiveTry_ {
CYSetupContext(context);
+ JSObjectRef global(CYGetGlobalObject(context));
+ JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
+ CYSetProperty(context, cy, CYJSString("cydget"), CYCastJSValue(context, true));
} CYObjectiveCatch }
_extern void CydgetMemoryParse(const uint16_t **data, size_t *size) { try {