1/*
2 * Copyright (C) 2018-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#pragma once
27
28#include "JSCell.h"
29
30namespace JSC {
31
32template<typename To, typename From>
33inline To jsCast(From* from)
34{
35 static_assert(std::is_base_of<JSCell, typename std::remove_pointer<To>::type>::value && std::is_base_of<JSCell, typename std::remove_pointer<From>::type>::value, "JS casting expects that the types you are casting to/from are subclasses of JSCell");
36 ASSERT_WITH_SECURITY_IMPLICATION(!from || from->JSCell::inherits(from->JSCell::vm(), std::remove_pointer<To>::type::info()));
37 return static_cast<To>(from);
38}
39
40template<typename To>
41inline To jsCast(JSValue from)
42{
43 static_assert(std::is_base_of<JSCell, typename std::remove_pointer<To>::type>::value, "JS casting expects that the types you are casting to is a subclass of JSCell");
44 ASSERT_WITH_SECURITY_IMPLICATION(from.isCell() && from.asCell()->JSCell::inherits(from.asCell()->vm(), std::remove_pointer<To>::type::info()));
45 return static_cast<To>(from.asCell());
46}
47
48// Specific type overloads.
49#define FOR_EACH_JS_DYNAMIC_CAST_JS_TYPE_OVERLOAD(macro) \
50 macro(JSFixedArray, JSType::JSFixedArrayType, JSType::JSFixedArrayType) \
51 macro(JSObject, FirstObjectType, LastObjectType) \
52 macro(JSFinalObject, JSType::FinalObjectType, JSType::FinalObjectType) \
53 macro(JSFunction, JSType::JSFunctionType, JSType::JSFunctionType) \
54 macro(InternalFunction, JSType::InternalFunctionType, JSType::InternalFunctionType) \
55 macro(JSArray, JSType::ArrayType, JSType::DerivedArrayType) \
56 macro(JSArrayBuffer, JSType::ArrayBufferType, JSType::ArrayBufferType) \
57 macro(JSArrayBufferView, FirstTypedArrayType, LastTypedArrayType) \
58 macro(JSPromise, JSType::JSPromiseType, JSType::JSPromiseType) \
59 macro(JSSet, JSType::JSSetType, JSType::JSSetType) \
60 macro(JSMap, JSType::JSMapType, JSType::JSMapType) \
61 macro(JSWeakSet, JSType::JSWeakSetType, JSType::JSWeakSetType) \
62 macro(JSWeakMap, JSType::JSWeakMapType, JSType::JSWeakMapType) \
63 macro(NumberObject, JSType::NumberObjectType, JSType::NumberObjectType) \
64 macro(ProxyObject, JSType::ProxyObjectType, JSType::ProxyObjectType) \
65 macro(RegExpObject, JSType::RegExpObjectType, JSType::RegExpObjectType) \
66 macro(WebAssemblyToJSCallee, JSType::WebAssemblyToJSCalleeType, JSType::WebAssemblyToJSCalleeType) \
67 macro(DirectArguments, JSType::DirectArgumentsType, JSType::DirectArgumentsType) \
68 macro(ScopedArguments, JSType::ScopedArgumentsType, JSType::ScopedArgumentsType) \
69 macro(ClonedArguments, JSType::ClonedArgumentsType, JSType::ClonedArgumentsType) \
70 macro(JSGlobalObject, JSType::GlobalObjectType, JSType::GlobalObjectType) \
71 macro(JSGlobalLexicalEnvironment, JSType::GlobalLexicalEnvironmentType, JSType::GlobalLexicalEnvironmentType) \
72 macro(JSSegmentedVariableObject, JSType::GlobalObjectType, JSType::GlobalLexicalEnvironmentType) \
73 macro(JSModuleEnvironment, JSType::ModuleEnvironmentType, JSType::ModuleEnvironmentType) \
74 macro(JSLexicalEnvironment, JSType::LexicalEnvironmentType, JSType::ModuleEnvironmentType) \
75 macro(JSSymbolTableObject, JSType::GlobalObjectType, JSType::ModuleEnvironmentType) \
76 macro(JSScope, JSType::GlobalObjectType, JSType::WithScopeType) \
77
78
79// Forward declare the classes because they may not already exist.
80#define FORWARD_DECLARE_OVERLOAD_CLASS(className, jsType, op) class className;
81FOR_EACH_JS_DYNAMIC_CAST_JS_TYPE_OVERLOAD(FORWARD_DECLARE_OVERLOAD_CLASS)
82#undef FORWARD_DECLARE_OVERLOAD_CLASS
83
84namespace JSCastingHelpers {
85
86template<bool isFinal>
87struct FinalTypeDispatcher {
88 template<typename Target, typename From>
89 static inline bool inheritsGeneric(VM& vm, From* from)
90 {
91 static_assert(!std::is_same<JSObject*, Target*>::value, "This ensures our overloads work");
92 static_assert(std::is_base_of<JSCell, Target>::value && std::is_base_of<JSCell, typename std::remove_pointer<From>::type>::value, "JS casting expects that the types you are casting to/from are subclasses of JSCell");
93 // Do not use inherits<Target>(vm) since inherits<T> depends on this function.
94 return from->JSCell::inherits(vm, Target::info());
95 }
96};
97
98template<>
99struct FinalTypeDispatcher</* isFinal */ true> {
100 template<typename Target, typename From>
101 static inline bool inheritsGeneric(VM& vm, From* from)
102 {
103 static_assert(!std::is_same<JSObject*, Target*>::value, "This ensures our overloads work");
104 static_assert(std::is_base_of<JSCell, Target>::value && std::is_base_of<JSCell, typename std::remove_pointer<From>::type>::value, "JS casting expects that the types you are casting to/from are subclasses of JSCell");
105 static_assert(std::is_final<Target>::value, "Target is a final type");
106 bool canCast = from->JSCell::classInfo(vm) == Target::info();
107 // Do not use inherits<Target>(vm) since inherits<T> depends on this function.
108 ASSERT_UNUSED(vm, canCast == from->JSCell::inherits(vm, Target::info()));
109 return canCast;
110 }
111};
112
113template<typename Target, typename From>
114inline bool inheritsJSTypeImpl(VM& vm, From* from, JSType firstType, JSType lastType)
115{
116 static_assert(std::is_base_of<JSCell, Target>::value && std::is_base_of<JSCell, typename std::remove_pointer<From>::type>::value, "JS casting expects that the types you are casting to/from are subclasses of JSCell");
117 bool canCast = firstType <= from->type() && from->type() <= lastType;
118 // Do not use inherits<Target>(vm) since inherits<T> depends on this function.
119 ASSERT_UNUSED(vm, canCast == from->JSCell::inherits(vm, Target::info()));
120 return canCast;
121}
122
123// C++ has bad syntax so we need to use this struct because C++ doesn't have a
124// way to say that we are overloading just the first type in a template list...
125template<typename Target>
126struct InheritsTraits {
127 template<typename From>
128 static inline bool inherits(VM& vm, From* from) { return FinalTypeDispatcher<std::is_final<Target>::value>::template inheritsGeneric<Target>(vm, from); }
129};
130
131#define DEFINE_TRAITS_FOR_JS_TYPE_OVERLOAD(className, firstJSType, lastJSType) \
132 template<> \
133 struct InheritsTraits<className> { \
134 template<typename From> \
135 static inline bool inherits(VM& vm, From* from) { return inheritsJSTypeImpl<className, From>(vm, from, static_cast<JSType>(firstJSType), static_cast<JSType>(lastJSType)); } \
136 }; \
137
138FOR_EACH_JS_DYNAMIC_CAST_JS_TYPE_OVERLOAD(DEFINE_TRAITS_FOR_JS_TYPE_OVERLOAD)
139
140#undef DEFINE_TRAITS_FOR_JS_TYPE_OVERLOAD
141
142
143template<typename Target, typename From>
144bool inherits(VM& vm, From* from)
145{
146 using Dispatcher = InheritsTraits<Target>;
147 return Dispatcher::template inherits(vm, from);
148}
149
150} // namespace JSCastingHelpers
151
152template<typename To, typename From>
153To jsDynamicCast(VM& vm, From* from)
154{
155 using Dispatcher = JSCastingHelpers::InheritsTraits<typename std::remove_cv<typename std::remove_pointer<To>::type>::type>;
156 if (LIKELY(Dispatcher::template inherits(vm, from)))
157 return static_cast<To>(from);
158 return nullptr;
159}
160
161template<typename To>
162To jsDynamicCast(VM& vm, JSValue from)
163{
164 if (UNLIKELY(!from.isCell()))
165 return nullptr;
166 return jsDynamicCast<To>(vm, from.asCell());
167}
168
169template<typename To, typename From>
170To jsSecureCast(VM& vm, From from)
171{
172 auto* result = jsDynamicCast<To>(vm, from);
173 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(result);
174 return result;
175}
176
177}
178