]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Started the refactoring required for multi-context.
[cycript.git] / Library.cpp
index 029797db952d6bec28781805bb9ac96136371a92..ca7b1af82621cbba0e8fe791ec0a0997e1c67397 100644 (file)
@@ -112,6 +112,10 @@ void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, J
     JSObjectSetProperty(context, object, name, value, attributes, &exception);
     CYThrow(context, exception);
 }
+
+void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone) {
+    CYSetProperty(context, object, name, JSObjectMakeFunctionWithCallback(context, name, callback), attributes);
+}
 /* }}} */
 /* JavaScript Strings {{{ */
 JSStringRef CYCopyJSString(const char *value) {
@@ -300,31 +304,31 @@ static JSGlobalContextRef Context_;
 static JSObjectRef System_;
 
 static JSClassRef Functor_;
+static JSClassRef Global_;
 static JSClassRef Pointer_;
 static JSClassRef Runtime_;
 static JSClassRef Struct_;
 
-static JSStringRef Result_;
-
 JSObjectRef Array_;
 JSObjectRef Error_;
 JSObjectRef Function_;
 JSObjectRef String_;
 
-JSStringRef length_;
-JSStringRef message_;
-JSStringRef name_;
-JSStringRef prototype_;
-JSStringRef toCYON_;
-JSStringRef toJSON_;
-
-JSObjectRef Object_prototype_;
+JSObjectRef Array_prototype_;
 JSObjectRef Function_prototype_;
+JSObjectRef Object_prototype_;
 
-JSObjectRef Array_prototype_;
-JSObjectRef Array_pop_;
-JSObjectRef Array_push_;
-JSObjectRef Array_splice_;
+JSStringRef length_s;
+JSStringRef message_s;
+JSStringRef name_s;
+JSStringRef pop_s;
+JSStringRef prototype_s;
+JSStringRef push_s;
+JSStringRef splice_s;
+JSStringRef toCYON_s;
+JSStringRef toJSON_s;
+
+static JSStringRef Result_;
 
 sqlite3 *Bridge_;
 
@@ -589,13 +593,13 @@ const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSValueRef value
 }
 
 const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
-    JSValueRef toCYON(CYGetProperty(context, object, toCYON_));
+    JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
     if (CYIsCallable(context, toCYON)) {
         JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
         return CYPoolCString(pool, context, value);
     }
 
