Commit 97f6266f authored by bmeurer's avatar bmeurer Committed by Commit bot

[turbofan] Migrate remaining DataView builtins to C++.

R=franzih@chromium.org
BUG=v8:3533

Review-Url: https://codereview.chromium.org/2306033002
Cr-Commit-Position: refs/heads/master@{#39143}
parent eed164b3
......@@ -1979,6 +1979,39 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
SimpleInstallGetter(prototype, factory->byte_offset_string(),
Builtins::kDataViewPrototypeGetByteOffset, false,
kDataViewByteOffset);
SimpleInstallFunction(prototype, "getInt8",
Builtins::kDataViewPrototypeGetInt8, 1, false);
SimpleInstallFunction(prototype, "setInt8",
Builtins::kDataViewPrototypeSetInt8, 2, false);
SimpleInstallFunction(prototype, "getUint8",
Builtins::kDataViewPrototypeGetUint8, 1, false);
SimpleInstallFunction(prototype, "setUint8",
Builtins::kDataViewPrototypeSetUint8, 2, false);
SimpleInstallFunction(prototype, "getInt16",
Builtins::kDataViewPrototypeGetInt16, 1, false);
SimpleInstallFunction(prototype, "setInt16",
Builtins::kDataViewPrototypeSetInt16, 2, false);
SimpleInstallFunction(prototype, "getUint16",
Builtins::kDataViewPrototypeGetUint16, 1, false);
SimpleInstallFunction(prototype, "setUint16",
Builtins::kDataViewPrototypeSetUint16, 2, false);
SimpleInstallFunction(prototype, "getInt32",
Builtins::kDataViewPrototypeGetInt32, 1, false);
SimpleInstallFunction(prototype, "setInt32",
Builtins::kDataViewPrototypeSetInt32, 2, false);
SimpleInstallFunction(prototype, "getUint32",
Builtins::kDataViewPrototypeGetUint32, 1, false);
SimpleInstallFunction(prototype, "setUint32",
Builtins::kDataViewPrototypeSetUint32, 2, false);
SimpleInstallFunction(prototype, "getFloat32",
Builtins::kDataViewPrototypeGetFloat32, 1, false);
SimpleInstallFunction(prototype, "setFloat32",
Builtins::kDataViewPrototypeSetFloat32, 2, false);
SimpleInstallFunction(prototype, "getFloat64",
Builtins::kDataViewPrototypeGetFloat64, 1, false);
SimpleInstallFunction(prototype, "setFloat64",
Builtins::kDataViewPrototypeSetFloat64, 2, false);
}
{ // -- M a p
......
......@@ -129,5 +129,209 @@ BUILTIN(DataViewPrototypeGetByteOffset) {
return data_view->byte_offset();
}
namespace {
bool NeedToFlipBytes(bool is_little_endian) {
#ifdef V8_TARGET_LITTLE_ENDIAN
return !is_little_endian;
#else
return is_little_endian;
#endif
}
template <size_t n>
void CopyBytes(uint8_t* target, uint8_t const* source) {
for (size_t i = 0; i < n; i++) {
*(target++) = *(source++);
}
}
template <size_t n>
void FlipBytes(uint8_t* target, uint8_t const* source) {
source = source + (n - 1);
for (size_t i = 0; i < n; i++) {
*(target++) = *(source--);
}
}
// ES6 section 24.2.1.1 GetViewValue (view, requestIndex, isLittleEndian, type)
template <typename T>
MaybeHandle<Object> GetViewValue(Isolate* isolate, Handle<JSDataView> data_view,
Handle<Object> request_index,
bool is_little_endian) {
ASSIGN_RETURN_ON_EXCEPTION(
isolate, request_index,
Object::ToIndex(isolate, request_index,
MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
size_t get_index = 0;
if (!TryNumberToSize(*request_index, &get_index)) {
THROW_NEW_ERROR(
isolate, NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
}
Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()),
isolate);
size_t const data_view_byte_offset = NumberToSize(data_view->byte_offset());
size_t const data_view_byte_length = NumberToSize(data_view->byte_length());
if (get_index + sizeof(T) > data_view_byte_length ||
get_index + sizeof(T) < get_index) { // overflow
THROW_NEW_ERROR(
isolate, NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
}
union {
T data;
uint8_t bytes[sizeof(T)];
} v;
size_t const buffer_offset = data_view_byte_offset + get_index;
DCHECK_GE(NumberToSize(buffer->byte_length()), buffer_offset + sizeof(T));
uint8_t const* const source =
static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
if (NeedToFlipBytes(is_little_endian)) {
FlipBytes<sizeof(T)>(v.bytes, source);
} else {
CopyBytes<sizeof(T)>(v.bytes, source);
}
return isolate->factory()->NewNumber(v.data);
}
template <typename T>
T DataViewConvertValue(double value);
template <>
int8_t DataViewConvertValue<int8_t>(double value) {
return static_cast<int8_t>(DoubleToInt32(value));
}
template <>
int16_t DataViewConvertValue<int16_t>(double value) {
return static_cast<int16_t>(DoubleToInt32(value));
}
template <>
int32_t DataViewConvertValue<int32_t>(double value) {
return DoubleToInt32(value);
}
template <>
uint8_t DataViewConvertValue<uint8_t>(double value) {
return static_cast<uint8_t>(DoubleToUint32(value));
}
template <>
uint16_t DataViewConvertValue<uint16_t>(double value) {
return static_cast<uint16_t>(DoubleToUint32(value));
}
template <>
uint32_t DataViewConvertValue<uint32_t>(double value) {
return DoubleToUint32(value);
}
template <>
float DataViewConvertValue<float>(double value) {
return static_cast<float>(value);
}
template <>
double DataViewConvertValue<double>(double value) {
return value;
}
// ES6 section 24.2.1.2 SetViewValue (view, requestIndex, isLittleEndian, type,
// value)
template <typename T>
MaybeHandle<Object> SetViewValue(Isolate* isolate, Handle<JSDataView> data_view,
Handle<Object> request_index,
bool is_little_endian, Handle<Object> value) {
ASSIGN_RETURN_ON_EXCEPTION(
isolate, request_index,
Object::ToIndex(isolate, request_index,
MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
ASSIGN_RETURN_ON_EXCEPTION(isolate, value, Object::ToNumber(value), Object);
size_t get_index = 0;
if (!TryNumberToSize(*request_index, &get_index)) {
THROW_NEW_ERROR(
isolate, NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
}
Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()),
isolate);
size_t const data_view_byte_offset = NumberToSize(data_view->byte_offset());
size_t const data_view_byte_length = NumberToSize(data_view->byte_length());
if (get_index + sizeof(T) > data_view_byte_length ||
get_index + sizeof(T) < get_index) { // overflow
THROW_NEW_ERROR(
isolate, NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset),
Object);
}
union {
T data;
uint8_t bytes[sizeof(T)];
} v;
v.data = DataViewConvertValue<T>(value->Number());
size_t const buffer_offset = data_view_byte_offset + get_index;
DCHECK(NumberToSize(buffer->byte_length()) >= buffer_offset + sizeof(T));
uint8_t* const target =
static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
if (NeedToFlipBytes(is_little_endian)) {
FlipBytes<sizeof(T)>(target, v.bytes);
} else {
CopyBytes<sizeof(T)>(target, v.bytes);
}
return isolate->factory()->undefined_value();
}
} // namespace
#define DATA_VIEW_PROTOTYPE_GET(Type, type) \
BUILTIN(DataViewPrototypeGet##Type) { \
HandleScope scope(isolate); \
CHECK_RECEIVER(JSDataView, data_view, "DataView.prototype.get" #Type); \
Handle<Object> byte_offset = args.atOrUndefined(isolate, 1); \
Handle<Object> is_little_endian = args.atOrUndefined(isolate, 2); \
Handle<Object> result; \
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( \
isolate, result, \
GetViewValue<type>(isolate, data_view, byte_offset, \
is_little_endian->BooleanValue())); \
return *result; \
}
DATA_VIEW_PROTOTYPE_GET(Int8, int8_t)
DATA_VIEW_PROTOTYPE_GET(Uint8, uint8_t)
DATA_VIEW_PROTOTYPE_GET(Int16, int16_t)
DATA_VIEW_PROTOTYPE_GET(Uint16, uint16_t)
DATA_VIEW_PROTOTYPE_GET(Int32, int32_t)
DATA_VIEW_PROTOTYPE_GET(Uint32, uint32_t)
DATA_VIEW_PROTOTYPE_GET(Float32, float)
DATA_VIEW_PROTOTYPE_GET(Float64, double)
#undef DATA_VIEW_PROTOTYPE_GET
#define DATA_VIEW_PROTOTYPE_SET(Type, type) \
BUILTIN(DataViewPrototypeSet##Type) { \
HandleScope scope(isolate); \
CHECK_RECEIVER(JSDataView, data_view, "DataView.prototype.set" #Type); \
Handle<Object> byte_offset = args.atOrUndefined(isolate, 1); \
Handle<Object> value = args.atOrUndefined(isolate, 2); \
Handle<Object> is_little_endian = args.atOrUndefined(isolate, 3); \
Handle<Object> result; \
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( \
isolate, result, \
SetViewValue<type>(isolate, data_view, byte_offset, \
is_little_endian->BooleanValue(), value)); \
return *result; \
}
DATA_VIEW_PROTOTYPE_SET(Int8, int8_t)
DATA_VIEW_PROTOTYPE_SET(Uint8, uint8_t)
DATA_VIEW_PROTOTYPE_SET(Int16, int16_t)
DATA_VIEW_PROTOTYPE_SET(Uint16, uint16_t)
DATA_VIEW_PROTOTYPE_SET(Int32, int32_t)
DATA_VIEW_PROTOTYPE_SET(Uint32, uint32_t)
DATA_VIEW_PROTOTYPE_SET(Float32, float)
DATA_VIEW_PROTOTYPE_SET(Float64, double)
#undef DATA_VIEW_PROTOTYPE_SET
} // namespace internal
} // namespace v8
......@@ -245,6 +245,22 @@ namespace internal {
CPP(DataViewPrototypeGetBuffer) \
CPP(DataViewPrototypeGetByteLength) \
CPP(DataViewPrototypeGetByteOffset) \
CPP(DataViewPrototypeGetInt8) \
CPP(DataViewPrototypeSetInt8) \
CPP(DataViewPrototypeGetUint8) \
CPP(DataViewPrototypeSetUint8) \
CPP(DataViewPrototypeGetInt16) \
CPP(DataViewPrototypeSetInt16) \
CPP(DataViewPrototypeGetUint16) \
CPP(DataViewPrototypeSetUint16) \
CPP(DataViewPrototypeGetInt32) \
CPP(DataViewPrototypeSetInt32) \
CPP(DataViewPrototypeGetUint32) \
CPP(DataViewPrototypeSetUint32) \
CPP(DataViewPrototypeGetFloat32) \
CPP(DataViewPrototypeSetFloat32) \
CPP(DataViewPrototypeGetFloat64) \
CPP(DataViewPrototypeSetFloat64) \
\
/* Date */ \
CPP(DateConstructor) \
......
......@@ -19,7 +19,6 @@ var GetMethod;
var GlobalArray = global.Array;
var GlobalArrayBuffer = global.ArrayBuffer;
var GlobalArrayBufferPrototype = GlobalArrayBuffer.prototype;
var GlobalDataView = global.DataView;
var GlobalObject = global.Object;
var InnerArrayCopyWithin;
var InnerArrayEvery;
......@@ -915,68 +914,4 @@ endmacro
TYPED_ARRAYS(SETUP_TYPED_ARRAY)
// --------------------------- DataView -----------------------------
macro DATA_VIEW_TYPES(FUNCTION)
FUNCTION(Int8)
FUNCTION(Uint8)
FUNCTION(Int16)
FUNCTION(Uint16)
FUNCTION(Int32)
FUNCTION(Uint32)
FUNCTION(Float32)
FUNCTION(Float64)
endmacro
macro DATA_VIEW_GETTER_SETTER(TYPENAME)
function DataViewGetTYPENAMEJS(offset, little_endian) {
if (!IS_DATAVIEW(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'DataView.getTYPENAME', this);
}
offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOffset);
return %DataViewGetTYPENAME(this, offset, !!little_endian);
}
%FunctionSetLength(DataViewGetTYPENAMEJS, 1);
function DataViewSetTYPENAMEJS(offset, value, little_endian) {
if (!IS_DATAVIEW(this)) {
throw %make_type_error(kIncompatibleMethodReceiver,
'DataView.setTYPENAME', this);
}
offset = IS_UNDEFINED(offset) ? 0 : ToIndex(offset, kInvalidDataViewAccessorOffset);
%DataViewSetTYPENAME(this, offset, TO_NUMBER(value), !!little_endian);
}
%FunctionSetLength(DataViewSetTYPENAMEJS, 2);
endmacro
DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER)
utils.InstallFunctions(GlobalDataView.prototype, DONT_ENUM, [
"getInt8", DataViewGetInt8JS,
"setInt8", DataViewSetInt8JS,
"getUint8", DataViewGetUint8JS,
"setUint8", DataViewSetUint8JS,
"getInt16", DataViewGetInt16JS,
"setInt16", DataViewSetInt16JS,
"getUint16", DataViewGetUint16JS,
"setUint16", DataViewSetUint16JS,
"getInt32", DataViewGetInt32JS,
"setInt32", DataViewSetInt32JS,
"getUint32", DataViewGetUint32JS,
"setUint32", DataViewSetUint32JS,
"getFloat32", DataViewGetFloat32JS,
"setFloat32", DataViewSetFloat32JS,
"getFloat64", DataViewGetFloat64JS,
"setFloat64", DataViewSetFloat64JS
]);
})
......@@ -419,217 +419,5 @@ RUNTIME_FUNCTION(Runtime_IsSharedInteger32TypedArray) {
obj->type() == kExternalInt32Array);
}
inline static bool NeedToFlipBytes(bool is_little_endian) {
#ifdef V8_TARGET_LITTLE_ENDIAN
return !is_little_endian;
#else
return is_little_endian;
#endif
}
template <int n>
inline void CopyBytes(uint8_t* target, uint8_t* source) {
for (int i = 0; i < n; i++) {
*(target++) = *(source++);
}
}
template <int n>
inline void FlipBytes(uint8_t* target, uint8_t* source) {
source = source + (n - 1);
for (int i = 0; i < n; i++) {
*(target++) = *(source--);
}
}
template <typename T>
inline static bool DataViewGetValue(Isolate* isolate,
Handle<JSDataView> data_view,
Handle<Object> byte_offset_obj,
bool is_little_endian, T* result) {
size_t byte_offset = 0;
if (!TryNumberToSize(*byte_offset_obj, &byte_offset)) {
return false;
}
Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()));
size_t data_view_byte_offset = NumberToSize(data_view->byte_offset());
size_t data_view_byte_length = NumberToSize(data_view->byte_length());
if (byte_offset + sizeof(T) > data_view_byte_length ||
byte_offset + sizeof(T) < byte_offset) { // overflow
return false;
}
union Value {
T data;
uint8_t bytes[sizeof(T)];
};
Value value;
size_t buffer_offset = data_view_byte_offset + byte_offset;
DCHECK(NumberToSize(buffer->byte_length()) >= buffer_offset + sizeof(T));
uint8_t* source =
static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
if (NeedToFlipBytes(is_little_endian)) {
FlipBytes<sizeof(T)>(value.bytes, source);
} else {
CopyBytes<sizeof(T)>(value.bytes, source);
}
*result = value.data;
return true;
}
template <typename T>
static bool DataViewSetValue(Isolate* isolate, Handle<JSDataView> data_view,
Handle<Object> byte_offset_obj,
bool is_little_endian, T data) {
size_t byte_offset = 0;
if (!TryNumberToSize(*byte_offset_obj, &byte_offset)) {
return false;
}
Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()));
size_t data_view_byte_offset = NumberToSize(data_view->byte_offset());
size_t data_view_byte_length = NumberToSize(data_view->byte_length());
if (byte_offset + sizeof(T) > data_view_byte_length ||
byte_offset + sizeof(T) < byte_offset) { // overflow
return false;
}
union Value {
T data;
uint8_t bytes[sizeof(T)];
};
Value value;
value.data = data;
size_t buffer_offset = data_view_byte_offset + byte_offset;
DCHECK(NumberToSize(buffer->byte_length()) >= buffer_offset + sizeof(T));
uint8_t* target =
static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
if (NeedToFlipBytes(is_little_endian)) {
FlipBytes<sizeof(T)>(target, value.bytes);
} else {
CopyBytes<sizeof(T)>(target, value.bytes);
}
return true;
}
#define DATA_VIEW_GETTER(TypeName, Type, Converter) \
RUNTIME_FUNCTION(Runtime_DataViewGet##TypeName) { \
HandleScope scope(isolate); \
DCHECK(args.length() == 3); \
CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \
CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \
CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 2); \
Type result; \
if (DataViewGetValue(isolate, holder, offset, is_little_endian, \
&result)) { \
return *isolate->factory()->Converter(result); \
} else { \
THROW_NEW_ERROR_RETURN_FAILURE( \
isolate, \
NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset)); \
} \
}
DATA_VIEW_GETTER(Uint8, uint8_t, NewNumberFromUint)
DATA_VIEW_GETTER(Int8, int8_t, NewNumberFromInt)
DATA_VIEW_GETTER(Uint16, uint16_t, NewNumberFromUint)
DATA_VIEW_GETTER(Int16, int16_t, NewNumberFromInt)
DATA_VIEW_GETTER(Uint32, uint32_t, NewNumberFromUint)
DATA_VIEW_GETTER(Int32, int32_t, NewNumberFromInt)
DATA_VIEW_GETTER(Float32, float, NewNumber)
DATA_VIEW_GETTER(Float64, double, NewNumber)
#undef DATA_VIEW_GETTER
template <typename T>
static T DataViewConvertValue(double value);
template <>
int8_t DataViewConvertValue<int8_t>(double value) {
return static_cast<int8_t>(DoubleToInt32(value));
}
template <>
int16_t DataViewConvertValue<int16_t>(double value) {
return static_cast<int16_t>(DoubleToInt32(value));
}
template <>
int32_t DataViewConvertValue<int32_t>(double value) {
return DoubleToInt32(value);
}
template <>
uint8_t DataViewConvertValue<uint8_t>(double value) {
return static_cast<uint8_t>(DoubleToUint32(value));
}
template <>
uint16_t DataViewConvertValue<uint16_t>(double value) {
return static_cast<uint16_t>(DoubleToUint32(value));
}
template <>
uint32_t DataViewConvertValue<uint32_t>(double value) {
return DoubleToUint32(value);
}
template <>
float DataViewConvertValue<float>(double value) {
return static_cast<float>(value);
}
template <>
double DataViewConvertValue<double>(double value) {
return value;
}
#define DATA_VIEW_SETTER(TypeName, Type) \
RUNTIME_FUNCTION(Runtime_DataViewSet##TypeName) { \
HandleScope scope(isolate); \
DCHECK(args.length() == 4); \
CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \
CONVERT_NUMBER_ARG_HANDLE_CHECKED(offset, 1); \
CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); \
CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 3); \
Type v = DataViewConvertValue<Type>(value->Number()); \
if (DataViewSetValue(isolate, holder, offset, is_little_endian, v)) { \
return isolate->heap()->undefined_value(); \
} else { \
THROW_NEW_ERROR_RETURN_FAILURE( \
isolate, \
NewRangeError(MessageTemplate::kInvalidDataViewAccessorOffset)); \
} \
}
DATA_VIEW_SETTER(Uint8, uint8_t)
DATA_VIEW_SETTER(Int8, int8_t)
DATA_VIEW_SETTER(Uint16, uint16_t)
DATA_VIEW_SETTER(Int16, int16_t)
DATA_VIEW_SETTER(Uint32, uint32_t)
DATA_VIEW_SETTER(Int32, int32_t)
DATA_VIEW_SETTER(Float32, float)
DATA_VIEW_SETTER(Float64, double)
#undef DATA_VIEW_SETTER
} // namespace internal
} // namespace v8
......@@ -904,23 +904,7 @@ namespace internal {
F(IsTypedArray, 1, 1) \
F(IsSharedTypedArray, 1, 1) \
F(IsSharedIntegerTypedArray, 1, 1) \
F(IsSharedInteger32TypedArray, 1, 1) \
F(DataViewGetUint8, 3, 1) \
F(DataViewGetInt8, 3, 1) \
F(DataViewGetUint16, 3, 1) \
F(DataViewGetInt16, 3, 1) \
F(DataViewGetUint32, 3, 1) \
F(DataViewGetInt32, 3, 1) \
F(DataViewGetFloat32, 3, 1) \
F(DataViewGetFloat64, 3, 1) \
F(DataViewSetUint8, 4, 1) \
F(DataViewSetInt8, 4, 1) \
F(DataViewSetUint16, 4, 1) \
F(DataViewSetInt16, 4, 1) \
F(DataViewSetUint32, 4, 1) \
F(DataViewSetInt32, 4, 1) \
F(DataViewSetFloat32, 4, 1) \
F(DataViewSetFloat64, 4, 1)
F(IsSharedInteger32TypedArray, 1, 1)
#define FOR_EACH_INTRINSIC_WASM(F) \
F(WasmGrowMemory, 1, 1) \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment