JSDeviceMotionEventCustom.cpp [plain text]
#include "config.h"
#if ENABLE(DEVICE_ORIENTATION)
#include "JSDeviceMotionEvent.h"
#include "DeviceMotionData.h"
#include "DeviceMotionEvent.h"
#include <runtime/IdentifierInlines.h>
#include <runtime/JSCJSValueInlines.h>
#include <runtime/ObjectConstructor.h>
#include <runtime/StructureInlines.h>
using namespace JSC;
namespace WebCore {
static RefPtr<DeviceMotionData::Acceleration> readAccelerationArgument(JSValue value, ExecState& state)
{
if (value.isUndefinedOrNull())
return nullptr;
JSObject* object = value.toObject(&state);
ASSERT(!state.hadException());
JSValue xValue = object->get(&state, Identifier::fromString(&state, "x"));
if (state.hadException())
return nullptr;
bool canProvideX = !xValue.isUndefinedOrNull();
double x = xValue.toNumber(&state);
if (state.hadException())
return nullptr;
JSValue yValue = object->get(&state, Identifier::fromString(&state, "y"));
if (state.hadException())
return nullptr;
bool canProvideY = !yValue.isUndefinedOrNull();
double y = yValue.toNumber(&state);
if (state.hadException())
return nullptr;
JSValue zValue = object->get(&state, Identifier::fromString(&state, "z"));
if (state.hadException())
return nullptr;
bool canProvideZ = !zValue.isUndefinedOrNull();
double z = zValue.toNumber(&state);
if (state.hadException())
return nullptr;
if (!canProvideX && !canProvideY && !canProvideZ)
return nullptr;
return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
}
static RefPtr<DeviceMotionData::RotationRate> readRotationRateArgument(JSValue value, ExecState& state)
{
if (value.isUndefinedOrNull())
return nullptr;
JSObject* object = value.toObject(&state);
ASSERT(!state.hadException());
JSValue alphaValue = object->get(&state, Identifier::fromString(&state, "alpha"));
if (state.hadException())
return nullptr;
bool canProvideAlpha = !alphaValue.isUndefinedOrNull();
double alpha = alphaValue.toNumber(&state);
if (state.hadException())
return nullptr;
JSValue betaValue = object->get(&state, Identifier::fromString(&state, "beta"));
if (state.hadException())
return nullptr;
bool canProvideBeta = !betaValue.isUndefinedOrNull();
double beta = betaValue.toNumber(&state);
if (state.hadException())
return nullptr;
JSValue gammaValue = object->get(&state, Identifier::fromString(&state, "gamma"));
if (state.hadException())
return nullptr;
bool canProvideGamma = !gammaValue.isUndefinedOrNull();
double gamma = gammaValue.toNumber(&state);
if (state.hadException())
return nullptr;
if (!canProvideAlpha && !canProvideBeta && !canProvideGamma)
return nullptr;
return DeviceMotionData::RotationRate::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
}
static JSObject* createAccelerationObject(const DeviceMotionData::Acceleration* acceleration, ExecState& state)
{
JSObject* object = constructEmptyObject(&state);
object->putDirect(state.vm(), Identifier::fromString(&state, "x"), acceleration->canProvideX() ? jsNumber(acceleration->x()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "y"), acceleration->canProvideY() ? jsNumber(acceleration->y()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "z"), acceleration->canProvideZ() ? jsNumber(acceleration->z()) : jsNull());
return object;
}
static JSObject* createRotationRateObject(const DeviceMotionData::RotationRate* rotationRate, ExecState& state)
{
JSObject* object = constructEmptyObject(&state);
object->putDirect(state.vm(), Identifier::fromString(&state, "alpha"), rotationRate->canProvideAlpha() ? jsNumber(rotationRate->alpha()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "beta"), rotationRate->canProvideBeta() ? jsNumber(rotationRate->beta()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "gamma"), rotationRate->canProvideGamma() ? jsNumber(rotationRate->gamma()) : jsNull());
return object;
}
JSValue JSDeviceMotionEvent::acceleration(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->acceleration())
return jsNull();
return createAccelerationObject(imp.deviceMotionData()->acceleration(), state);
}
JSValue JSDeviceMotionEvent::accelerationIncludingGravity(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->accelerationIncludingGravity())
return jsNull();
return createAccelerationObject(imp.deviceMotionData()->accelerationIncludingGravity(), state);
}
JSValue JSDeviceMotionEvent::rotationRate(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->rotationRate())
return jsNull();
return createRotationRateObject(imp.deviceMotionData()->rotationRate(), state);
}
JSValue JSDeviceMotionEvent::interval(ExecState&) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->canProvideInterval())
return jsNull();
return jsNumber(imp.deviceMotionData()->interval());
}
JSValue JSDeviceMotionEvent::initDeviceMotionEvent(ExecState& state)
{
const String type = state.argument(0).toString(&state)->value(&state);
bool bubbles = state.argument(1).toBoolean(&state);
bool cancelable = state.argument(2).toBoolean(&state);
RefPtr<DeviceMotionData::Acceleration> acceleration = readAccelerationArgument(state.argument(3), state);
if (state.hadException())
return jsUndefined();
RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = readAccelerationArgument(state.argument(4), state);
if (state.hadException())
return jsUndefined();
RefPtr<DeviceMotionData::RotationRate> rotationRate = readRotationRateArgument(state.argument(5), state);
if (state.hadException())
return jsUndefined();
bool intervalProvided = !state.argument(6).isUndefinedOrNull();
double interval = state.argument(6).toNumber(&state);
auto deviceMotionData = DeviceMotionData::create(WTFMove(acceleration), WTFMove(accelerationIncludingGravity), WTFMove(rotationRate), intervalProvided, interval);
wrapped().initDeviceMotionEvent(type, bubbles, cancelable, deviceMotionData.ptr());
return jsUndefined();
}
}
#endif // ENABLE(DEVICE_ORIENTATION)