APICast.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2006 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 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 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. #ifndef APICast_h
  26. #define APICast_h
  27. #include "JSAPIValueWrapper.h"
  28. #include "JSCJSValue.h"
  29. #include "JSCJSValueInlines.h"
  30. #include "JSGlobalObject.h"
  31. #include "HeapCellInlines.h"
  32. namespace JSC {
  33. class ExecState;
  34. class PropertyNameArray;
  35. class VM;
  36. class JSObject;
  37. class JSValue;
  38. }
  39. typedef const struct OpaqueJSContextGroup* JSContextGroupRef;
  40. typedef const struct OpaqueJSContext* JSContextRef;
  41. typedef struct OpaqueJSContext* JSGlobalContextRef;
  42. typedef struct OpaqueJSPropertyNameAccumulator* JSPropertyNameAccumulatorRef;
  43. typedef const struct OpaqueJSValue* JSValueRef;
  44. typedef struct OpaqueJSValue* JSObjectRef;
  45. /* Opaque typing convenience methods */
  46. inline JSC::ExecState* toJS(JSContextRef c)
  47. {
  48. ASSERT(c);
  49. return reinterpret_cast<JSC::ExecState*>(const_cast<OpaqueJSContext*>(c));
  50. }
  51. inline JSC::ExecState* toJS(JSGlobalContextRef c)
  52. {
  53. ASSERT(c);
  54. return reinterpret_cast<JSC::ExecState*>(c);
  55. }
  56. inline JSC::JSGlobalObject* toJSGlobalObject(JSGlobalContextRef context)
  57. {
  58. return toJS(context)->lexicalGlobalObject();
  59. }
  60. inline JSC::JSValue toJS(JSC::ExecState* exec, JSValueRef v)
  61. {
  62. ASSERT_UNUSED(exec, exec);
  63. #if !CPU(ADDRESS64)
  64. JSC::JSCell* jsCell = reinterpret_cast<JSC::JSCell*>(const_cast<OpaqueJSValue*>(v));
  65. if (!jsCell)
  66. return JSC::jsNull();
  67. JSC::JSValue result;
  68. if (jsCell->isAPIValueWrapper())
  69. result = JSC::jsCast<JSC::JSAPIValueWrapper*>(jsCell)->value();
  70. else
  71. result = jsCell;
  72. #else
  73. JSC::JSValue result = bitwise_cast<JSC::JSValue>(v);
  74. #endif
  75. if (!result)
  76. return JSC::jsNull();
  77. if (result.isCell())
  78. RELEASE_ASSERT(result.asCell()->methodTable(exec->vm()));
  79. return result;
  80. }
  81. inline JSC::JSValue toJSForGC(JSC::ExecState* exec, JSValueRef v)
  82. {
  83. ASSERT_UNUSED(exec, exec);
  84. #if !CPU(ADDRESS64)
  85. JSC::JSCell* jsCell = reinterpret_cast<JSC::JSCell*>(const_cast<OpaqueJSValue*>(v));
  86. if (!jsCell)
  87. return JSC::JSValue();
  88. JSC::JSValue result = jsCell;
  89. #else
  90. JSC::JSValue result = bitwise_cast<JSC::JSValue>(v);
  91. #endif
  92. if (result && result.isCell())
  93. RELEASE_ASSERT(result.asCell()->methodTable(exec->vm()));
  94. return result;
  95. }
  96. // Used in JSObjectGetPrivate as that may be called during finalization
  97. inline JSC::JSObject* uncheckedToJS(JSObjectRef o)
  98. {
  99. return reinterpret_cast<JSC::JSObject*>(o);
  100. }
  101. inline JSC::JSObject* toJS(JSObjectRef o)
  102. {
  103. JSC::JSObject* object = uncheckedToJS(o);
  104. if (object)
  105. RELEASE_ASSERT(object->methodTable(*object->vm()));
  106. return object;
  107. }
  108. inline JSC::PropertyNameArray* toJS(JSPropertyNameAccumulatorRef a)
  109. {
  110. return reinterpret_cast<JSC::PropertyNameArray*>(a);
  111. }
  112. inline JSC::VM* toJS(JSContextGroupRef g)
  113. {
  114. return reinterpret_cast<JSC::VM*>(const_cast<OpaqueJSContextGroup*>(g));
  115. }
  116. inline JSValueRef toRef(JSC::VM& vm, JSC::JSValue v)
  117. {
  118. ASSERT(vm.currentThreadIsHoldingAPILock());
  119. #if !CPU(ADDRESS64)
  120. if (!v)
  121. return 0;
  122. if (!v.isCell())
  123. return reinterpret_cast<JSValueRef>(JSC::JSAPIValueWrapper::create(vm, v));
  124. return reinterpret_cast<JSValueRef>(v.asCell());
  125. #else
  126. UNUSED_PARAM(vm);
  127. return bitwise_cast<JSValueRef>(v);
  128. #endif
  129. }
  130. inline JSValueRef toRef(JSC::ExecState* exec, JSC::JSValue v)
  131. {
  132. return toRef(exec->vm(), v);
  133. }
  134. inline JSObjectRef toRef(JSC::JSObject* o)
  135. {
  136. return reinterpret_cast<JSObjectRef>(o);
  137. }
  138. inline JSObjectRef toRef(const JSC::JSObject* o)
  139. {
  140. return reinterpret_cast<JSObjectRef>(const_cast<JSC::JSObject*>(o));
  141. }
  142. inline JSContextRef toRef(JSC::ExecState* e)
  143. {
  144. return reinterpret_cast<JSContextRef>(e);
  145. }
  146. inline JSGlobalContextRef toGlobalRef(JSC::ExecState* e)
  147. {
  148. ASSERT(e == e->lexicalGlobalObject()->globalExec());
  149. return reinterpret_cast<JSGlobalContextRef>(e);
  150. }
  151. inline JSPropertyNameAccumulatorRef toRef(JSC::PropertyNameArray* l)
  152. {
  153. return reinterpret_cast<JSPropertyNameAccumulatorRef>(l);
  154. }
  155. inline JSContextGroupRef toRef(JSC::VM* g)
  156. {
  157. return reinterpret_cast<JSContextGroupRef>(g);
  158. }
  159. #endif // APICast_h