From 53ba31e9e6b378d1fbe7bc8eba0e02b7fc358a52 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Wed, 4 Nov 2009 04:04:38 +0000 Subject: [PATCH] Finished implementing array ffi. --- Library.cpp | 38 +++++++++++++++++++++++++++++--------- ObjectiveC/Library.mm | 2 +- cycript.hpp | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Library.cpp b/Library.cpp index 01e3801..029797d 100644 --- a/Library.cpp +++ b/Library.cpp @@ -409,7 +409,7 @@ struct Pointer : Type_privateData *type_; size_t length_; - Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type, size_t length = _not(size_t)) : + Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) : CYOwned(value, context, owner), type_(new(pool_) Type_privateData(type)), length_(length) @@ -671,7 +671,7 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef } CYCatch } JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { - Pointer *internal(new Pointer(pointer, context, owner, type)); + Pointer *internal(new Pointer(pointer, context, owner, length, type)); return JSObjectMake(context, Pointer_, internal); } @@ -725,6 +725,27 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type CYPoolFFI_(float, float) CYPoolFFI_(double, double) + case sig::array_P: { + uint8_t *base(reinterpret_cast(data)); + JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL); + for (size_t index(0); index != type->data.data.size; ++index) { + ffi_type *field(ffi->elements[index]); + + JSValueRef rhs; + if (aggregate == NULL) + rhs = value; + else { + rhs = CYGetProperty(context, aggregate, index); + if (JSValueIsUndefined(context, rhs)) + throw CYJSError(context, "unable to extract array value"); + } + + CYPoolFFI(pool, context, type->data.data.type, field, base, rhs); + // XXX: alignment? + base += field->size; + } + } break; + case sig::pointer_P: *reinterpret_cast(data) = CYCastPointer(context, value); break; @@ -769,8 +790,7 @@ void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value)) return; - fprintf(stderr, "CYPoolFFI(%c)\n", type->primitive); - _assert(false); + CYThrow("unimplemented signature code: '%c''\n", type->primitive); } } @@ -823,7 +843,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner)) return value; - CYThrow("failed conversion from FFI format: '%c'\n", type->primitive); + CYThrow("unimplemented signature code: '%c''\n", type->primitive); } } @@ -1217,17 +1237,17 @@ static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef obje Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); sig::Type *type(internal->type_); - size_t size; + size_t length; if (type->primitive != sig::array_P) - size = 0; + length = _not(size_t); else { - size = type->data.data.size; + length = type->data.data.size; type = type->data.data.type; } void *value(malloc(internal->GetFFI()->size)); - return CYMakePointer(context, value, _not(size_t), type, NULL, NULL); + return CYMakePointer(context, value, length, type, NULL, NULL); } CYCatch } static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index f578112..c3f073a 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -2101,7 +2101,7 @@ static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef ffi = typical->ffi_; } - return CYMakePointer(context, &internal->value_, type, ffi, object); + return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object); } static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { diff --git a/cycript.hpp b/cycript.hpp index a1e1f02..05fc892 100644 --- a/cycript.hpp +++ b/cycript.hpp @@ -142,7 +142,7 @@ extern struct CYHooks *hooks_; char *sqlite3_column_pooled(apr_pool_t *pool, sqlite3_stmt *stmt, int n); -JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner); +JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner); void CYFinalize(JSObjectRef object); -- 2.47.2