#define _GNU_SOURCE
#include <substrate.h>
-#include "Struct.hpp"
+#include "cycript.h"
#include "sig/parse.hpp"
#include "sig/ffi_type.hpp"
#include "Pooling.hpp"
+#include "Struct.hpp"
#include <unistd.h>
#include <CoreFoundation/CFLogUtilities.h>
#include <CFNetwork/CFNetwork.h>
-#include <Foundation/Foundation.h>
-
-#include <JavaScriptCore/JSBase.h>
-#include <JavaScriptCore/JSValueRef.h>
-#include <JavaScriptCore/JSObjectRef.h>
-#include <JavaScriptCore/JSContextRef.h>
-#include <JavaScriptCore/JSStringRef.h>
-#include <JavaScriptCore/JSStringRefCF.h>
#include <WebKit/WebScriptObject.h>
#include <ext/stdio_filebuf.h>
#include <set>
#include <map>
-#include <sstream>
#include "Parser.hpp"
#include "Cycript.tab.hh"
} while (false)
-#define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true);
-
static JSContextRef Context_;
static JSClassRef Functor_;
@end
-JSContextRef JSGetContext() {
+extern "C" JSContextRef CYGetJSContext() {
return Context_;
}
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);
}
@end
-CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value) {
+extern "C" CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) {
id object(CYCastNSObject(context, value));
return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]);
}
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);
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:
};
CYDriver::CYDriver(const std::string &filename) :
+ state_(CYClear),
+ data_(NULL),
+ size_(0),
filename_(filename),
source_(NULL)
{
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("<stdin>");
-
- 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<const NSString *>(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
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);
}