-    JSValueRef toJSON(CYGetProperty(context, object, toJSON_));
+    JSValueRef toJSON(CYGetProperty(context, object, toJSON_s));
     if (CYIsCallable(context, toJSON)) {
         JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
         JSValueRef exception(NULL);
@@ -645,7 +649,7 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef
 
     str << '[';
 
-    JSValueRef length(CYGetProperty(context, _this, length_));
+    JSValueRef length(CYGetProperty(context, _this, length_s));
     bool comma(false);
 
     for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) {
@@ -865,7 +869,7 @@ static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void
 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *)) {
     // XXX: in case of exceptions this will leak
     // XXX: in point of fact, this may /need/ to leak :(
-    Closure_privateData *internal(new Closure_privateData(CYGetJSContext(), function, type));
+    Closure_privateData *internal(new Closure_privateData(context, function, type));
 
     ffi_closure *closure((ffi_closure *) _syscall(mmap(
         NULL, sizeof(ffi_closure),
@@ -949,7 +953,7 @@ static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object,
     CYPool pool;
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
 
-    if (JSStringIsEqual(property, length_))
+    if (JSStringIsEqual(property, length_s))
         return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_);
 
     Type_privateData *typical(internal->type_);
@@ -1146,7 +1150,7 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object,
             return NULL;
 
         case 0:
-            return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL);
+            return JSEvaluateScript(CYGetJSContext(context), CYJSString(value), NULL, NULL, 0, NULL);
 
         case 1:
             if (void (*symbol)() = reinterpret_cast<void (*)()>(CYCastSymbol(name.data)))
@@ -1422,6 +1426,65 @@ void CYInitialize() {
     _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_));
 
     JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
+
+    JSClassDefinition definition;
+
+    definition = kJSClassDefinitionEmpty;
+    definition.className = "Functor";
+    definition.staticFunctions = cy::Functor::StaticFunctions;
+    definition.callAsFunction = &Functor_callAsFunction;
+    definition.finalize = &CYFinalize;
+    Functor_ = JSClassCreate(&definition);
+
+    definition = kJSClassDefinitionEmpty;
+    definition.className = "Pointer";
+    definition.staticFunctions = Pointer_staticFunctions;
+    definition.getProperty = &Pointer_getProperty;
+    definition.setProperty = &Pointer_setProperty;
+    definition.finalize = &CYFinalize;
+    Pointer_ = JSClassCreate(&definition);
+
+    definition = kJSClassDefinitionEmpty;
+    definition.className = "Struct";
+    definition.staticFunctions = Struct_staticFunctions;
+    definition.getProperty = &Struct_getProperty;
+    definition.setProperty = &Struct_setProperty;
+    definition.getPropertyNames = &Struct_getPropertyNames;
+    definition.finalize = &CYFinalize;
+    Struct_ = JSClassCreate(&definition);
+
+    definition = kJSClassDefinitionEmpty;
+    definition.className = "Type";
+    definition.staticFunctions = Type_staticFunctions;
+    definition.getProperty = &Type_getProperty;
+    definition.callAsFunction = &Type_callAsFunction;
+    definition.callAsConstructor = &Type_callAsConstructor;
+    definition.finalize = &CYFinalize;
+    Type_privateData::Class_ = JSClassCreate(&definition);
+
+    definition = kJSClassDefinitionEmpty;
+    definition.className = "Runtime";
+    definition.getProperty = &Runtime_getProperty;
+    Runtime_ = JSClassCreate(&definition);
+
+    definition = kJSClassDefinitionEmpty;
+    //definition.getProperty = &Global_getProperty;
+    Global_ = JSClassCreate(&definition);
+
+    length_s = JSStringCreateWithUTF8CString("length");
+    message_s = JSStringCreateWithUTF8CString("message");
+    name_s = JSStringCreateWithUTF8CString("name");
+    pop_s = JSStringCreateWithUTF8CString("pop");
+    prototype_s = JSStringCreateWithUTF8CString("prototype");
+    push_s = JSStringCreateWithUTF8CString("push");
+    splice_s = JSStringCreateWithUTF8CString("splice");
+    toCYON_s = JSStringCreateWithUTF8CString("toCYON");
+    toJSON_s = JSStringCreateWithUTF8CString("toJSON");
+
+    Result_ = JSStringCreateWithUTF8CString("_");
+
+    if (hooks_ != NULL && hooks_->Initialize != NULL)
+        (*hooks_->Initialize)();
 }
 
 apr_pool_t *CYGetGlobalPool() {
@@ -1481,8 +1544,7 @@ JSValueRef CYPoolError::CastJSValue(JSContextRef context) const {
 }
 
 CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
-    if (context == NULL)
-        context = CYGetJSContext();
+    _assert(context != NULL);
 
     CYPool pool;
 
@@ -1494,125 +1556,70 @@ CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
     value_ = CYCastJSError(context, message);
 }
 
-JSGlobalContextRef CYGetJSContext() {
-    CYInitialize();
-
-    if (Context_ == NULL) {
-        JSClassDefinition definition;
-
-        definition = kJSClassDefinitionEmpty;
-        definition.className = "Functor";
-        definition.staticFunctions = cy::Functor::StaticFunctions;
-        definition.callAsFunction = &Functor_callAsFunction;
-        definition.finalize = &CYFinalize;
-        Functor_ = JSClassCreate(&definition);
-
-        definition = kJSClassDefinitionEmpty;
-        definition.className = "Pointer";
-        definition.staticFunctions = Pointer_staticFunctions;
-        definition.getProperty = &Pointer_getProperty;
-        definition.setProperty = &Pointer_setProperty;
-        definition.finalize = &CYFinalize;
-        Pointer_ = JSClassCreate(&definition);
-
-        definition = kJSClassDefinitionEmpty;
-        definition.className = "Struct";
-        definition.staticFunctions = Struct_staticFunctions;
-        definition.getProperty = &Struct_getProperty;
-        definition.setProperty = &Struct_setProperty;
-        definition.getPropertyNames = &Struct_getPropertyNames;
-        definition.finalize = &CYFinalize;
-        Struct_ = JSClassCreate(&definition);
-
-        definition = kJSClassDefinitionEmpty;
-        definition.className = "Type";
-        definition.staticFunctions = Type_staticFunctions;
-        definition.getProperty = &Type_getProperty;
-        definition.callAsFunction = &Type_callAsFunction;
-        definition.callAsConstructor = &Type_callAsConstructor;
-        definition.finalize = &CYFinalize;
-        Type_privateData::Class_ = 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));
-
-        Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
-        JSValueProtect(context, Array_);
-
-        Error_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")));
-        JSValueProtect(context, Error_);
+JSGlobalContextRef CYGetJSContext(JSContextRef context) {
+    // XXX: do something better
+    return Context_;
+}
 
