objc_class.mm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2004, 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "objc_class.h"
  27. #include "objc_instance.h"
  28. #include "WebScriptObject.h"
  29. namespace JSC {
  30. namespace Bindings {
  31. ObjcClass::ObjcClass(ClassStructPtr aClass)
  32. : _isa(aClass)
  33. {
  34. }
  35. static CFMutableDictionaryRef classesByIsA = 0;
  36. static void _createClassesByIsAIfNecessary()
  37. {
  38. if (!classesByIsA)
  39. classesByIsA = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
  40. }
  41. ObjcClass* ObjcClass::classForIsA(ClassStructPtr isa)
  42. {
  43. _createClassesByIsAIfNecessary();
  44. ObjcClass* aClass = (ObjcClass*)CFDictionaryGetValue(classesByIsA, isa);
  45. if (!aClass) {
  46. aClass = new ObjcClass(isa);
  47. CFDictionaryAddValue(classesByIsA, isa, aClass);
  48. }
  49. return aClass;
  50. }
  51. /*
  52. By default, a JavaScript method name is produced by concatenating the
  53. components of an ObjectiveC method name, replacing ':' with '_', and
  54. escaping '_' and '$' with a leading '$', such that '_' becomes "$_" and
  55. '$' becomes "$$". For example:
  56. ObjectiveC name Default JavaScript name
  57. moveTo:: moveTo__
  58. moveTo_ moveTo$_
  59. moveTo$_ moveTo$$$_
  60. This function performs the inverse of that operation.
  61. @result Fills 'buffer' with the ObjectiveC method name that corresponds to 'JSName'.
  62. */
  63. typedef Vector<char, 256> JSNameConversionBuffer;
  64. static inline void convertJSMethodNameToObjc(const CString& jsName, JSNameConversionBuffer& buffer)
  65. {
  66. buffer.reserveInitialCapacity(jsName.length() + 1);
  67. const char* source = jsName.data();
  68. while (true) {
  69. if (*source == '$') {
  70. ++source;
  71. buffer.uncheckedAppend(*source);
  72. } else if (*source == '_')
  73. buffer.uncheckedAppend(':');
  74. else
  75. buffer.uncheckedAppend(*source);
  76. if (!*source)
  77. return;
  78. ++source;
  79. }
  80. }
  81. Method* ObjcClass::methodNamed(PropertyName propertyName, Instance*) const
  82. {
  83. String name(propertyName.publicName());
  84. if (name.isNull())
  85. return 0;
  86. if (Method* method = m_methodCache.get(name.impl()))
  87. return method;
  88. CString jsName = name.ascii();
  89. JSNameConversionBuffer buffer;
  90. convertJSMethodNameToObjc(jsName, buffer);
  91. RetainPtr<CFStringRef> methodName = adoptCF(CFStringCreateWithCString(NULL, buffer.data(), kCFStringEncodingASCII));
  92. Method* methodPtr = 0;
  93. ClassStructPtr thisClass = _isa;
  94. while (thisClass && !methodPtr) {
  95. unsigned numMethodsInClass = 0;
  96. MethodStructPtr* objcMethodList = class_copyMethodList(thisClass, &numMethodsInClass);
  97. for (unsigned i = 0; i < numMethodsInClass; i++) {
  98. MethodStructPtr objcMethod = objcMethodList[i];
  99. SEL objcMethodSelector = method_getName(objcMethod);
  100. const char* objcMethodSelectorName = sel_getName(objcMethodSelector);
  101. NSString* mappedName = nil;
  102. // See if the class wants to exclude the selector from visibility in JavaScript.
  103. if ([thisClass respondsToSelector:@selector(isSelectorExcludedFromWebScript:)])
  104. if ([thisClass isSelectorExcludedFromWebScript:objcMethodSelector])
  105. continue;
  106. // See if the class want to provide a different name for the selector in JavaScript.
  107. // Note that we do not do any checks to guarantee uniqueness. That's the responsiblity
  108. // of the class.
  109. if ([thisClass respondsToSelector:@selector(webScriptNameForSelector:)])
  110. mappedName = [thisClass webScriptNameForSelector:objcMethodSelector];
  111. if ((mappedName && [mappedName isEqual:(NSString*)methodName.get()]) || strcmp(objcMethodSelectorName, buffer.data()) == 0) {
  112. OwnPtr<Method> method = adoptPtr(new ObjcMethod(thisClass, objcMethodSelector));
  113. methodPtr = method.get();
  114. m_methodCache.add(name.impl(), method.release());
  115. break;
  116. }
  117. }
  118. thisClass = class_getSuperclass(thisClass);
  119. free(objcMethodList);
  120. }
  121. return methodPtr;
  122. }
  123. Field* ObjcClass::fieldNamed(PropertyName propertyName, Instance* instance) const
  124. {
  125. String name(propertyName.publicName());
  126. if (name.isNull())
  127. return 0;
  128. Field* field = m_fieldCache.get(name.impl());
  129. if (field)
  130. return field;
  131. ClassStructPtr thisClass = _isa;
  132. CString jsName = name.ascii();
  133. RetainPtr<CFStringRef> fieldName = adoptCF(CFStringCreateWithCString(NULL, jsName.data(), kCFStringEncodingASCII));
  134. id targetObject = (static_cast<ObjcInstance*>(instance))->getObject();
  135. id attributes = [targetObject attributeKeys];
  136. if (attributes) {
  137. // Class overrides attributeKeys, use that array of key names.
  138. unsigned count = [attributes count];
  139. for (unsigned i = 0; i < count; i++) {
  140. NSString* keyName = [attributes objectAtIndex:i];
  141. const char* UTF8KeyName = [keyName UTF8String]; // ObjC actually only supports ASCII names.
  142. // See if the class wants to exclude the selector from visibility in JavaScript.
  143. if ([thisClass respondsToSelector:@selector(isKeyExcludedFromWebScript:)])
  144. if ([thisClass isKeyExcludedFromWebScript:UTF8KeyName])
  145. continue;
  146. // See if the class want to provide a different name for the selector in JavaScript.
  147. // Note that we do not do any checks to guarantee uniqueness. That's the responsiblity
  148. // of the class.
  149. NSString* mappedName = nil;
  150. if ([thisClass respondsToSelector:@selector(webScriptNameForKey:)])
  151. mappedName = [thisClass webScriptNameForKey:UTF8KeyName];
  152. if ((mappedName && [mappedName isEqual:(NSString*)fieldName.get()]) || [keyName isEqual:(NSString*)fieldName.get()]) {
  153. OwnPtr<Field> newField = adoptPtr(new ObjcField((CFStringRef)keyName));
  154. field = newField.get();
  155. m_fieldCache.add(name.impl(), newField.release());
  156. break;
  157. }
  158. }
  159. } else {
  160. // Class doesn't override attributeKeys, so fall back on class runtime
  161. // introspection.
  162. while (thisClass) {
  163. unsigned numFieldsInClass = 0;
  164. IvarStructPtr* ivarsInClass = class_copyIvarList(thisClass, &numFieldsInClass);
  165. for (unsigned i = 0; i < numFieldsInClass; i++) {
  166. IvarStructPtr objcIVar = ivarsInClass[i];
  167. const char* objcIvarName = ivar_getName(objcIVar);
  168. NSString* mappedName = 0;
  169. // See if the class wants to exclude the selector from visibility in JavaScript.
  170. if ([thisClass respondsToSelector:@selector(isKeyExcludedFromWebScript:)])
  171. if ([thisClass isKeyExcludedFromWebScript:objcIvarName])
  172. continue;
  173. // See if the class want to provide a different name for the selector in JavaScript.
  174. // Note that we do not do any checks to guarantee uniqueness. That's the responsiblity
  175. // of the class.
  176. if ([thisClass respondsToSelector:@selector(webScriptNameForKey:)])
  177. mappedName = [thisClass webScriptNameForKey:objcIvarName];
  178. if ((mappedName && [mappedName isEqual:(NSString*)fieldName.get()]) || strcmp(objcIvarName, jsName.data()) == 0) {
  179. OwnPtr<Field> newField = adoptPtr(new ObjcField(objcIVar));
  180. field = newField.get();
  181. m_fieldCache.add(name.impl(), newField.release());
  182. break;
  183. }
  184. }
  185. thisClass = class_getSuperclass(thisClass);
  186. free(ivarsInClass);
  187. }
  188. }
  189. return field;
  190. }
  191. JSValue ObjcClass::fallbackObject(ExecState* exec, Instance* instance, PropertyName propertyName)
  192. {
  193. ObjcInstance* objcInstance = static_cast<ObjcInstance*>(instance);
  194. id targetObject = objcInstance->getObject();
  195. if (![targetObject respondsToSelector:@selector(invokeUndefinedMethodFromWebScript:withArguments:)])
  196. return jsUndefined();
  197. return ObjcFallbackObjectImp::create(exec, exec->lexicalGlobalObject(), objcInstance, propertyName.publicName());
  198. }
  199. }
  200. }