+// XXX: this macro is unhygenic
+#define CYCastCString(context, value) ({ \
+ char *utf8; \
+ if (value == NULL) \
+ utf8 = NULL; \
+ else if (JSStringRef string = CYCopyJSString(context, value)) { \
+ utf8 = CYCastCString_(string); \
+ JSStringRelease(string); \
+ } else \
+ utf8 = NULL; \
+ utf8; \
+})
+/* }}} */
+/* Objective-C Strings {{{ */
+const char *CYPoolCString(apr_pool_t *pool, 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 [NSException exceptionWithName:NSInternalInconsistencyException reason:@"[NSString getCString:maxLength:encoding:] == NO" userInfo:nil];
+ 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);
+}
+
+#ifdef __APPLE__