Skip to content

merge immutable-arraybuffer tests #4445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ iterator-sequencing
# https://github.com/tc39/proposal-canonical-tz
canonical-tz

# Upsert
# https://github.com/tc39/proposal-upsert
upsert

# Immutable Array Buffer
# https://github.com/tc39/proposal-immutable-arraybuffer
immutable-arraybuffer

## Standard language features
#
# Language features that have been included in a published version of the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify immutable property of ArrayBuffer
esid: sec-get-arraybuffer.prototype.immutable
features: [ArrayBuffer, immutable-arraybuffer]
---*/

var ab = new ArrayBuffer(1);

assert.sameValue(ab.immutable, false);

ab = ab.transferToImmutable();

assert.sameValue(ab.immutable, true);

ab = new ArrayBuffer(1, { maxByteLength: 2 });

assert.sameValue(ab.immutable, false);

ab = ab.transferToImmutable();

assert.sameValue(ab.immutable, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify immutable property requires ArrayBuffer receiver
esid: sec-get-arraybuffer.prototype.immutable
features: [ArrayBuffer, immutable-arraybuffer]
---*/

assert.throws(TypeError, function() {
ArrayBuffer.prototype.immutable;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify immutable property is implemented by an accessor
esid: sec-get-arraybuffer.prototype.immutable
features: [ArrayBuffer, immutable-arraybuffer]
---*/

var getter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, 'immutable'
).get;

assert.sameValue(typeof getter, 'function');

assert.throws(TypeError, function() {
getter();
});
17 changes: 17 additions & 0 deletions test/built-ins/ArrayBuffer/prototype/immutable/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify immutable getter's length property attributes and value
esid: sec-get-arraybuffer.prototype.immutable
includes: [propertyHelper.js]
features: [ArrayBuffer, immutable-arraybuffer]
---*/

var desc = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'immutable');

verifyProperty(desc.get, 'length', {
value: 0,
enumerable: false,
writable: false,
configurable: true
});
17 changes: 17 additions & 0 deletions test/built-ins/ArrayBuffer/prototype/immutable/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify immutable getter's name property attributes and value
esid: sec-get-arraybuffer.prototype.immutable
includes: [propertyHelper.js]
features: [ArrayBuffer, immutable-arraybuffer]
---*/

var desc = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'immutable');

verifyProperty(desc.get, 'name', {
value: 'get immutable',
enumerable: false,
writable: false,
configurable: true
});
18 changes: 18 additions & 0 deletions test/built-ins/ArrayBuffer/prototype/immutable/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: immutable property has no setter
esid: sec-get-arraybuffer.prototype.immutable
includes: [propertyHelper.js]
features: [ArrayBuffer, immutable-arraybuffer]
---*/

var desc = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'immutable');

assert.sameValue(desc.set, undefined);
assert.sameValue(typeof desc.get, 'function');

verifyProperty(ArrayBuffer.prototype, 'immutable', {
enumerable: false,
configurable: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: immmutable getter is a function that throws if receiver is not an ArrayBuffer
esid: sec-get-arraybuffer.prototype.immutable
features: [DataView, Int8Array, ArrayBuffer, immutable-arraybuffer]
---*/

var getter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
).get;

assert.sameValue(typeof getter, "function");

assert.throws(TypeError, function() {
getter.call({});
});

assert.throws(TypeError, function() {
getter.call([]);
});

var ta = new Int8Array(8);
assert.throws(TypeError, function() {
getter.call(ta);
});

var dv = new DataView(new ArrayBuffer(8), 0);
assert.throws(TypeError, function() {
getter.call(dv);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: immutable getter throws if this is not an Object
esid: sec-get-arraybuffer.prototype.immutable
features: [Symbol, ArrayBuffer, immutable-arraybuffer]
---*/

var getter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
).get;

assert.sameValue(typeof getter, "function");

assert.throws(TypeError, function() {
getter.call(undefined);
}, "this is undefined");

assert.throws(TypeError, function() {
getter.call(null);
}, "this is null");

assert.throws(TypeError, function() {
getter.call(42);
}, "this is 42");

assert.throws(TypeError, function() {
getter.call("1");
}, "this is a string");

assert.throws(TypeError, function() {
getter.call(true);
}, "this is true");

assert.throws(TypeError, function() {
getter.call(false);
}, "this is false");

var s = Symbol("s");
assert.throws(TypeError, function() {
getter.call(s);
}, "this is a Symbol");
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: immutable getter throws if receiver is growable SharedArrayBuffer
esid: sec-get-arraybuffer.prototype.immutable
features: [SharedArrayBuffer, ArrayBuffer, immutable-arraybuffer]
---*/

var immutable = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
);

var getter = immutable.get;
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
var immutable = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
);
var getter = immutable.get;
var getter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
).get;

var sab = new SharedArrayBuffer(4, {maxByteLength: 20});

assert.sameValue(typeof getter, "function");

assert.throws(TypeError, function() {
getter.call(sab);
}, "`this` cannot be a SharedArrayBuffer");
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: immutable getter throws if this is a SharedArrayBuffer
esid: sec-get-arraybuffer.prototype.immutable
features: [SharedArrayBuffer, ArrayBuffer, immutable-arraybuffer]
---*/

var immutable = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
);

var getter = immutable.get;
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
var immutable = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
);
var getter = immutable.get;
var getter = Object.getOwnPropertyDescriptor(
ArrayBuffer.prototype, "immutable"
).get;

