+const char *CYPoolCCYON(apr_pool_t *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) {
+ JSValueRef toCYON(CYGetProperty(context, object, toCYON_));
+ if (CYIsCallable(context, toCYON)) {
+ JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
+ return CYPoolCString(pool, context, value);
+ }
+
+ JSValueRef toJSON(CYGetProperty(context, object, toJSON_));
+ if (CYIsCallable(context, toJSON)) {
+ JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
+ JSValueRef exception(NULL);
+ const char *cyon(CYPoolCCYON(pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), &exception));
+ CYThrow(context, exception);
+ return cyon;
+ }
+
+ std::ostringstream str;
+
+ str << '{';
+
+ // XXX: this is, sadly, going to leak
+ JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context, object));
+
+ bool comma(false);
+
+ for (size_t index(0), count(JSPropertyNameArrayGetCount(names)); index != count; ++index) {
+ JSStringRef name(JSPropertyNameArrayGetNameAtIndex(names, index));
+ JSValueRef value(CYGetProperty(context, object, name));
+
+ if (comma)
+ str << ',';
+ else
+ comma = true;
+
+ CYUTF8String string(CYPoolUTF8String(pool, context, name));
+ if (CYIsKey(string))
+ str << string.data;
+ else
+ CYStringify(str, string.data, string.size);
+
+ str << ':' << CYPoolCCYON(pool, context, value);
+ }
+
+ str << '}';
+
+ JSPropertyNameArrayRelease(names);
+
+ std::string string(str.str());
+ return apr_pstrmemdup(pool, 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 {
+ CYPool pool;
+ std::ostringstream str;
+
+ str << '[';
+
+ JSValueRef length(CYGetProperty(context, _this, length_));
+ bool comma(false);
+
+ for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
+ JSValueRef value(CYGetProperty(context, _this, index));
+
+ if (comma)
+ str << ',';
+ else
+ comma = true;
+
+ if (!JSValueIsUndefined(context, value))
+ str << CYPoolCCYON(pool, context, value);
+ else {
+ str << ',';
+ comma = false;
+ }
+ }
+
+ str << ']';
+
+ std::string value(str.str());
+ return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
+} CYCatch }
+
+static JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
+ Pointer *internal(new Pointer(pointer, context, owner, type));
+ return JSObjectMake(context, Pointer_, internal);
+}
+
+static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
+ Functor_privateData *internal(new Functor_privateData(type, function));
+ return JSObjectMake(context, Functor_, internal);
+}
+
+static bool CYGetOffset(apr_pool_t *pool, JSContextRef context, JSStringRef value, ssize_t &index) {
+ return CYGetOffset(CYPoolCString(pool, context, value), index);
+}
+
+static void *CYCastPointer_(JSContextRef context, JSValueRef value) {
+ switch (JSValueGetType(context, value)) {
+ case kJSTypeNull:
+ return NULL;
+ /*case kJSTypeString:
+ return dlsym(RTLD_DEFAULT, CYCastCString(context, value));
+ case kJSTypeObject:
+ if (JSValueIsObjectOfClass(context, value, Pointer_)) {
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
+ return internal->value_;
+ }*/
+ default:
+ double number(CYCastDouble(context, value));
+ if (std::isnan(number))
+ throw CYError(context, "cannot convert value to pointer");
+ return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
+ }
+}
+
+template <typename Type_>
+static _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
+ return reinterpret_cast<Type_>(CYCastPointer_(context, value));
+}
+
+#define CYObjectiveTry \
+ try
+#define CYObjectiveCatch \
+ catch (const CYError &error) { \
+ @throw CYCastNSObject(NULL, error.context_, error.value_); \
+ }
+
+#define CYPoolTry { \
+ id _saved(nil); \
+ NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
+ @try
+#define CYPoolCatch(value) \
+ @catch (NSException *error) { \
+ _saved = [error retain]; \
+ throw CYError(context, CYCastJSValue(context, error)); \
+ return value; \
+ } @finally { \
+ [_pool release]; \
+ if (_saved != nil) \
+ [_saved autorelease]; \
+ } \
+}
+
+static void CYPoolFFI(apr_pool_t *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;
+
+#define CYPoolFFI_(primitive, native) \
+ case sig::primitive ## _P: \
+ *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \
+ break;
+
+ CYPoolFFI_(uchar, unsigned char)
+ CYPoolFFI_(char, char)
+ CYPoolFFI_(ushort, unsigned short)
+ CYPoolFFI_(short, short)
+ CYPoolFFI_(ulong, unsigned long)
+ CYPoolFFI_(long, long)
+ CYPoolFFI_(uint, unsigned int)
+ CYPoolFFI_(int, int)
+ CYPoolFFI_(ulonglong, unsigned long long)
+ CYPoolFFI_(longlong, long long)
+ CYPoolFFI_(float, float)
+ CYPoolFFI_(double, double)
+
+ case sig::pointer_P:
+ *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value);
+ break;
+
+ case sig::string_P:
+ *reinterpret_cast<const char **>(data) = CYPoolCString(pool, context, value);
+ break;
+
+ case sig::struct_P: {
+ uint8_t *base(reinterpret_cast<uint8_t *>(data));
+ JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
+ for (size_t index(0); index != type->data.signature.count; ++index) {
+ sig::Element *element(&type->data.signature.elements[index]);
+ ffi_type *field(ffi->elements[index]);
+
+ JSValueRef rhs;
+ if (aggregate == NULL)
+ rhs = value;
+ else {
+ rhs = CYGetProperty(context, aggregate, index);
+ if (JSValueIsUndefined(context, rhs)) {
+ if (element->name != NULL)
+ rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
+ else
+ goto undefined;
+ if (JSValueIsUndefined(context, rhs)) undefined:
+ throw CYError(context, "unable to extract structure value");
+ }
+ }
+
+ CYPoolFFI(pool, context, element->type, field, base, rhs);
+ // XXX: alignment?
+ base += field->size;
+ }
+ } break;
+
+ case sig::void_P:
+ break;
+
+ default:
+ if (hooks_ != NULL && hooks_->PoolFFI != NULL)
+ if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value))
+ return;
+
+ fprintf(stderr, "CYPoolFFI(%c)\n", type->primitive);
+ _assert(false);
+ }
+}
+
+static JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) {
+ switch (type->primitive) {
+ case sig::boolean_P:
+ return CYCastJSValue(context, *reinterpret_cast<bool *>(data));
+
+#define CYFromFFI_(primitive, native) \
+ case sig::primitive ## _P: \
+ return CYCastJSValue(context, *reinterpret_cast<native *>(data)); \
+
+ CYFromFFI_(uchar, unsigned char)
+ CYFromFFI_(char, char)
+ CYFromFFI_(ushort, unsigned short)
+ CYFromFFI_(short, short)
+ CYFromFFI_(ulong, unsigned long)
+ CYFromFFI_(long, long)
+ CYFromFFI_(uint, unsigned int)
+ CYFromFFI_(int, int)
+ CYFromFFI_(ulonglong, unsigned long long)
+ CYFromFFI_(longlong, long long)
+ CYFromFFI_(float, float)
+ CYFromFFI_(double, double)
+
+ case sig::pointer_P:
+ if (void *pointer = *reinterpret_cast<void **>(data))
+ return CYMakePointer(context, pointer, type->data.data.type, ffi, owner);
+ else goto null;
+
+ case sig::string_P:
+ if (char *utf8 = *reinterpret_cast<char **>(data))
+ return CYCastJSValue(context, utf8);
+ else goto null;
+
+ case sig::struct_P:
+ return CYMakeStruct(context, data, type, ffi, owner);
+ case sig::void_P:
+ return CYJSUndefined(context);
+
+ null:
+ return CYJSNull(context);
+ default:
+ if (hooks_ != NULL && hooks_->FromFFI != NULL)
+ if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner))
+ return value;
+
+ fprintf(stderr, "CYFromFFI(%c)\n", type->primitive);
+ _assert(false);
+ }
+}
+
+static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+ Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
+
+ JSContextRef context(internal->context_);
+
+ size_t count(internal->cif_.nargs);
+ JSValueRef values[count];
+
+ for (size_t index(0); index != count; ++index)
+ values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
+
+ JSValueRef value(CYCallAsFunction(context, internal->function_, NULL, count, values));
+ CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
+}
+
+static 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(CYGetJSContext(), function, type));
+
+ ffi_closure *closure((ffi_closure *) _syscall(mmap(
+ NULL, sizeof(ffi_closure),
+ PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
+ -1, 0
+ )));
+
+ ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal));
+ _assert(status == FFI_OK);
+
+ _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC));
+
+ internal->value_ = closure;
+
+ return internal;
+}
+
+static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const char *type) {
+ Closure_privateData *internal(CYMakeFunctor_(context, function, type, &FunctionClosure_));
+ return JSObjectMake(context, Functor_, internal);
+}
+
+static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) {
+ JSValueRef exception(NULL);
+ bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception));
+ CYThrow(context, exception);
+
+ if (function) {
+ JSObjectRef function(CYCastJSObject(context, value));
+ return CYMakeFunctor(context, function, type);
+ } else {
+ void (*function)()(CYCastPointer<void (*)()>(context, value));
+ return CYMakeFunctor(context, function, type);
+ }
+}
+
+static bool Index_(apr_pool_t *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)
+ return false;
+
+ const char *name(CYPoolCString(pool, context, property));
+ size_t length(strlen(name));
+ double number(CYCastDouble(name, length));
+
+ size_t count(type->data.signature.count);
+
+ if (std::isnan(number)) {
+ if (property == NULL)
+ return false;
+
+ sig::Element *elements(type->data.signature.elements);
+
+ for (size_t local(0); local != count; ++local) {
+ sig::Element *element(&elements[local]);
+ if (element->name != NULL && strcmp(name, element->name) == 0) {
+ index = local;
+ goto base;
+ }
+ }
+
+ return false;
+ } else {
+ index = static_cast<ssize_t>(number);
+ if (index != number || index < 0 || static_cast<size_t>(index) >= count)
+ return false;
+ }
+
+ base:
+ ffi_type **elements(typical->GetFFI()->elements);
+
+ base = reinterpret_cast<uint8_t *>(internal->value_);
+ for (ssize_t local(0); local != index; ++local)
+ base += elements[local]->size;
+
+ return true;
+}
+
+static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { CYTry {
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ ffi_type *ffi(typical->GetFFI());
+
+ uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
+ base += ffi->size * index;
+
+ JSObjectRef owner(internal->GetOwner() ?: object);
+ return CYFromFFI(context, typical->type_, ffi, base, false, owner);
+} CYCatch }
+
+static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ CYPool pool;
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ if (typical->type_ == NULL)
+ return NULL;
+
+ ssize_t offset;
+ if (!CYGetOffset(pool, context, property, offset))
+ return NULL;
+
+ return Pointer_getIndex(context, object, offset, exception);
+}
+
+static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
+ return Pointer_getIndex(context, object, 0, exception);
+}
+
+static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { CYTry {
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ ffi_type *ffi(typical->GetFFI());
+
+ uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
+ base += ffi->size * index;
+
+ CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
+ return true;
+} CYCatch }
+
+static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ CYPool pool;
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ if (typical->type_ == NULL)
+ return NULL;
+
+ ssize_t offset;
+ if (!CYGetOffset(pool, context, property, offset))
+ return NULL;
+
+ return Pointer_setIndex(context, object, offset, value, exception);
+}
+
+static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+ return Pointer_setIndex(context, object, 0, value, exception);
+}
+
+static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this)));
+ Type_privateData *typical(internal->type_);
+ return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this);
+}
+
+static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ ssize_t index;
+ uint8_t *base;
+
+ if (!Index_(pool, context, internal, property, index, base))
+ return NULL;
+
+ JSObjectRef owner(internal->GetOwner() ?: object);
+
+ return CYFromFFI(context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, false, owner);
+} CYCatch }
+
+static bool Struct_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+
+ ssize_t index;
+ uint8_t *base;
+
+ if (!Index_(pool, context, internal, property, index, base))
+ return false;
+
+ CYPoolFFI(NULL, context, typical->type_->data.signature.elements[index].type, typical->GetFFI()->elements[index], base, value);
+ return true;
+} CYCatch }
+
+static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
+ sig::Type *type(typical->type_);
+
+ if (type == NULL)
+ return;
+
+ size_t count(type->data.signature.count);
+ sig::Element *elements(type->data.signature.elements);
+
+ char number[32];
+
+ for (size_t index(0); index != count; ++index) {
+ const char *name;
+ name = elements[index].name;
+
+ if (name == NULL) {
+ sprintf(number, "%lu", index);
+ name = number;
+ }
+
+ JSPropertyNameAccumulatorAddName(names, CYJSString(name));
+ }
+}
+
+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 {
+ if (setups + count != signature->count - 1)
+ throw CYError(context, "incorrect number of arguments to ffi function");
+
+ size_t size(setups + count);
+ void *values[size];
+ memcpy(values, setup, sizeof(void *) * setups);
+
+ for (size_t index(setups); index != size; ++index) {
+ sig::Element *element(&signature->elements[index + 1]);
+ 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]);
+ }
+
+ uint8_t value[cif->rtype->size];
+ ffi_call(cif, function, value, values);
+
+ return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
+} CYCatch }
+
+static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYPool pool;
+ Functor_privateData *internal(reinterpret_cast<Functor_privateData *>(JSObjectGetPrivate(object)));
+ return CYCallFunction(pool, context, 0, NULL, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
+}
+
+static JSObjectRef CYMakeType(JSContextRef context, const char *type) {
+ Type_privateData *internal(new Type_privateData(NULL, type));
+ return JSObjectMake(context, Type_privateData::Class_, internal);
+}
+
+static JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) {
+ Type_privateData *internal(new Type_privateData(type));
+ return JSObjectMake(context, Type_privateData::Class_, internal);
+}
+
+static void *CYCastSymbol(const char *name) {
+ return dlsym(RTLD_DEFAULT, name);
+}
+
+static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+ CYPool pool;
+ CYUTF8String name(CYPoolUTF8String(pool, context, property));
+
+ if (hooks_ != NULL && hooks_->RuntimeProperty != NULL)
+ if (JSValueRef value = (*hooks_->RuntimeProperty)(context, name))
+ return value;
+
+ sqlite3_stmt *statement;
+
+ _sqlcall(sqlite3_prepare(Bridge_,
+ "select "
+ "\"bridge\".\"mode\", "
+ "\"bridge\".\"value\" "
+ "from \"bridge\" "
+ "where"
+ " \"bridge\".\"name\" = ?"
+ " limit 1"
+ , -1, &statement, NULL));
+
+ _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC));
+
+ int mode;
+ const char *value;
+
+ if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
+ mode = -1;
+ value = NULL;
+ } else {
+ mode = sqlite3_column_int(statement, 0);
+ value = sqlite3_column_pooled(pool, statement, 1);
+ }
+
+ _sqlcall(sqlite3_finalize(statement));
+
+ switch (mode) {
+ default:
+ _assert(false);
+ case -1:
+ return NULL;
+
+ case 0:
+ return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL);
+ case 1:
+ return CYMakeFunctor(context, reinterpret_cast<void (*)()>(CYCastSymbol(name.data)), value);
+
+ case 2: {
+ // XXX: this is horrendously inefficient
+ sig::Signature signature;
+ sig::Parse(pool, &signature, value, &Structor_);
+ ffi_cif cif;
+ sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+ return CYFromFFI(context, signature.elements[0].type, cif.rtype, CYCastSymbol(name.data));
+ }
+
+ // XXX: implement case 3
+ case 4:
+ return CYMakeType(context, value);
+ }
+} CYCatch }
+
+static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ if (count != 2)
+ throw CYError(context, "incorrect number of arguments to Functor constructor");
+
+ void *value(CYCastPointer<void *>(context, arguments[0]));
+ const char *type(CYCastCString(context, arguments[1]));
+
+ CYPool pool;
+
+ sig::Signature signature;
+ sig::Parse(pool, &signature, type, &Structor_);
+
+ return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL);
+} CYCatch }
+
+static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ if (count != 1)
+ throw CYError(context, "incorrect number of arguments to Type constructor");
+ const char *type(CYCastCString(context, arguments[0]));
+ return CYMakeType(context, type);
+} CYCatch }
+
+static JSValueRef Type_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
+ sig::Type type;
+
+ if (JSStringIsEqualToUTF8CString(property, "$cyi")) {
+ type.primitive = sig::pointer_P;
+ type.data.data.size = 0;
+ } else {
+ CYPool pool;
+ size_t index(CYGetIndex(pool, context, property));
+ if (index == _not(size_t))
+ return NULL;
+ type.primitive = sig::array_P;
+ type.data.data.size = index;
+ }
+
+ type.name = NULL;
+ type.flags = 0;
+
+ type.data.data.type = internal->type_;
+
+ return CYMakeType(context, &type);
+} CYCatch }
+
+static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
+ if (count != 1)
+ throw CYError(context, "incorrect number of arguments to type cast function");
+ sig::Type *type(internal->type_);
+ ffi_type *ffi(internal->GetFFI());
+ // XXX: alignment?
+ uint8_t value[ffi->size];
+ CYPool pool;
+ CYPoolFFI(pool, context, type, ffi, value, arguments[0]);
+ return CYFromFFI(context, type, ffi, value);
+} CYCatch }
+
+static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ if (count != 0)
+ throw CYError(context, "incorrect number of arguments to type cast function");
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+
+ sig::Type *type(internal->type_);
+ size_t size;
+
+ if (type->primitive != sig::array_P)
+ size = 0;
+ else {
+ size = type->data.data.size;
+ type = type->data.data.type;
+ }
+
+ void *value(malloc(internal->GetFFI()->size));
+ return CYMakePointer(context, value, type, NULL, NULL);
+} CYCatch }
+
+static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ if (count != 2)
+ throw CYError(context, "incorrect number of arguments to Functor constructor");
+ const char *type(CYCastCString(context, arguments[1]));
+ return CYMakeFunctor(context, arguments[0], type);
+} CYCatch }
+
+static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+ return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
+} CYCatch }
+
+static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ return CYValue_callAsFunction_valueOf(context, object, _this, count, arguments, exception);
+}
+
+static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+ char string[32];
+ sprintf(string, "%p", internal->value_);
+
+ return CYCastJSValue(context, string);
+} CYCatch }
+
+static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+ CYPool pool;
+ const char *type(sig::Unparse(pool, internal->type_));
+ return CYCastJSValue(context, CYJSString(type));
+} CYCatch }
+
+static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+ CYPool pool;
+ const char *type(sig::Unparse(pool, internal->type_));
+ size_t size(strlen(type));
+ char *cyon(new(pool) char[12 + size + 1]);
+ memcpy(cyon, "new Type(\"", 10);
+ cyon[12 + size] = '\0';
+ cyon[12 + size - 2] = '"';
+ cyon[12 + size - 1] = ')';
+ memcpy(cyon + 10, type, size);
+ return CYCastJSValue(context, CYJSString(cyon));
+} CYCatch }
+
+static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
+}
+
+static JSStaticValue Pointer_staticValues[2] = {
+ {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, NULL, 0}
+};
+
+static JSStaticFunction Pointer_staticFunctions[4] = {
+ {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+static JSStaticFunction Struct_staticFunctions[2] = {
+ {"$cya", &Struct_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+static JSStaticFunction Functor_staticFunctions[4] = {
+ {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+static JSStaticFunction Type_staticFunctions[4] = {
+ {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {NULL, NULL, 0}
+};
+
+void CYSetArgs(int argc, const char *argv[]) {
+ JSContextRef context(CYGetJSContext());
+ JSValueRef args[argc];
+ for (int i(0); i != argc; ++i)
+ args[i] = CYCastJSValue(context, argv[i]);
+ JSValueRef exception(NULL);
+ JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception));
+ CYThrow(context, exception);
+ CYSetProperty(context, System_, CYJSString("args"), array);
+}
+
+JSObjectRef CYGetGlobalObject(JSContextRef context) {
+ return JSContextGetGlobalObject(context);
+}
+
+const char *CYExecute(apr_pool_t *pool, const char *code) {
+ JSContextRef context(CYGetJSContext());
+ JSValueRef exception(NULL), result;
+
+ void *handle;
+ if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
+ handle = (*hooks_->ExecuteStart)();
+ else
+ handle = NULL;
+
+ const char *json;
+
+ try {
+ result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
+ } catch (const char *error) {
+ return error;
+ }
+
+ if (exception != NULL) { error:
+ result = exception;
+ exception = NULL;
+ }
+
+ if (JSValueIsUndefined(context, result))
+ return NULL;
+
+ try {
+ json = CYPoolCCYON(pool, context, result, &exception);
+ } catch (const char *error) {
+ return error;
+ }
+
+ if (exception != NULL)
+ goto error;
+
+ CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
+
+ if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
+ (*hooks_->ExecuteEnd)(handle);
+ return json;
+}
+
+static apr_pool_t *Pool_;
+
+apr_pool_t *CYGetGlobalPool() {
+ return Pool_;
+}
+
+MSInitialize {
+ JSContextRef context(NULL);
+ _aprcall(apr_initialize());
+ _aprcall(apr_pool_create(&Pool_, NULL));
+ _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_));
+}
+
+void CYThrow(JSContextRef context, JSValueRef value) {
+ if (value == NULL)
+ return;
+ throw CYError(context, value);
+}
+
+CYError::CYError(JSContextRef context, const char *format, ...) {
+ if (context == NULL)
+ context = CYGetJSContext();
+
+ CYPool pool;
+
+ va_list args;
+ va_start (args, format);
+ const char *message(apr_pvsprintf(pool, format, args));
+ va_end (args);
+
+ JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(message))};
+
+ JSValueRef exception(NULL);
+ value_ = JSObjectCallAsConstructor(context, Error_, 1, arguments, &exception);
+ CYThrow(context, exception);
+}
+
+void CYObjectiveC(JSContextRef context, JSObjectRef global);
+
+#ifdef __OBJC__
+/* Objective-C {{{ */
+#include <Foundation/Foundation.h>
+#include "ObjectiveC/Internal.hpp"
+#include "Struct.hpp"
+
+#ifdef __APPLE__
+#include <WebKit/WebScriptObject.h>
+#endif
+
+/* Objective-C Pool Release {{{ */
+apr_status_t CYPoolRelease_(void *data) {
+ id object(reinterpret_cast<id>(data));
+ [object release];
+ return APR_SUCCESS;
+}
+
+id CYPoolRelease_(apr_pool_t *pool, id object) {
+ if (object == nil)
+ return nil;
+ else if (pool == NULL)
+ return [object autorelease];
+ else {
+ apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
+ return object;
+ }
+}
+
+template <typename Type_>
+Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
+ return (Type_) CYPoolRelease_(pool, (id) object);
+}
+/* }}} */
+/* Objective-C Strings {{{ */
+const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
+ if (pool == NULL)
+ return [value UTF8String];
+ else {
+ size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
+ char *string(new(pool) char[size]);
+ if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
+ throw CYError(context, "[NSString getCString:maxLength:encoding:] == NO");
+ return string;
+ }
+}
+
+JSStringRef CYCopyJSString_(NSString *value) {
+#ifdef __APPLE__
+ return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
+#else
+ CYPool pool;
+ return CYCopyJSString(CYPoolCString(pool, value));
+#endif
+}
+
+JSStringRef CYCopyJSString(id value) {
+ if (value == nil)
+ return NULL;
+ // XXX: this definition scares me; is anyone using this?!
+ NSString *string([value description]);
+ return CYCopyJSString_(string);
+}
+
+NSString *CYCopyNSString(const CYUTF8String &value) {
+#ifdef __APPLE__
+ return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
+#else
+ return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
+#endif
+}
+
+NSString *CYCopyNSString(JSStringRef value) {
+#ifdef __APPLE__
+ return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
+#else
+ return CYCopyNSString(CYCastCString_(value));
+#endif
+}
+
+NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
+ return CYCopyNSString(CYJSString(context, value));
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
+ return CYPoolRelease(pool, CYCopyNSString(value));
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
+ const char *name(sel_getName(sel));
+ return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
+}
+
+NSString *CYCastNSString(apr_pool_t *pool, JSStringRef value) {
+ return CYPoolRelease(pool, CYCopyNSString(value));
+}