Extend subarray web compatibility fix to --harmony-species

ES2016 TypedArray subclassing semantics break the Node.js Buffer module,
also used on the web. I wrote a pull request against the web and Node
versions to fix the issue, but the pull request has not yet been granted,
and this is blocking shipping the change. For now, this patch extends the
web compatibility workaround to the --harmony-species flag, so that
Symbol.species and associated subclassing semantics can ship independently.

R=cbruni
BUG=v8:4665
LOG=Y

Review URL: https://codereview.chromium.org/1678123002

Cr-Commit-Position: refs/heads/master@{#33826}
This commit is contained in:
littledan 2016-02-08 13:16:26 -08:00 committed by Commit bot
parent 46bf5aa240
commit e1de522afa
2 changed files with 37 additions and 2 deletions

View File

@ -300,8 +300,10 @@ function NAMESubArray(begin, end) {
var newLength = endInt - beginInt;
var beginByteOffset =
%_ArrayBufferViewGetByteOffset(this) + beginInt * ELEMENT_SIZE;
return TypedArraySpeciesCreate(this, %TypedArrayGetBuffer(this),
beginByteOffset, newLength, true);
// BUG(v8:4665): For web compatibility, subarray needs to always build an
// instance of the default constructor.
// TODO(littledan): Switch to the standard or standardize the fix
return new GlobalNAME(%TypedArrayGetBuffer(this), beginByteOffset, newLength);
}
endmacro

View File

@ -0,0 +1,33 @@
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-species
// First test case
function FirstBuffer () {}
FirstBuffer.prototype.__proto__ = Uint8Array.prototype
FirstBuffer.__proto__ = Uint8Array
var buf = new Uint8Array(10)
buf.__proto__ = FirstBuffer.prototype
var buf2 = buf.subarray(2)
assertEquals(8, buf2.length);
// Second test case
function SecondBuffer (arg) {
var arr = new Uint8Array(arg)
arr.__proto__ = SecondBuffer.prototype
return arr
}
SecondBuffer.prototype.__proto__ = Uint8Array.prototype
SecondBuffer.__proto__ = Uint8Array
var buf3 = new SecondBuffer(10)
var buf4 = buf3.subarray(2)
assertEquals(8, buf4.length);