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