JSObjectRef object(JSObjectMake(context, _class, NULL));
JSObjectSetPrototype(context, object, prototype);
+ JSValueProtect(context, object);
value = object;
return object;
}
}
};
-struct Internal :
+struct CYOwned :
CYValue
{
+ private:
+ JSContextRef context_;
JSObjectRef owner_;
- Internal(id value, JSObjectRef owner) :
+ public:
+ CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
CYValue(value),
+ context_(context),
owner_(owner)
+ {
+ JSValueProtect(context_, owner_);
+ }
+
+ virtual ~CYOwned() {
+ JSValueUnprotect(context_, owner_);
+ }
+
+ JSObjectRef GetOwner() const {
+ return owner_;
+ }
+};
+
+struct Internal :
+ CYOwned
+{
+ Internal(id value, JSContextRef context, JSObjectRef owner) :
+ CYOwned(value, context, owner)
{
}
static JSObjectRef Make(JSContextRef context, id object, JSObjectRef owner) {
- return JSObjectMake(context, Internal_, new Internal(object, owner));
+ return JSObjectMake(context, Internal_, new Internal(object, context, owner));
}
id GetValue() const {
}
struct Pointer :
- CYValue
+ CYOwned
{
- JSObjectRef owner_;
Type_privateData *type_;
- Pointer(void *value, sig::Type *type, JSObjectRef owner) :
- CYValue(value),
- owner_(owner),
+ Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) :
+ CYOwned(value, context, owner),
type_(new(pool_) Type_privateData(type))
{
}
};
struct Struct_privateData :
- CYValue
+ CYOwned
{
- JSObjectRef owner_;
Type_privateData *type_;
- Struct_privateData(JSObjectRef owner) :
- owner_(owner)
+ Struct_privateData(JSContextRef context, JSObjectRef owner) :
+ CYOwned(NULL, context, owner)
{
}
};
static TypeMap Types_;
JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
- Struct_privateData *internal(new Struct_privateData(owner));
+ Struct_privateData *internal(new Struct_privateData(context, owner));
apr_pool_t *pool(internal->pool_);
Type_privateData *typical(new(pool) Type_privateData(type, ffi));
internal->type_ = typical;
JSContextRef context_;
JSObjectRef function_;
- Closure_privateData(const char *type) :
- Functor_privateData(type, NULL)
+ Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
+ Functor_privateData(type, NULL),
+ context_(context),
+ function_(function)
{
+ JSValueProtect(context_, function_);
+ }
+
+ virtual ~Closure_privateData() {
+ JSValueUnprotect(context_, function_);
}
};
if ((self = [super init]) != nil) {
object_ = object;
context_ = context;
+ JSValueProtect(context_, object_);
} return self;
}
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
- (NSObject *) cy$toJSON:(NSString *)key {
JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
if (!CYIsCallable(context_, toJSON))
if ((self = [super init]) != nil) {
object_ = object;
context_ = context;
+ JSValueProtect(context_, object_);
} return self;
}
+- (void) dealloc {
+ JSValueUnprotect(context_, object_);
+ [super dealloc];
+}
+
- (NSUInteger) count {
return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
}
}
JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
- Pointer *internal(new Pointer(pointer, type, owner));
+ Pointer *internal(new Pointer(pointer, context, owner, type));
return JSObjectMake(context, Pointer_, internal);
}
Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) {
// XXX: in case of exceptions this will leak
// XXX: in point of fact, this may /need/ to leak :(
- Closure_privateData *internal(new Closure_privateData(type));
+ Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type));
ffi_closure *closure((ffi_closure *) _syscall(mmap(
NULL, sizeof(ffi_closure),
internal->value_ = closure;
- internal->context_ = CYGetJSContext();
- internal->function_ = function;
-
return internal;
}
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->owner_;
+ return internal->GetOwner();
}
bool Index_(apr_pool_t *pool, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
base += ffi->size * index;
- JSObjectRef owner(internal->owner_ ?: object);
+ JSObjectRef owner(internal->GetOwner() ?: object);
CYTry {
return CYFromFFI(context, typical->type_, ffi, base, false, owner);
if (!Index_(pool, internal, property, index, base))
return NULL;
- JSObjectRef owner(internal->owner_ ?: object);
+ JSObjectRef owner(internal->GetOwner() ?: object);
CYTry {
return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
if (JSValueIsUndefined(context, result))
return NULL;
- const char *json(CYPoolCCYON(pool, context, result, &exception));
+ const char *json;
+
+ try {
+ json = CYPoolCCYON(pool, context, result, &exception);
+ } catch (const char *error) {
+ return error;
+ }
+
if (exception != NULL)
goto error;
definition.className = "ObjectiveC::Image::Classes";
definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
- definition.finalize = &Finalize;
ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
definition = kJSClassDefinitionEmpty;
CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
Result_ = JSStringCreateWithUTF8CString("_");
+
+ JSValueProtect(context, Array_);
+ JSValueProtect(context, Function_);
+ JSValueProtect(context, String_);
+
+ JSValueProtect(context, Instance_prototype_);
+ JSValueProtect(context, Object_prototype_);
+
+ JSValueProtect(context, Array_prototype_);
+ JSValueProtect(context, Array_pop_);
+ JSValueProtect(context, Array_push_);
+ JSValueProtect(context, Array_splice_);
}
return Context_;