Skip to content

Commit

Permalink
[Refactor] update AOs to align with latest spec PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 22, 2024
1 parent 07297bb commit 3b1c01c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
25 changes: 10 additions & 15 deletions aos/AddDisposableResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');

var Type = require('es-abstract/2024/Type');

var isDisposeCapabilityRecord = require('./records/dispose-capability-record');

var CreateDisposableResource = require('./CreateDisposableResource');
Expand All @@ -26,25 +24,22 @@ module.exports = function AddDisposableResource(disposeCapability, V, hint) {
}

if (!disposeCapability['[[DisposableResourceStack]]']) {
throw new $TypeError('Assertion failed: `disposeCapability.[[DisposableResourceStack]]` must not be ~empty~');
throw new $TypeError('Assertion failed: `disposeCapability.[[DisposableResourceStack]]` must not be ~EMPTY~');
}

var resource;
if (arguments.length < 4) { // step 2
if (V == null) {
return 'unused'; // step 2.a
}
if (Type(V) !== 'Object') {
throw new $TypeError('`V` must be an Object'); // step 2.b
if (arguments.length < 4) { // step 1
if (V == null && hint === 'SYNC_DISPOSE') {
return 'UNUSED'; // step 1.a
}
resource = CreateDisposableResource(V, hint); // step 2.c
} else { // step 3
resource = CreateDisposableResource(V, hint); // step 1.c
} else { // step 2
if (typeof V !== 'undefined') {
throw new $TypeError('Assertion failed: `V` must be undefined when `method` is present'); // step 3.a
throw new $TypeError('Assertion failed: `V` must be undefined when `method` is present'); // step 2.a
}
resource = CreateDisposableResource(void undefined, hint, method); // step 3.b
resource = CreateDisposableResource(void undefined, hint, method); // step 2.b
}
$push(disposeCapability['[[DisposableResourceStack]]'], resource); // step 4
$push(disposeCapability['[[DisposableResourceStack]]'], resource); // step 3

return 'unused'; // step 5
return 'UNUSED'; // step 4
};
24 changes: 14 additions & 10 deletions aos/CreateDisposableResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ module.exports = function CreateDisposableResource(V, hint) {
throw new $SyntaxError('Assertion failed: `hint` must be `~SYNC-DISPOSE~` or `~ASYNC-DISPOSE~`');
}

if (typeof V !== 'undefined' && Type(V) !== 'Object') {
throw new $TypeError('`V` must be an Object'); // step 1.a
}

var method;
if (arguments.length < 3) { // step 1
if (typeof V === 'undefined') {
throw new $TypeError('`V` must not be `undefined` when `method` is not provided'); // step 1.a
}
method = GetDisposeMethod(V, hint); // step 1.b
if (typeof method === 'undefined') {
throw new $TypeError('dispose method must not be `undefined` on `V` when an object `V` is provided'); // step 1.a
if (V == null) { // step 1.a
// eslint-disable-next-line no-param-reassign
V = void undefined; // step 1.a.i
method = void undefined; // step 1.a.ii
} else {
if (typeof V !== 'undefined' && Type(V) !== 'Object') {
throw new $TypeError('`V` must be an Object, or `null` or `undefined`'); // step 1.b.i
}

method = GetDisposeMethod(V, hint); // step 1.b.ii

if (typeof method === 'undefined') {
throw new $TypeError('dispose method must not be `undefined` on `V` when an object `V` is provided'); // step 1.b.i
}
}
} else { // step 2
method = arguments[2];
Expand Down
5 changes: 3 additions & 2 deletions aos/Dispose.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ module.exports = function Dispose(V, hint, method) {
throw new $SyntaxError('Assertion failed: `method` must be `undefined` or a function');
}

var result = Call(method, V);
var result = typeof method === 'undefined' ? method : Call(method, V); // step 1, 2

if (hint === 'ASYNC-DISPOSE') {
return PromiseResolve($Promise, result);
return PromiseResolve($Promise, result); // step 3.a, 4
}
};
2 changes: 1 addition & 1 deletion aos/DisposeResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function DisposeResources(disposeCapability, completion) {
var stack = disposeCapability['[[DisposableResourceStack]]'];

if (!stack) {
throw new $TypeError('Assertion failed: `disposeCapability.[[DisposableResourceStack]]` must not be ~empty~'); // step 1
throw new $TypeError('Assertion failed: `disposeCapability.[[DisposableResourceStack]]` must not be ~EMPTY~'); // step 1
}

// for DisposableStack or AsyncDisposableStack, all are sync, or all are async.
Expand Down
9 changes: 9 additions & 0 deletions aos/GetDisposeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');

var Call = require('es-abstract/2024/Call');
var GetMethod = require('es-abstract/2024/GetMethod');
var Type = require('es-abstract/2024/Type');

Expand All @@ -27,6 +28,14 @@ module.exports = function GetDisposeMethod(V, hint) {
throw new $SyntaxError('`Symbol.dispose` is not supported');
}
method = GetMethod(V, symbolDispose); // step 1.b.i, 2.a

if (typeof method !== 'undefined') { // step 1.b.ii
return function () { // step 1.b.ii.1, 1.b.ii.3
// eslint-disable-next-line no-invalid-this
var O = this; // step 1.b.ii.1.a
Call(method, O); // step // step 1.b.ii.1.b
};
}
}

return method; // step 3
Expand Down
2 changes: 1 addition & 1 deletion aos/records/disposable-resource-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ module.exports = function isDisposeCapabilityRecord(x) {
|| ($Object(x['[[ResourceValue]]']) === x['[[ResourceValue]]'])
)
&& (x['[[Hint]]'] === 'SYNC-DISPOSE' || x['[[Hint]]'] === 'ASYNC-DISPOSE')
&& typeof x['[[DisposeMethod]]'] === 'function';
&& (typeof x['[[DisposeMethod]]'] === 'function' || typeof x['[[DisposeMethod]]'] === 'undefined');
};
2 changes: 1 addition & 1 deletion aos/records/dispose-capability-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function isDisposeCapabilityRecord(x) {
&& hasOwn(x, '[[DisposableResourceStack]]')
&& isArray(x['[[DisposableResourceStack]]'])
&& (
x['[[DisposableResourceStack]]'] === 0
x['[[DisposableResourceStack]]'].length === 0
|| every(x['[[DisposableResourceStack]]'], isDisposableResourceRecord)
);
};

0 comments on commit 3b1c01c

Please sign in to comment.