-        Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
-        JSValueProtect(context, Function_);
+void CYSetupContext(JSGlobalContextRef context) {
+    JSObjectRef global(CYGetGlobalObject(context));
 
-        String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")));
-        JSValueProtect(context, String_);
+    Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")));
+    JSValueProtect(context, Array_);
+    Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_s));
+    JSValueProtect(context, Array_prototype_);
 
-        length_ = JSStringCreateWithUTF8CString("length");
-        message_ = JSStringCreateWithUTF8CString("message");
-        name_ = JSStringCreateWithUTF8CString("name");
-        prototype_ = JSStringCreateWithUTF8CString("prototype");
-        toCYON_ = JSStringCreateWithUTF8CString("toCYON");
-        toJSON_ = JSStringCreateWithUTF8CString("toJSON");
+    Error_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")));
+    JSValueProtect(context, Error_);
 
-        JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object"))));
-        Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_));
-        JSValueProtect(context, Object_prototype_);
+    Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")));
+    JSValueProtect(context, Function_);
+    Function_prototype_ = (JSObjectRef) CYGetProperty(context, Function_, prototype_s);
+    JSValueProtect(context, Function_prototype_);
 
-        Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_));
-        Array_pop_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("pop")));
-        Array_push_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("push")));
-        Array_splice_ = CYCastJSObject(context, CYGetProperty(context, Array_prototype_, CYJSString("splice")));
+    JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object"))));
+    Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_s));
+    JSValueProtect(context, Object_prototype_);
 
-        CYSetProperty(context, Array_prototype_, toCYON_, JSObjectMakeFunctionWithCallback(context, toCYON_, &Array_callAsFunction_toCYON), kJSPropertyAttributeDontEnum);
+    String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")));
+    JSValueProtect(context, String_);
 
-        JSValueProtect(context, Array_prototype_);
-        JSValueProtect(context, Array_pop_);
-        JSValueProtect(context, Array_push_);
-        JSValueProtect(context, Array_splice_);
+    JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL));
 
-        JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
+    CYSetProperty(context, Array_prototype_, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum);
 
-        Function_prototype_ = (JSObjectRef) CYGetProperty(context, Function_, prototype_);
-        JSValueProtect(context, Function_prototype_);
+    JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
 
-        JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_), Function_prototype_);
+    JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_s), Function_prototype_);
 
-        CYSetProperty(context, global, CYJSString("Functor"), Functor);
-        CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
-        CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new));
+    CYSetProperty(context, global, CYJSString("Functor"), Functor);
+    CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new));
+    CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new));
 
-        JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
-        CYSetProperty(context, global, CYJSString("Cycript"), cycript);
-        CYSetProperty(context, cycript, CYJSString("gc"), JSObjectMakeFunctionWithCallback(context, CYJSString("gc"), &Cycript_gc_callAsFunction));
+    JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
+    CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+    CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction);
 
-        CYSetProperty(context, global, CYJSString("$cyq"), JSObjectMakeFunctionWithCallback(context, CYJSString("$cyq"), &$cyq));
+    CYSetProperty(context, global, CYJSString("$cyq"), &$cyq);
 
-        System_ = JSObjectMake(context, NULL, NULL);
-        JSValueProtect(context, System_);
+    System_ = JSObjectMake(context, NULL, NULL);
+    JSValueProtect(context, System_);
 
-        CYSetProperty(context, global, CYJSString("system"), System_);
-        CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
-        //CYSetProperty(context, System_, CYJSString("global"), global);
+    CYSetProperty(context, global, CYJSString("system"), System_);
+    CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context));
+    //CYSetProperty(context, System_, CYJSString("global"), global);
+    CYSetProperty(context, System_, CYJSString("print"), &System_print);
 
-        CYSetProperty(context, System_, CYJSString("print"), JSObjectMakeFunctionWithCallback(context, CYJSString("print"), &System_print));
+    if (hooks_ != NULL && hooks_->SetupContext != NULL)
+        (*hooks_->SetupContext)(context);
+}
 
-        Result_ = JSStringCreateWithUTF8CString("_");
+JSGlobalContextRef CYGetJSContext() {
+    CYInitialize();
 
-        if (hooks_ != NULL && hooks_->SetupContext != NULL)
-            (*hooks_->SetupContext)(context);
+    if (Context_ == NULL) {
+        Context_ = JSGlobalContextCreate(Global_);
+        CYSetupContext(Context_);
     }
 
     return Context_;