-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es/proposal): Update explicit resource management to match spec (#…
- Loading branch information
Showing
26 changed files
with
404 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
function _using_ctx() { | ||
var _disposeSuppressedError = | ||
typeof SuppressedError === "function" | ||
? // eslint-disable-next-line no-undef | ||
SuppressedError | ||
: (function (error, suppressed) { | ||
var err = new Error(); | ||
err.name = "SuppressedError"; | ||
err.suppressed = suppressed; | ||
err.error = error; | ||
return err; | ||
}), | ||
empty = {}, | ||
stack = []; | ||
function using(isAwait, value) { | ||
if (value != null) { | ||
if (Object(value) !== value) { | ||
throw new TypeError( | ||
"using declarations can only be used with objects, functions, null, or undefined.", | ||
); | ||
} | ||
// core-js-pure uses Symbol.for for polyfilling well-known symbols | ||
if (isAwait) { | ||
var dispose = | ||
value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]; | ||
} | ||
if (dispose == null) { | ||
dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")]; | ||
} | ||
if (typeof dispose !== "function") { | ||
throw new TypeError(`Property [Symbol.dispose] is not a function.`); | ||
} | ||
stack.push({ v: value, d: dispose, a: isAwait }); | ||
} else if (isAwait) { | ||
// provide the nullish `value` as `d` for minification gain | ||
stack.push({ d: value, a: isAwait }); | ||
} | ||
return value; | ||
} | ||
return { | ||
// error | ||
e: empty, | ||
// using | ||
u: using.bind(null, false), | ||
// await using | ||
a: using.bind(null, true), | ||
// dispose | ||
d: function () { | ||
var error = this.e; | ||
|
||
function next() { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
while ((resource = stack.pop())) { | ||
try { | ||
var resource, | ||
disposalResult = resource.d && resource.d.call(resource.v); | ||
if (resource.a) { | ||
return Promise.resolve(disposalResult).then(next, err); | ||
} | ||
} catch (e) { | ||
return err(e); | ||
} | ||
} | ||
if (error !== empty) throw error; | ||
} | ||
|
||
function err(e) { | ||
error = error !== empty ? new _disposeSuppressedError(error, e) : e; | ||
|
||
return next(); | ||
} | ||
|
||
return next(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../swc_ecma_transforms_proposal/tests/explicit-resource-management/exec-async/issue-8853.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { deepStrictEqual } = require('node:assert') | ||
|
||
let i = 0 | ||
let err | ||
try { | ||
await using _x1 = { | ||
async [Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() { | ||
throw [1, ++i] | ||
} | ||
} | ||
|
||
await using _x2 = { | ||
async [Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() { | ||
throw [2, ++i] | ||
} | ||
} | ||
|
||
await using _x3 = { | ||
async [Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() { | ||
throw [3, ++i] | ||
} | ||
} | ||
|
||
await using _x4 = { | ||
async [Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() { | ||
throw [4, ++i] | ||
} | ||
} | ||
|
||
throw [5, ++i] | ||
} catch (e) { | ||
err = e | ||
} | ||
|
||
console.log(err) | ||
deepStrictEqual(err.suppressed, [1, 5]) | ||
deepStrictEqual(err.error.suppressed, [2, 4]) | ||
deepStrictEqual(err.error.error.suppressed, [3, 3]) | ||
deepStrictEqual(err.error.error.error.suppressed, [4, 2]) | ||
deepStrictEqual(err.error.error.error.error, [5, 1]) |
32 changes: 32 additions & 0 deletions
32
...ansforms_proposal/tests/explicit-resource-management/transform-await/issue-8853/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
let i = 0 | ||
let err | ||
try { | ||
await using _x1 = { | ||
async [Symbol.asyncDispose]() { | ||
throw [1, ++i] | ||
} | ||
} | ||
|
||
await using _x2 = { | ||
async [Symbol.asyncDispose]() { | ||
throw [2, ++i] | ||
} | ||
} | ||
|
||
await using _x3 = { | ||
async [Symbol.asyncDispose]() { | ||
throw [3, ++i] | ||
} | ||
} | ||
|
||
await using _x4 = { | ||
async [Symbol.asyncDispose]() { | ||
throw [4, ++i] | ||
} | ||
} | ||
|
||
throw [5, ++i] | ||
} catch (e) { | ||
err = e | ||
} |
49 changes: 49 additions & 0 deletions
49
...nsforms_proposal/tests/explicit-resource-management/transform-await/issue-8853/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
let i = 0; | ||
let err; | ||
try { | ||
try { | ||
var _usingCtx = _using_ctx(); | ||
const _x1 = _usingCtx.a({ | ||
async [Symbol.asyncDispose] () { | ||
throw [ | ||
1, | ||
++i | ||
]; | ||
} | ||
}); | ||
const _x2 = _usingCtx.a({ | ||
async [Symbol.asyncDispose] () { | ||
throw [ | ||
2, | ||
++i | ||
]; | ||
} | ||
}); | ||
const _x3 = _usingCtx.a({ | ||
async [Symbol.asyncDispose] () { | ||
throw [ | ||
3, | ||
++i | ||
]; | ||
} | ||
}); | ||
const _x4 = _usingCtx.a({ | ||
async [Symbol.asyncDispose] () { | ||
throw [ | ||
4, | ||
++i | ||
]; | ||
} | ||
}); | ||
throw [ | ||
5, | ||
++i | ||
]; | ||
} catch (_) { | ||
_usingCtx.e = _; | ||
} finally{ | ||
await _usingCtx.d(); | ||
} | ||
} catch (e) { | ||
err = e; | ||
} |
13 changes: 6 additions & 7 deletions
13
...a_transforms_proposal/tests/explicit-resource-management/transform-await/mixed/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
{ | ||
try { | ||
var _stack = []; | ||
var a = _using(_stack, 1); | ||
var b = _using(_stack, 2, true); | ||
var c = _using(_stack, 3); | ||
var _usingCtx = _using_ctx(); | ||
const a = _usingCtx.u(1); | ||
const b = _usingCtx.a(2); | ||
const c = _usingCtx.u(3); | ||
} catch (_) { | ||
var _error = _; | ||
var _hasError = true; | ||
_usingCtx.e = _; | ||
} finally{ | ||
await _dispose(_stack, _error, _hasError); | ||
await _usingCtx.d(); | ||
} | ||
} |
Oops, something went wrong.