var sab = new SharedArrayBuffer(4);

assert.sameValue(typeof getter, "function");

assert.throws(TypeError, function() {
getter.call(sab);
}, "`this` cannot be a SharedArrayBuffer");
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: verify transferToImmutable property attributes
esid: sec-arraybuffer.prototype.transfertoimmutable
includes: [propertyHelper.js]
features: [immutable-arraybuffer]
---*/

verifyProperty(ArrayBuffer.prototype, 'transferToImmutable', {
enumerable: false,
writable: true,
configurable: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: transferToImmutable is extensible
esid: sec-arraybuffer.prototype.transfertoimmutable
features: [immutable-arraybuffer]
---*/

assert(Object.isExtensible(ArrayBuffer.prototype.transferToImmutable));
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: transfer ArrayBuffer to larger immutable ArrayBuffer
esid: sec-arraybuffer.prototype.transfertoimmutable
features: [resizable-arraybuffer, immutable-arraybuffer]
---*/

var source = new ArrayBuffer(4);

var sourceArray = new Uint8Array(source);
sourceArray[0] = 1;
sourceArray[1] = 2;
sourceArray[2] = 3;
sourceArray[3] = 4;

var dest = source.transferToImmutable(5);

assert.sameValue(source.byteLength, 0, 'source.byteLength');
assert.sameValue(source.detached, true, 'source.byteLength');

assert.sameValue(dest.immutable, true, 'dest.immutable');
assert.sameValue(dest.resizable, false, 'dest.resizable');
assert.sameValue(dest.byteLength, 5, 'dest.byteLength');
assert.sameValue(dest.maxByteLength, 5, 'dest.maxByteLength');

var destArray = new Uint8Array(dest);

assert.sameValue(destArray[0], 1, 'destArray[0]');
assert.sameValue(destArray[1], 2, 'destArray[1]');
assert.sameValue(destArray[2], 3, 'destArray[2]');
assert.sameValue(destArray[3], 4, 'destArray[3]');
assert.sameValue(destArray[4], 0, 'destArray[4]');
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: transfer ArrayBuffer to same size immutable ArrayBuffer
esid: sec-arraybuffer.prototype.transfertoimmutable
features: [resizable-arraybuffer, immutable-arraybuffer]
---*/

var source = new ArrayBuffer(4);

var sourceArray = new Uint8Array(source);
sourceArray[0] = 1;
sourceArray[1] = 2;
sourceArray[2] = 3;
sourceArray[3] = 4;

var dest = source.transferToImmutable();

assert.sameValue(source.byteLength, 0, 'source.byteLength');
assert.sameValue(source.detached, true, 'source.byteLength');

assert.sameValue(dest.immutable, true, 'dest.immutable');
assert.sameValue(dest.resizable, false, 'dest.resizable');
assert.sameValue(dest.byteLength, 4, 'dest.byteLength');
assert.sameValue(dest.maxByteLength, 4, 'dest.maxByteLength');

var destArray = new Uint8Array(dest);

assert.sameValue(destArray[0], 1, 'destArray[0]');
assert.sameValue(destArray[1], 2, 'destArray[1]');
assert.sameValue(destArray[2], 3, 'destArray[2]');
assert.sameValue(destArray[3], 4, 'destArray[3]');
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: transfer ArrayBuffer to smaller immutable ArrayBuffer
esid: sec-arraybuffer.prototype.transfertoimmutable
features: [resizable-arraybuffer, immutable-arraybuffer]
---*/

var source = new ArrayBuffer(4);

var sourceArray = new Uint8Array(source);
sourceArray[0] = 1;
sourceArray[1] = 2;
sourceArray[2] = 3;
sourceArray[3] = 4;

var dest = source.transferToImmutable(3);

assert.sameValue(source.byteLength, 0, 'source.byteLength');
assert.sameValue(source.detached, true, 'source.byteLength');

assert.sameValue(dest.immutable, true, 'dest.immutable');
assert.sameValue(dest.resizable, false, 'dest.resizable');
assert.sameValue(dest.byteLength, 3, 'dest.byteLength');
assert.sameValue(dest.maxByteLength, 3, 'dest.maxByteLength');

var destArray = new Uint8Array(dest);

assert.sameValue(destArray[0], 1, 'destArray[0]');
assert.sameValue(destArray[1], 2, 'destArray[1]');
assert.sameValue(destArray[2], 3, 'destArray[2]');
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2025 Moddable Tech, Inc. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: transfer ArrayBuffer to zero length immutable ArrayBuffer
esid: sec-arraybuffer.prototype.transfertoimmutable
features: [resizable-arraybuffer, immutable-arraybuffer]
---*/

var source = new ArrayBuffer(4);

var sourceArray = new Uint8Array(source);
sourceArray[0] = 1;
sourceArray[1] = 2;
sourceArray[2] = 3;
sourceArray[3] = 4;

var dest = source.transferToImmutable(0);

assert.sameValue(source.byteLength, 0, 'source.byteLength');
assert.sameValue(source.detached, true, 'source.byteLength');

assert.sameValue(dest.immutable, true, 'dest.immutable');
assert.sameValue(dest.resizable, false, 'dest.resizable');
assert.sameValue(dest.byteLength, 0, 'dest.byteLength');
assert.sameValue(dest.maxByteLength, 0, 'dest.maxByteLength');
Loading