}
};
-struct Functor_privateData :
- Pointer_privateData
-{
- sig::Signature signature_;
- ffi_cif cif_;
-
- Functor_privateData(const char *type, void (*value)()) :
- Pointer_privateData(reinterpret_cast<void *>(value))
- {
- sig::Parse(pool_, &signature_, type);
- sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
- }
-};
-
-struct ffoData :
- Functor_privateData
-{
- JSContextRef context_;
- JSObjectRef function_;
-
- ffoData(const char *type) :
- Functor_privateData(type, NULL)
- {
- }
-};
-
struct Selector_privateData : Pointer_privateData {
Selector_privateData(SEL value) :
Pointer_privateData(value)
}
+struct CStringMapLess :
+ std::binary_function<const char *, const char *, bool>
+{
+ _finline bool operator ()(const char *lhs, const char *rhs) const {
+ return strcmp(lhs, rhs) < 0;
+ }
+};
+
struct Type_privateData {
sig::Type type_;
ffi_type ffi_;
}
};
-struct CStringMapLess :
- std::binary_function<const char *, const char *, bool>
-{
- _finline bool operator ()(const char *lhs, const char *rhs) const {
- return strcmp(lhs, rhs) < 0;
- }
-};
-
typedef std::map<const char *, Type_privateData *, CStringMapLess> TypeMap;
static TypeMap Types_;
return JSObjectMake(context, Struct_, internal);
}
+void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *type) {
+ CYPoolTry {
+ if (NSMutableArray *entry = [[Bridge_ objectAtIndex:2] objectForKey:[NSString stringWithUTF8String:name]]) {
+ switch ([[entry objectAtIndex:0] intValue]) {
+ case 0:
+ static CYPool Pool_;
+ sig::Parse(Pool_, &type->data.signature, [[entry objectAtIndex:1] UTF8String], &Structor_);
+ break;
+ }
+ }
+ } CYPoolCatch()
+}
+
+struct Functor_privateData :
+ Pointer_privateData
+{
+ sig::Signature signature_;
+ ffi_cif cif_;
+
+ Functor_privateData(const char *type, void (*value)()) :
+ Pointer_privateData(reinterpret_cast<void *>(value))
+ {
+ sig::Parse(pool_, &signature_, type, &Structor_);
+ sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
+ }
+};
+
+struct ffoData :
+ Functor_privateData
+{
+ JSContextRef context_;
+ JSObjectRef function_;
+
+ ffoData(const char *type) :
+ Functor_privateData(type, NULL)
+ {
+ }
+};
+
JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
if (!transient)
object = [object retain];
- (NSObject *) cy$toJSON:(NSString *)key;
- (NSString *) cy$toCYON;
+- (NSString *) cy$toKey;
- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient;
return [[self cy$toJSON:@""] cy$toCYON];
}
+- (NSString *) cy$toKey {
+ return [self cy$toCYON];
+}
+
- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
return CYMakeInstance(context, self, transient);
}
- (NSString *) cy$toCYON {
NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
- [json appendString:@"({"];
+ [json appendString:@"{"];
bool comma(false);
for (id key in self) {
[json appendString:@","];
else
comma = true;
- [json appendString:[key cy$toCYON]];
+ [json appendString:[key cy$toKey]];
[json appendString:@":"];
NSObject *object([self objectForKey:key]);
[json appendString:[object cy$toCYON]];
}
- [json appendString:@"})"];
+ [json appendString:@"}"];
return json;
}
}
- (NSString *) cy$toCYON {
+ // XXX: this should use the better code from Output.cpp
CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self));
CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0);
return [reinterpret_cast<const NSString *>(json) autorelease];
}
+- (NSString *) cy$toKey {
+ const char *value([self UTF8String]);
+ size_t size(strlen(value));
+
+ if (size == 0 || !WordStartRange_[value[0]])
+ goto cyon;
+ for (size_t i(1); i != size; ++i)
+ if (!WordEndRange_[value[i]])
+ goto cyon;
+ return self;
+
+ cyon:
+ return [self cy$toCYON];
+}
+
- (void *) cy$symbol {
CYPool pool;
return dlsym(RTLD_DEFAULT, CYPoolCString(pool, self));
case sig::struct_P: {
uint8_t *base(reinterpret_cast<uint8_t *>(data));
- bool aggregate(JSValueIsObject(context, value));
+ JSObjectRef aggregate(JSValueIsObject(context, value) ? (JSObjectRef) value : NULL);
for (size_t index(0); index != type->data.signature.count; ++index) {
- ffi_type *element(ffi->elements[index]);
- JSValueRef rhs(aggregate ? CYGetProperty(context, (JSObjectRef) value, index) : value);
- CYPoolFFI(pool, context, type->data.signature.elements[index].type, element, base, rhs);
+ 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)) {
+ rhs = CYGetProperty(context, aggregate, CYJSString(element->name));
+ if (JSValueIsUndefined(context, rhs))
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"unable to extract structure value" userInfo:nil];
+ }
+ }
+
+ CYPoolFFI(pool, context, element->type, field, base, rhs);
// XXX: alignment?
- base += element->size;
+ base += field->size;
}
} break;
const char *name(CYPoolCString(pool, property, &length));
double number(CYCastDouble(name, length));
+ size_t count(typical->type_.data.signature.count);
+
if (std::isnan(number)) {
if (property == NULL)
return false;
- // XXX: implement!
+ sig::Element *elements(typical->type_.data.signature.elements);
+
+ for (size_t local(0); local != count; ++local)
+ if (strcmp(name, elements[local].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) >= typical->type_.data.signature.count)
+ if (index != number || index < 0 || static_cast<size_t>(index) >= count)
return false;
}
+ base:
base = reinterpret_cast<uint8_t *>(internal->value_);
for (ssize_t local(0); local != index; ++local)
base += typical->ffi_.elements[local]->size;
}
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_);
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
- ssize_t index;
- uint8_t *base;
+ ssize_t index;
+ uint8_t *base;
- if (!Index_(pool, internal, property, index, base))
- return NULL;
+ if (!Index_(pool, internal, property, index, base))
+ return NULL;
+ CYTry {
return CYFromFFI(context, typical->type_.data.signature.elements[index].type, typical->ffi_.elements[index], base, object);
} 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_);
+ CYPool pool;
+ Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(object)));
+ Type_privateData *typical(internal->type_);
- ssize_t index;
- uint8_t *base;
+ ssize_t index;
+ uint8_t *base;
- if (!Index_(pool, internal, property, index, base))
- return false;
+ if (!Index_(pool, internal, property, index, base))
+ return false;
+ CYTry {
CYPoolFFI(NULL, context, typical->type_.data.signature.elements[index].type, typical->ffi_.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_);
+
+ size_t count(typical->type_.data.signature.count);
+ sig::Element *elements(typical->type_.data.signature.elements);
+
+ for (size_t index(0); index != count; ++index)
+ JSPropertyNameAccumulatorAddName(names, CYJSString(elements[index].name));
+}
+
JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
CYTry {
if (setups + count != signature->count - 1)
case 2:
// XXX: this is horrendously inefficient
sig::Signature signature;
- sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]));
+ sig::Parse(pool, &signature, CYPoolCString(pool, [entry objectAtIndex:1]), &Structor_);
ffi_cif cif;
sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
return CYFromFFI(context, signature.elements[0].type, cif.rtype, [name cy$symbol]);
setup[1] = &_cmd;
sig::Signature signature;
- sig::Parse(pool, &signature, type);
+ sig::Parse(pool, &signature, type, &Structor_);
ffi_cif cif;
sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
definition.className = "Struct";
definition.getProperty = &Struct_getProperty;
definition.setProperty = &Struct_setProperty;
+ definition.getPropertyNames = &Struct_getPropertyNames;
definition.finalize = &CYData::Finalize;
Struct_ = JSClassCreate(&definition);
namespace sig {
-void (*sig_aggregate)(apr_pool_t *pool, enum Primitive primitive, const char *name, struct Signature *signature, const char *types) = NULL;
-
-void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos);
-struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named);
+void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback);
+struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback);
/* XXX: I really screwed up this time */
return ndata;
}
-void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos) {
+void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback) {
_assert(*name != NULL);
- bool named = **name == '"';
+ // XXX: this is just a stupid check :(
+ bool named(**name == '"');
signature->elements = NULL;
signature->count = 0;
*name = quote + 1;
}
- element->type = Parse_(pool, name, eos, named);
+ element->type = Parse_(pool, name, eos, named, callback);
if (**name < '0' || **name > '9')
element->offset = _not(size_t);
}
}
-struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named) {
+struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback) {
char next = *(*name)++;
if (next == '?')
return NULL;
case '[':
type->primitive = array_P;
type->data.data.size = strtoul(*name, (char **) name, 10);
- type->data.data.type = Parse_(pool, name, eos, false);
+ type->data.data.type = Parse_(pool, name, eos, false, callback);
if (**name != ']') {
printf("']' != \"%s\"\n", *name);
_assert(false);
} else if (**name == '"') {
type->data.data.type = NULL;
} else {
- type->data.data.type = Parse_(pool, name, eos, named);
+ type->data.data.type = Parse_(pool, name, eos, named, callback);
}
break;
else
type->name = NULL;
+ // XXX: this types thing is a throwback to JocStrap
+
char *types;
- if (next != '=')
+ if (next != '=') {
types = NULL;
- else {
- const char *temp = *name;
- Parse_(pool, &type->data.signature, name, end);
+ } else {
+ const char *temp(*name);
+ Parse_(pool, &type->data.signature, name, end, callback);
types = (char *) apr_pstrmemdup(pool, temp, *name - temp - 1);
}
- if (type->name != NULL && sig_aggregate != NULL) {
- char *angle = strchr(type->name, '<');
- if (angle == NULL)
- (*sig_aggregate)(pool, type->primitive, type->name, &type->data.signature, types);
- else {
- angle = (char *) apr_pstrmemdup(pool, type->name, angle - type->name);
- (*sig_aggregate)(pool, type->primitive, angle, &type->data.signature, types);
- }
- }
+ if (callback != NULL)
+ (*callback)(pool, type->name, types, type);
} break;
case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
return type;
}
-void Parse(apr_pool_t *pool, struct Signature *signature, const char *name) {
+void Parse(apr_pool_t *pool, struct Signature *signature, const char *name, Callback callback) {
const char *temp = name;
- Parse_(pool, signature, &temp, '\0');
+ Parse_(pool, signature, &temp, '\0', callback);
_assert(temp[-1] == '\0');
}