1 | /* |
2 | * Copyright (C) 2016-2017 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 "JSDOMConvertStrings.h" |
30 | #include <JavaScriptCore/ObjectConstructor.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | namespace Detail { |
35 | |
36 | template<typename IDLStringType> |
37 | struct IdentifierConverter; |
38 | |
39 | template<> struct IdentifierConverter<IDLDOMString> { |
40 | static String convert(JSC::ExecState&, const JSC::Identifier& identifier) |
41 | { |
42 | return identifier.string(); |
43 | } |
44 | }; |
45 | |
46 | template<> struct IdentifierConverter<IDLByteString> { |
47 | static String convert(JSC::ExecState& state, const JSC::Identifier& identifier) |
48 | { |
49 | return identifierToByteString(state, identifier); |
50 | } |
51 | }; |
52 | |
53 | template<> struct IdentifierConverter<IDLUSVString> { |
54 | static String convert(JSC::ExecState& state, const JSC::Identifier& identifier) |
55 | { |
56 | return identifierToUSVString(state, identifier); |
57 | } |
58 | }; |
59 | |
60 | } |
61 | |
62 | template<typename K, typename V> struct Converter<IDLRecord<K, V>> : DefaultConverter<IDLRecord<K, V>> { |
63 | using ReturnType = typename IDLRecord<K, V>::ImplementationType; |
64 | using KeyType = typename K::ImplementationType; |
65 | using ValueType = typename V::ImplementationType; |
66 | |
67 | static ReturnType convert(JSC::ExecState& state, JSC::JSValue value) |
68 | { |
69 | auto& vm = state.vm(); |
70 | auto scope = DECLARE_THROW_SCOPE(vm); |
71 | |
72 | // 1. Let result be a new empty instance of record<K, V>. |
73 | // 2. If Type(O) is Undefined or Null, return result. |
74 | if (value.isUndefinedOrNull()) |
75 | return { }; |
76 | |
77 | // 3. If Type(O) is not Object, throw a TypeError. |
78 | if (!value.isObject()) { |
79 | throwTypeError(&state, scope); |
80 | return { }; |
81 | } |
82 | |
83 | JSC::JSObject* object = JSC::asObject(value); |
84 | |
85 | ReturnType result; |
86 | |
87 | // 4. Let keys be ? O.[[OwnPropertyKeys]](). |
88 | JSC::PropertyNameArray keys(&vm, JSC::PropertyNameMode::Strings, JSC::PrivateSymbolMode::Exclude); |
89 | object->methodTable(vm)->getOwnPropertyNames(object, &state, keys, JSC::EnumerationMode(JSC::DontEnumPropertiesMode::Include)); |
90 | |
91 | RETURN_IF_EXCEPTION(scope, { }); |
92 | |
93 | // 5. Repeat, for each element key of keys in List order: |
94 | for (auto& key : keys) { |
95 | // 1. Let desc be ? O.[[GetOwnProperty]](key). |
96 | JSC::PropertyDescriptor descriptor; |
97 | bool didGetDescriptor = object->getOwnPropertyDescriptor(&state, key, descriptor); |
98 | RETURN_IF_EXCEPTION(scope, { }); |
99 | |
100 | // 2. If desc is not undefined and desc.[[Enumerable]] is true: |
101 | |
102 | // It's necessary to filter enumerable here rather than using the default EnumerationMode, |
103 | // to prevent an observable extra [[GetOwnProperty]] operation in the case of ProxyObject records. |
104 | if (didGetDescriptor && descriptor.enumerable()) { |
105 | // 1. Let typedKey be key converted to an IDL value of type K. |
106 | auto typedKey = Detail::IdentifierConverter<K>::convert(state, key); |
107 | RETURN_IF_EXCEPTION(scope, { }); |
108 | |
109 | // 2. Let value be ? Get(O, key). |
110 | auto subValue = object->get(&state, key); |
111 | RETURN_IF_EXCEPTION(scope, { }); |
112 | |
113 | // 3. Let typedValue be value converted to an IDL value of type V. |
114 | auto typedValue = Converter<V>::convert(state, subValue); |
115 | RETURN_IF_EXCEPTION(scope, { }); |
116 | |
117 | // 4. If typedKey is already a key in result, set its value to typedValue. |
118 | // Note: This can happen when O is a proxy object. |
119 | // FIXME: Handle this case. |
120 | |
121 | // 5. Otherwise, append to result a mapping (typedKey, typedValue). |
122 | result.append({ typedKey, typedValue }); |
123 | } |
124 | } |
125 | |
126 | // 6. Return result. |
127 | return result; |
128 | } |
129 | }; |
130 | |
131 | template<typename K, typename V> struct JSConverter<IDLRecord<K, V>> { |
132 | static constexpr bool needsState = true; |
133 | static constexpr bool needsGlobalObject = true; |
134 | |
135 | template<typename MapType> |
136 | static JSC::JSValue convert(JSC::ExecState& state, JSDOMGlobalObject& globalObject, const MapType& map) |
137 | { |
138 | auto& vm = state.vm(); |
139 | |
140 | // 1. Let result be ! ObjectCreate(%ObjectPrototype%). |
141 | auto result = constructEmptyObject(&state, globalObject.objectPrototype()); |
142 | |
143 | // 2. Repeat, for each mapping (key, value) in D: |
144 | for (const auto& keyValuePair : map) { |
145 | // 1. Let esKey be key converted to an ECMAScript value. |
146 | // Note, this step is not required, as we need the key to be |
147 | // an Identifier, not a JSValue. |
148 | |
149 | // 2. Let esValue be value converted to an ECMAScript value. |
150 | auto esValue = toJS<V>(state, globalObject, keyValuePair.value); |
151 | |
152 | // 3. Let created be ! CreateDataProperty(result, esKey, esValue). |
153 | bool created = result->putDirect(vm, JSC::Identifier::fromString(&vm, keyValuePair.key), esValue); |
154 | |
155 | // 4. Assert: created is true. |
156 | ASSERT_UNUSED(created, created); |
157 | } |
158 | |
159 | // 3. Return result. |
160 | return result; |
161 | } |
162 | }; |
163 | |
164 | } // namespace WebCore |
165 | |