+- (void) execute:(NSValue *)value {
+ CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
+ NSLog(@"b:%p", execute->data_);
+ NSLog(@"s:%s", execute->data_);
+ execute->data_ = CYExecute(execute->pool_, execute->data_);
+ NSLog(@"a:%p", execute->data_);
+}
+
+@end
+
+struct CYClient :
+ CYData
+{
+ int socket_;
+ apr_thread_t *thread_;
+
+ CYClient(int socket) :
+ socket_(socket)
+ {
+ }
+
+ ~CYClient() {
+ _syscall(close(socket_));
+ }
+
+ void Handle() { _pooled
+ CYClient_ *client = [[[CYClient_ alloc] init] autorelease];
+
+ for (;;) {
+ size_t size;
+ if (!CYRecvAll(socket_, &size, sizeof(size)))
+ return;
+
+ CYPool pool;
+ char *data(new(pool) char[size + 1]);
+ if (!CYRecvAll(socket_, data, size))
+ return;
+ data[size] = '\0';
+
+ CYDriver driver("");
+ cy::parser parser(driver);
+
+ driver.data_ = data;
+ driver.size_ = size;
+
+ const char *json;
+ if (parser.parse() != 0 || !driver.errors_.empty()) {
+ json = NULL;
+ size = _not(size_t);
+ } else {
+ std::ostringstream str;
+ driver.source_->Show(str);
+ std::string code(str.str());
+ CYExecute_ execute = {pool, code.c_str()};
+ [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES];
+ json = execute.data_;
+ size = json == NULL ? _not(size_t) : strlen(json);
+ }
+
+ if (!CYSendAll(socket_, &size, sizeof(size)))
+ return;
+ if (json != NULL)
+ if (!CYSendAll(socket_, json, size))
+ return;
+ }
+ }
+};
+
+static void * APR_THREAD_FUNC OnClient(apr_thread_t *thread, void *data) {
+ CYClient *client(reinterpret_cast<CYClient *>(data));
+ client->Handle();
+ delete client;
+ return NULL;
+}
+
+static void * APR_THREAD_FUNC Cyrver(apr_thread_t *thread, void *data) {
+ for (;;) {
+ int socket(_syscall(accept(Socket_, NULL, NULL)));
+ CYClient *client(new CYClient(socket));
+ apr_threadattr_t *attr;
+ _aprcall(apr_threadattr_create(&attr, Pool_));
+ _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_));
+ }
+
+ return NULL;
+}
+
+void Unlink() {
+ pid_t pid(getpid());
+ char path[104];
+ sprintf(path, "/tmp/.s.cy.%u", pid);
+ unlink(path);
+}
+
+MSInitialize { _pooled
+ _aprcall(apr_initialize());
+ _aprcall(apr_pool_create(&Pool_, NULL));
+
+ Bridge_ = [[NSMutableArray arrayWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
+ NSCFBoolean_ = objc_getClass("NSCFBoolean");
+
+ Socket_ = _syscall(socket(PF_UNIX, SOCK_STREAM, 0));
+
+ struct sockaddr_un address;
+ memset(&address, 0, sizeof(address));
+ address.sun_family = AF_UNIX;
+
+ pid_t pid(getpid());
+ sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
+
+ try {
+ _syscall(bind(Socket_, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
+ atexit(&Unlink);
+ _syscall(listen(Socket_, 0));
+
+ apr_threadattr_t *attr;
+ _aprcall(apr_threadattr_create(&attr, Pool_));
+
+ apr_thread_t *thread;
+ _aprcall(apr_thread_create(&thread, attr, &Cyrver, NULL, Pool_));
+ } catch (...) {
+ NSLog(@"failed to setup Cyrver");
+ }
+}
+
+JSGlobalContextRef CYGetJSContext() {
+ if (Context_ == NULL) {
+ JSClassDefinition definition;
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Functor";
+ definition.staticFunctions = Functor_staticFunctions;
+ definition.callAsFunction = &Functor_callAsFunction;
+ definition.finalize = &CYData::Finalize;
+ Functor_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Instance";
+ definition.staticValues = CYValue_staticValues;
+ definition.staticFunctions = Instance_staticFunctions;
+ definition.getProperty = &Instance_getProperty;
+ definition.setProperty = &Instance_setProperty;
+ definition.deleteProperty = &Instance_deleteProperty;
+ definition.callAsConstructor = &Instance_callAsConstructor;
+ definition.finalize = &CYData::Finalize;
+ Instance_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Pointer";
+ definition.staticFunctions = Pointer_staticFunctions;
+ definition.getProperty = &Pointer_getProperty;
+ definition.setProperty = &Pointer_setProperty;
+ definition.finalize = &CYData::Finalize;
+ Pointer_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Selector";
+ definition.staticValues = CYValue_staticValues;
+ //definition.staticValues = Selector_staticValues;
+ definition.staticFunctions = Selector_staticFunctions;
+ definition.callAsFunction = &Selector_callAsFunction;
+ definition.finalize = &CYData::Finalize;
+ Selector_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Struct";
+ definition.getProperty = &Struct_getProperty;
+ definition.setProperty = &Struct_setProperty;
+ definition.getPropertyNames = &Struct_getPropertyNames;
+ definition.finalize = &CYData::Finalize;
+ Struct_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Type";
+ definition.callAsFunction = &Type_callAsFunction;
+ definition.callAsConstructor = &Type_callAsConstructor;
+ definition.finalize = &CYData::Finalize;
+ Type_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ definition.className = "Runtime";
+ definition.getProperty = &Runtime_getProperty;
+ Runtime_ = JSClassCreate(&definition);
+
+ definition = kJSClassDefinitionEmpty;
+ //definition.getProperty = &Global_getProperty;
+ JSClassRef Global(JSClassCreate(&definition));
+
+ JSGlobalContextRef context(JSGlobalContextCreate(Global));
+ Context_ = context;
+
+ JSObjectRef global(CYGetGlobalObject(context));
+
+ JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
+ CYSetProperty(context, global, CYJSString("ObjectiveC"), JSObjectMake(context, Runtime_, NULL));
+
+ CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
+ CYSetProperty(context, global, CYJSString("Instance"), JSObjectMakeConstructor(context, Instance_, NULL));
+ CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
+ CYSetProperty(context, global, CYJSString("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
+ CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_, &Type_new));
+
+ MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
+
+ 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));
+
+ System_ = JSObjectMake(context, NULL, NULL);
+ CYSetProperty(context, global, CYJSString("system"), System_);
+ CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
+ //CYSetProperty(context, System_, CYJSString("global"), global);
+
+ CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
+
+ Result_ = JSStringCreateWithUTF8CString("_");
+
+ length_ = JSStringCreateWithUTF8CString("length");
+ message_ = JSStringCreateWithUTF8CString("message");
+ name_ = JSStringCreateWithUTF8CString("name");
+ toCYON_ = JSStringCreateWithUTF8CString("toCYON");
+ toJSON_ = JSStringCreateWithUTF8CString("toJSON");
+
+ Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
+ Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+ }
+
+ return Context_;