Skip to content
Merged
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
23 changes: 23 additions & 0 deletions test/built-ins/DisposableStack/prototype/Symbol.toStringTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype-@@toStringTag
description: >
`Symbol.toStringTag` property descriptor
info: |
The initial value of the @@toStringTag property is the String value
'DisposableStack'.

This property has the attributes { [[Writable]]: false, [[Enumerable]]:
false, [[Configurable]]: true }.
includes: [propertyHelper.js]
features: [explicit-resource-management, Symbol, Symbol.toStringTag]
---*/

verifyProperty(DisposableStack.prototype, Symbol.toStringTag, {
value: 'DisposableStack',
writable: false,
enumerable: false,
configurable: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.adopt
description: Adds a disposable resource to the stack
info: |
DisposableStack.prototype.adopt ( value, onDispose )

1. Let disposableStack be the this value.
2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
4. If IsCallable(onDispose) is false, throw a TypeError exception.
5. Let closure be a new Abstract Closure with no parameters that captures value and onDispose and performs the following steps when called:
a. Perform ? Call(onDispose, undefined, « value »).
6. Let F be CreateBuiltinFunction(closure, 0, "", « »).
7. Perform ? AddDisposableResource(disposableStack.[[DisposeCapability]], undefined, sync-dispose, F).
...

AddDisposableResource ( disposeCapability, V, hint [, method ] )

1. If method is not present then,
...
2. Else,
a. Assert: V is undefined.
b. Let resource be ? CreateDisposableResource(undefined, hint, method).
3. Append resource to disposeCapability.[[DisposableResourceStack]].
4. Return unused.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
var resource = { disposed: false };
stack.adopt(resource, r => { r.disposed = true });
stack.dispose();
assert.sameValue(resource.disposed, true, 'Expected resource to have been disposed');
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.adopt
description: Allows any 'value'
info: |
DisposableStack.prototype.adopt ( value, onDispose )

1. Let disposableStack be the this value.
2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
4. If IsCallable(onDispose) is false, throw a TypeError exception.
5. Let closure be a new Abstract Closure with no parameters that captures value and onDispose and performs the following steps when called:
a. Perform ? Call(onDispose, undefined, « value »).
6. Let F be CreateBuiltinFunction(closure, 0, "", « »).
7. Perform ? AddDisposableResource(disposableStack.[[DisposeCapability]], undefined, sync-dispose, F).
...

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
stack.adopt(null, _ => {});
stack.adopt(undefined, _ => {});
stack.adopt({}, _ => {});
stack.adopt({ [Symbol.dispose]() {} }, _ => {});
stack.adopt(() => {}, _ => {});
stack.adopt(true, _ => {});
stack.adopt(false, _ => {});
stack.adopt(1, _ => {});
stack.adopt('object', _ => {});
stack.adopt(Symbol(), _ => {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.adopt
description: Adds a disposable resource to the stack
info: |
DisposableStack.prototype.adopt ( value, onDispose )

1. Let disposableStack be the this value.
2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
4. If IsCallable(onDispose) is false, throw a TypeError exception.
5. Let closure be a new Abstract Closure with no parameters that captures value and onDispose and performs the following steps when called:
a. Perform ? Call(onDispose, undefined, « value »).
6. Let F be CreateBuiltinFunction(closure, 0, "", « »).
7. Perform ? AddDisposableResource(disposableStack.[[DisposeCapability]], undefined, sync-dispose, F).
...

AddDisposableResource ( disposeCapability, V, hint [, method ] )

1. If method is not present then,
...
2. Else,
a. Assert: V is undefined.
b. Let resource be ? CreateDisposableResource(undefined, hint, method).
3. Append resource to disposeCapability.[[DisposableResourceStack]].
4. Return unused.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
var disposed = [];
var resource1 = {};
function dispose1(res) { disposed.push([res, dispose1]); }
var resource2 = {};
function dispose2(res) { disposed.push([res, dispose2]); }
stack.adopt(resource1, dispose1);
stack.adopt(resource2, dispose2);
stack.dispose();
assert.sameValue(2, disposed.length);
assert.sameValue(disposed[0][0], resource2, 'Expected resource2 to be the first disposed resource');
assert.sameValue(disposed[0][1], dispose2, 'Expected dispose2 to be the first onDispose invoked');
assert.sameValue(disposed[1][0], resource1, 'Expected resource1 to be the second disposed resource');
assert.sameValue(disposed[1][1], dispose1, 'Expected dispose1 to be the second onDispose invoked');
20 changes: 20 additions & 0 deletions test/built-ins/DisposableStack/prototype/adopt/returns-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.adopt
description: Returns the argument provided.
info: |
DisposableStack.prototype.adopt ( value, onDispose )

...
8. Return value.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
var resource = {};
assert.sameValue(stack.adopt(resource, _ => {}), resource);
assert.sameValue(stack.adopt(null, _ => {}), null);
assert.sameValue(stack.adopt(undefined, _ => {}), undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.adopt
description: Throws if onDispose argument not callable
info: |
DisposableStack.prototype.adopt ( value, onDispose )

...
4. If IsCallable(onDispose) is false, throw a TypeError exception.
...

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
assert.throws(TypeError, function() {
stack.adopt(null, null);
}, 'null');

assert.throws(TypeError, function() {
stack.adopt(null, undefined);
}, 'undefined');

assert.throws(TypeError, function() {
stack.adopt(null, true);
}, 'true');

assert.throws(TypeError, function() {
stack.adopt(null, false);
}, 'false');

assert.throws(TypeError, function() {
stack.adopt(null, 1);
}, 'number');

assert.throws(TypeError, function() {
stack.adopt(null, 'object');
}, 'string');

assert.throws(TypeError, function() {
stack.adopt(null, {});
}, 'object');

var s = Symbol();
assert.throws(TypeError, function() {
stack.adopt(null, s);
}, 'symbol');
31 changes: 31 additions & 0 deletions test/built-ins/DisposableStack/prototype/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-properties-of-the-disposablestack-prototype-object
description: DisposableStack.prototype.constructor
info: |
DisposableStack.prototype.constructor

Normative Optional

The initial value of DisposableStack.prototype.constructor is the intrinsic object %DisposableStack%.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

This section is to be treated identically to the "Annex B" of ECMA-262, but to be written in-line with the main specification.
includes: [propertyHelper.js]
features: [explicit-resource-management]
---*/

var actual = Object.prototype.hasOwnProperty.call(DisposableStack, 'constructor');

// If implemented, it should conform to the spec text
if (actual) {
verifyProperty(DisposableStack.prototype, 'constructor', {
value: DisposableStack,
writable: true,
enumerable: false,
configurable: true
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.defer
description: Adds an onDispose callback to the stack
info: |
DisposableStack.prototype.defer ( onDispose )

1. Let disposableStack be the this value.
2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
4. If IsCallable(onDispose) is false, throw a TypeError exception.
5. Perform ? AddDisposableResource(disposableStack.[[DisposeCapability]], undefined, sync-dispose, onDispose).
...

AddDisposableResource ( disposeCapability, V, hint [, method ] )

1. If method is not present then,
...
2. Else,
a. Assert: V is undefined.
b. Let resource be ? CreateDisposableResource(undefined, hint, method).
3. Append resource to disposeCapability.[[DisposableResourceStack]].
4. Return unused.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
var disposed = false;
stack.defer(() => { disposed = true });
stack.dispose();
assert.sameValue(disposed, true, 'Expected callback to have been called');
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.defer
description: Adds a disposable resource to the stack
info: |
DisposableStack.prototype.defer ( onDispose )

1. Let disposableStack be the this value.
2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
4. If IsCallable(onDispose) is false, throw a TypeError exception.
5. Perform ? AddDisposableResource(disposableStack.[[DisposeCapability]], undefined, sync-dispose, onDispose).
...

AddDisposableResource ( disposeCapability, V, hint [, method ] )

1. If method is not present then,
...
2. Else,
a. Assert: V is undefined.
b. Let resource be ? CreateDisposableResource(undefined, hint, method).
3. Append resource to disposeCapability.[[DisposableResourceStack]].
4. Return unused.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
var disposed = [];
function dispose1() { disposed.push(dispose1); }
function dispose2() { disposed.push(dispose2); }
stack.defer(dispose1);
stack.defer(dispose2);
stack.dispose();
assert.sameValue(2, disposed.length);
assert.sameValue(disposed[0], dispose2, 'Expected dispose2 to be the first onDispose invoked');
assert.sameValue(disposed[1], dispose1, 'Expected dispose1 to be the second onDispose invoked');
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.defer
description: Returns the argument provided.
info: |
DisposableStack.prototype.defer ( onDispose )

...
6. Return undefined.

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
assert.sameValue(stack.defer(_ => {}), undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2023 Ron Buckton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-disposablestack.prototype.defer
description: Adds a callback to the stack
info: |
DisposableStack.prototype.defer ( onDispose )

...
4. If IsCallable(onDispose) is false, throw a TypeError exception.
...

features: [explicit-resource-management]
---*/

var stack = new DisposableStack();
assert.throws(TypeError, function() {
stack.defer(null);
}, 'null');

assert.throws(TypeError, function() {
stack.defer(undefined);
}, 'undefined');

assert.throws(TypeError, function() {
stack.defer(true);
}, 'true');

assert.throws(TypeError, function() {
stack.defer(false);
}, 'false');

assert.throws(TypeError, function() {
stack.defer(1);
}, 'number');

assert.throws(TypeError, function() {
stack.defer('object');
}, 'string');

assert.throws(TypeError, function() {
stack.defer({});
}, 'object');

var s = Symbol();
assert.throws(TypeError, function() {
stack.defer(s);
}, 'symbol');
Loading