/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2012 Jay Freeman (saurik)
+ * Copyright (C) 2009-2013 Jay Freeman (saurik)
*/
-/* GNU Lesser General Public License, Version 3 {{{ */
+/* GNU General Public License, Version 3 {{{ */
/*
- * Cycript is free software: you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or (at your
- * option) any later version.
+ * Cycript is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
*
- * Cycript is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
- * License for more details.
+ * Cycript is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * You should have received a copy of the GNU Lesser General Public License
+ * You should have received a copy of the GNU General Public License
* along with Cycript. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include "Internal.hpp"
#include <dlfcn.h>
-#include <iconv.h>
#include "cycript.hpp"
#include <sys/mman.h>
#include <iostream>
-#include <ext/stdio_filebuf.h>
#include <set>
#include <map>
#include <iomanip>
return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value));
}
-CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
+CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) {
return CYPoolUTF8String(pool, CYCastUTF16String(value));
}
-const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
+const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value) {
CYUTF8String utf8(CYPoolUTF8String(pool, context, value));
_assert(memchr(utf8.data, '\0', utf8.size) == NULL);
return utf8.data;
}
-const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value) {
return JSValueIsNull(context, value) ? NULL : CYPoolCString(pool, context, CYJSString(context, value));
}
/* }}} */
/* Index Offsets {{{ */
-size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
+size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) {
return CYGetIndex(CYPoolUTF8String(pool, context, value));
}
/* }}} */
void CYFinalize(JSObjectRef object) {
CYData *internal(reinterpret_cast<CYData *>(JSObjectGetPrivate(object)));
+ _assert(internal->count_ != _not(unsigned));
if (--internal->count_ == 0)
delete internal;
}
-void Structor_(apr_pool_t *pool, sig::Type *&type) {
+void Structor_(CYPool &pool, sig::Type *&type) {
if (
type->primitive == sig::pointer_P &&
type->data.data.type != NULL &&
Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, sig::Type *type) :
CYOwned(value, context, owner),
- type_(new(pool_) Type_privateData(type)),
+ type_(new(*pool_) Type_privateData(type)),
length_(length)
{
}
JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
Struct_privateData *internal(new Struct_privateData(context, owner));
- apr_pool_t *pool(internal->pool_);
+ CYPool &pool(*internal->pool_);
Type_privateData *typical(new(pool) Type_privateData(type, ffi));
internal->type_ = typical;
internal->value_ = data;
else {
size_t size(typical->GetFFI()->size);
- void *copy(apr_palloc(internal->pool_, size));
+ void *copy(internal->pool_->malloc<void>(size));
memcpy(copy, data, size);
internal->value_ = copy;
}
static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYPool pool;
- const char *name(apr_psprintf(pool, "%s%"APR_SIZE_T_FMT"", CYPoolCString(pool, context, arguments[0]), Nonce_++));
+ const char *name(pool.strcat(CYPoolCString(pool, context, arguments[0]), pool.itoa(Nonce_++), NULL));
return CYCastJSValue(context, name);
}
return CYJSUndefined(context);
}
-const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry {
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry {
switch (JSType type = JSValueGetType(context, value)) {
case kJSTypeUndefined:
return "undefined";
std::ostringstream str;
CYNumerify(str, CYCastDouble(context, value));
std::string value(str.str());
- return apr_pstrmemdup(pool, value.c_str(), value.size());
+ return pool.strmemdup(value.c_str(), value.size());
} break;
case kJSTypeString: {
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value)));
CYStringify(str, string.data, string.size);
std::string value(str.str());
- return apr_pstrmemdup(pool, value.c_str(), value.size());
+ return pool.strmemdup(value.c_str(), value.size());
} break;
case kJSTypeObject:
}
} CYCatch(NULL) }
-const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) {
JSValueRef exception(NULL);
const char *cyon(CYPoolCCYON(pool, context, value, &exception));
CYThrow(context, exception);
return cyon;
}
-const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) {
JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
if (CYIsCallable(context, toCYON)) {
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
JSPropertyNameArrayRelease(names);
std::string string(str.str());
- return apr_pstrmemdup(pool, string.c_str(), string.size());
+ return pool.strmemdup(string.c_str(), string.size());
}
static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
return JSObjectMake(context, Functor_, internal);
}
-static bool CYGetOffset(apr_pool_t *pool, JSContextRef context, JSStringRef value, ssize_t &index) {
+static bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index) {
return CYGetOffset(CYPoolCString(pool, context, value), index);
}
}
}
-void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
+void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) {
switch (type->primitive) {
case sig::boolean_P:
*reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value);
break;
case sig::string_P:
- *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value);
+ _assert(pool != NULL);
+ *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value);
break;
case sig::struct_P: {
}
}
-static bool Index_(apr_pool_t *pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
+static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *internal, JSStringRef property, ssize_t &index, uint8_t *&base) {
Type_privateData *typical(internal->type_);
sig::Type *type(typical->type_);
if (type == NULL)
}
}
-JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry {
+JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { CYTry {
if (setups + count != signature->count - 1)
throw CYJSError(context, "incorrect number of arguments to ffi function");
ffi_type *ffi(cif->arg_types[index]);
// XXX: alignment?
values[index] = new(pool) uint8_t[ffi->size];
- CYPoolFFI(pool, context, element->type, ffi, values[index], arguments[index - setups]);
+ CYPoolFFI(&pool, context, element->type, ffi, values[index], arguments[index - setups]);
}
uint8_t value[cif->rtype->size];
// XXX: alignment?
uint8_t value[ffi->size];
CYPool pool;
- CYPoolFFI(pool, context, type, ffi, value, arguments[0]);
+ CYPoolFFI(&pool, context, type, ffi, value, arguments[0]);
return CYFromFFI(context, type, ffi, value);
} CYCatch(NULL) }
const char *type(sig::Unparse(pool, internal->type_));
std::ostringstream str;
CYStringify(str, type, strlen(type));
- char *cyon(apr_pstrcat(pool, "new Type(", str.str().c_str(), ")", NULL));
+ char *cyon(pool.strcat("new Type(", str.str().c_str(), ")", NULL));
return CYCastJSValue(context, CYJSString(cyon));
} CYCatch(NULL) }
return JSContextGetGlobalObject(context);
}
-const char *CYExecute(apr_pool_t *pool, CYUTF8String code) {
+const char *CYExecute(CYPool &pool, CYUTF8String code) {
JSContextRef context(CYGetJSContext());
JSValueRef exception(NULL), result;
initialized_ = true;
else return;
- CYInitializeStatic();
-
JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
JSClassDefinition definition;
throw CYJSError(context, value);
}
-const char *CYJSError::PoolCString(apr_pool_t *pool) const {
+const char *CYJSError::PoolCString(CYPool &pool) const {
// XXX: this used to be CYPoolCString
return CYPoolCCYON(pool, context_, value_);
}
va_list args;
va_start(args, format);
- const char *message(apr_pvsprintf(pool, format, args));
+ // XXX: there might be a beter way to think about this
+ const char *message(pool.vsprintf(64, format, args));
va_end(args);
value_ = CYCastJSError(context, message);