}
static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ FILE *file(stdout);
+
if (count == 0)
- printf("\n");
+ fputc('\n', file);
else {
CYPool pool;
- printf("%s\n", CYPoolCString(pool, context, arguments[0]));
+ CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0])));
+ fwrite(string.data, string.size, 1, file);
}
+ fflush(file);
return CYJSUndefined(context);
} CYCatch(NULL) }
(JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
}
+static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CYPool pool;
+ CYUTF8String before(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0])));
+ std::stringstream value(std::string(before.data, before.size));
+ CYUTF8String after(CYPoolCode(pool, value));
+ return CYCastJSValue(context, CYJSString(after));
+} CYCatch_(NULL, "SyntaxError") }
+
static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYGarbageCollect(context);
return CYJSUndefined(context);
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;
}
_visible const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
- JSValueRef exception(NULL);
- if (false) error:
- return CYPoolCString(pool, context, CYJSString(context, exception));
-
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);
+ }
}
_visible void CYCancel() {
return CYPoolCCYON(pool, context_, value_, objects);
}
-JSValueRef CYJSError::CastJSValue(JSContextRef context) const {
- // XXX: what if the context is different?
+JSValueRef CYJSError::CastJSValue(JSContextRef context, const char *name) const {
+ // XXX: what if the context is different? or the name? I dunno. ("epic" :/)
return value_;
}
-JSValueRef CYCastJSError(JSContextRef context, const char *message) {
- JSObjectRef Error(CYGetCachedObject(context, CYJSString("Error")));
+JSValueRef CYCastJSError(JSContextRef context, const char *name, const char *message) {
+ JSObjectRef Error(CYGetCachedObject(context, CYJSString(name)));
JSValueRef arguments[1] = {CYCastJSValue(context, message)};
return _jsccall(JSObjectCallAsConstructor, context, Error, 1, arguments);
}
-JSValueRef CYPoolError::CastJSValue(JSContextRef context) const {
- return CYCastJSError(context, message_);
+JSValueRef CYPoolError::CastJSValue(JSContextRef context, const char *name) const {
+ return CYCastJSError(context, name, message_);
}
CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
const char *message(pool.vsprintf(64, format, args));
va_end(args);
- value_ = CYCastJSError(context, message);
+ value_ = CYCastJSError(context, "Error", message);
}
JSGlobalContextRef CYGetJSContext(JSContextRef context) {
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);
JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s)));
CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype);
+
+ JSObjectRef SyntaxError(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("SyntaxError"))));
+ CYSetProperty(context, cy, CYJSString("SyntaxError"), SyntaxError);
/* }}} */
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));
CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+ CYSetProperty(context, cycript, CYJSString("compile"), &Cycript_compile_callAsFunction);
CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction);
JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));