X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/b1ff2d781f508d4538ed628218c4ac2860940671..94d55b5c2b4cd5c317891dfbdd6c6186e1759101:/Library.mm diff --git a/Library.mm b/Library.mm index 64f0bf5..b6b2f71 100644 --- a/Library.mm +++ b/Library.mm @@ -40,12 +40,13 @@ #define _GNU_SOURCE #include -#include "Struct.hpp" +#include "cycript.h" #include "sig/parse.hpp" #include "sig/ffi_type.hpp" #include "Pooling.hpp" +#include "Struct.hpp" #include @@ -53,14 +54,6 @@ #include #include -#include - -#include -#include -#include -#include -#include -#include #include @@ -72,7 +65,6 @@ #include #include #include -#include #include "Parser.hpp" #include "Cycript.tab.hh" @@ -90,8 +82,6 @@ } while (false) -#define _pooled _H _pool([[NSAutoreleasePool alloc] init], true); - static JSContextRef Context_; static JSClassRef Functor_; @@ -271,7 +261,7 @@ JSObjectRef CYMakeObject(JSContextRef context, id object) { @end -JSContextRef JSGetContext() { +extern "C" JSContextRef CYGetJSContext() { return Context_; } @@ -404,7 +394,7 @@ JSValueRef CYCastJSValue(JSContextRef context, id value) { return value == nil ? JSValueMakeNull(context) : [value cy$JSValueInContext:context]; } -void CYThrow(JSContextRef context, id error, JSValueRef *exception) { +extern "C" void CYThrowNSError(JSContextRef context, id error, JSValueRef *exception) { *exception = CYCastJSValue(context, error); } @@ -479,7 +469,7 @@ void CYThrow(JSContextRef context, id error, JSValueRef *exception) { @end -CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value) { +extern "C" CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) { id object(CYCastNSObject(context, value)); return reinterpret_cast([(object == nil ? @"null" : [object cy$toJSON]) retain]); } @@ -507,13 +497,13 @@ static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef addr JSStringRef script(JSStringCreateWithCFString(code)); CFRelease(code); - JSValueRef result(JSEvaluateScript(JSGetContext(), script, NULL, NULL, 0, NULL)); + JSValueRef result(JSEvaluateScript(CYGetJSContext(), script, NULL, NULL, 0, NULL)); JSStringRelease(script); CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); - CFStringRef json(JSValueToJSONCopy(JSGetContext(), result)); + CFStringRef json(CYCopyJSONString(CYGetJSContext(), result)); CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); CFRelease(json); @@ -841,7 +831,7 @@ static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, J if (NSMutableArray *entry = [Bridge_ objectForKey:name]) switch ([[entry objectAtIndex:0] intValue]) { case 0: - return JSEvaluateScript(JSGetContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); + return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); case 1: return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]); case 2: @@ -918,6 +908,9 @@ static JSStaticValue Pointer_staticValues[2] = { }; CYDriver::CYDriver(const std::string &filename) : + state_(CYClear), + data_(NULL), + size_(0), filename_(filename), source_(NULL) { @@ -928,68 +921,11 @@ CYDriver::~CYDriver() { ScannerDestroy(); } -void CYDriver::Clear() { - source_ = NULL; - pool_.Clear(); -} - -extern int cydebug; - -void cy::parser::error(const cy::parser::location_type &loc, const std::string &msg) { - std::cerr << loc << ": " << msg << std::endl; -} - -void CYConsole(FILE *fin, FILE *fout, FILE *ferr) { - //cydebug = 1; - - CYDriver driver(""); - - while (!feof(fin)) { _pooled - driver.Clear(); - - cy::parser parser(driver); - if (parser.parse() != 0) - continue; - - if (driver.source_ == NULL) { - fputs("driver.source == NULL\n", fout); - break; - } - - std::ostringstream str; - driver.source_->Show(str); - - std::string code(str.str()); - std::cout << code << std::endl; - - JSStringRef script(JSStringCreateWithUTF8CString(code.c_str())); - - JSContextRef context(JSGetContext()); - - JSValueRef exception(NULL); - JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); - JSStringRelease(script); - - if (exception != NULL) - result = exception; - - if (!JSValueIsUndefined(context, result)) { - CFStringRef json; - - @try { json: - json = JSValueToJSONCopy(context, result); - } @catch (id error) { - CYThrow(context, error, &result); - goto json; - } - - fputs([reinterpret_cast(json) UTF8String], fout); - CFRelease(json); - - fputs("\n", fout); - fflush(fout); - } - } +void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { + CYDriver::Error error; + error.location_ = location; + error.message_ = message; + driver.errors_.push_back(error); } MSInitialize { _pooled @@ -1062,8 +998,8 @@ MSInitialize { _pooled length_ = JSStringCreateWithUTF8CString("length"); JSValueRef exception(NULL); - JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYJSString("Array"), &exception)); + JSValueRef value(JSObjectGetProperty(CYGetJSContext(), global, CYJSString("Array"), &exception)); CYThrow(context, exception); - Array_ = JSValueToObject(JSGetContext(), value, &exception); + Array_ = JSValueToObject(CYGetJSContext(), value, &exception); CYThrow(context, exception); }