Skip to content

Commit

Permalink
b=630117, rename typed array slice() -> subarray(); r=jwalden, a=block
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuk committed Feb 1, 2011
1 parent e0cf36d commit 5c95af4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
16 changes: 8 additions & 8 deletions js/src/jstypedarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,9 @@ class TypedArrayTemplate
cx->destroy<ThisTypeArray>(tarray);
}

/* slice(start[, end]) */
/* subarray(start[, end]) */
static JSBool
fun_slice(JSContext *cx, uintN argc, Value *vp)
fun_subarray(JSContext *cx, uintN argc, Value *vp)
{
JSObject *obj = ToObject(cx, &vp[1]);
if (!obj)
Expand All @@ -832,10 +832,10 @@ class TypedArrayTemplate
return false;

if (obj->getClass() != fastClass()) {
// someone tried to apply this slice() to the wrong class
// someone tried to apply this subarray() to the wrong class
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_INCOMPATIBLE_METHOD,
fastClass()->name, "slice", obj->getClass()->name);
fastClass()->name, "subarray", obj->getClass()->name);
return false;
}

Expand Down Expand Up @@ -875,7 +875,7 @@ class TypedArrayTemplate
if (begin > end)
begin = end;

ThisTypeArray *ntarray = tarray->slice(cx, begin, end);
ThisTypeArray *ntarray = tarray->subarray(cx, begin, end);
if (!ntarray) {
// this should rarely ever happen
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
Expand Down Expand Up @@ -982,7 +982,7 @@ class TypedArrayTemplate
return reinterpret_cast<ThisTypeArray*>(obj->getPrivate());
}

// helper used by both the constructor and Slice()
// helper used by both the constructor and Subarray()
static bool
makeFastWithPrivate(JSContext *cx, JSObject *obj, ThisTypeArray *tarray)
{
Expand Down Expand Up @@ -1093,7 +1093,7 @@ class TypedArrayTemplate
inline void copyIndexToValue(JSContext *cx, uint32 index, Value *vp);

ThisTypeArray *
slice(JSContext *cx, uint32 begin, uint32 end)
subarray(JSContext *cx, uint32 begin, uint32 end)
{
if (begin > length || end > length)
return NULL;
Expand Down Expand Up @@ -1484,7 +1484,7 @@ JSPropertySpec TypedArray::jsprops[] = {

#define IMPL_TYPED_ARRAY_STATICS(_typedArray) \
template<> JSFunctionSpec _typedArray::jsfuncs[] = { \
JS_FN("slice", _typedArray::fun_slice, 2, 0), \
JS_FN("subarray", _typedArray::fun_subarray, 2, 0), \
JS_FN("set", _typedArray::fun_set, 2, 0), \
JS_FS_END \
}
Expand Down
34 changes: 21 additions & 13 deletions js/src/tests/js1_8_5/extensions/typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ function test()
checkThrows(function() new Float32Array(buf, 500));
checkThrows(function() new Float32Array(buf, 0, 50));

var sl = a.slice(5,10);
var sl = a.subarray(5,10);
check(function() sl.length == 5);
check(function() sl.buffer == a.buffer);
check(function() sl.byteLength == 20);
check(function() sl.byteOffset == 20);

check(function() a.slice(5,5).length == 0);
check(function() a.slice(-5).length == 5);
check(function() a.slice(-100).length == 20);
check(function() a.slice(0, 2).length == 2);
check(function() a.slice().length == a.length);
check(function() a.slice(-7,-5).length == 2);
check(function() a.slice(-5,-7).length == 0);
check(function() a.slice(15).length == 5);
check(function() a.subarray(5,5).length == 0);
check(function() a.subarray(-5).length == 5);
check(function() a.subarray(-100).length == 20);
check(function() a.subarray(0, 2).length == 2);
check(function() a.subarray().length == a.length);
check(function() a.subarray(-7,-5).length == 2);
check(function() a.subarray(-5,-7).length == 0);
check(function() a.subarray(15).length == 5);

a = new Uint8Array([0xaa, 0xbb, 0xcc]);
check(function() a.length == 3);
Expand Down Expand Up @@ -280,32 +280,40 @@ function test()
a[0] == 7 && a[1] == 8 && a[2] == 9 &&
a[3] == 7 && a[4] == 8 && a[5] == 9 &&
a[6] == 0 && a[7] == 0 && a[8] == 0);
a.set(a.slice(0,3), 6);
a.set(a.subarray(0,3), 6);
check(function()
a[0] == 7 && a[1] == 8 && a[2] == 9 &&
a[3] == 7 && a[4] == 8 && a[5] == 9 &&
a[6] == 7 && a[7] == 8 && a[8] == 9);
a.set([1,2,3,4,5,6,7,8,9]);
a.set(a.slice(0,6), 3);
a.set(a.subarray(0,6), 3);
check(function()
a[0] == 1 && a[1] == 2 && a[2] == 3 &&
a[3] == 1 && a[4] == 2 && a[5] == 3 &&
a[6] == 4 && a[7] == 5 && a[8] == 6);
a.set(a.slice(3,9), 0);
a.set(a.subarray(3,9), 0);
check(function()
a[0] == 1 && a[1] == 2 && a[2] == 3 &&
a[3] == 4 && a[4] == 5 && a[5] == 6 &&
a[6] == 4 && a[7] == 5 && a[8] == 6);
// verify that subarray() returns a new view that
// references the same buffer
a.subarray(0,3).set(a.subarray(3,6), 0);
check(function()
a[0] == 4 && a[1] == 5 && a[2] == 6 &&
a[3] == 4 && a[4] == 5 && a[5] == 6 &&
a[6] == 4 && a[7] == 5 && a[8] == 6);
a = new ArrayBuffer(0x10);
checkThrows(function() new Uint32Array(buffer, 4, 0x3FFFFFFF));
checkThrows(function() new Float32Array(null));
a = new Uint8Array(0x100);
checkThrows(function() Uint32Array.prototype.slice.apply(a, [0, 0x100]));
checkThrows(function() Uint32Array.prototype.subarray.apply(a, [0, 0x100]));
// The prototypes are objects that don't have a length property, so they act
// like empty arrays.
Expand Down

0 comments on commit 5c95af4

Please sign in to comment.