From 9e7cf36beb748d738fc51d2d8e3e4fe2e2a6ea72 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Fri, 8 Jan 2016 16:56:55 -0800 Subject: [PATCH] Casting an array/string pointer shouldn't copy it. --- Execute.cpp | 26 ++++++++++++-------------- Internal.hpp | 33 +++++++++++++++++++++++++++++++++ Pooling.hpp | 1 + 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Execute.cpp b/Execute.cpp index 57c812f..70fce72 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -251,12 +251,6 @@ struct CArray : type_(new(*pool_) Type_privateData(type, ffi)), length_(length) { - if (owner == NULL) { - size_t size(ffi->size * length); - void *copy(pool_->malloc(size, ffi->alignment)); - memcpy(copy, value_, size); - value_ = copy; - } } }; @@ -270,8 +264,6 @@ struct CString : value_(value), owner_(context, owner) { - if (owner == NULL) - value_ = pool_->strdup(value_); } }; @@ -1272,7 +1264,8 @@ JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, voi element.type->PoolFFI(&pool, context, ffi, values[index], arguments[index - setups]); } - uint8_t *value(pool.malloc(std::max(cif->rtype->size, sizeof(ffi_arg)), std::max(cif->rtype->alignment, alignof(ffi_arg)))); + CYBuffer buffer(context); + uint8_t *value(buffer->malloc(std::max(cif->rtype->size, sizeof(ffi_arg)), std::max(cif->rtype->alignment, alignof(ffi_arg)))); void (*call)(CYPool &, JSContextRef, ffi_cif *, void (*)(), void *, void **) = &CYCallFunction; // XXX: this only supports one hook, but it is a bad idea anyway @@ -1281,7 +1274,7 @@ JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, voi call = hook->CallFunction; call(pool, context, cif, function, value, values); - return signature.elements[0].type->FromFFI(context, cif->rtype, value, initialize); + return signature.elements[0].type->FromFFI(context, cif->rtype, value, initialize, buffer); } static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { @@ -1667,14 +1660,14 @@ static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, if (sig::Function *function = dynamic_cast(internal->type_)) return CYMakeFunctor(context, arguments[0], function->variadic, function->signature); - CYPool pool; + CYBuffer buffer(context); sig::Type *type(internal->type_); ffi_type *ffi(internal->GetFFI()); - void *data(pool.malloc(ffi->size, ffi->alignment)); + void *data(buffer->malloc(ffi->size, ffi->alignment)); - type->PoolFFI(&pool, context, ffi, data, arguments[0]); - JSValueRef value(type->FromFFI(context, ffi, data)); + type->PoolFFI(buffer, context, ffi, data, arguments[0]); + JSValueRef value(type->FromFFI(context, ffi, data, false, buffer)); if (JSValueGetType(context, value) == kJSTypeNumber) { JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); @@ -2144,6 +2137,11 @@ void CYInitializeDynamic() { definition.finalize = &CYFinalize; CYPrivate::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "Root"; + definition.finalize = &CYFinalize; + CYPrivate::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "Struct"; definition.staticFunctions = Struct_staticFunctions; diff --git a/Internal.hpp b/Internal.hpp index 4e37d7a..7e6b1fd 100644 --- a/Internal.hpp +++ b/Internal.hpp @@ -42,6 +42,12 @@ sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate); struct CYRoot : CYData { + // XXX: without this, CYData is zero-initialized?! + CYRoot() : + CYData() + { + } + _finline JSValueRef GetPrototype(JSContextRef context) const { return NULL; } @@ -198,6 +204,33 @@ struct CYProtect { } }; +class CYBuffer { + private: + JSObjectRef owner_; + CYPool *pool_; + + public: + CYBuffer(JSContextRef context) : + owner_(CYPrivate::Make(context)), + pool_(CYPrivate::Get(context, owner_)->pool_) + { + auto internal(CYPrivate::Get(context, owner_)); + internal->pool_->malloc(10); + } + + operator JSObjectRef() const { + return owner_; + } + + operator CYPool *() const { + return pool_; + } + + CYPool *operator ->() const { + return pool_; + } +}; + namespace cy { struct Functor : CYRoot diff --git a/Pooling.hpp b/Pooling.hpp index fc9d7eb..1c155fe 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -235,6 +235,7 @@ struct CYData { CYData() : count_(1) { + _assert(pool_ != NULL); } CYData(CYPool &pool) : -- 2.45.2