From a1ae298586df883c0247ee2481fa58caa6a4c756 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 15 Dec 2015 07:23:06 -0800 Subject: [PATCH] Throw SyntaxError, not Error, for Cycript.compile. --- Error.hpp | 4 ++-- Exception.hpp | 8 +++++--- Execute.cpp | 19 +++++++++++-------- ObjectiveC/Library.mm | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Error.hpp b/Error.hpp index f63180e..9fc6e85 100644 --- a/Error.hpp +++ b/Error.hpp @@ -41,7 +41,7 @@ struct CYJSError : CYJSError(JSContextRef context, const char *format, ...); virtual const char *PoolCString(CYPool &pool) const; - virtual JSValueRef CastJSValue(JSContextRef context) const; + virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const; }; #endif @@ -58,7 +58,7 @@ struct _visible CYPoolError : virtual const char *PoolCString(CYPool &pool) const; #ifdef CY_EXECUTE - virtual JSValueRef CastJSValue(JSContextRef context) const; + virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const; #endif }; diff --git a/Exception.hpp b/Exception.hpp index 994dd7b..dcc119b 100644 --- a/Exception.hpp +++ b/Exception.hpp @@ -41,7 +41,7 @@ struct _visible CYException { virtual const char *PoolCString(CYPool &pool) const = 0; #ifdef CY_EXECUTE - virtual JSValueRef CastJSValue(JSContextRef context) const = 0; + virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const = 0; #endif }; @@ -53,14 +53,16 @@ void CYThrow(JSContextRef context, JSValueRef value); #define CYTry \ try -#define CYCatch(value) \ +#define CYCatch_(value, name) \ catch (const CYException &error) { \ - *exception = error.CastJSValue(context); \ + *exception = error.CastJSValue(context, name); \ return value; \ } catch (...) { \ *exception = CYCastJSValue(context, "catch(...)"); \ return value; \ } +#define CYCatch(value) \ + CYCatch_(value, "Error") #define _assert_(mode, test, code, format, ...) do \ if (!(test)) \ diff --git a/Execute.cpp b/Execute.cpp index 68a0d39..2123b3f 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -367,7 +367,7 @@ static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectR std::stringstream value(CYPoolCString(pool, context, arguments[0])); CYUTF8String code(CYPoolCode(pool, value)); return CYCastJSValue(context, CYJSString(code)); -} CYCatch(NULL) } +} 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); @@ -1732,19 +1732,19 @@ const char *CYJSError::PoolCString(CYPool &pool) const { 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, ...) { @@ -1758,7 +1758,7 @@ 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) { @@ -1896,6 +1896,9 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { 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); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 86c705f..e8c43d2 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -58,7 +58,7 @@ try #define CYObjectiveCatch \ catch (const CYException &error) { \ - @throw CYCastNSObject(NULL, context, error.CastJSValue(context)); \ + @throw CYCastNSObject(NULL, context, error.CastJSValue(context, "Error")); \ } \ } -- 2.45.2