X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/4cb8aa43e7ccadcc4b7868ae45974ffc5f272c3d..0939f8690378961597bb9bfc4820465e66387936:/ObjectiveC/Library.mm diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index c1cacf8..2cb81b7 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -1,48 +1,24 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2010 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU Lesser General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * Cycript is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. * - * 1. Redistributions of source code must retain the - * above copyright notice, this list of conditions - * and the following disclaimer. - * 2. Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions - * and the following disclaimer in the documentation - * and/or other materials provided with the - * distribution. - * 3. The name of the author may not be used to endorse - * or promote products derived from this software - * without specific prior written permission. + * Cycript is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * You should have received a copy of the GNU Lesser General Public License + * along with Cycript. If not, see . +**/ /* }}} */ -#if defined(__APPLE__) && defined(__arm__) -#include -#else -#include -#endif - #ifdef __APPLE__ #include "Struct.hpp" #endif @@ -51,7 +27,7 @@ #include "ObjectiveC/Internal.hpp" -#include +#include #include "cycript.hpp" @@ -61,6 +37,7 @@ #include #include #include +#include #endif #include "Error.hpp" @@ -131,6 +108,7 @@ #define object_getInstanceVariable(object, name, value) ({ \ objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \ + _assert(value != NULL); \ if (ivar != NULL) \ GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \ ivar; \ @@ -260,6 +238,7 @@ static JSClassRef Internal_; static JSClassRef Message_; static JSClassRef Messages_; static JSClassRef Selector_; +static JSClassRef StringInstance_; static JSClassRef Super_; static JSClassRef ObjectiveC_Classes_; @@ -315,19 +294,16 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) { JSClassRef _class(NULL); JSValueRef prototype; - JSObjectRef object(JSObjectMake(context, _class, NULL)); - if (self == NSArray_) prototype = CYGetCachedObject(context, CYJSString("Array_prototype")); else if (self == NSDictionary_) prototype = CYGetCachedObject(context, CYJSString("Object_prototype")); - else if (self == NSString_) { - prototype = CYGetCachedObject(context, CYJSString("String_prototype")); - - CYSetProperty(context, object, toString_s, &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); - } else + else if (self == NSString_) + prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype")); + else prototype = CYGetClassPrototype(context, class_getSuperclass(self)); + JSObjectRef object(JSObjectMake(context, _class, NULL)); JSObjectSetPrototype(context, object, prototype); CYSetProperty(context, cy, name, object); @@ -2135,7 +2111,7 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje } CYCatch } /* Hook: objc_registerClassPair {{{ */ -#if defined(__APPLE__) && defined(__arm__) +#if defined(__APPLE__) && defined(__arm__) && 0 // XXX: replace this with associated objects MSHook(void, CYDealloc, id self, SEL sel) { @@ -2395,6 +2371,12 @@ static JSStaticFunction Selector_staticFunctions[5] = { {NULL, NULL, 0} }; +static JSStaticFunction StringInstance_staticFunctions[2] = { + //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { apr_pool_t *pool(CYGetGlobalPool()); @@ -2468,6 +2450,11 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition.finalize = &CYFinalize; Selector_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "StringInstance"; + definition.staticFunctions = StringInstance_staticFunctions; + StringInstance_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "Super"; definition.staticFunctions = Internal_staticFunctions; @@ -2500,7 +2487,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; ObjectiveC_Protocols_ = JSClassCreate(&definition); -#if defined(__APPLE__) && defined(__arm__) +#if defined(__APPLE__) && defined(__arm__) && 0 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); #endif @@ -2528,16 +2515,23 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new)); JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL)); JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); + JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL)); JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s))); CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype); + JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s))); + CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype); + + JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype"))); + JSObjectSetPrototype(context, StringInstance_prototype, String_prototype); + CYSetProperty(context, cycript, CYJSString("Instance"), Instance); CYSetProperty(context, cycript, CYJSString("Selector"), Selector); CYSetProperty(context, cycript, CYJSString("Super"), Super); -#if defined(__APPLE__) && defined(__arm__) +#if defined(__APPLE__) && defined(__arm__) && 0 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum); #endif