-bool CYIsCallable(JSContextRef context, JSValueRef value) {
- return value != NULL && JSValueIsObject(context, value) && JSObjectIsFunction(context, (JSObjectRef) value);
-}
-
-static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- if (count == 0)
- printf("\n");
- else {
- CYPool pool;
- printf("%s\n", CYPoolCString(pool, context, arguments[0]));
- }
-
- return CYJSUndefined(context);
-} CYCatch }
-
-static size_t Nonce_(0);
-
-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_++));
- return CYCastJSValue(context, name);
-}
-
-static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- JSGarbageCollect(context);
- return CYJSUndefined(context);
-}
-
-const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry {
- switch (JSType type = JSValueGetType(context, value)) {
- case kJSTypeUndefined:
- return "undefined";
- case kJSTypeNull:
- return "null";
- case kJSTypeBoolean:
- return CYCastBool(context, value) ? "true" : "false";
-
- case kJSTypeNumber: {
- std::ostringstream str;
- CYNumerify(str, CYCastDouble(context, value));
- std::string value(str.str());
- return apr_pstrmemdup(pool, value.c_str(), value.size());
- } break;
-
- case kJSTypeString: {
- std::ostringstream str;
- 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());
- } break;
-
- case kJSTypeObject:
- return CYPoolCCYON(pool, context, (JSObjectRef) value);
- default:
- throw CYJSError(context, "JSValueGetType() == 0x%x", type);
- }
-} CYCatch }
-
-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_s));
- if (CYIsCallable(context, toCYON)) {
- JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
- _assert(value != NULL);
- return CYPoolCString(pool, context, value);
- }
-
- JSValueRef toJSON(CYGetProperty(context, object, toJSON_s));
- 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_s));
- 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 }
-
-JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
- Pointer *internal(new Pointer(pointer, context, owner, length, type));
- return JSObjectMake(context, Pointer_, internal);
-}
-
-static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const char *type) {
- cy::Functor *internal(new cy::Functor(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);
-}
-
-void *CYCastPointer_(JSContextRef context, JSValueRef value) {
- switch (JSValueGetType(context, value)) {
- case kJSTypeNull:
- return NULL;
- /*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 CYJSError(context, "cannot convert value to pointer");
- return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
- }
-}
-
-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::array_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.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<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 CYJSError(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;
-
- CYThrow("unimplemented signature code: '%c''\n", type->primitive);
- }
-}
-
-JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) {
- 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::array_P:
- if (void *pointer = data)
- return CYMakePointer(context, pointer, type->data.data.size, type->data.data.type, NULL, owner);
- else goto null;
-
- case sig::pointer_P:
- if (void *pointer = *reinterpret_cast<void **>(data))
- return CYMakePointer(context, pointer, _not(size_t), type->data.data.type, NULL, 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;
-
- CYThrow("unimplemented signature code: '%c''\n", type->primitive);
- }
-}
-
-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);
-}
-
-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(context, 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_));
- JSObjectRef object(JSObjectMake(context, Functor_, internal));
- // XXX: see above notes about needing to leak
- JSValueProtect(CYGetJSContext(context), object);
- return object;
-}
-
-JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) {
- return CYCastJSObject(context, CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name));
-}
-
-static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) {
- JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function")));
-