v8/test/mjsunit/harmony/bigint/dataview.js
Jakob Kummerow 3ef16185e4 Reland "[bigint] Implement DataView.{get,set}Big*64"
Originally reviewed at https://chromium-review.googlesource.com/929429
and landed as r51486 / d50c7731e8.

Update in reland: whitelisted new builtins as side effect free.

Bug: v8:6791
Change-Id: Iff45700c8a4eca23f3ee6fc9c0cb340dc027cbc6
Reviewed-on: https://chromium-review.googlesource.com/932802
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51491}
2018-02-23 00:57:38 +00:00

79 lines
2.5 KiB
JavaScript

// Copyright 2018 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-bigint
var buffer = new ArrayBuffer(64);
var dataview = new DataView(buffer, 8, 24);
var bytes = new Uint8Array(buffer);
var b1 = 0xff1234567890abcdefn;
var b1_64 = BigInt.asUintN(64, b1);
dataview.setBigInt64(8, b1);
assertEquals(0x12, bytes[16]);
assertEquals(0x34, bytes[17]);
assertEquals(0x56, bytes[18]);
assertEquals(0x78, bytes[19]);
assertEquals(0x90, bytes[20]);
assertEquals(0xab, bytes[21]);
assertEquals(0xcd, bytes[22]);
assertEquals(0xef, bytes[23]);
assertEquals(b1_64, dataview.getBigInt64(8));
dataview.setBigInt64(8, b1, true); // Little-endian.
assertEquals(0xef, bytes[16]);
assertEquals(0xcd, bytes[17]);
assertEquals(0xab, bytes[18]);
assertEquals(0x90, bytes[19]);
assertEquals(0x78, bytes[20]);
assertEquals(0x56, bytes[21]);
assertEquals(0x34, bytes[22]);
assertEquals(0x12, bytes[23]);
assertEquals(b1_64, dataview.getBigInt64(8, true));
dataview.setBigUint64(8, b1);
assertEquals(0x12, bytes[16]);
assertEquals(0x34, bytes[17]);
assertEquals(0x56, bytes[18]);
assertEquals(0x78, bytes[19]);
assertEquals(0x90, bytes[20]);
assertEquals(0xab, bytes[21]);
assertEquals(0xcd, bytes[22]);
assertEquals(0xef, bytes[23]);
assertEquals(b1_64, dataview.getBigUint64(8));
dataview.setBigUint64(8, b1, true); // Little-endian.
assertEquals(0xef, bytes[16]);
assertEquals(0xcd, bytes[17]);
assertEquals(0xab, bytes[18]);
assertEquals(0x90, bytes[19]);
assertEquals(0x78, bytes[20]);
assertEquals(0x56, bytes[21]);
assertEquals(0x34, bytes[22]);
assertEquals(0x12, bytes[23]);
assertEquals(b1_64, dataview.getBigUint64(8, true));
var b2 = -0x76543210fedcba98n;
dataview.setBigInt64(8, b2, true);
assertEquals(0x68, bytes[16]);
assertEquals(0x45, bytes[17]);
assertEquals(0x23, bytes[18]);
assertEquals(0x01, bytes[19]);
assertEquals(0xef, bytes[20]);
assertEquals(0xcd, bytes[21]);
assertEquals(0xab, bytes[22]);
assertEquals(0x89, bytes[23]);
assertEquals(b2, dataview.getBigInt64(8, true));
assertEquals(0x89abcdef01234568n, dataview.getBigUint64(8, true));
assertThrows(() => dataview.setBigInt64(0, 1), TypeError);
assertThrows(() => dataview.setBigUint64(0, 1), TypeError);
assertThrows(() => dataview.setInt32(0, 1n), TypeError);
assertThrows(() => dataview.setUint32(0, 1n), TypeError);
// None of the stores wrote out of bounds.
for (var i = 0; i < 16; i++) assertEquals(0, bytes[i]);
for (var i = 24; i < 64; i++) assertEquals(0, bytes[i]);