#include "Display.hpp"
#include "Driver.hpp"
+#include "Error.hpp"
#include "Highlight.hpp"
#include "Syntax.hpp"
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);
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];
*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_;
bool debug(false);
bool expand(false);
bool lower(true);
- bool syntax(true);
out_ = &std::cout;
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;
}
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;
} 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_;
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);
}
}
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;
+ }
+ }
}
}
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() {
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));
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);
}
};
-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;
}
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);