1 | /* |
2 | * Copyright (C) 2016 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "IDLTypes.h" |
29 | #include "JSDOMConvertBase.h" |
30 | #include "StringAdaptors.h" |
31 | |
32 | namespace WebCore { |
33 | |
34 | WEBCORE_EXPORT String identifierToByteString(JSC::ExecState&, const JSC::Identifier&); |
35 | WEBCORE_EXPORT String valueToByteString(JSC::ExecState&, JSC::JSValue); |
36 | WEBCORE_EXPORT String identifierToUSVString(JSC::ExecState&, const JSC::Identifier&); |
37 | WEBCORE_EXPORT String valueToUSVString(JSC::ExecState&, JSC::JSValue); |
38 | |
39 | inline String propertyNameToString(JSC::PropertyName propertyName) |
40 | { |
41 | ASSERT(!propertyName.isSymbol()); |
42 | return propertyName.uid() ? propertyName.uid() : propertyName.publicName(); |
43 | } |
44 | |
45 | inline AtomicString propertyNameToAtomicString(JSC::PropertyName propertyName) |
46 | { |
47 | return AtomicString(propertyName.uid() ? propertyName.uid() : propertyName.publicName()); |
48 | } |
49 | |
50 | // MARK: - |
51 | // MARK: String types |
52 | |
53 | template<> struct Converter<IDLDOMString> : DefaultConverter<IDLDOMString> { |
54 | static String convert(JSC::ExecState& state, JSC::JSValue value) |
55 | { |
56 | return value.toWTFString(&state); |
57 | } |
58 | }; |
59 | |
60 | template<> struct JSConverter<IDLDOMString> { |
61 | static constexpr bool needsState = true; |
62 | static constexpr bool needsGlobalObject = false; |
63 | |
64 | static JSC::JSValue convert(JSC::ExecState& state, const String& value) |
65 | { |
66 | return JSC::jsStringWithCache(&state, value); |
67 | } |
68 | |
69 | static JSC::JSValue convert(JSC::ExecState& state, const UncachedString& value) |
70 | { |
71 | return JSC::jsString(&state, value.string); |
72 | } |
73 | |
74 | static JSC::JSValue convert(JSC::ExecState& state, const OwnedString& value) |
75 | { |
76 | return JSC::jsOwnedString(&state, value.string); |
77 | } |
78 | }; |
79 | |
80 | template<> struct Converter<IDLByteString> : DefaultConverter<IDLByteString> { |
81 | static String convert(JSC::ExecState& state, JSC::JSValue value) |
82 | { |
83 | return valueToByteString(state, value); |
84 | } |
85 | }; |
86 | |
87 | template<> struct JSConverter<IDLByteString> { |
88 | static constexpr bool needsState = true; |
89 | static constexpr bool needsGlobalObject = false; |
90 | |
91 | static JSC::JSValue convert(JSC::ExecState& state, const String& value) |
92 | { |
93 | return JSC::jsStringWithCache(&state, value); |
94 | } |
95 | |
96 | static JSC::JSValue convert(JSC::ExecState& state, const UncachedString& value) |
97 | { |
98 | return JSC::jsString(&state, value.string); |
99 | } |
100 | |
101 | static JSC::JSValue convert(JSC::ExecState& state, const OwnedString& value) |
102 | { |
103 | return JSC::jsOwnedString(&state, value.string); |
104 | } |
105 | }; |
106 | |
107 | template<> struct Converter<IDLUSVString> : DefaultConverter<IDLUSVString> { |
108 | static String convert(JSC::ExecState& state, JSC::JSValue value) |
109 | { |
110 | return valueToUSVString(state, value); |
111 | } |
112 | }; |
113 | |
114 | template<> struct JSConverter<IDLUSVString> { |
115 | static constexpr bool needsState = true; |
116 | static constexpr bool needsGlobalObject = false; |
117 | |
118 | static JSC::JSValue convert(JSC::ExecState& state, const String& value) |
119 | { |
120 | return JSC::jsStringWithCache(&state, value); |
121 | } |
122 | |
123 | static JSC::JSValue convert(JSC::ExecState& state, const UncachedString& value) |
124 | { |
125 | return JSC::jsString(&state, value.string); |
126 | } |
127 | |
128 | static JSC::JSValue convert(JSC::ExecState& state, const OwnedString& value) |
129 | { |
130 | return JSC::jsOwnedString(&state, value.string); |
131 | } |
132 | }; |
133 | |
134 | // MARK: - |
135 | // MARK: String type adaptors |
136 | |
137 | template<typename T> struct Converter<IDLTreatNullAsEmptyAdaptor<T>> : DefaultConverter<IDLTreatNullAsEmptyAdaptor<T>> { |
138 | static String convert(JSC::ExecState& state, JSC::JSValue value) |
139 | { |
140 | if (value.isNull()) |
141 | return emptyString(); |
142 | return Converter<T>::convert(state, value); |
143 | } |
144 | }; |
145 | |
146 | template<typename T> struct JSConverter<IDLTreatNullAsEmptyAdaptor<T>> { |
147 | static constexpr bool needsState = true; |
148 | static constexpr bool needsGlobalObject = false; |
149 | |
150 | static JSC::JSValue convert(JSC::ExecState& state, const String& value) |
151 | { |
152 | return JSConverter<T>::convert(state, value); |
153 | } |
154 | }; |
155 | |
156 | template<typename T> struct Converter<IDLAtomicStringAdaptor<T>> : DefaultConverter<IDLAtomicStringAdaptor<T>> { |
157 | static AtomicString convert(JSC::ExecState& state, JSC::JSValue value) |
158 | { |
159 | static_assert(std::is_same<T, IDLDOMString>::value, "This adaptor is only supported for IDLDOMString at the moment." ); |
160 | |
161 | return value.toString(&state)->toAtomicString(&state); |
162 | } |
163 | }; |
164 | |
165 | template<typename T> struct JSConverter<IDLAtomicStringAdaptor<T>> { |
166 | static constexpr bool needsState = true; |
167 | static constexpr bool needsGlobalObject = false; |
168 | |
169 | static JSC::JSValue convert(JSC::ExecState& state, const AtomicString& value) |
170 | { |
171 | static_assert(std::is_same<T, IDLDOMString>::value, "This adaptor is only supported for IDLDOMString at the moment." ); |
172 | |
173 | return JSConverter<T>::convert(state, value); |
174 | } |
175 | }; |
176 | |
177 | template<typename T> struct Converter<IDLRequiresExistingAtomicStringAdaptor<T>> : DefaultConverter<IDLRequiresExistingAtomicStringAdaptor<T>> { |
178 | static AtomicString convert(JSC::ExecState& state, JSC::JSValue value) |
179 | { |
180 | static_assert(std::is_same<T, IDLDOMString>::value, "This adaptor is only supported for IDLDOMString at the moment." ); |
181 | |
182 | return AtomicString(value.toString(&state)->toExistingAtomicString(&state)); |
183 | } |
184 | }; |
185 | |
186 | template<typename T> struct JSConverter<IDLRequiresExistingAtomicStringAdaptor<T>> { |
187 | static constexpr bool needsState = true; |
188 | static constexpr bool needsGlobalObject = false; |
189 | |
190 | static JSC::JSValue convert(JSC::ExecState& state, const AtomicString& value) |
191 | { |
192 | static_assert(std::is_same<T, IDLDOMString>::value, "This adaptor is only supported for IDLDOMString at the moment." ); |
193 | |
194 | return JSConverter<T>::convert(state, value); |
195 | } |
196 | }; |
197 | |
198 | |
199 | } // namespace WebCore |
200 | |