+++ /dev/null
-#define _GNU_SOURCE
-
-#include <substrate.h>
-#include "cycript.hpp"
-
-#include <cstdio>
-#include <sstream>
-
-#include <setjmp.h>
-
-#include <readline/readline.h>
-#include <readline/history.h>
-
-#include <sys/mman.h>
-
-#include <errno.h>
-#include <unistd.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#include "Cycript.tab.hh"
-
-static jmp_buf ctrlc_;
-
-void sigint(int) {
- longjmp(ctrlc_, 1);
-}
-
-void Run(const char *code, FILE *fout) {
- JSStringRef script(JSStringCreateWithUTF8CString(code));
-
- JSContextRef context(CYGetJSContext());
-
- JSValueRef exception(NULL);
- JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
- JSStringRelease(script);
-
- if (exception != NULL)
- result = exception;
-
- if (!JSValueIsUndefined(context, result)) {
- CYPool pool;
- const char *json;
-
- json = CYPoolJSONString(pool, context, result);
-
- if (fout != NULL) {
- fputs(json, fout);
- fputs("\n", fout);
- fflush(fout);
- }
- }
-}
-
-void Console() {
- bool bypass(false);
- bool debug(false);
-
- FILE *fout(stdout);
-
- rl_bind_key('\t', rl_insert);
-
- struct sigaction action;
- sigemptyset(&action.sa_mask);
- action.sa_handler = &sigint;
- action.sa_flags = 0;
- sigaction(SIGINT, &action, NULL);
-
- restart: for (;;) {
- std::string command;
- std::vector<std::string> lines;
-
- bool extra(false);
- const char *prompt("cy# ");
-
- if (setjmp(ctrlc_) != 0) {
- fputs("\n", fout);
- fflush(fout);
- goto restart;
- }
-
- read:
- char *line(readline(prompt));
- if (line == NULL)
- break;
-
- if (!extra) {
- extra = true;
- if (line[0] == '\\') {
- std::string data(line + 1);
- if (data == "bypass") {
- bypass = !bypass;
- fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
- fflush(fout);
- } else if (data == "debug") {
- debug = !debug;
- fprintf(fout, "debug == %s\n", debug ? "true" : "false");
- fflush(fout);
- }
- add_history(line);
- goto restart;
- }
- }
-
- lines.push_back(line);
- command += line;
- free(line);
-
- std::string code;
-
- if (bypass)
- code = command;
- else {
- CYDriver driver("");
- cy::parser parser(driver);
-
- driver.data_ = command.c_str();
- driver.size_ = command.size();
-
- if (parser.parse() != 0 || !driver.errors_.empty()) {
- for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
- cy::position begin(i->location_.begin);
- if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
- std::cerr << i->message_ << std::endl;
- add_history(command.c_str());
- goto restart;
- }
- }
-
- driver.errors_.clear();
-
- command += '\n';
- prompt = "cy> ";
- goto read;
- }
-
- if (driver.source_ == NULL)
- goto restart;
-
- std::ostringstream str;
- driver.source_->Show(str);
- code = str.str();
- }
-
- add_history(command.c_str());
-
- if (debug)
- std::cout << code << std::endl;
-
- Run(code.c_str(), fout);
- }
-
- fputs("\n", fout);
- fflush(fout);
-}
-
-void *Map(const char *path, size_t *psize) {
- int fd;
- _syscall(fd = open(path, O_RDONLY));
-
- struct stat stat;
- _syscall(fstat(fd, &stat));
- size_t size(stat.st_size);
-
- *psize = size;
-
- void *base;
- _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
-
- _syscall(close(fd));
- return base;
-}
-
-int main(int argc, const char *argv[]) {
- const char *script;
-
- if (argc == 1)
- script = NULL;
- else {
- CYSetArgs(argc - 1, argv + 1);
- script = argv[1];
- }
-
- if (script == NULL || strcmp(script, "-") == 0)
- Console();
- else {
- CYDriver driver(script);
- cy::parser parser(driver);
-
- size_t size;
- char *start(reinterpret_cast<char *>(Map(script, &size)));
- char *end(start + size);
-
- if (size >= 2 && start[0] == '#' && start[1] == '!') {
- start += 2;
- while (start != end && *start++ != '\n');
- }
-
- driver.data_ = start;
- driver.size_ = end - start;
-
- if (parser.parse() != 0 || !driver.errors_.empty()) {
- for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
- std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
- } else if (driver.source_ != NULL) {
- std::ostringstream str;
- driver.source_->Show(str);
- std::string code(str.str());
- std::cout << code << std::endl;
- Run(code.c_str(), stdout);
- }
- }
-
- return 0;
-}
--- /dev/null
+#define _GNU_SOURCE
+
+#include <substrate.h>
+#include "cycript.hpp"
+
+#include <cstdio>
+#include <sstream>
+
+#include <setjmp.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include <sys/mman.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "Cycript.tab.hh"
+
+static jmp_buf ctrlc_;
+
+void sigint(int) {
+ longjmp(ctrlc_, 1);
+}
+
+void Run(const char *code, FILE *fout) { _pooled
+ JSStringRef script(JSStringCreateWithUTF8CString(code));
+
+ JSContextRef context(CYGetJSContext());
+
+ JSValueRef exception(NULL);
+ JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
+ JSStringRelease(script);
+
+ if (exception != NULL) { error:
+ result = exception;
+ exception = NULL;
+ }
+
+ if (!JSValueIsUndefined(context, result)) {
+ CYPool pool;
+ const char *json;
+
+ json = CYPoolJSONString(pool, context, result, &exception);
+ if (exception != NULL)
+ goto error;
+
+ if (fout != NULL) {
+ fputs(json, fout);
+ fputs("\n", fout);
+ fflush(fout);
+ }
+ }
+}
+
+void Console() {
+ bool bypass(false);
+ bool debug(false);
+
+ FILE *fout(stdout);
+
+ rl_bind_key('\t', rl_insert);
+
+ struct sigaction action;
+ sigemptyset(&action.sa_mask);
+ action.sa_handler = &sigint;
+ action.sa_flags = 0;
+ sigaction(SIGINT, &action, NULL);
+
+ restart: for (;;) {
+ std::string command;
+ std::vector<std::string> lines;
+
+ bool extra(false);
+ const char *prompt("cy# ");
+
+ if (setjmp(ctrlc_) != 0) {
+ fputs("\n", fout);
+ fflush(fout);
+ goto restart;
+ }
+
+ read:
+ char *line(readline(prompt));
+ if (line == NULL)
+ break;
+
+ if (!extra) {
+ extra = true;
+ if (line[0] == '\\') {
+ std::string data(line + 1);
+ if (data == "bypass") {
+ bypass = !bypass;
+ fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
+ fflush(fout);
+ } else if (data == "debug") {
+ debug = !debug;
+ fprintf(fout, "debug == %s\n", debug ? "true" : "false");
+ fflush(fout);
+ }
+ add_history(line);
+ goto restart;
+ }
+ }
+
+ lines.push_back(line);
+ command += line;
+ free(line);
+
+ std::string code;
+
+ if (bypass)
+ code = command;
+ else {
+ CYDriver driver("");
+ cy::parser parser(driver);
+
+ driver.data_ = command.c_str();
+ driver.size_ = command.size();
+
+ if (parser.parse() != 0 || !driver.errors_.empty()) {
+ for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) {
+ cy::position begin(i->location_.begin);
+ if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) {
+ std::cerr << i->message_ << std::endl;
+ add_history(command.c_str());
+ goto restart;
+ }
+ }
+
+ driver.errors_.clear();
+
+ command += '\n';
+ prompt = "cy> ";
+ goto read;
+ }
+
+ if (driver.source_ == NULL)
+ goto restart;
+
+ std::ostringstream str;
+ driver.source_->Show(str);
+ code = str.str();
+ }
+
+ add_history(command.c_str());
+
+ if (debug)
+ std::cout << code << std::endl;
+
+ Run(code.c_str(), fout);
+ }
+
+ fputs("\n", fout);
+ fflush(fout);
+}
+
+void *Map(const char *path, size_t *psize) {
+ int fd;
+ _syscall(fd = open(path, O_RDONLY));
+
+ struct stat stat;
+ _syscall(fstat(fd, &stat));
+ size_t size(stat.st_size);
+
+ *psize = size;
+
+ void *base;
+ _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
+
+ _syscall(close(fd));
+ return base;
+}
+
+int main(int argc, const char *argv[]) {
+ const char *script;
+
+ if (argc == 1)
+ script = NULL;
+ else {
+ CYSetArgs(argc - 1, argv + 1);
+ script = argv[1];
+ }
+
+ if (script == NULL || strcmp(script, "-") == 0)
+ Console();
+ else {
+ CYDriver driver(script);
+ cy::parser parser(driver);
+
+ size_t size;
+ char *start(reinterpret_cast<char *>(Map(script, &size)));
+ char *end(start + size);
+
+ if (size >= 2 && start[0] == '#' && start[1] == '!') {
+ start += 2;
+ while (start != end && *start++ != '\n');
+ }
+
+ driver.data_ = start;
+ driver.size_ = end - start;
+
+ if (parser.parse() != 0 || !driver.errors_.empty()) {
+ for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
+ std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
+ } else if (driver.source_ != NULL) {
+ std::ostringstream str;
+ driver.source_->Show(str);
+ std::string code(str.str());
+ std::cout << code << std::endl;
+ Run(code.c_str(), stdout);
+ }
+ }
+
+ return 0;
+}
CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
} while (false)
+#define CYPoolTry { \
+ id _saved(nil); \
+ NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
+ @try
+#define CYPoolCatch(value) \
+ @catch (NSException *error) { \
+ _saved = [error retain]; \
+ @throw; \
+ return value; \
+ } @finally { \
+ [_pool release]; \
+ if (_saved != nil) \
+ [_saved autorelease]; \
+ } \
+}
+
static JSGlobalContextRef Context_;
static JSObjectRef System_;
}
};
-JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient = true) {
+JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
if (!transient)
object = [object retain];
jocData *data(new jocData(object, transient));
@interface NSObject (Cycript)
- (bool) cy$isUndefined;
- (NSString *) cy$toJSON;
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient;
@end
@interface NSString (Cycript)
return [self description];
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
- return CYMakeInstance(context, self);
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
+ return CYMakeInstance(context, self, transient);
}
@end
return @"undefined";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
return CYJSUndefined(context);
}
return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false";
}
-- (JSValueRef) cy$JSValueInContext:(JSContextRef)context {
+- (JSValueRef) cy$JSValueInContext:(JSContextRef)context transient:(bool)transient {
return [self class] != NSCFBoolean_ ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
}
return Context_;
}
+#define CYTry \
+ @try
#define CYCatch \
@catch (id error) { \
NSLog(@"e:%@", error); \
return CYCastJSValue(context, CYJSString(value));
}
-JSValueRef CYCastJSValue(JSContextRef context, id value) {
- return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context];
+JSValueRef CYCastJSValue(JSContextRef context, id value, bool transient = true) {
+ return value == nil ? CYJSNull(context) : [value cy$JSValueInContext:context transient:transient];
}
JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value) {
}
void CYThrow(JSContextRef context, id error, JSValueRef *exception) {
+ if (exception == NULL)
+ throw error;
*exception = CYCastJSValue(context, error);
}
@end
-CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) { _pooled
- id object(CYCastNSObject(NULL, context, value));
- return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]);
+CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ CYTry {
+ CYPoolTry {
+ id object(CYCastNSObject(NULL, context, value));
+ return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]);
+ } CYPoolCatch(NULL)
+ } CYCatch
}
-const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
- NSString *json((NSString *) CYCopyJSONString(context, value));
- const char *string(CYPoolCString(pool, json));
- [json release];
- return string;
+const char *CYPoolJSONString(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception) {
+ if (NSString *json = (NSString *) CYCopyJSONString(context, value, exception)) {
+ const char *string(CYPoolCString(pool, json));
+ [json release];
+ return string;
+ } else return NULL;
}
static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1));
CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8"));
- CFStringRef json(CYCopyJSONString(CYGetJSContext(), result));
+ CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL));
CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL));
CFRelease(json);
}
static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSString(pool, property));
NSLog(@"get:%@", name);
}
static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSString(pool, property));
NSLog(@"set:%@", name);
}
static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSString(pool, property));
NSLog(@"delete:%@", name);
}
static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
jocData *data(reinterpret_cast<jocData *>(JSObjectGetPrivate(object)));
- return CYMakeInstance(context, [data->GetValue() alloc]);
+ return CYMakeInstance(context, [data->GetValue() alloc], true);
} CYCatch
}
break;
case sig::typename_P:
- value = CYMakeInstance(context, *reinterpret_cast<Class *>(data));
+ value = CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
break;
case sig::selector_P:
}
static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
- @try {
+ CYTry {
if (count != signature->count - 1)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil];
}
void Closure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
- NSLog(@"Closure()");
ffoData *data(reinterpret_cast<ffoData *>(arg));
JSContextRef context(data->context_);
}
static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSString(pool, property));
if (Class _class = NSClassFromString(name))
- return CYMakeInstance(context, _class);
+ return CYMakeInstance(context, _class, true);
if (NSMutableArray *entry = [Bridge_ objectForKey:name])
switch ([[entry objectAtIndex:0] intValue]) {
case 0:
}
static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
NSLog(@"%s", CYCastCString(context, arguments[0]));
return CYJSUndefined(context);
} CYCatch
}
static JSValueRef CYApplicationMain(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
CYPool pool;
NSString *name(CYCastNSObject(pool, context, arguments[0]));
int argc(*_NSGetArgc());
CYPool pool;
- @try {
+ CYTry {
if (count < 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil];
Class _class(object_getClass(self));
if (Method method = class_getInstanceMethod(_class, _cmd))
type = method_getTypeEncoding(method);
- else { _pooled
- NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
- if (method == nil)
- @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
- type = CYPoolCString(pool, [method _typeString]);
+ else {
+ CYPoolTry {
+ NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
+ if (method == nil)
+ @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil];
+ type = CYPoolCString(pool, [method _typeString]);
+ } CYPoolCatch(NULL)
}
} CYCatch
}
JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 1)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector constructor" userInfo:nil];
const char *name(CYCastCString(context, arguments[0]));
}
JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Functor constructor" userInfo:nil];
const char *type(CYCastCString(context, arguments[1]));
return Function_;
}
-static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled
- @try {
+static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ CYTry {
jocData *data(reinterpret_cast<jocData *>(JSObjectGetPrivate(_this)));
- return CYCastJSValue(context, CYJSString([data->GetValue() description]));
+ NSString *description; CYPoolTry {
+ description = [data->GetValue() description];
+ } CYPoolCatch(NULL)
+ return CYCastJSValue(context, CYJSString(description));
} CYCatch
}
static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
selData *data(reinterpret_cast<selData *>(JSObjectGetPrivate(_this)));
return CYCastJSValue(context, sel_getName(data->GetValue()));
} CYCatch
}
static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
- @try {
+ CYTry {
if (count != 2)
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
CYPool pool;