#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFLogUtilities.h>
+#include <JavaScriptCore/JSStringRefCF.h>
#include <WebKit/WebScriptObject.h>
#include <sys/mman.h>
#include "Parser.hpp"
#include "Cycript.tab.hh"
-#include <apr-1/apr_thread_proc.h>
+#include <apr_thread_proc.h>
#undef _assert
#undef _trace
void CYThrow(JSContextRef context, JSValueRef value);
+const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value, JSValueRef *exception);
+JSStringRef CYCopyJSString(const char *value);
+
+void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value);
+
+JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)());
+JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
+
/* JavaScript Properties {{{ */
JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index) {
JSValueRef exception(NULL);
virtual ~Instance() {
if ((flags_ & Transient) == 0)
// XXX: does this handle background threads correctly?
+ // XXX: this simply does not work on the console because I'm stupid
[GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
}
@end
-CYRange DigitRange_ (0x3ff000000000000LLU, 0x000000000000000LLU); // 0-9
-CYRange WordStartRange_(0x000001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$
-CYRange WordEndRange_ (0x3ff001000000000LLU, 0x7fffffe87fffffeLLU); // A-Za-z_$0-9
-
#define CYTry \
@try
#define CYCatch \
extern "C" {
int *_NSGetArgc(void);
char ***_NSGetArgv(void);
- int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
}
static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
CYTry {
- NSLog(@"%s", CYCastCString(context, arguments[0]));
+ if (count == 0)
+ NSLog(@"");
+ else
+ NSLog(@"%s", CYCastCString(context, arguments[0]));
return CYJSUndefined(context);
} CYCatch
}
}
/* }}} */
+static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+ JSGarbageCollect(context);
+ return CYJSUndefined(context);
+}
+
static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
JSValueRef setup[count + 2];
setup[0] = _this;
{NULL, NULL, 0}
};
-CYDriver::CYDriver(const std::string &filename) :
- state_(CYClear),
- data_(NULL),
- size_(0),
- file_(NULL),
- strict_(false),
- filename_(filename),
- program_(NULL)
-{
- ScannerInit();
-}
-
-CYDriver::~CYDriver() {
- ScannerDestroy();
-}
-
-void CYDriver::Warning(const cy::location &location, const char *message) {
- if (!strict_)
- return;
-
- CYDriver::Error error;
- error.warning_ = true;
- error.location_ = location;
- error.message_ = message;
- errors_.push_back(error);
-}
-
-void cy::parser::error(const cy::parser::location_type &location, const std::string &message) {
- CYDriver::Error error;
- error.warning_ = false;
- error.location_ = location;
- error.message_ = message;
- driver.errors_.push_back(error);
-}
-
void CYSetArgs(int argc, const char *argv[]) {
JSContextRef context(CYGetJSContext());
JSValueRef args[argc];
return json;
}
-bool CYRecvAll_(int socket, uint8_t *data, size_t size) {
- while (size != 0) if (size_t writ = _syscall(recv(socket, data, size, 0))) {
- data += writ;
- size -= writ;
- } else
- return false;
- return true;
-}
-
-bool CYSendAll_(int socket, const uint8_t *data, size_t size) {
- while (size != 0) if (size_t writ = _syscall(send(socket, data, size, 0))) {
- data += writ;
- size -= writ;
- } else
- return false;
- return true;
-}
-
static apr_pool_t *Pool_;
struct CYExecute_ {
json = NULL;
size = _not(size_t);
} else {
+ CYContext context(driver.pool_);
+ driver.program_->Replace(context);
std::ostringstream str;
CYOutput out(str);
- driver.program_->Multiple(out);
+ out << *driver.program_;
std::string code(str.str());
CYExecute_ execute = {pool, code.c_str()};
[client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES];
class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
+ JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+ CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction));
+
CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq));