- struct sockaddr_in address;
- address.sin_len = sizeof(address);
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(10000 + pid);
-
- CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address)));
-
- CFSocketSignature signature;
- signature.protocolFamily = AF_INET;
- signature.socketType = SOCK_STREAM;
- signature.protocol = IPPROTO_TCP;
- signature.address = data;
-
- CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL));
- CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode);
-
- JSClassDefinition definition;
-
- definition = kJSClassDefinitionEmpty;
- definition.className = "Pointer";
- definition.staticValues = Pointer_staticValues;
- definition.staticFunctions = Pointer_staticFunctions;
- definition.finalize = &CYData::Finalize;
- Pointer_ = JSClassCreate(&definition);
-
- definition = kJSClassDefinitionEmpty;
- definition.className = "Functor";
- definition.staticValues = Pointer_staticValues;
- definition.staticFunctions = Pointer_staticFunctions;
- definition.callAsFunction = &Functor_callAsFunction;
- definition.finalize = &CYData::Finalize;
- Functor_ = JSClassCreate(&definition);
-
- definition = kJSClassDefinitionEmpty;
- definition.className = "Struct";
- definition.getProperty = &Struct_getProperty;
- definition.setProperty = &Struct_setProperty;
- definition.finalize = &CYData::Finalize;
- Struct_ = JSClassCreate(&definition);
-
- definition = kJSClassDefinitionEmpty;
- definition.className = "Selector";
- definition.staticValues = Pointer_staticValues;
- //definition.staticValues = Selector_staticValues;
- definition.staticFunctions = Selector_staticFunctions;
- definition.callAsFunction = &Selector_callAsFunction;
- definition.finalize = &CYData::Finalize;
- Selector_ = JSClassCreate(&definition);
-
- definition = kJSClassDefinitionEmpty;
- definition.className = "Instance";
- definition.staticValues = Pointer_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 = "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("Selector"), JSObjectMakeConstructor(context, Selector_, &Selector_new));
- CYSetProperty(context, global, CYJSString("Functor"), JSObjectMakeConstructor(context, Functor_, &Functor_new));
-
- CYSetProperty(context, global, CYJSString("CYApplicationMain"), JSObjectMakeFunctionWithCallback(context, CYJSString("CYApplicationMain"), &CYApplicationMain));
- 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));
-
- name_ = JSStringCreateWithUTF8CString("name");
- message_ = JSStringCreateWithUTF8CString("message");
- length_ = JSStringCreateWithUTF8CString("length");
-
- Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
- Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+ 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_;