+void Pointer::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+ bool guess(false);
+ *reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value, &guess);
+ if (!guess || pool == NULL || !JSValueIsObject(context, value))
+ return;
+ JSObjectRef object(CYCastJSObject(context, value));
+ if (CYHasProperty(context, object, length_s)) {
+ size_t length(CYArrayLength(context, object));
+ ffi_type *element(type.GetFFI(*pool));
+ size_t size(element->size * length);
+ uint8_t *base(pool->malloc<uint8_t>(size, element->alignment));
+ CYArrayCopy(pool, context, base, length, type, element, value, object);
+ *reinterpret_cast<void **>(data) = base;
+ }
+}
+
+void Array::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+ if (size == 0)
+ return;
+ uint8_t *base(reinterpret_cast<uint8_t *>(data));
+ JSObjectRef object(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
+ CYArrayCopy(pool, context, base, size, type, ffi->elements[0], value, object);
+}
+
+void Enum::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+ return type.PoolFFI(pool, context, ffi, data, value);
+}
+
+void Aggregate::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+ _assert(!overlap);
+ _assert(signature.count != _not(size_t));
+
+ size_t offset(0);
+ uint8_t *base(reinterpret_cast<uint8_t *>(data));
+ JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
+ for (size_t index(0); index != signature.count; ++index) {
+ sig::Element *element(&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");
+ }
+ }
+
+ element->type->PoolFFI(pool, context, field, base + offset, rhs);
+ offset += field->size;
+ CYAlign(offset, field->alignment);
+ }
+}
+
+void Function::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+ _assert(false);
+}
+
+#define CYFromFFI_(Type_) \
+template <> \
+JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \
+ return CYCastJSValue(context, *reinterpret_cast<Type_ *>(data)); \
+}
+
+CYFromFFI_(bool)
+CYFromFFI_(wchar_t)
+CYFromFFI_(float)
+CYFromFFI_(double)
+CYFromFFI_(long double)
+
+CYFromFFI_(signed char)
+CYFromFFI_(signed int)
+CYFromFFI_(signed long int)
+CYFromFFI_(signed long long int)
+CYFromFFI_(signed short int)
+
+CYFromFFI_(unsigned char)
+CYFromFFI_(unsigned int)
+CYFromFFI_(unsigned long int)
+CYFromFFI_(unsigned long long int)
+CYFromFFI_(unsigned short int)
+
+#ifdef __SIZEOF_INT128__
+CYFromFFI_(signed __int128)
+CYFromFFI_(unsigned __int128)
+#endif
+
+template <>
+JSValueRef Primitive<char>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ uint16_t string(uint8_t(*reinterpret_cast<char *>(data)));
+ JSValueRef value(CYCastJSValue(context, CYJSString(CYUTF16String(&string, 1))));
+ JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("String")), 1, &value));
+ CYSetProperty(context, typed, cyt_s, CYMakeType(context, sig::Primitive<char>()), kJSPropertyAttributeDontEnum);
+ CYSetPrototype(context, typed, CYGetCachedValue(context, CYJSString("Character_prototype")));
+ return typed;
+}
+
+JSValueRef Void::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ return CYJSUndefined(context);
+}
+
+JSValueRef Unknown::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ _assert(false);
+}
+
+JSValueRef String::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ if (char *value = *reinterpret_cast<char **>(data))
+ return CYPrivate<CString>::Make(context, value, context, owner);
+ return CYJSNull(context);
+}
+
+JSValueRef Bits::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ _assert(false);
+}
+
+JSValueRef Pointer::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ if (void *value = *reinterpret_cast<void **>(data))
+ return CYMakePointer(context, value, type, NULL, owner);
+ return CYJSNull(context);
+}
+
+JSValueRef Array::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ return CYPrivate<CArray>::Make(context, data, size, type, ffi->elements[0], context, owner);
+}
+
+JSValueRef Enum::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ return type.FromFFI(context, ffi, data, initialize, owner);
+}
+
+JSValueRef Aggregate::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ _assert(!overlap);
+ _assert(signature.count != _not(size_t));
+ return CYPrivate<Struct_privateData>::Make(context, data, *this, ffi, context, owner);
+}
+
+JSValueRef Function::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+ return CYMakeFunctor(context, reinterpret_cast<void (*)()>(data), variadic, signature);
+}
+
+}
+