1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JavaScriptEvaluationResult.h"
#include "APIArray.h"
#include "APIDictionary.h"
#include "APINodeInfo.h"
#include "APINumber.h"
#include "APISerializedScriptValue.h"
#include "APIString.h"
#include "WKSharedAPICast.h"
#include "WebFrame.h"
#include <WebCore/Document.h>
#include <WebCore/ExceptionDetails.h>
#include <WebCore/JSNodeCustom.h>
#include <WebCore/JSWebKitNodeInfo.h>
#include <WebCore/JSWebKitSerializedNode.h>
#include <WebCore/ScriptWrappableInlines.h>
#include <WebCore/SerializedScriptValue.h>
namespace WebKit {
JavaScriptEvaluationResult::JavaScriptEvaluationResult(JSObjectID root, HashMap<JSObjectID, Value>&& map)
: m_map(WTFMove(map))
, m_root(root) { }
RefPtr<API::Object> JavaScriptEvaluationResult::toAPI(Value&& root)
{
return WTF::switchOn(WTFMove(root), [] (EmptyType) -> RefPtr<API::Object> {
return nullptr;
}, [] (bool value) -> RefPtr<API::Object> {
return API::Boolean::create(value);
}, [] (double value) -> RefPtr<API::Object> {
return API::Double::create(value);
}, [] (String&& value) -> RefPtr<API::Object> {
return API::String::create(value);
}, [] (Seconds value) -> RefPtr<API::Object> {
return API::Double::create(value.seconds());
}, [&] (Vector<JSObjectID>&& vector) -> RefPtr<API::Object> {
Ref array = API::Array::create();
m_arrays.append({ WTFMove(vector), array });
return { WTFMove(array) };
}, [&] (HashMap<JSObjectID, JSObjectID>&& map) -> RefPtr<API::Object> {
Ref dictionary = API::Dictionary::create();
m_dictionaries.append({ WTFMove(map), dictionary });
return { WTFMove(dictionary) };
}, [] (NodeInfo&&) -> RefPtr<API::Object> {
return nullptr;
}, [] (UniqueRef<WebCore::SerializedNode>&&) -> RefPtr<API::Object> {
return nullptr;
});
}
RefPtr<API::Object> JavaScriptEvaluationResult::toAPI()
{
for (auto&& [identifier, value] : std::exchange(m_map, { }))
m_instantiatedObjects.add(identifier, toAPI(WTFMove(value)));
for (auto [vector, array] : std::exchange(m_arrays, { })) {
for (auto identifier : vector) {
if (RefPtr object = m_instantiatedObjects.get(identifier))
Ref { array }->append(object.releaseNonNull());
}
}
for (auto [map, dictionary] : std::exchange(m_dictionaries, { })) {
for (auto [keyIdentifier, valueIdentifier] : map) {
RefPtr key = dynamicDowncast<API::String>(m_instantiatedObjects.get(keyIdentifier));
if (!key)
continue;
RefPtr value = m_instantiatedObjects.get(valueIdentifier);
if (!value)
continue;
Ref { dictionary }->add(key->string(), WTFMove(value));
}
}
return std::exchange(m_instantiatedObjects, { }).take(m_root);
}
WKRetainPtr<WKTypeRef> JavaScriptEvaluationResult::toWK()
{
return WebKit::toAPI(toAPI().get());
}
JSObjectID JavaScriptEvaluationResult::addObjectToMap(JSGlobalContextRef context, JSValueRef object)
{
if (!object) {
if (!m_nullObjectID) {
m_nullObjectID = JSObjectID::generate();
m_map.add(*m_nullObjectID, Value { EmptyType::Undefined });
}
return *m_nullObjectID;
}
Protected<JSValueRef> value(context, object);
auto it = m_jsObjectsInMap.find(value);
if (it != m_jsObjectsInMap.end())
return it->value;
auto identifier = JSObjectID::generate();
m_jsObjectsInMap.set(WTFMove(value), identifier);
m_map.add(identifier, toValue(context, object));
return identifier;
}
static std::optional<std::pair<JSGlobalContextRef, JSValueRef>> roundTripThroughSerializedScriptValue(JSGlobalContextRef serializationContext, JSGlobalContextRef deserializationContext, JSValueRef value)
{
// FIXME: Make the SerializedScriptValue roundtrip allow JSWebKitNodeInfo to allow arrays of WebKitNodeInfo.
auto* globalObject = ::toJS(serializationContext);
JSC::JSValue jsValue = ::toJS(globalObject, value);
if (auto* object = jsValue.isObject() ? jsValue.toObject(globalObject) : nullptr) {
if (object->inherits<WebCore::JSWebKitNodeInfo>() || object->inherits<WebCore::JSWebKitSerializedNode>())
return { { serializationContext, value } };
}
if (RefPtr serialized = WebCore::SerializedScriptValue::create(serializationContext, value, nullptr))
return { { deserializationContext, serialized->deserialize(deserializationContext, nullptr) } };
return std::nullopt;
}
std::optional<JavaScriptEvaluationResult> JavaScriptEvaluationResult::extract(JSGlobalContextRef context, JSValueRef value)
{
JSRetainPtr deserializationContext = API::SerializedScriptValue::deserializationContext();
auto result = roundTripThroughSerializedScriptValue(context, deserializationContext.get(), value);
if (!result)
return std::nullopt;
return { JavaScriptEvaluationResult { result->first, result->second } };
}
// Similar to JSValue's valueToObjectWithoutCopy.
auto JavaScriptEvaluationResult::toValue(JSGlobalContextRef context, JSValueRef value) -> Value
{
if (!JSValueIsObject(context, value)) {
if (JSValueIsBoolean(context, value))
return JSValueToBoolean(context, value);
if (JSValueIsNumber(context, value)) {
value = JSValueMakeNumber(context, JSValueToNumber(context, value, 0));
return JSValueToNumber(context, value, 0);
}
if (JSValueIsString(context, value)) {
auto* globalObject = ::toJS(context);
JSC::JSValue jsValue = ::toJS(globalObject, value);
return jsValue.toWTFString(globalObject);
}
if (JSValueIsNull(context, value))
return EmptyType::Null;
return EmptyType::Undefined;
}
JSObjectRef object = JSValueToObject(context, value, 0);
if (auto* info = jsDynamicCast<WebCore::JSWebKitNodeInfo*>(::toJS(::toJS(context), object))) {
Ref nodeInfo { info->wrapped() };
return NodeInfo { nodeInfo->nodeIdentifier(), nodeInfo->contentFrameIdentifier() };
}
if (auto* node = jsDynamicCast<WebCore::JSWebKitSerializedNode*>(::toJS(::toJS(context), object))) {
Ref serializedNode { node->wrapped() };
return makeUniqueRef<WebCore::SerializedNode>(serializedNode->serializedNode());
}
if (JSValueIsDate(context, object))
return Seconds(JSValueToNumber(context, object, 0) / 1000.0);
if (JSValueIsArray(context, object)) {
SUPPRESS_UNCOUNTED_ARG JSValueRef lengthPropertyName = JSValueMakeString(context, adopt(JSStringCreateWithUTF8CString("length")).get());
JSValueRef lengthValue = JSObjectGetPropertyForKey(context, object, lengthPropertyName, nullptr);
double lengthDouble = JSValueToNumber(context, lengthValue, nullptr);
if (lengthDouble < 0 || lengthDouble > static_cast<double>(std::numeric_limits<size_t>::max()))
return EmptyType::Undefined;
size_t length = lengthDouble;
Vector<JSObjectID> vector;
if (!vector.tryReserveInitialCapacity(length))
return EmptyType::Undefined;
for (size_t i = 0; i < length; ++i)
vector.append(addObjectToMap(context, JSObjectGetPropertyAtIndex(context, object, i, nullptr)));
return { WTFMove(vector) };
}
JSPropertyNameArrayRef names = JSObjectCopyPropertyNames(context, object);
size_t length = JSPropertyNameArrayGetCount(names);
HashMap<JSObjectID, JSObjectID> map;
for (size_t i = 0; i < length; i++) {
JSRetainPtr<JSStringRef> key = JSPropertyNameArrayGetNameAtIndex(names, i);
SUPPRESS_UNCOUNTED_ARG map.add(addObjectToMap(context, JSValueMakeString(context, key.get())), addObjectToMap(context, JSObjectGetPropertyForKey(context, object, JSValueMakeString(context, key.get()), nullptr)));
}
JSPropertyNameArrayRelease(names);
return { WTFMove(map) };
}
JavaScriptEvaluationResult::JavaScriptEvaluationResult(JSGlobalContextRef context, JSValueRef value)
: m_root(addObjectToMap(context, value))
{
m_jsObjectsInMap.clear();
m_nullObjectID = std::nullopt;
}
JSValueRef JavaScriptEvaluationResult::toJS(JSGlobalContextRef context, Value&& root)
{
auto globalObjectTuple = [] (auto context) {
auto* lexicalGlobalObject = ::toJS(context);
RELEASE_ASSERT(lexicalGlobalObject->template inherits<WebCore::JSDOMGlobalObject>());
auto* domGlobalObject = jsCast<WebCore::JSDOMGlobalObject*>(lexicalGlobalObject);
RefPtr document = dynamicDowncast<WebCore::Document>(domGlobalObject->scriptExecutionContext());
RELEASE_ASSERT(document);
return std::make_tuple(lexicalGlobalObject, domGlobalObject, WTFMove(document));
};
return WTF::switchOn(WTFMove(root), [&] (EmptyType emptyType) -> JSValueRef {
switch (emptyType) {
case EmptyType::Undefined:
return JSValueMakeUndefined(context);
case EmptyType::Null:
return JSValueMakeNull(context);
}
}, [&] (bool value) -> JSValueRef {
return JSValueMakeBoolean(context, value);
}, [&] (double value) -> JSValueRef {
return JSValueMakeNumber(context, value);
}, [&] (String&& value) -> JSValueRef {
auto string = OpaqueJSString::tryCreate(WTFMove(value));
return JSValueMakeString(context, string.get());
}, [&] (Seconds value) -> JSValueRef {
JSValueRef argument = JSValueMakeNumber(context, value.value() * 1000.0);
return JSObjectMakeDate(context, 1, &argument, 0);
}, [&] (Vector<JSObjectID>&& vector) -> JSValueRef {
JSValueRef array = JSObjectMakeArray(context, 0, nullptr, 0);
m_jsArrays.append({ WTFMove(vector), Protected<JSValueRef>(context, array) });
return array;
}, [&] (HashMap<JSObjectID, JSObjectID>&& map) -> JSValueRef {
JSObjectRef dictionary = JSObjectMake(context, 0, 0);
m_jsDictionaries.append({ WTFMove(map), Protected<JSObjectRef>(context, dictionary) });
return dictionary;
}, [&] (NodeInfo&& nodeInfo) -> JSValueRef {
RefPtr node = WebCore::Node::fromIdentifier(nodeInfo.nodeIdentifier);
if (!node)
return JSValueMakeUndefined(context);
auto [lexicalGlobalObject, domGlobalObject, document] = globalObjectTuple(context);
if (document.get() != &node->document())
return JSValueMakeUndefined(context);
return ::toRef(lexicalGlobalObject, WebCore::toJS(lexicalGlobalObject, domGlobalObject, *node));
}, [&] (UniqueRef<WebCore::SerializedNode>&& serializedNode) -> JSValueRef {
auto [lexicalGlobalObject, domGlobalObject, document] = globalObjectTuple(context);
return ::toRef(lexicalGlobalObject, WebCore::SerializedNode::deserialize(WTFMove(serializedNode.get()), lexicalGlobalObject, domGlobalObject, *document));
});
}
Protected<JSValueRef> JavaScriptEvaluationResult::toJS(JSGlobalContextRef context)
{
for (auto&& [identifier, value] : std::exchange(m_map, { }))
m_instantiatedJSObjects.add(identifier, Protected<JSValueRef>(context, toJS(context, WTFMove(value))));
for (auto& [vector, array] : std::exchange(m_jsArrays, { })) {
JSObjectRef jsArray = JSValueToObject(context, array.get(), 0);
for (size_t index = 0; index < vector.size(); ++index) {
auto identifier = vector[index];
if (Protected<JSValueRef> element = m_instantiatedJSObjects.get(identifier))
JSObjectSetPropertyAtIndex(context, jsArray, index, element.get(), 0);
}
}
for (auto& [map, dictionary] : std::exchange(m_jsDictionaries, { })) {
for (auto [keyIdentifier, valueIdentifier] : map) {
Protected<JSValueRef> key = m_instantiatedJSObjects.get(keyIdentifier);
if (!key)
continue;
ASSERT(JSValueIsString(context, key.get()));
SUPPRESS_UNCOUNTED_ARG auto keyString = adopt(JSValueToStringCopy(context, key.get(), nullptr));
if (!keyString)
continue;
Protected<JSValueRef> value = m_instantiatedJSObjects.get(valueIdentifier);
if (!value)
continue;
SUPPRESS_UNCOUNTED_ARG JSObjectSetProperty(context, dictionary.get(), keyString.get(), value.get(), 0, 0);
}
}
return std::exchange(m_instantiatedJSObjects, { }).take(m_root);
}
JavaScriptEvaluationResult::JavaScriptEvaluationResult(JavaScriptEvaluationResult&&) = default;
JavaScriptEvaluationResult& JavaScriptEvaluationResult::operator=(JavaScriptEvaluationResult&&) = default;
JavaScriptEvaluationResult::~JavaScriptEvaluationResult() = default;
String JavaScriptEvaluationResult::toString() const
{
auto it = m_map.find(m_root);
if (it == m_map.end())
return { };
auto* string = std::get_if<String>(&it->value);
if (!string)
return { };
return *string;
}
} // namespace WebKit
|