Skip to content

Commit 73ba883

Browse files
authored
lib: use private field in AbortController
Instead of validating the receiver ourselves, let V8 handle the validation using the semantics of private fields. PR-URL: #43820 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent e54ee80 commit 73ba883

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

lib/internal/abort_controller.js

+3-16
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,21 @@ function abortSignal(signal, reason) {
292292
signal.dispatchEvent(event);
293293
}
294294

295-
// TODO(joyeecheung): use private fields and we'll get invalid access
296-
// validation from V8 instead of throwing ERR_INVALID_THIS ourselves.
297-
const kSignal = Symbol('signal');
298-
299-
function validateAbortController(obj) {
300-
if (obj?.[kSignal] === undefined)
301-
throw new ERR_INVALID_THIS('AbortController');
302-
}
303-
304295
class AbortController {
305-
constructor() {
306-
this[kSignal] = createAbortSignal();
307-
}
296+
#signal = createAbortSignal();
308297

309298
/**
310299
* @type {AbortSignal}
311300
*/
312301
get signal() {
313-
validateAbortController(this);
314-
return this[kSignal];
302+
return this.#signal;
315303
}
316304

317305
/**
318306
* @param {any} reason
319307
*/
320308
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
321-
validateAbortController(this);
322-
abortSignal(this[kSignal], reason);
309+
abortSignal(this.#signal, reason);
323310
}
324311

325312
[customInspectSymbol](depth, options) {

test/parallel/test-abortcontroller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ const { setTimeout: sleep } = require('timers/promises');
108108
for (const badController of badAbortControllers) {
109109
throws(
110110
() => acSignalGet.call(badController),
111-
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
111+
{ name: 'TypeError' }
112112
);
113113
throws(
114114
() => acAbort.call(badController),
115-
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
115+
{ name: 'TypeError' }
116116
);
117117
}
118118
}
@@ -139,7 +139,7 @@ const { setTimeout: sleep } = require('timers/promises');
139139
for (const badSignal of badAbortSignals) {
140140
throws(
141141
() => signalAbortedGet.call(badSignal),
142-
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
142+
{ name: 'TypeError' }
143143
);
144144
}
145145
}

0 commit comments

Comments
 (0)