objc_utility.mm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2004 Apple Computer, 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_utility.h"
  27. #include "objc_instance.h"
  28. #include "runtime_array.h"
  29. #include "runtime_object.h"
  30. #include "WebScriptObject.h"
  31. #include <runtime/JSGlobalObject.h>
  32. #include <runtime/JSLock.h>
  33. #include <wtf/Assertions.h>
  34. #if !defined(_C_LNG_LNG)
  35. #define _C_LNG_LNG 'q'
  36. #endif
  37. #if !defined(_C_ULNG_LNG)
  38. #define _C_ULNG_LNG 'Q'
  39. #endif
  40. #if !defined(_C_CONST)
  41. #define _C_CONST 'r'
  42. #endif
  43. #if !defined(_C_BYCOPY)
  44. #define _C_BYCOPY 'O'
  45. #endif
  46. #if !defined(_C_BYREF)
  47. #define _C_BYREF 'R'
  48. #endif
  49. #if !defined(_C_ONEWAY)
  50. #define _C_ONEWAY 'V'
  51. #endif
  52. #if !defined(_C_GCINVISIBLE)
  53. #define _C_GCINVISIBLE '!'
  54. #endif
  55. namespace JSC {
  56. namespace Bindings {
  57. /*
  58. JavaScript to ObjC
  59. Number coerced to char, short, int, long, float, double, or NSNumber, as appropriate
  60. String NSString
  61. wrapper id
  62. Object WebScriptObject
  63. null NSNull
  64. [], other exception
  65. */
  66. ObjcValue convertValueToObjcValue(ExecState* exec, JSValue value, ObjcValueType type)
  67. {
  68. ObjcValue result;
  69. double d = 0;
  70. if (value.isNumber() || value.isString() || value.isBoolean())
  71. d = value.toNumber(exec);
  72. switch (type) {
  73. case ObjcObjectType: {
  74. JSLockHolder lock(exec);
  75. JSGlobalObject *originGlobalObject = exec->dynamicGlobalObject();
  76. RootObject* originRootObject = findRootObject(originGlobalObject);
  77. JSGlobalObject* globalObject = 0;
  78. if (value.isObject() && asObject(value)->isGlobalObject())
  79. globalObject = jsCast<JSGlobalObject*>(asObject(value));
  80. if (!globalObject)
  81. globalObject = originGlobalObject;
  82. RootObject* rootObject = findRootObject(globalObject);
  83. result.objectValue = rootObject
  84. ? [webScriptObjectClass() _convertValueToObjcValue:value originRootObject:originRootObject rootObject:rootObject]
  85. : nil;
  86. }
  87. break;
  88. case ObjcCharType:
  89. case ObjcUnsignedCharType:
  90. result.charValue = (char)d;
  91. break;
  92. case ObjcShortType:
  93. case ObjcUnsignedShortType:
  94. result.shortValue = (short)d;
  95. break;
  96. case ObjcBoolType:
  97. result.booleanValue = (bool)d;
  98. break;
  99. case ObjcIntType:
  100. case ObjcUnsignedIntType:
  101. result.intValue = (int)d;
  102. break;
  103. case ObjcLongType:
  104. case ObjcUnsignedLongType:
  105. result.longValue = (long)d;
  106. break;
  107. case ObjcLongLongType:
  108. case ObjcUnsignedLongLongType:
  109. result.longLongValue = (long long)d;
  110. break;
  111. case ObjcFloatType:
  112. result.floatValue = (float)d;
  113. break;
  114. case ObjcDoubleType:
  115. result.doubleValue = (double)d;
  116. break;
  117. case ObjcVoidType:
  118. bzero(&result, sizeof(ObjcValue));
  119. break;
  120. case ObjcInvalidType:
  121. default:
  122. // FIXME: throw an exception?
  123. break;
  124. }
  125. return result;
  126. }
  127. JSValue convertNSStringToString(ExecState* exec, NSString *nsstring)
  128. {
  129. JSLockHolder lock(exec);
  130. JSValue aValue = jsString(exec, String(nsstring));
  131. return aValue;
  132. }
  133. /*
  134. ObjC to JavaScript
  135. ---- ----------
  136. char number
  137. short number
  138. int number
  139. long number
  140. float number
  141. double number
  142. NSNumber boolean or number
  143. NSString string
  144. NSArray array
  145. NSNull null
  146. WebScriptObject underlying JavaScript object
  147. WebUndefined undefined
  148. id object wrapper
  149. other should not happen
  150. */
  151. JSValue convertObjcValueToValue(ExecState* exec, void* buffer, ObjcValueType type, RootObject* rootObject)
  152. {
  153. JSLockHolder lock(exec);
  154. switch (type) {
  155. case ObjcObjectType: {
  156. id obj = *(id*)buffer;
  157. if ([obj isKindOfClass:[NSString class]])
  158. return convertNSStringToString(exec, (NSString *)obj);
  159. if ([obj isKindOfClass:webUndefinedClass()])
  160. return jsUndefined();
  161. if ((CFBooleanRef)obj == kCFBooleanTrue)
  162. return jsBoolean(true);
  163. if ((CFBooleanRef)obj == kCFBooleanFalse)
  164. return jsBoolean(false);
  165. if ([obj isKindOfClass:[NSNumber class]])
  166. return jsNumber([obj doubleValue]);
  167. if ([obj isKindOfClass:[NSArray class]])
  168. return RuntimeArray::create(exec, new ObjcArray(obj, rootObject));
  169. if ([obj isKindOfClass:webScriptObjectClass()]) {
  170. JSObject* imp = [obj _imp];
  171. return imp ? imp : jsUndefined();
  172. }
  173. if ([obj isKindOfClass:[NSNull class]])
  174. return jsNull();
  175. if (obj == 0)
  176. return jsUndefined();
  177. return ObjcInstance::create(obj, rootObject)->createRuntimeObject(exec);
  178. }
  179. case ObjcCharType:
  180. return jsNumber(*(char*)buffer);
  181. case ObjcUnsignedCharType:
  182. return jsNumber(*(unsigned char*)buffer);
  183. case ObjcShortType:
  184. return jsNumber(*(short*)buffer);
  185. case ObjcUnsignedShortType:
  186. return jsNumber(*(unsigned short*)buffer);
  187. case ObjcBoolType:
  188. return jsBoolean(*(bool*)buffer);
  189. case ObjcIntType:
  190. return jsNumber(*(int*)buffer);
  191. case ObjcUnsignedIntType:
  192. return jsNumber(*(unsigned int*)buffer);
  193. case ObjcLongType:
  194. return jsNumber(*(long*)buffer);
  195. case ObjcUnsignedLongType:
  196. return jsNumber(*(unsigned long*)buffer);
  197. case ObjcLongLongType:
  198. return jsNumber(*(long long*)buffer);
  199. case ObjcUnsignedLongLongType:
  200. return jsNumber(*(unsigned long long*)buffer);
  201. case ObjcFloatType:
  202. return jsNumber(*(float*)buffer);
  203. case ObjcDoubleType:
  204. return jsNumber(*(double*)buffer);
  205. default:
  206. // Should never get here. Argument types are filtered.
  207. fprintf(stderr, "%s: invalid type (%d)\n", __PRETTY_FUNCTION__, (int)type);
  208. ASSERT_NOT_REACHED();
  209. }
  210. return jsUndefined();
  211. }
  212. ObjcValueType objcValueTypeForType(const char *type)
  213. {
  214. int typeLength = strlen(type);
  215. ObjcValueType objcValueType = ObjcInvalidType;
  216. for (int i = 0; i < typeLength; ++i) {
  217. char typeChar = type[i];
  218. switch (typeChar) {
  219. case _C_CONST:
  220. case _C_BYCOPY:
  221. case _C_BYREF:
  222. case _C_ONEWAY:
  223. case _C_GCINVISIBLE:
  224. // skip these type modifiers
  225. break;
  226. case _C_ID:
  227. objcValueType = ObjcObjectType;
  228. break;
  229. case _C_CHR:
  230. objcValueType = ObjcCharType;
  231. break;
  232. case _C_UCHR:
  233. objcValueType = ObjcUnsignedCharType;
  234. break;
  235. case _C_SHT:
  236. objcValueType = ObjcShortType;
  237. break;
  238. case _C_USHT:
  239. objcValueType = ObjcUnsignedShortType;
  240. break;
  241. case _C_BOOL:
  242. objcValueType = ObjcBoolType;
  243. break;
  244. case _C_INT:
  245. objcValueType = ObjcIntType;
  246. break;
  247. case _C_UINT:
  248. objcValueType = ObjcUnsignedIntType;
  249. break;
  250. case _C_LNG:
  251. objcValueType = ObjcLongType;
  252. break;
  253. case _C_ULNG:
  254. objcValueType = ObjcUnsignedLongType;
  255. break;
  256. case _C_LNG_LNG:
  257. objcValueType = ObjcLongLongType;
  258. break;
  259. case _C_ULNG_LNG:
  260. objcValueType = ObjcUnsignedLongLongType;
  261. break;
  262. case _C_FLT:
  263. objcValueType = ObjcFloatType;
  264. break;
  265. case _C_DBL:
  266. objcValueType = ObjcDoubleType;
  267. break;
  268. case _C_VOID:
  269. objcValueType = ObjcVoidType;
  270. break;
  271. default:
  272. // Unhandled type. We don't handle C structs, unions, etc.
  273. // FIXME: throw an exception?
  274. WTFLogAlways("Unhandled ObjC type specifier: \"%c\" used in ObjC bridge.", typeChar);
  275. RELEASE_ASSERT_NOT_REACHED();
  276. }
  277. if (objcValueType != ObjcInvalidType)
  278. break;
  279. }
  280. return objcValueType;
  281. }
  282. JSObject *throwError(ExecState *exec, NSString *message)
  283. {
  284. ASSERT(message);
  285. JSObject *error = JSC::throwError(exec, JSC::createError(exec, String(message)));
  286. return error;
  287. }
  288. }
  289. }