From e66ced89a433143539ae398bf21c1e74993db5fa Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 8 Dec 2015 04:16:31 -0800 Subject: [PATCH] Remove ?syntax and set failure exit code on throw. --- Console.cpp | 52 ++++++++++++++++++---------------------------------- Execute.cpp | 47 ++++++++++++++++++++++++++++++----------------- Library.cpp | 4 ++++ String.hpp | 4 +++- 4 files changed, 55 insertions(+), 52 deletions(-) diff --git a/Console.cpp b/Console.cpp index e2cf364..3d0c5de 100644 --- a/Console.cpp +++ b/Console.cpp @@ -68,6 +68,7 @@ #include "Display.hpp" #include "Driver.hpp" +#include "Error.hpp" #include "Highlight.hpp" #include "Syntax.hpp" @@ -158,14 +159,7 @@ static CYUTF8String Run(CYPool &pool, int client, const std::string &code) { static std::ostream *out_; -static void Write(bool syntax, const char *data, size_t size, std::ostream &out) { - if (syntax) - CYLexerHighlight(data, size, out); - else - out.write(data, size); -} - -static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expand = false) { +static void Output(CYUTF8String json, std::ostream *out, bool expand = false) { const char *data(json.data); size_t size(json.size); @@ -176,7 +170,7 @@ static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expan data[0] != '@' && data[0] != '"' && data[0] != '\'' || data[0] == '@' && data[1] != '"' && data[1] != '\'' ) - Write(syntax, data, size, *out); + CYLexerHighlight(data, size, *out); else for (size_t i(0); i != size; ++i) if (data[i] != '\\') *out << data[i]; @@ -198,15 +192,6 @@ static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expan *out << std::endl; } -static void Run(int client, bool syntax, const char *data, size_t size, std::ostream *out = NULL, bool expand = false) { - CYPool pool; - Output(syntax, Run(pool, client, CYUTF8String(data, size)), out, expand); -} - -static void Run(int client, bool syntax, std::string &code, std::ostream *out = NULL, bool expand = false) { - Run(client, syntax, code.c_str(), code.size(), out, expand); -} - int (*append_history$)(int, const char *); static std::string command_; @@ -281,7 +266,6 @@ static void Console(CYOptions &options) { bool debug(false); bool expand(false); bool lower(true); - bool syntax(true); out_ = &std::cout; @@ -292,6 +276,10 @@ static void Console(CYOptions &options) { rl_attempted_completion_function = &Complete; rl_bind_key('\t', rl_complete); +#if RL_READLINE_VERSION >= 0x0600 + rl_redisplay_function = CYDisplayUpdate; +#endif + struct sigaction action; sigemptyset(&action.sa_mask); action.sa_handler = &sigint; @@ -312,14 +300,6 @@ static void Console(CYOptions &options) { } read: - -#if RL_READLINE_VERSION >= 0x0600 - if (syntax) - rl_redisplay_function = CYDisplayUpdate; - else - rl_redisplay_function = rl_redisplay; -#endif - mode_ = Parsing; char *line(readline(prompt)); mode_ = Working; @@ -354,9 +334,6 @@ static void Console(CYOptions &options) { } else if (data == "lower") { lower = !lower; *out_ << "lower == " << (lower ? "true" : "false") << std::endl; - } else if (data == "syntax") { - syntax = !syntax; - *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl; } command_ = line; history += command_; @@ -439,11 +416,12 @@ static void Console(CYOptions &options) { if (debug) { std::cout << "cy= "; - Write(syntax, code.c_str(), code.size(), std::cout); + CYLexerHighlight(code.c_str(), code.size(), std::cout); std::cout << std::endl; } - Run(client_, syntax, code, out_, expand); + CYPool pool; + Output(Run(pool, client_, code), &std::cout, expand); } } @@ -816,8 +794,14 @@ int Main(int argc, char * const argv[], char const * const envp[]) { std::string code(str.str()); if (compile) std::cout << code; - else - Run(client_, false, code, &std::cout); + else { + CYUTF8String json(Run(pool, client_, code)); + if (CYStartsWith(json, "throw ")) { + CYLexerHighlight(json.data, json.size, std::cerr); + std::cerr << std::endl; + return 1; + } + } } } diff --git a/Execute.cpp b/Execute.cpp index 80b9e63..2882dcb 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -526,6 +526,21 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef 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; @@ -1587,31 +1602,25 @@ static bool CYShouldTerminate(JSContextRef context, void *arg) { } _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 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 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() { @@ -1854,6 +1863,9 @@ extern "C" void CYSetupContext(JSGlobalContextRef 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); @@ -1880,6 +1892,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { /* }}} */ 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)); diff --git a/Library.cpp b/Library.cpp index f5eb1c3..59decd8 100644 --- a/Library.cpp +++ b/Library.cpp @@ -213,6 +213,10 @@ double CYCastDouble(const char *value) { return CYCastDouble(value, strlen(value)); } +_visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) { + return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0; +} + CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) { CYLocalPool local; CYDriver driver(local, stream); diff --git a/String.hpp b/String.hpp index 2f52007..e0e292e 100644 --- a/String.hpp +++ b/String.hpp @@ -54,7 +54,7 @@ struct CYUTF8String { } }; -static inline std::ostream &operator <<(std::ostream &lhs, CYUTF8String &rhs) { +static inline std::ostream &operator <<(std::ostream &lhs, const CYUTF8String &rhs) { lhs.write(rhs.data, rhs.size); return lhs; } @@ -74,6 +74,8 @@ size_t CYGetIndex(const CYUTF8String &value); bool CYIsKey(CYUTF8String value); bool CYGetOffset(const char *value, ssize_t &index); +bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle); + CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16); CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8); -- 2.47.2