return iconv(cd, const_cast<Type_>(inbuf), inbytesleft, outbuf, outbytesleft);
}
-static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
+CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
_assert(pool != NULL);
CYUTF16String utf16(CYCastUTF16String(value));
const char *in(reinterpret_cast<const char *>(utf16.data));
-#ifdef __APPLE__
- iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
-#else
+#ifdef __GLIBC__
iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2")));
+#else
+ iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
#endif
size_t size(JSStringGetMaximumUTF8CStringSize(value));
/* Index Offsets {{{ */
size_t CYGetIndex(const CYUTF8String &value) {
if (value.data[0] != '0') {
- char *end;
- size_t index(strtoul(value.data, &end, 10));
- if (value.data + value.size == end)
- return index;
- } else if (value.data[1] == '\0')
+ size_t index(0);
+ for (size_t i(0); i != value.size; ++i) {
+ if (!DigitRange_[value.data[i]])
+ return _not(size_t);
+ index *= 10;
+ index += value.data[i] - '0';
+ }
+ return index;
+ } else if (value.size == 1)
return 0;
- return _not(size_t);
+ else
+ return _not(size_t);
}
size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
return CYGetIndex(CYPoolUTF8String(pool, context, value));
}
+// XXX: this isn't actually right
bool CYGetOffset(const char *value, ssize_t &index) {
if (value[0] != '0') {
char *end;
break;
default:
+ // this test is designed to be "awewsome", generating neither warnings nor incorrect results
if (*value < 0x20 || *value >= 0x7f)
- str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
+ str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value));
else simple:
str << *value;
}
name = elements[index].name;
if (name == NULL) {
- sprintf(number, "%lu", index);
+ sprintf(number, "%zu", index);
name = number;
}
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));
- }
+ case 1:
+ if (void (*symbol)() = reinterpret_cast<void (*)()>(CYCastSymbol(name.data)))
+ return CYMakeFunctor(context, symbol, value);
+ else return NULL;
+
+ case 2:
+ if (void *symbol = CYCastSymbol(name.data)) {
+ // 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, symbol);
+ } else return NULL;
// XXX: implement case 3
case 4:
{NULL, NULL, 0}
};
+static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *);
+
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]);
-#ifdef __APPLE__
- JSValueRef exception(NULL);
- JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception));
- CYThrow(context, exception);
-#else
- JSValueRef value(CYCallAsFunction(context, Array_, NULL, argc, args));
- JSObjectRef array(CYCastJSObject(context, value));
-#endif
+
+ JSObjectRef array;
+ if (JSObjectMakeArray$ != NULL) {
+ JSValueRef exception(NULL);
+ array = (*JSObjectMakeArray$)(context, argc, args, &exception);
+ CYThrow(context, exception);
+ } else {
+ JSValueRef value(CYCallAsFunction(context, Array_, NULL, argc, args));
+ array = CYCastJSObject(context, value);
+ }
+
CYSetProperty(context, System_, CYJSString("args"), array);
}
_aprcall(apr_initialize());
_aprcall(apr_pool_create(&Pool_, NULL));
_sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_));
+
+ JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
}
apr_pool_t *CYGetGlobalPool() {
CYThrow(context, exception);
}
-void CYObjectiveC(JSContextRef context, JSObjectRef global);
-
JSGlobalContextRef CYGetJSContext() {
CYInitialize();
Result_ = JSStringCreateWithUTF8CString("_");
-// XXX: this is very wrong and sad
-#ifdef __APPLE__
- CYObjectiveC(context, global);
-#endif
+ if (hooks_ != NULL && hooks_->SetupContext != NULL)
+ (*hooks_->SetupContext)(context);
}
return Context_;