#include "config.h"
#include "JSIPCBinding.h"
#if ENABLE(IPC_TESTING_API)
#include <JavaScriptCore/JSCJSValueInlines.h>
#include <JavaScriptCore/JSGlobalObjectInlines.h>
#include <JavaScriptCore/ObjectConstructor.h>
#include <WebCore/FloatRect.h>
#include <WebCore/IntRect.h>
#include <WebCore/RegistrableDomain.h>
#include <wtf/URL.h>
namespace IPC {
static JSC::JSValue jsValueForDecodedStringArgumentValue(JSC::JSGlobalObject* globalObject, const String& value, ASCIILiteral type)
{
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "type"_s), JSC::jsNontrivialString(vm, type));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "value"_s), value.isNull() ? JSC::jsNull() : JSC::jsString(vm, value));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
return object;
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const String& value)
{
return jsValueForDecodedStringArgumentValue(globalObject, value, "String"_s);
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const URL& value)
{
return jsValueForDecodedStringArgumentValue(globalObject, value.string(), "URL"_s);
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const WebCore::RegistrableDomain& value)
{
return jsValueForDecodedStringArgumentValue(globalObject, value.string(), "RegistrableDomain"_s);
}
template<typename NumericType>
JSC::JSValue jsValueForDecodedNumericArgumentValue(JSC::JSGlobalObject* globalObject, NumericType value, const String& type)
{
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "type"_s), JSC::jsNontrivialString(vm, type));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "value"_s), JSC::JSValue(value));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
return object;
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, bool value)
{
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "type"_s), JSC::jsNontrivialString(vm, "bool"));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "value"_s), JSC::jsBoolean(value));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
return object;
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, double value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "double");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, float value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "float");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, int8_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "int8_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, int16_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "int16_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, int32_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "int32_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, int64_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "int64_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, uint8_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "uint8_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, uint16_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "uint16_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, uint32_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "uint32_t");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, uint64_t value)
{
return jsValueForDecodedNumericArgumentValue(globalObject, value, "uint64_t");
}
template<typename RectType>
JSC::JSValue jsValueForDecodedArgumentRect(JSC::JSGlobalObject* globalObject, const RectType& value, const String& type)
{
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* object = JSC::constructEmptyObject(globalObject, globalObject->objectPrototype());
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "type"_s), JSC::jsNontrivialString(vm, type));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "x"_s), JSC::JSValue(value.x()));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "y"_s), JSC::JSValue(value.y()));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "width"_s), JSC::JSValue(value.width()));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
object->putDirect(vm, JSC::Identifier::fromString(vm, "height"_s), JSC::JSValue(value.height()));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
return object;
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const WebCore::IntRect& value)
{
return jsValueForDecodedArgumentRect(globalObject, value, "IntRect");
}
template<>
JSC::JSValue jsValueForDecodedArgumentValue(JSC::JSGlobalObject* globalObject, const WebCore::FloatRect& value)
{
return jsValueForDecodedArgumentRect(globalObject, value, "FloatRect");
}
bool putJSValueForDecodedArgumentAtIndexOrArrayBufferIfUndefined(JSC::JSGlobalObject* globalObject, JSC::JSArray* array, unsigned index, JSC::JSValue value, const uint8_t* buffer, size_t length)
{
auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
if (value.isUndefined()) {
auto arrayBuffer = JSC::ArrayBuffer::create(buffer, length);
if (auto* structure = globalObject->arrayBufferStructure(arrayBuffer->sharingMode()))
value = JSC::JSArrayBuffer::create(globalObject->vm(), structure, WTFMove(arrayBuffer));
}
array->putDirectIndex(globalObject, index, value);
RETURN_IF_EXCEPTION(scope, false);
return true;
}
}
#endif