Commit 81fe5b3c authored by pan.deng's avatar pan.deng Committed by Commit bot

Support SAB atomics for offset-TypedArray

BUG=497295
LOG=n

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

Cr-Commit-Position: refs/heads/master@{#32064}
parent 8e5840e1
...@@ -444,18 +444,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) { ...@@ -444,18 +444,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoCompareExchange<ctype>(isolate, buffer, index, oldobj, newobj); return DoCompareExchange<ctype>(isolate, source, index, oldobj, newobj);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoCompareExchangeUint8Clamped(isolate, buffer, index, oldobj, return DoCompareExchangeUint8Clamped(isolate, source, index, oldobj,
newobj); newobj);
default: default:
...@@ -475,18 +476,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsLoad) { ...@@ -475,18 +476,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsLoad) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoLoad<ctype>(isolate, buffer, index); return DoLoad<ctype>(isolate, source, index);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoLoad<uint8_t>(isolate, buffer, index); return DoLoad<uint8_t>(isolate, source, index);
default: default:
break; break;
...@@ -506,18 +508,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsStore) { ...@@ -506,18 +508,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsStore) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoStore<ctype>(isolate, buffer, index, value); return DoStore<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoStoreUint8Clamped(isolate, buffer, index, value); return DoStoreUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -537,18 +540,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsAdd) { ...@@ -537,18 +540,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsAdd) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoAdd<ctype>(isolate, buffer, index, value); return DoAdd<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoAddUint8Clamped(isolate, buffer, index, value); return DoAddUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -568,18 +572,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsSub) { ...@@ -568,18 +572,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsSub) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoSub<ctype>(isolate, buffer, index, value); return DoSub<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoSubUint8Clamped(isolate, buffer, index, value); return DoSubUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -599,18 +604,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsAnd) { ...@@ -599,18 +604,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsAnd) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoAnd<ctype>(isolate, buffer, index, value); return DoAnd<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoAndUint8Clamped(isolate, buffer, index, value); return DoAndUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -630,18 +636,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsOr) { ...@@ -630,18 +636,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsOr) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoOr<ctype>(isolate, buffer, index, value); return DoOr<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoOrUint8Clamped(isolate, buffer, index, value); return DoOrUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -661,18 +668,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsXor) { ...@@ -661,18 +668,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsXor) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoXor<ctype>(isolate, buffer, index, value); return DoXor<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoXorUint8Clamped(isolate, buffer, index, value); return DoXorUint8Clamped(isolate, source, index, value);
default: default:
break; break;
...@@ -692,18 +700,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsExchange) { ...@@ -692,18 +700,19 @@ RUNTIME_FUNCTION(Runtime_AtomicsExchange) {
RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
void* buffer = sta->GetBuffer()->backing_store(); uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
NumberToSize(isolate, sta->byte_offset());
switch (sta->type()) { switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
case kExternal##Type##Array: \ case kExternal##Type##Array: \
return DoExchange<ctype>(isolate, buffer, index, value); return DoExchange<ctype>(isolate, source, index, value);
INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE #undef TYPED_ARRAY_CASE
case kExternalUint8ClampedArray: case kExternalUint8ClampedArray:
return DoExchangeUint8Clamped(isolate, buffer, index, value); return DoExchangeUint8Clamped(isolate, source, index, value);
default: default:
break; break;
......
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var add = stdlib.Atomics.add; var add = stdlib.Atomics.add;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) { ...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -71,10 +68,10 @@ function clearArray() { ...@@ -71,10 +68,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
assertEquals(0, f(0, 10), name); assertEquals(0, f(0, 10), name);
assertEquals(10, ta[0]); assertEquals(10, ta[0]);
...@@ -85,9 +82,21 @@ function testElementType(taConstr, f) { ...@@ -85,9 +82,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.addi8); function testElement(m, offset) {
testElementType(Int16Array, m.addi16); testElementType(Int8Array, m.addi8, offset);
testElementType(Int32Array, m.addi32); testElementType(Int16Array, m.addi16, offset);
testElementType(Uint8Array, m.addu8); testElementType(Int32Array, m.addi32, offset);
testElementType(Uint16Array, m.addu16); testElementType(Uint8Array, m.addu8, offset);
testElementType(Uint32Array, m.addu32); testElementType(Uint16Array, m.addu16, offset);
testElementType(Uint32Array, m.addu32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var and = stdlib.Atomics.and; var and = stdlib.Atomics.and;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) { ...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -71,10 +68,10 @@ function clearArray() { ...@@ -71,10 +68,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
ta[0] = 0x7f; ta[0] = 0x7f;
assertEquals(0x7f, f(0, 0xf), name); assertEquals(0x7f, f(0, 0xf), name);
...@@ -86,9 +83,21 @@ function testElementType(taConstr, f) { ...@@ -86,9 +83,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.andi8); function testElement(m, offset) {
testElementType(Int16Array, m.andi16); testElementType(Int8Array, m.andi8, offset);
testElementType(Int32Array, m.andi32); testElementType(Int16Array, m.andi16, offset);
testElementType(Uint8Array, m.andu8); testElementType(Int32Array, m.andi32, offset);
testElementType(Uint16Array, m.andu16); testElementType(Uint8Array, m.andu8, offset);
testElementType(Uint32Array, m.andu32); testElementType(Uint16Array, m.andu16, offset);
testElementType(Uint32Array, m.andu32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var compareExchange = stdlib.Atomics.compareExchange; var compareExchange = stdlib.Atomics.compareExchange;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -67,9 +67,6 @@ function Module(stdlib, foreign, heap) { ...@@ -67,9 +67,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -77,10 +74,10 @@ function clearArray() { ...@@ -77,10 +74,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f, oobValue) { function testElementType(taConstr, f, oobValue, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
assertEquals(0, ta[0]); assertEquals(0, ta[0]);
assertEquals(0, f(0, 0, 50), name); assertEquals(0, f(0, 0, 50), name);
...@@ -93,9 +90,21 @@ function testElementType(taConstr, f, oobValue) { ...@@ -93,9 +90,21 @@ function testElementType(taConstr, f, oobValue) {
assertEquals(oobValue, f(ta.length, 0, 0), name); assertEquals(oobValue, f(ta.length, 0, 0), name);
} }
testElementType(Int8Array, m.compareExchangei8, 0); function testElement(m, offset) {
testElementType(Int16Array, m.compareExchangei16, 0); testElementType(Int8Array, m.compareExchangei8, 0, offset);
testElementType(Int32Array, m.compareExchangei32, 0); testElementType(Int16Array, m.compareExchangei16, 0, offset);
testElementType(Uint8Array, m.compareExchangeu8, 0); testElementType(Int32Array, m.compareExchangei32, 0, offset);
testElementType(Uint16Array, m.compareExchangeu16, 0); testElementType(Uint8Array, m.compareExchangeu8, 0, offset);
testElementType(Uint32Array, m.compareExchangeu32, 0); testElementType(Uint16Array, m.compareExchangeu16, 0, offset);
testElementType(Uint32Array, m.compareExchangeu32, 0, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var exchange = stdlib.Atomics.exchange; var exchange = stdlib.Atomics.exchange;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -71,10 +71,10 @@ function clearArray() { ...@@ -71,10 +71,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
ta[0] = 0x7f; ta[0] = 0x7f;
assertEquals(0x7f, f(0, 0xf), name); assertEquals(0x7f, f(0, 0xf), name);
...@@ -84,9 +84,21 @@ function testElementType(taConstr, f) { ...@@ -84,9 +84,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.exchangei8); function testElement(m, offset) {
testElementType(Int16Array, m.exchangei16); testElementType(Int8Array, m.exchangei8, offset);
testElementType(Int32Array, m.exchangei32); testElementType(Int16Array, m.exchangei16, offset);
testElementType(Uint8Array, m.exchangeu8); testElementType(Int32Array, m.exchangei32, offset);
testElementType(Uint16Array, m.exchangeu16); testElementType(Uint8Array, m.exchangeu8, offset);
testElementType(Uint32Array, m.exchangeu32); testElementType(Uint16Array, m.exchangeu16, offset);
testElementType(Uint32Array, m.exchangeu32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var load = stdlib.Atomics.load; var load = stdlib.Atomics.load;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -55,9 +55,6 @@ function Module(stdlib, foreign, heap) { ...@@ -55,9 +55,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -65,10 +62,10 @@ function clearArray() { ...@@ -65,10 +62,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f, oobValue) { function testElementType(taConstr, f, oobValue, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
ta[0] = 10; ta[0] = 10;
assertEquals(10, f(0), name); assertEquals(10, f(0), name);
...@@ -78,9 +75,21 @@ function testElementType(taConstr, f, oobValue) { ...@@ -78,9 +75,21 @@ function testElementType(taConstr, f, oobValue) {
assertEquals(oobValue, f(ta.length), name); assertEquals(oobValue, f(ta.length), name);
} }
testElementType(Int8Array, m.loadi8, 0); function testElement(m, offset) {
testElementType(Int16Array, m.loadi16, 0); testElementType(Int8Array, m.loadi8, 0, offset);
testElementType(Int32Array, m.loadi32, 0); testElementType(Int16Array, m.loadi16, 0, offset);
testElementType(Uint8Array, m.loadu8, 0); testElementType(Int32Array, m.loadi32, 0, offset);
testElementType(Uint16Array, m.loadu16, 0); testElementType(Uint8Array, m.loadu8, 0, offset);
testElementType(Uint32Array, m.loadu32, 0); testElementType(Uint16Array, m.loadu16, 0, offset);
testElementType(Uint32Array, m.loadu32, 0, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var or = stdlib.Atomics.or; var or = stdlib.Atomics.or;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -71,10 +71,10 @@ function clearArray() { ...@@ -71,10 +71,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
assertEquals(0, f(0, 0xf), name); assertEquals(0, f(0, 0xf), name);
assertEquals(0xf, ta[0]); assertEquals(0xf, ta[0]);
...@@ -85,9 +85,21 @@ function testElementType(taConstr, f) { ...@@ -85,9 +85,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.ori8); function testElement(m, offset) {
testElementType(Int16Array, m.ori16); testElementType(Int8Array, m.ori8, offset);
testElementType(Int32Array, m.ori32); testElementType(Int16Array, m.ori16, offset);
testElementType(Uint8Array, m.oru8); testElementType(Int32Array, m.ori32, offset);
testElementType(Uint16Array, m.oru16); testElementType(Uint8Array, m.oru8, offset);
testElementType(Uint32Array, m.oru32); testElementType(Uint16Array, m.oru16, offset);
testElementType(Uint32Array, m.oru32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var store = stdlib.Atomics.store; var store = stdlib.Atomics.store;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) { ...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -71,10 +68,10 @@ function clearArray() { ...@@ -71,10 +68,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f, oobValue) { function testElementType(taConstr, f, oobValue, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
assertEquals(10, f(0, 10), name); assertEquals(10, f(0, 10), name);
assertEquals(10, ta[0]); assertEquals(10, ta[0]);
...@@ -83,9 +80,21 @@ function testElementType(taConstr, f, oobValue) { ...@@ -83,9 +80,21 @@ function testElementType(taConstr, f, oobValue) {
assertEquals(oobValue, f(ta.length, 0), name); assertEquals(oobValue, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.storei8, 0); function testElement(m, offset) {
testElementType(Int16Array, m.storei16, 0); testElementType(Int8Array, m.storei8, 0, offset);
testElementType(Int32Array, m.storei32, 0); testElementType(Int16Array, m.storei16, 0, offset);
testElementType(Uint8Array, m.storeu8, 0); testElementType(Int32Array, m.storei32, 0, offset);
testElementType(Uint16Array, m.storeu16, 0); testElementType(Uint8Array, m.storeu8, 0, offset);
testElementType(Uint32Array, m.storeu32, 0); testElementType(Uint16Array, m.storeu16, 0, offset);
testElementType(Uint32Array, m.storeu32, 0, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var sub = stdlib.Atomics.sub; var sub = stdlib.Atomics.sub;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) { ...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -71,10 +68,10 @@ function clearArray() { ...@@ -71,10 +68,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
ta[0] = 30; ta[0] = 30;
assertEquals(30, f(0, 10), name); assertEquals(30, f(0, 10), name);
...@@ -86,9 +83,21 @@ function testElementType(taConstr, f) { ...@@ -86,9 +83,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.subi8); function testElement(m, offset) {
testElementType(Int16Array, m.subi16); testElementType(Int8Array, m.subi8, offset);
testElementType(Int32Array, m.subi32); testElementType(Int16Array, m.subi16, offset);
testElementType(Uint8Array, m.subu8); testElementType(Int32Array, m.subi32, offset);
testElementType(Uint16Array, m.subu16); testElementType(Uint8Array, m.subu8, offset);
testElementType(Uint32Array, m.subu32); testElementType(Uint16Array, m.subu16, offset);
testElementType(Uint32Array, m.subu32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
// Flags: --harmony-sharedarraybuffer // Flags: --harmony-sharedarraybuffer
function Module(stdlib, foreign, heap) { function Module(stdlib, foreign, heap, offset) {
"use asm"; "use asm";
var MEM8 = new stdlib.Int8Array(heap); var MEM8 = new stdlib.Int8Array(heap, offset);
var MEM16 = new stdlib.Int16Array(heap); var MEM16 = new stdlib.Int16Array(heap, offset);
var MEM32 = new stdlib.Int32Array(heap); var MEM32 = new stdlib.Int32Array(heap, offset);
var MEMU8 = new stdlib.Uint8Array(heap); var MEMU8 = new stdlib.Uint8Array(heap, offset);
var MEMU16 = new stdlib.Uint16Array(heap); var MEMU16 = new stdlib.Uint16Array(heap, offset);
var MEMU32 = new stdlib.Uint32Array(heap); var MEMU32 = new stdlib.Uint32Array(heap, offset);
var xor = stdlib.Atomics.xor; var xor = stdlib.Atomics.xor;
var fround = stdlib.Math.fround; var fround = stdlib.Math.fround;
...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) { ...@@ -61,9 +61,6 @@ function Module(stdlib, foreign, heap) {
}; };
} }
var sab = new SharedArrayBuffer(16);
var m = Module(this, {}, sab);
function clearArray() { function clearArray() {
var ui8 = new Uint8Array(sab); var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) { for (var i = 0; i < sab.byteLength; ++i) {
...@@ -71,10 +68,10 @@ function clearArray() { ...@@ -71,10 +68,10 @@ function clearArray() {
} }
} }
function testElementType(taConstr, f) { function testElementType(taConstr, f, offset) {
clearArray(); clearArray();
var ta = new taConstr(sab); var ta = new taConstr(sab, offset);
var name = Object.prototype.toString.call(ta); var name = Object.prototype.toString.call(ta);
assertEquals(0, f(0, 0xf), name); assertEquals(0, f(0, 0xf), name);
assertEquals(0xf, ta[0]); assertEquals(0xf, ta[0]);
...@@ -85,9 +82,21 @@ function testElementType(taConstr, f) { ...@@ -85,9 +82,21 @@ function testElementType(taConstr, f) {
assertEquals(0, f(ta.length, 0), name); assertEquals(0, f(ta.length, 0), name);
} }
testElementType(Int8Array, m.xori8); function testElement(m, offset) {
testElementType(Int16Array, m.xori16); testElementType(Int8Array, m.xori8, offset);
testElementType(Int32Array, m.xori32); testElementType(Int16Array, m.xori16, offset);
testElementType(Uint8Array, m.xoru8); testElementType(Int32Array, m.xori32, offset);
testElementType(Uint16Array, m.xoru16); testElementType(Uint8Array, m.xoru8, offset);
testElementType(Uint32Array, m.xoru32); testElementType(Uint16Array, m.xoru16, offset);
testElementType(Uint32Array, m.xoru32, offset);
}
var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);
offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);
...@@ -77,12 +77,13 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -77,12 +77,13 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
(function TestBadIndex() { (function TestBadIndex() {
var sab = new SharedArrayBuffer(8); var sab = new SharedArrayBuffer(8);
var si32a = new Int32Array(sab); var si32a = new Int32Array(sab);
var si32a2 = new Int32Array(sab, 4);
// Non-integer indexes are converted to an integer first, so they should all // Non-integer indexes are converted to an integer first, so they should all
// operate on index 0. // operate on index 0.
[undefined, null, false, 'hi', {}].forEach(function(i) { [undefined, null, false, 'hi', {}].forEach(function(i) {
var name = String(i);
var name = String(i);
testAtomicOp(Atomics.compareExchange, si32a, i, 0, name); testAtomicOp(Atomics.compareExchange, si32a, i, 0, name);
testAtomicOp(Atomics.load, si32a, i, 0, name); testAtomicOp(Atomics.load, si32a, i, 0, name);
testAtomicOp(Atomics.store, si32a, i, 0, name); testAtomicOp(Atomics.store, si32a, i, 0, name);
...@@ -109,6 +110,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -109,6 +110,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
assertEquals(undefined, Atomics.exchange(si32a, i, 0), name); assertEquals(undefined, Atomics.exchange(si32a, i, 0), name);
}); });
// Out-of-bounds indexes for offset-array
[-1, 1, 100].forEach(function(i) {
var name = String(i);
assertEquals(undefined, Atomics.compareExchange(si32a2, i, 0, 0), name);
assertEquals(undefined, Atomics.load(si32a2, i), name);
assertEquals(undefined, Atomics.store(si32a2, i, 0), name);
assertEquals(undefined, Atomics.add(si32a2, i, 0), name);
assertEquals(undefined, Atomics.sub(si32a2, i, 0), name);
assertEquals(undefined, Atomics.and(si32a2, i, 0), name);
assertEquals(undefined, Atomics.or(si32a2, i, 0), name);
assertEquals(undefined, Atomics.xor(si32a2, i, 0), name);
assertEquals(undefined, Atomics.exchange(si32a2, i, 0), name);
});
// Monkey-patch length and make sure these functions still return undefined. // Monkey-patch length and make sure these functions still return undefined.
Object.defineProperty(si32a, 'length', {get: function() { return 1000; }}); Object.defineProperty(si32a, 'length', {get: function() { return 1000; }});
[2, 100].forEach(function(i) { [2, 100].forEach(function(i) {
...@@ -128,39 +143,53 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -128,39 +143,53 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
(function TestGoodIndex() { (function TestGoodIndex() {
var sab = new SharedArrayBuffer(64); var sab = new SharedArrayBuffer(64);
var si32a = new Int32Array(sab); var si32a = new Int32Array(sab);
var si32a2 = new Int32Array(sab, 32);
var valueOf = {valueOf: function(){ return 3;}}; var valueOf = {valueOf: function(){ return 3;}};
var toString = {toString: function(){ return '3';}}; var toString = {toString: function(){ return '3';}};
[3, 3.5, '3', '3.5', valueOf, toString].forEach(function(i) { [3, 3.5, '3', '3.5', valueOf, toString].forEach(function(i) {
var name = String(i); var name = String(i);
[si32a, si32a2].forEach(function(array) {
testAtomicOp(Atomics.compareExchange, si32a, i, 3, name); testAtomicOp(Atomics.compareExchange, array, i, 3, name);
testAtomicOp(Atomics.load, si32a, i, 3, name); testAtomicOp(Atomics.load, array, i, 3, name);
testAtomicOp(Atomics.store, si32a, i, 3, name); testAtomicOp(Atomics.store, array, i, 3, name);
testAtomicOp(Atomics.add, si32a, i, 3, name); testAtomicOp(Atomics.add, array, i, 3, name);
testAtomicOp(Atomics.sub, si32a, i, 3, name); testAtomicOp(Atomics.sub, array, i, 3, name);
testAtomicOp(Atomics.and, si32a, i, 3, name); testAtomicOp(Atomics.and, array, i, 3, name);
testAtomicOp(Atomics.or, si32a, i, 3, name); testAtomicOp(Atomics.or, array, i, 3, name);
testAtomicOp(Atomics.xor, si32a, i, 3, name); testAtomicOp(Atomics.xor, array, i, 3, name);
testAtomicOp(Atomics.exchange, si32a, i, 3, name); testAtomicOp(Atomics.exchange, array, i, 3, name);
})
}); });
})(); })();
function clearArray(sab) {
var ui8 = new Uint8Array(sab);
for (var i = 0; i < sab.byteLength; ++i) {
ui8[i] = 0;
}
}
(function TestCompareExchange() { (function TestCompareExchange() {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
// sta[i] == 0, CAS will store [sta, sta2].forEach(function(array) {
assertEquals(0, Atomics.compareExchange(sta, i, 0, 50), name); clearArray(array.buffer);
assertEquals(50, sta[i], name); var name = Object.prototype.toString.call(array);
for (var i = 0; i < array.length; ++i) {
// sta[i] == 50, CAS will not store // array[i] == 0, CAS will store
assertEquals(50, Atomics.compareExchange(sta, i, 0, 100), name); assertEquals(0, Atomics.compareExchange(array, i, 0, 50), name);
assertEquals(50, sta[i], name); assertEquals(50, array[i], name);
}
// array[i] == 50, CAS will not store
assertEquals(50, Atomics.compareExchange(array, i, 0, 100), name);
assertEquals(50, array[i], name);
}
})
}); });
})(); })();
...@@ -168,13 +197,18 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -168,13 +197,18 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 0; [sta, sta2].forEach(function(array) {
assertEquals(0, Atomics.load(sta, i), name); clearArray(array.buffer);
sta[i] = 50; var name = Object.prototype.toString.call(array);
assertEquals(50, Atomics.load(sta, i), name); for (var i = 0; i < array.length; ++i) {
} array[i] = 0;
assertEquals(0, Atomics.load(array, i), name);
array[i] = 50;
assertEquals(50, Atomics.load(array, i), name);
}
})
}); });
})(); })();
...@@ -182,14 +216,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -182,14 +216,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
assertEquals(50, Atomics.store(sta, i, 50), name); [sta, sta2].forEach(function(array) {
assertEquals(50, sta[i], name); clearArray(array.buffer);
var name = Object.prototype.toString.call(array);
assertEquals(100, Atomics.store(sta, i, 100), name); for (var i = 0; i < array.length; ++i) {
assertEquals(100, sta[i], name); assertEquals(50, Atomics.store(array, i, 50), name);
} assertEquals(50, array[i], name);
assertEquals(100, Atomics.store(array, i, 100), name);
assertEquals(100, array[i], name);
}
})
}); });
})(); })();
...@@ -197,14 +236,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -197,14 +236,19 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
assertEquals(0, Atomics.add(sta, i, 50), name); [sta, sta2].forEach(function(array) {
assertEquals(50, sta[i], name); clearArray(array.buffer);
var name = Object.prototype.toString.call(array);
assertEquals(50, Atomics.add(sta, i, 70), name); for (var i = 0; i < array.length; ++i) {
assertEquals(120, sta[i], name); assertEquals(0, Atomics.add(array, i, 50), name);
} assertEquals(50, array[i], name);
assertEquals(50, Atomics.add(array, i, 70), name);
assertEquals(120, array[i], name);
}
})
}); });
})(); })();
...@@ -212,15 +256,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -212,15 +256,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 120; [sta, sta2].forEach(function(array) {
assertEquals(120, Atomics.sub(sta, i, 50), name); clearArray(array.buffer);
assertEquals(70, sta[i], name); var name = Object.prototype.toString.call(array);
for (var i = 0; i < array.length; ++i) {
assertEquals(70, Atomics.sub(sta, i, 70), name); array[i] = 120;
assertEquals(0, sta[i], name); assertEquals(120, Atomics.sub(array, i, 50), name);
} assertEquals(70, array[i], name);
assertEquals(70, Atomics.sub(array, i, 70), name);
assertEquals(0, array[i], name);
}
})
}); });
})(); })();
...@@ -228,15 +277,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -228,15 +277,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 0x3f; [sta, sta2].forEach(function(array) {
assertEquals(0x3f, Atomics.and(sta, i, 0x30), name); clearArray(array.buffer);
assertEquals(0x30, sta[i], name); var name = Object.prototype.toString.call(sta);
for (var i = 0; i < array.length; ++i) {
assertEquals(0x30, Atomics.and(sta, i, 0x20), name); array[i] = 0x3f;
assertEquals(0x20, sta[i], name); assertEquals(0x3f, Atomics.and(array, i, 0x30), name);
} assertEquals(0x30, array[i], name);
assertEquals(0x30, Atomics.and(array, i, 0x20), name);
assertEquals(0x20, array[i], name);
}
})
}); });
})(); })();
...@@ -244,15 +298,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -244,15 +298,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 0x30; [sta, sta2].forEach(function(array) {
assertEquals(0x30, Atomics.or(sta, i, 0x1c), name); clearArray(array.buffer);
assertEquals(0x3c, sta[i], name); var name = Object.prototype.toString.call(array);
for (var i = 0; i < array.length; ++i) {
assertEquals(0x3c, Atomics.or(sta, i, 0x09), name); array[i] = 0x30;
assertEquals(0x3d, sta[i], name); assertEquals(0x30, Atomics.or(array, i, 0x1c), name);
} assertEquals(0x3c, array[i], name);
assertEquals(0x3c, Atomics.or(array, i, 0x09), name);
assertEquals(0x3d, array[i], name);
}
})
}); });
})(); })();
...@@ -260,15 +319,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -260,15 +319,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 0x30; [sta, sta2].forEach(function(array) {
assertEquals(0x30, Atomics.xor(sta, i, 0x1c), name); clearArray(array.buffer);
assertEquals(0x2c, sta[i], name); var name = Object.prototype.toString.call(array);
for (var i = 0; i < array.length; ++i) {
assertEquals(0x2c, Atomics.xor(sta, i, 0x09), name); array[i] = 0x30;
assertEquals(0x25, sta[i], name); assertEquals(0x30, Atomics.xor(array, i, 0x1c), name);
} assertEquals(0x2c, array[i], name);
assertEquals(0x2c, Atomics.xor(array, i, 0x09), name);
assertEquals(0x25, array[i], name);
}
})
}); });
})(); })();
...@@ -276,15 +340,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) { ...@@ -276,15 +340,20 @@ function testAtomicOp(op, ia, index, expectedIndex, name) {
IntegerTypedArrayConstructors.forEach(function(t) { IntegerTypedArrayConstructors.forEach(function(t) {
var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT); var sab = new SharedArrayBuffer(10 * t.constr.BYTES_PER_ELEMENT);
var sta = new t.constr(sab); var sta = new t.constr(sab);
var name = Object.prototype.toString.call(sta); var sta2 = new t.constr(sab, 5 * t.constr.BYTES_PER_ELEMENT);
for (var i = 0; i < 10; ++i) {
sta[i] = 0x30; [sta, sta2].forEach(function(array) {
assertEquals(0x30, Atomics.exchange(sta, i, 0x1c), name); clearArray(array.buffer);
assertEquals(0x1c, sta[i], name); var name = Object.prototype.toString.call(array);
for (var i = 0; i < array.length; ++i) {
assertEquals(0x1c, Atomics.exchange(sta, i, 0x09), name); array[i] = 0x30;
assertEquals(0x09, sta[i], name); assertEquals(0x30, Atomics.exchange(array, i, 0x1c), name);
} assertEquals(0x1c, array[i], name);
assertEquals(0x1c, Atomics.exchange(array, i, 0x09), name);
assertEquals(0x09, array[i], name);
}
})
}); });
})(); })();
......
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