X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/37954781d9756ece500551055562183a1e28e943..0226d428e0b3b0913a0c067f86cbf90339cd4c92:/Handler.mm?ds=sidebyside diff --git a/Handler.mm b/Handler.mm index 238d080..79528c4 100644 --- a/Handler.mm +++ b/Handler.mm @@ -1,4 +1,4 @@ -/* Cycript - Remote Execution Server and Disassembler +/* Cycript - Inlining/Optimizing JavaScript Compiler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -49,6 +49,11 @@ #include #include +#include +#include +#include +#include + struct CYExecute_ { apr_pool_t *pool_; const char * volatile data_; @@ -89,8 +94,16 @@ struct CYClient : } void Handle() { - CYClient_ *client = [[CYClient_ alloc] init]; - @try { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + CYClient_ *client = [[[CYClient_ alloc] init] autorelease]; + + bool dispatch; + if (CFStringRef mode = CFRunLoopCopyCurrentMode(CFRunLoopGetMain())) { + dispatch = true; + CFRelease(mode); + } else + dispatch = false; for (;;) { size_t size; @@ -114,6 +127,8 @@ struct CYClient : json = NULL; size = _not(size_t); } else { + NSAutoreleasePool *ar = [[NSAutoreleasePool alloc] init]; + CYContext context(driver.pool_); driver.program_->Replace(context); std::ostringstream str; @@ -121,9 +136,15 @@ struct CYClient : out << *driver.program_; std::string code(str.str()); CYExecute_ execute = {pool, code.c_str()}; - [client performSelectorOnMainThread:@selector(execute:) withObject:[NSValue valueWithPointer:&execute] waitUntilDone:YES]; + NSValue *value([NSValue valueWithPointer:&execute]); + if (dispatch) + [client performSelectorOnMainThread:@selector(execute:) withObject:value waitUntilDone:YES]; + else + [client execute:value]; json = execute.data_; size = json == NULL ? _not(size_t) : strlen(json); + + [ar release]; } if (!CYSendAll(socket_, &size, sizeof(size))) @@ -133,9 +154,7 @@ struct CYClient : return; } - } @finally { - [client release]; - } + [pool release]; } }; @@ -152,3 +171,24 @@ extern "C" void CYHandleClient(apr_pool_t *pool, int socket) { _aprcall(apr_threadattr_create(&attr, client->pool_)); _aprcall(apr_thread_create(&client->thread_, attr, &OnClient, client, client->pool_)); } + +extern "C" void CYHandleServer(pid_t pid) { + CYInitialize(); + + int socket(_syscall(::socket(PF_UNIX, SOCK_STREAM, 0))); try { + struct sockaddr_un address; + memset(&address, 0, sizeof(address)); + address.sun_family = AF_UNIX; + sprintf(address.sun_path, "/tmp/.s.cy.%u", pid); + + _syscall(connect(socket, reinterpret_cast(&address), SUN_LEN(&address))); + + apr_pool_t *pool; + apr_pool_create(&pool, NULL); + + CYHandleClient(pool, socket); + } catch (const CYException &error) { + CYPool pool; + fprintf(stderr, "%s\n", error.PoolCString(pool)); + } +}