**/
/* }}} */
-#include "Internal.hpp"
+#include "cycript.hpp"
+
+#include <iostream>
+#include <set>
+#include <map>
+#include <iomanip>
+#include <sstream>
+#include <cmath>
#include <dlfcn.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
-#include "cycript.hpp"
-
-#include "sig/parse.hpp"
-#include "sig/ffi_type.hpp"
-
-#include "Pooling.hpp"
-#include "Execute.hpp"
-
#include <sys/mman.h>
#include <sys/stat.h>
-#include <iostream>
-#include <set>
-#include <map>
-#include <iomanip>
-#include <sstream>
-#include <cmath>
+#include "sig/parse.hpp"
+#include "sig/ffi_type.hpp"
#include "Code.hpp"
#include "Decode.hpp"
#include "Error.hpp"
+#include "Execute.hpp"
+#include "Internal.hpp"
#include "JavaScript.hpp"
+#include "Pooling.hpp"
#include "String.hpp"
static std::vector<CYHook *> &GetHooks() {
return CYJSUndefined(context);
} CYCatch(NULL) }
-static size_t Nonce_(0);
-
-static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- CYPool pool;
- const char *name(pool.strcat(CYPoolCString(pool, context, arguments[0]), pool.itoa(Nonce_++), NULL));
- return CYCastJSValue(context, name);
-} CYCatch(NULL) }
-
static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef);
-void CYGarbageCollect(JSContextRef context) {
+_visible void CYGarbageCollect(JSContextRef context) {
(JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
}
JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
if (CYIsCallable(context, toCYON)) {
// XXX: this needs to be abstracted behind some kind of function
- JSValueRef arguments[1] = {CYCastJSValue(context, static_cast<double>(reinterpret_cast<uintptr_t>(&objects)))};
+ JSValueRef arguments[1] = {CYCastJSValue(context, reinterpret_cast<uintptr_t>(&objects))};
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 1, arguments));
_assert(value != NULL);
return CYPoolCString(pool, context, value);
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
+static JSValueRef Error_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CYPool pool;
+ std::ostringstream str;
+
+ str << "new " << CYPoolUTF8String(pool, context, CYJSString(context, CYGetProperty(context, _this, name_s))) << "(";
+
+ CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, CYGetProperty(context, _this, message_s))));
+ CYStringify(str, string.data, string.size);
+
+ str << ")";
+
+ std::string value(str.str());
+ return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
+} CYCatch(NULL) }
+
static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYPool pool;
std::ostringstream str;
static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
CYLocalPool pool;
- std::ostringstream out;
+ std::stringbuf out;
CYOptions options;
CYOutput output(out, options);
(new(pool) CYEncodedType(Decode(pool, internal->type_)))->Output(output, CYNoFlags);
static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *);
-void CYSetArgs(int argc, const char *argv[]) {
+_visible void CYSetArgs(int argc, const char *argv[]) {
JSContextRef context(CYGetJSContext());
JSValueRef args[argc];
for (int i(0); i != argc; ++i)
return cancel_;
}
-const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
- JSValueRef exception(NULL);
- if (false) error:
- return CYPoolCString(pool, context, CYJSString(context, exception));
-
+_visible const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
ExecutionHandle handle(context);
cancel_ = false;
if (&JSContextGroupSetExecutionTimeLimit != NULL)
JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context), 0.5, &CYShouldTerminate, NULL);
- JSValueRef result(JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception));
- if (exception != NULL)
- goto error;
-
- if (JSValueIsUndefined(context, result))
- return NULL;
-
- std::set<void *> objects;
- const char *json(CYPoolCCYON(pool, context, result, objects, &exception));
- if (exception != NULL)
- goto error;
+ try {
+ JSValueRef result(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0));
+ if (JSValueIsUndefined(context, result))
+ return NULL;
- CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
+ std::set<void *> objects;
+ const char *json(_jsccall(CYPoolCCYON, pool, context, result, objects));
+ CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
- return json;
+ return json;
+ } catch (const CYException &error) {
+ return pool.strcat("throw ", error.PoolCString(pool), NULL);
+ }
}
-void CYCancel() {
+_visible void CYCancel() {
cancel_ = true;
}
return reinterpret_cast<Context *>(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_;
}
-extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size);
-
void *CYMapFile(const char *path, size_t *psize) {
int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT));
if (fd == -1)
JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error"))));
CYSetProperty(context, cy, CYJSString("Error"), Error);
+ JSObjectRef Error_prototype(CYCastJSObject(context, CYGetProperty(context, Error, prototype_s)));
+ CYSetProperty(context, cy, CYJSString("Error_prototype"), Error_prototype);
+
JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))));
CYSetProperty(context, cy, CYJSString("Function"), Function);
/* }}} */
CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
+ CYSetProperty(context, Error_prototype, toCYON_s, &Error_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
CYSetPrototype(context, last, all);
}
- CYSetProperty(context, global, CYJSString("$cyq"), &$cyq, kJSPropertyAttributeDontEnum);
-
JSObjectRef System(JSObjectMake(context, NULL, NULL));
CYSetProperty(context, cy, CYJSString("System"), System);
static JSGlobalContextRef context_;
-JSGlobalContextRef CYGetJSContext() {
+_visible JSGlobalContextRef CYGetJSContext() {
CYInitializeDynamic();
if (context_ == NULL) {
return context_;
}
-void CYDestroyContext() {
+_visible void CYDestroyContext() {
if (context_ == NULL)
return;
JSGlobalContextRelease(context_);