Commit fb52f5c5 authored by binji's avatar binji Committed by Commit bot

[SAB] Implement SharedArrayBuffer.prototype.slice

BUG=v8:5897

Review-Url: https://codereview.chromium.org/2741413006
Cr-Commit-Position: refs/heads/master@{#44075}
parent d71ef941
......@@ -221,8 +221,10 @@ class Genesis BASE_EMBEDDED {
void InitializeGlobal_experimental_fast_array_builtins();
Handle<JSFunction> InstallArrayBuffer(Handle<JSObject> target,
const char* name, Builtins::Name call,
BuiltinFunctionId id, bool is_shared);
const char* name,
Builtins::Name call_byteLength,
BuiltinFunctionId byteLength_id,
Builtins::Name call_slice);
Handle<JSFunction> InstallInternalArray(Handle<JSObject> target,
const char* name,
ElementsKind elements_kind);
......@@ -2562,7 +2564,8 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
{ // -- A r r a y B u f f e r
Handle<JSFunction> array_buffer_fun = InstallArrayBuffer(
global, "ArrayBuffer", Builtins::kArrayBufferPrototypeGetByteLength,
BuiltinFunctionId::kArrayBufferByteLength, false);
BuiltinFunctionId::kArrayBufferByteLength,
Builtins::kArrayBufferPrototypeSlice);
InstallWithIntrinsicDefaultProto(isolate, array_buffer_fun,
Context::ARRAY_BUFFER_FUN_INDEX);
InstallSpeciesGetter(array_buffer_fun);
......@@ -3723,7 +3726,8 @@ void Genesis::InitializeGlobal_harmony_sharedarraybuffer() {
Handle<JSFunction> shared_array_buffer_fun =
InstallArrayBuffer(global, "SharedArrayBuffer",
Builtins::kSharedArrayBufferPrototypeGetByteLength,
BuiltinFunctionId::kSharedArrayBufferByteLength, true);
BuiltinFunctionId::kSharedArrayBufferByteLength,
Builtins::kSharedArrayBufferPrototypeSlice);
native_context()->set_shared_array_buffer_fun(*shared_array_buffer_fun);
Handle<String> name = factory->InternalizeUtf8String("Atomics");
......@@ -3927,9 +3931,9 @@ void Genesis::InitializeGlobal_icu_case_mapping() {
Handle<JSFunction> Genesis::InstallArrayBuffer(Handle<JSObject> target,
const char* name,
Builtins::Name call,
BuiltinFunctionId id,
bool is_shared) {
Builtins::Name call_byteLength,
BuiltinFunctionId byteLength_id,
Builtins::Name call_slice) {
// Create the %ArrayBufferPrototype%
// Setup the {prototype} with the given {name} for @@toStringTag.
Handle<JSObject> prototype =
......@@ -3956,14 +3960,10 @@ Handle<JSFunction> Genesis::InstallArrayBuffer(Handle<JSObject> target,
Builtins::kArrayBufferIsView, 1, true);
// Install the "byteLength" getter on the {prototype}.
SimpleInstallGetter(prototype, factory()->byte_length_string(), call, false,
id);
SimpleInstallGetter(prototype, factory()->byte_length_string(),
call_byteLength, false, byteLength_id);
// TODO(binji): support SharedArrayBuffer.prototype.slice as well.
if (!is_shared) {
SimpleInstallFunction(prototype, "slice",
Builtins::kArrayBufferPrototypeSlice, 2, true);
}
SimpleInstallFunction(prototype, "slice", call_slice, 2, true);
return array_buffer_fun;
}
......
......@@ -11,8 +11,8 @@
namespace v8 {
namespace internal {
#define CHECK_IS_NOT_SHARED_ARRAY_BUFFER(name, method) \
if (name->is_shared()) { \
#define CHECK_SHARED(expected, name, method) \
if (name->is_shared() != expected) { \
THROW_NEW_ERROR_RETURN_FAILURE( \
isolate, \
NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
......@@ -75,12 +75,22 @@ BUILTIN(ArrayBufferPrototypeGetByteLength) {
const char* const kMethodName = "get ArrayBuffer.prototype.byteLength";
HandleScope scope(isolate);
CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName);
CHECK_SHARED(false, array_buffer, kMethodName);
// TODO(franzih): According to the ES6 spec, we should throw a TypeError
// here if the JSArrayBuffer is detached.
return array_buffer->byte_length();
}
// ES7 sharedmem 6.3.4.1 get SharedArrayBuffer.prototype.byteLength
BUILTIN(SharedArrayBufferPrototypeGetByteLength) {
const char* const kMethodName = "get SharedArrayBuffer.prototype.byteLength";
HandleScope scope(isolate);
CHECK_RECEIVER(JSArrayBuffer, array_buffer,
"get SharedArrayBuffer.prototype.byteLength");
CHECK_SHARED(true, array_buffer, kMethodName);
return array_buffer->byte_length();
}
// ES6 section 24.1.3.1 ArrayBuffer.isView ( arg )
BUILTIN(ArrayBufferIsView) {
SealHandleScope shs(isolate);
......@@ -89,46 +99,46 @@ BUILTIN(ArrayBufferIsView) {
return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView());
}
// ES #sec-arraybuffer.prototype.slice
// ArrayBuffer.prototype.slice ( start, end )
BUILTIN(ArrayBufferPrototypeSlice) {
const char* const kMethodName = "ArrayBuffer.prototype.slice";
static Object* SliceHelper(BuiltinArguments args, Isolate* isolate,
const char* kMethodName, bool is_shared) {
HandleScope scope(isolate);
Handle<Object> start = args.at(1);
Handle<Object> end = args.atOrUndefined(isolate, 2);
// 2. If Type(O) is not Object, throw a TypeError exception.
// 3. If O does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
// * If Type(O) is not Object, throw a TypeError exception.
// * If O does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
// 4. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName);
// * [AB] If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
// * [SAB] If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
CHECK_SHARED(is_shared, array_buffer, kMethodName);
// 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
if (array_buffer->was_neutered()) {
// * [AB] If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
if (!is_shared && array_buffer->was_neutered()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// 6. Let len be O.[[ArrayBufferByteLength]].
// * [AB] Let len be O.[[ArrayBufferByteLength]].
// * [SAB] Let len be O.[[ArrayBufferByteLength]].
double const len = array_buffer->byte_length()->Number();
// 7. Let relativeStart be ? ToInteger(start).
// * Let relativeStart be ? ToInteger(start).
Handle<Object> relative_start;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start,
Object::ToInteger(isolate, start));
// 8. If relativeStart < 0, let first be max((len + relativeStart), 0); else
// let first be min(relativeStart, len).
// * If relativeStart < 0, let first be max((len + relativeStart), 0); else
// let first be min(relativeStart, len).
double const first = (relative_start->Number() < 0)
? Max(len + relative_start->Number(), 0.0)
: Min(relative_start->Number(), len);
Handle<Object> first_obj = isolate->factory()->NewNumber(first);
// 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
// ToInteger(end).
// * If end is undefined, let relativeEnd be len; else let relativeEnd be ?
// ToInteger(end).
double relative_end;
if (end->IsUndefined(isolate)) {
relative_end = len;
......@@ -139,24 +149,27 @@ BUILTIN(ArrayBufferPrototypeSlice) {
relative_end = relative_end_obj->Number();
}
// 10. If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
// final be min(relativeEnd, len).
// * If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
// final be min(relativeEnd, len).
double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0)
: Min(relative_end, len);
// 11. Let newLen be max(final-first, 0).
// * Let newLen be max(final-first, 0).
double const new_len = Max(final_ - first, 0.0);
Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len);
// 12. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
Handle<JSFunction> arraybuffer_fun = isolate->array_buffer_fun();
// * [AB] Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
// * [SAB] Let ctor be ? SpeciesConstructor(O, %SharedArrayBuffer%).
Handle<JSFunction> constructor_fun = is_shared
? isolate->shared_array_buffer_fun()
: isolate->array_buffer_fun();
Handle<Object> ctor;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, ctor,
Object::SpeciesConstructor(
isolate, Handle<JSReceiver>::cast(args.receiver()), arraybuffer_fun));
isolate, Handle<JSReceiver>::cast(args.receiver()), constructor_fun));
// 13. Let new be ? Construct(ctor, newLen).
// * Let new be ? Construct(ctor, newLen).
Handle<JSReceiver> new_;
{
const int argc = 1;
......@@ -172,8 +185,8 @@ BUILTIN(ArrayBufferPrototypeSlice) {
new_ = Handle<JSReceiver>::cast(new_obj);
}
// 14. If new does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
// * If new does not have an [[ArrayBufferData]] internal slot, throw a
// TypeError exception.
if (!new_->IsJSArrayBuffer()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
......@@ -182,42 +195,53 @@ BUILTIN(ArrayBufferPrototypeSlice) {
new_));
}
// 15. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
// * [AB] If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
// * [SAB] If IsSharedArrayBuffer(new) is false, throw a TypeError exception.
Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_);
CHECK_IS_NOT_SHARED_ARRAY_BUFFER(new_array_buffer, kMethodName);
CHECK_SHARED(is_shared, new_array_buffer, kMethodName);
// 16. If IsDetachedBuffer(new) is true, throw a TypeError exception.
if (new_array_buffer->was_neutered()) {
// * [AB] If IsDetachedBuffer(new) is true, throw a TypeError exception.
if (!is_shared && new_array_buffer->was_neutered()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// 17. If SameValue(new, O) is true, throw a TypeError exception.
if (new_->SameValue(*args.receiver())) {
// * [AB] If SameValue(new, O) is true, throw a TypeError exception.
if (!is_shared && new_->SameValue(*args.receiver())) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis));
}
// 18. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
// * [SAB] If new.[[ArrayBufferData]] and O.[[ArrayBufferData]] are the same
// Shared Data Block values, throw a TypeError exception.
if (is_shared &&
new_array_buffer->backing_store() == array_buffer->backing_store()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kSharedArrayBufferSpeciesThis));
}
// * If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
if (new_array_buffer->byte_length()->Number() < new_len) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kArrayBufferTooShort));
isolate,
NewTypeError(is_shared ? MessageTemplate::kSharedArrayBufferTooShort
: MessageTemplate::kArrayBufferTooShort));
}
// 19. NOTE: Side-effects of the above steps may have detached O.
// 20. If IsDetachedBuffer(O) is true, throw a TypeError exception.
if (array_buffer->was_neutered()) {
// * [AB] NOTE: Side-effects of the above steps may have detached O.
// * [AB] If IsDetachedBuffer(O) is true, throw a TypeError exception.
if (!is_shared && array_buffer->was_neutered()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
kMethodName)));
}
// 21. Let fromBuf be O.[[ArrayBufferData]].
// 22. Let toBuf be new.[[ArrayBufferData]].
// 23. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
// * Let fromBuf be O.[[ArrayBufferData]].
// * Let toBuf be new.[[ArrayBufferData]].
// * Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
size_t first_size = 0, new_len_size = 0;
CHECK(TryNumberToSize(*first_obj, &first_size));
CHECK(TryNumberToSize(*new_len_obj, &new_len_size));
......@@ -238,5 +262,18 @@ BUILTIN(ArrayBufferPrototypeSlice) {
return *new_;
}
// ES #sec-sharedarraybuffer.prototype.slice
BUILTIN(SharedArrayBufferPrototypeSlice) {
const char* const kMethodName = "SharedArrayBuffer.prototype.slice";
return SliceHelper(args, isolate, kMethodName, true);
}
// ES #sec-arraybuffer.prototype.slice
// ArrayBuffer.prototype.slice ( start, end )
BUILTIN(ArrayBufferPrototypeSlice) {
const char* const kMethodName = "ArrayBuffer.prototype.slice";
return SliceHelper(args, isolate, kMethodName, false);
}
} // namespace internal
} // namespace v8
......@@ -18,20 +18,8 @@
namespace v8 {
namespace internal {
// ES7 sharedmem 6.3.4.1 get SharedArrayBuffer.prototype.byteLength
BUILTIN(SharedArrayBufferPrototypeGetByteLength) {
HandleScope scope(isolate);
CHECK_RECEIVER(JSArrayBuffer, array_buffer,
"get SharedArrayBuffer.prototype.byteLength");
if (!array_buffer->is_shared()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
isolate->factory()->NewStringFromAsciiChecked(
"get SharedArrayBuffer.prototype.byteLength"),
args.receiver()));
}
return array_buffer->byte_length();
}
// See builtins-arraybuffer.cc for implementations of
// SharedArrayBuffer.prototye.byteLength and SharedArrayBuffer.prototype.slice
inline bool AtomicIsLockFree(uint32_t size) {
return size == 1 || size == 2 || size == 4;
......
......@@ -792,6 +792,7 @@ class Isolate;
\
/* SharedArrayBuffer */ \
CPP(SharedArrayBufferPrototypeGetByteLength) \
CPP(SharedArrayBufferPrototypeSlice) \
TFJ(AtomicsLoad, 2, kArray, kIndex) \
TFJ(AtomicsStore, 3, kArray, kIndex, kValue) \
TFJ(AtomicsExchange, 3, kArray, kIndex, kValue) \
......
......@@ -465,6 +465,11 @@ class ErrorUtils : public AllStatic {
"'caller' and 'arguments' are restricted function properties and cannot " \
"be accessed in this context.") \
T(ReturnMethodNotCallable, "The iterator's 'return' method is not callable") \
T(SharedArrayBufferTooShort, \
"Derived SharedArrayBuffer constructor created a buffer which was too " \
"small") \
T(SharedArrayBufferSpeciesThis, \
"SharedArrayBuffer subclass returned this from species constructor") \
T(StaticPrototype, "Classes may not have static property named prototype") \
T(StrictCannotAssign, "Cannot assign to read only '%' in strict mode") \
T(StrictDeleteProperty, "Cannot delete property '%' of %") \
......
......@@ -70,13 +70,6 @@ function TestByteLengthNotWritable() {
TestByteLengthNotWritable();
function TestArrayBufferNoSlice() {
var sab = new SharedArrayBuffer(10);
assertEquals(undefined, sab.slice);
}
TestArrayBufferNoSlice();
// Typed arrays using SharedArrayBuffers
// TODO(binji): how many of these tests are necessary if there are no new
......
......@@ -515,9 +515,6 @@
# https://bugs.chromium.org/p/v8/issues/detail?id=5984
'built-ins/SharedArrayBuffer/proto-from-ctor-realm': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=5897
'built-ins/SharedArrayBuffer/prototype/slice/*': [SKIP],
# https://bugs.chromium.org/p/v8/issues/detail?id=6045
'intl402/NumberFormat/prototype/format/11.3.2_TRF': [FAIL],
'intl402/NumberFormat/prototype/format/11.3.2_TRP': [FAIL],
......
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