1/*
2 * Copyright (C) 2006-2019 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
35namespace JSC {
36 class CallFrame;
37 class PropertyNameArray;
38 class VM;
39 class JSObject;
40 class JSValue;
41}
42
43typedef const struct OpaqueJSContextGroup* JSContextGroupRef;
44typedef const struct OpaqueJSContext* JSContextRef;
45typedef struct OpaqueJSContext* JSGlobalContextRef;
46typedef struct OpaqueJSPropertyNameAccumulator* JSPropertyNameAccumulatorRef;
47typedef const struct OpaqueJSValue* JSValueRef;
48typedef struct OpaqueJSValue* JSObjectRef;
49
50/* Opaque typing convenience methods */
51
52inline JSC::JSGlobalObject* toJS(JSContextRef context)
53{
54 ASSERT(context);
55 return reinterpret_cast<JSC::JSGlobalObject*>(const_cast<OpaqueJSContext*>(context));
56}
57
58inline JSC::JSGlobalObject* toJS(JSGlobalContextRef context)
59{
60 ASSERT(context);
61 return reinterpret_cast<JSC::JSGlobalObject*>(context);
62}
63
64inline JSC::JSGlobalObject* toJSGlobalObject(JSGlobalContextRef context)
65{
66 return toJS(context);
67}
68
69inline JSC::JSValue toJS(JSC::JSGlobalObject* globalObject, JSValueRef v)
70{
71 ASSERT_UNUSED(globalObject, globalObject);
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(getVM(globalObject)));
88 return result;
89}
90
91inline JSC::JSValue toJSForGC(JSC::JSGlobalObject* globalObject, JSValueRef v)
92{
93 ASSERT_UNUSED(globalObject, globalObject);
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(getVM(globalObject)));
104 return result;
105}
106
107// Used in JSObjectGetPrivate as that may be called during finalization
108inline JSC::JSObject* uncheckedToJS(JSObjectRef o)
109{
110 return reinterpret_cast<JSC::JSObject*>(o);
111}
112
113inline 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
121inline JSC::PropertyNameArray* toJS(JSPropertyNameAccumulatorRef a)
122{
123 return reinterpret_cast<JSC::PropertyNameArray*>(a);
124}
125
126inline JSC::VM* toJS(JSContextGroupRef g)
127{
128 return reinterpret_cast<JSC::VM*>(const_cast<OpaqueJSContextGroup*>(g));
129}
130
131inline 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
146inline JSValueRef toRef(JSC::JSGlobalObject* globalObject, JSC::JSValue v)
147{
148 return toRef(getVM(globalObject), v);
149}
150
151inline JSObjectRef toRef(JSC::JSObject* o)
152{
153 return reinterpret_cast<JSObjectRef>(o);
154}
155
156inline JSObjectRef toRef(const JSC::JSObject* o)
157{
158 return reinterpret_cast<JSObjectRef>(const_cast<JSC::JSObject*>(o));
159}
160
161inline JSContextRef toRef(JSC::JSGlobalObject* globalObject)
162{
163 return reinterpret_cast<JSContextRef>(globalObject);
164}
165
166inline JSGlobalContextRef toGlobalRef(JSC::JSGlobalObject* globalObject)
167{
168 return reinterpret_cast<JSGlobalContextRef>(globalObject);
169}
170
171inline JSPropertyNameAccumulatorRef toRef(JSC::PropertyNameArray* l)
172{
173 return reinterpret_cast<JSPropertyNameAccumulatorRef>(l);
174}
175
176inline JSContextGroupRef toRef(JSC::VM* g)
177{
178 return reinterpret_cast<JSContextGroupRef>(g);
179}
180
181#endif // APICast_h
182