1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

refactor(ext/webidl): remove option bags from "makeException" (#18679)

Creating these options bags is more costly than passing arguments
one-by-one. Especially since `prefix` and `context` are passed to all functions.
This commit is contained in:
Bartek Iwańczuk 2023-04-20 00:58:41 +02:00 committed by GitHub
parent c2e9c8cce5
commit 02da57e275
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 135 additions and 109 deletions

View file

@ -431,7 +431,8 @@ webidl.converters["HeadersInit"] = (V, opts) => {
throw webidl.makeException( throw webidl.makeException(
TypeError, TypeError,
"The provided value is not of type '(sequence<sequence<ByteString>> or record<ByteString, ByteString>)'", "The provided value is not of type '(sequence<sequence<ByteString>> or record<ByteString, ByteString>)'",
opts, opts.prefix,
opts.context,
); );
}; };
webidl.converters["Headers"] = webidl.createInterfaceConverter( webidl.converters["Headers"] = webidl.createInterfaceConverter(

View file

@ -341,7 +341,8 @@ class Request {
throw webidl.makeException( throw webidl.makeException(
TypeError, TypeError,
"`client` must be a Deno.HttpClient", "`client` must be a Deno.HttpClient",
{ prefix, context: "Argument 2" }, prefix,
"Argument 2",
); );
} }
request.clientRid = init.client?.rid ?? null; request.clientRid = init.client?.rid ?? null;

View file

@ -508,7 +508,7 @@ function extractSizeAlgorithm(strategy) {
[chunk], [chunk],
undefined, undefined,
webidl.converters["unrestricted double"], webidl.converters["unrestricted double"],
{ prefix: "Failed to call `sizeAlgorithm`" }, "Failed to call `sizeAlgorithm`",
); );
} }
@ -3315,10 +3315,7 @@ function setUpReadableByteStreamControllerFromUnderlyingSource(
[controller], [controller],
underlyingSource, underlyingSource,
webidl.converters.any, webidl.converters.any,
{ "Failed to call 'startAlgorithm' on 'ReadableByteStreamController'",
prefix:
"Failed to call 'startAlgorithm' on 'ReadableByteStreamController'",
},
); );
} }
if (underlyingSourceDict.pull !== undefined) { if (underlyingSourceDict.pull !== undefined) {
@ -3328,11 +3325,8 @@ function setUpReadableByteStreamControllerFromUnderlyingSource(
[controller], [controller],
underlyingSource, underlyingSource,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'pullAlgorithm' on 'ReadableByteStreamController'",
prefix: true,
"Failed to call 'pullAlgorithm' on 'ReadableByteStreamController'",
returnsPromise: true,
},
); );
} }
if (underlyingSourceDict.cancel !== undefined) { if (underlyingSourceDict.cancel !== undefined) {
@ -3342,11 +3336,8 @@ function setUpReadableByteStreamControllerFromUnderlyingSource(
[reason], [reason],
underlyingSource, underlyingSource,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'cancelAlgorithm' on 'ReadableByteStreamController'",
prefix: true,
"Failed to call 'cancelAlgorithm' on 'ReadableByteStreamController'",
returnsPromise: true,
},
); );
} }
const autoAllocateChunkSize = underlyingSourceDict["autoAllocateChunkSize"]; const autoAllocateChunkSize = underlyingSourceDict["autoAllocateChunkSize"];
@ -3437,10 +3428,7 @@ function setUpReadableStreamDefaultControllerFromUnderlyingSource(
[controller], [controller],
underlyingSource, underlyingSource,
webidl.converters.any, webidl.converters.any,
{ "Failed to call 'startAlgorithm' on 'ReadableStreamDefaultController'",
prefix:
"Failed to call 'startAlgorithm' on 'ReadableStreamDefaultController'",
},
); );
} }
if (underlyingSourceDict.pull !== undefined) { if (underlyingSourceDict.pull !== undefined) {
@ -3450,11 +3438,8 @@ function setUpReadableStreamDefaultControllerFromUnderlyingSource(
[controller], [controller],
underlyingSource, underlyingSource,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'pullAlgorithm' on 'ReadableStreamDefaultController'",
prefix: true,
"Failed to call 'pullAlgorithm' on 'ReadableStreamDefaultController'",
returnsPromise: true,
},
); );
} }
if (underlyingSourceDict.cancel !== undefined) { if (underlyingSourceDict.cancel !== undefined) {
@ -3464,11 +3449,8 @@ function setUpReadableStreamDefaultControllerFromUnderlyingSource(
[reason], [reason],
underlyingSource, underlyingSource,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'cancelAlgorithm' on 'ReadableStreamDefaultController'",
prefix: true,
"Failed to call 'cancelAlgorithm' on 'ReadableStreamDefaultController'",
returnsPromise: true,
},
); );
} }
setUpReadableStreamDefaultController( setUpReadableStreamDefaultController(
@ -3569,11 +3551,8 @@ function setUpTransformStreamDefaultControllerFromTransformer(
[chunk, controller], [chunk, controller],
transformer, transformer,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'transformAlgorithm' on 'TransformStreamDefaultController'",
prefix: true,
"Failed to call 'transformAlgorithm' on 'TransformStreamDefaultController'",
returnsPromise: true,
},
); );
} }
if (transformerDict.flush !== undefined) { if (transformerDict.flush !== undefined) {
@ -3583,11 +3562,8 @@ function setUpTransformStreamDefaultControllerFromTransformer(
[controller], [controller],
transformer, transformer,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'flushAlgorithm' on 'TransformStreamDefaultController'",
prefix: true,
"Failed to call 'flushAlgorithm' on 'TransformStreamDefaultController'",
returnsPromise: true,
},
); );
} }
setUpTransformStreamDefaultController( setUpTransformStreamDefaultController(
@ -3679,10 +3655,7 @@ function setUpWritableStreamDefaultControllerFromUnderlyingSink(
[controller], [controller],
underlyingSink, underlyingSink,
webidl.converters.any, webidl.converters.any,
{ "Failed to call 'startAlgorithm' on 'WritableStreamDefaultController'",
prefix:
"Failed to call 'startAlgorithm' on 'WritableStreamDefaultController'",
},
); );
} }
if (underlyingSinkDict.write !== undefined) { if (underlyingSinkDict.write !== undefined) {
@ -3692,11 +3665,8 @@ function setUpWritableStreamDefaultControllerFromUnderlyingSink(
[chunk, controller], [chunk, controller],
underlyingSink, underlyingSink,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'writeAlgorithm' on 'WritableStreamDefaultController'",
prefix: true,
"Failed to call 'writeAlgorithm' on 'WritableStreamDefaultController'",
returnsPromise: true,
},
); );
} }
if (underlyingSinkDict.close !== undefined) { if (underlyingSinkDict.close !== undefined) {
@ -3706,11 +3676,8 @@ function setUpWritableStreamDefaultControllerFromUnderlyingSink(
[], [],
underlyingSink, underlyingSink,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'closeAlgorithm' on 'WritableStreamDefaultController'",
prefix: true,
"Failed to call 'closeAlgorithm' on 'WritableStreamDefaultController'",
returnsPromise: true,
},
); );
} }
if (underlyingSinkDict.abort !== undefined) { if (underlyingSinkDict.abort !== undefined) {
@ -3720,11 +3687,8 @@ function setUpWritableStreamDefaultControllerFromUnderlyingSink(
[reason], [reason],
underlyingSink, underlyingSink,
webidl.converters["Promise<undefined>"], webidl.converters["Promise<undefined>"],
{ "Failed to call 'abortAlgorithm' on 'WritableStreamDefaultController'",
prefix: true,
"Failed to call 'abortAlgorithm' on 'WritableStreamDefaultController'",
returnsPromise: true,
},
); );
} }
setUpWritableStreamDefaultController( setUpWritableStreamDefaultController(
@ -5467,16 +5431,19 @@ class ReadableByteStreamController {
); );
} }
if (byteLength === 0) { if (byteLength === 0) {
throw webidl.makeException(TypeError, "length must be non-zero", { throw webidl.makeException(
TypeError,
"length must be non-zero",
prefix, prefix,
context: arg1, arg1,
}); );
} }
if (getArrayBufferByteLength(buffer) === 0) { if (getArrayBufferByteLength(buffer) === 0) {
throw webidl.makeException( throw webidl.makeException(
TypeError, TypeError,
"buffer length must be non-zero", "buffer length must be non-zero",
{ prefix, context: arg1 }, prefix,
arg1,
); );
} }
if (this[_closeRequested] === true) { if (this[_closeRequested] === true) {
@ -5790,10 +5757,7 @@ class TransformStream {
[this[_controller]], [this[_controller]],
transformer, transformer,
webidl.converters.any, webidl.converters.any,
{ "Failed to call 'start' on 'TransformStreamDefaultController'",
prefix:
"Failed to call 'start' on 'TransformStreamDefaultController'",
},
), ),
); );
} else { } else {

View file

@ -86,11 +86,9 @@ const {
Uint8ClampedArray, Uint8ClampedArray,
} = primordials; } = primordials;
function makeException(ErrorType, message, opts = {}) { function makeException(ErrorType, message, prefix, context) {
return new ErrorType( return new ErrorType(
`${opts.prefix ? opts.prefix + ": " : ""}${ `${prefix ? prefix + ": " : ""}${context ? context : "Value"} ${message}`,
opts.context ? opts.context : "Value"
} ${message}`,
); );
} }
@ -199,7 +197,12 @@ function createIntegerConversion(bitLength, typeOpts) {
if (opts.enforceRange) { if (opts.enforceRange) {
if (!NumberIsFinite(x)) { if (!NumberIsFinite(x)) {
throw makeException(TypeError, "is not a finite number", opts); throw makeException(
TypeError,
"is not a finite number",
opts.prefix,
opts.context,
);
} }
x = integerPart(x); x = integerPart(x);
@ -208,7 +211,8 @@ function createIntegerConversion(bitLength, typeOpts) {
throw makeException( throw makeException(
TypeError, TypeError,
`is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`, `is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
opts, opts.prefix,
opts.context,
); );
} }
@ -252,7 +256,12 @@ function createLongLongConversion(bitLength, { unsigned }) {
if (opts.enforceRange) { if (opts.enforceRange) {
if (!NumberIsFinite(x)) { if (!NumberIsFinite(x)) {
throw makeException(TypeError, "is not a finite number", opts); throw makeException(
TypeError,
"is not a finite number",
opts.prefix,
opts.context,
);
} }
x = integerPart(x); x = integerPart(x);
@ -261,7 +270,8 @@ function createLongLongConversion(bitLength, { unsigned }) {
throw makeException( throw makeException(
TypeError, TypeError,
`is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`, `is outside the accepted range of ${lowerBound} to ${upperBound}, inclusive`,
opts, opts.prefix,
opts.context,
); );
} }
@ -317,7 +327,8 @@ converters.float = (V, opts) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not a finite floating-point value", "is not a finite floating-point value",
opts, opts.prefix,
opts.context,
); );
} }
@ -331,7 +342,8 @@ converters.float = (V, opts) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is outside the range of a single-precision floating-point value", "is outside the range of a single-precision floating-point value",
opts, opts.prefix,
opts.context,
); );
} }
@ -359,7 +371,8 @@ converters.double = (V, opts) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not a finite floating-point value", "is not a finite floating-point value",
opts, opts.prefix,
opts.context,
); );
} }
@ -381,7 +394,8 @@ converters.DOMString = function (V, opts = {}) {
throw makeException( throw makeException(
TypeError, TypeError,
"is a symbol, which cannot be converted to a string", "is a symbol, which cannot be converted to a string",
opts, opts.prefix,
opts.context,
); );
} }
@ -393,7 +407,12 @@ const IS_BYTE_STRING = new SafeRegExp(/^[\x00-\xFF]*$/);
converters.ByteString = (V, opts) => { converters.ByteString = (V, opts) => {
const x = converters.DOMString(V, opts); const x = converters.DOMString(V, opts);
if (!RegExpPrototypeTest(IS_BYTE_STRING, x)) { if (!RegExpPrototypeTest(IS_BYTE_STRING, x)) {
throw makeException(TypeError, "is not a valid ByteString", opts); throw makeException(
TypeError,
"is not a valid ByteString",
opts.prefix,
opts.context,
);
} }
return x; return x;
}; };
@ -427,7 +446,12 @@ converters.USVString = (V, opts) => {
converters.object = (V, opts) => { converters.object = (V, opts) => {
if (type(V) !== "Object") { if (type(V) !== "Object") {
throw makeException(TypeError, "is not an object", opts); throw makeException(
TypeError,
"is not an object",
opts.prefix,
opts.context,
);
} }
return V; return V;
@ -439,7 +463,12 @@ converters.object = (V, opts) => {
// handling for that is omitted. // handling for that is omitted.
function convertCallbackFunction(V, opts) { function convertCallbackFunction(V, opts) {
if (typeof V !== "function") { if (typeof V !== "function") {
throw makeException(TypeError, "is not a function", opts); throw makeException(
TypeError,
"is not a function",
opts.prefix,
opts.context,
);
} }
return V; return V;
} }
@ -464,10 +493,16 @@ converters.ArrayBuffer = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not an ArrayBuffer or SharedArrayBuffer", "is not an ArrayBuffer or SharedArrayBuffer",
opts, opts.prefix,
opts.context,
); );
} }
throw makeException(TypeError, "is not an ArrayBuffer", opts); throw makeException(
TypeError,
"is not an ArrayBuffer",
opts.prefix,
opts.context,
);
} }
return V; return V;
@ -475,14 +510,20 @@ converters.ArrayBuffer = (V, opts = {}) => {
converters.DataView = (V, opts = {}) => { converters.DataView = (V, opts = {}) => {
if (!isDataView(V)) { if (!isDataView(V)) {
throw makeException(TypeError, "is not a DataView", opts); throw makeException(
TypeError,
"is not a DataView",
opts.prefix,
opts.context,
);
} }
if (!opts.allowShared && isSharedArrayBuffer(DataViewPrototypeGetBuffer(V))) { if (!opts.allowShared && isSharedArrayBuffer(DataViewPrototypeGetBuffer(V))) {
throw makeException( throw makeException(
TypeError, TypeError,
"is backed by a SharedArrayBuffer, which is not allowed", "is backed by a SharedArrayBuffer, which is not allowed",
opts, opts.prefix,
opts.context,
); );
} }
@ -511,7 +552,8 @@ ArrayPrototypeForEach(
throw makeException( throw makeException(
TypeError, TypeError,
`is not ${article} ${name} object`, `is not ${article} ${name} object`,
opts, opts.prefix,
opts.context,
); );
} }
if ( if (
@ -521,7 +563,8 @@ ArrayPrototypeForEach(
throw makeException( throw makeException(
TypeError, TypeError,
"is a view on a SharedArrayBuffer, which is not allowed", "is a view on a SharedArrayBuffer, which is not allowed",
opts, opts.prefix,
opts.context,
); );
} }
@ -537,7 +580,8 @@ converters.ArrayBufferView = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not a view on an ArrayBuffer or SharedArrayBuffer", "is not a view on an ArrayBuffer or SharedArrayBuffer",
opts, opts.prefix,
opts.context,
); );
} }
let buffer; let buffer;
@ -550,7 +594,8 @@ converters.ArrayBufferView = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is a view on a SharedArrayBuffer, which is not allowed", "is a view on a SharedArrayBuffer, which is not allowed",
opts, opts.prefix,
opts.context,
); );
} }
@ -569,7 +614,8 @@ converters.BufferSource = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is a view on a SharedArrayBuffer, which is not allowed", "is a view on a SharedArrayBuffer, which is not allowed",
opts, opts.prefix,
opts.context,
); );
} }
@ -580,7 +626,8 @@ converters.BufferSource = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not an ArrayBuffer or a view on one", "is not an ArrayBuffer or a view on one",
opts, opts.prefix,
opts.context,
); );
} }
if ( if (
@ -591,7 +638,8 @@ converters.BufferSource = (V, opts = {}) => {
throw makeException( throw makeException(
TypeError, TypeError,
"is not an ArrayBuffer, SharedArrayBuffer, or a view on one", "is not an ArrayBuffer, SharedArrayBuffer, or a view on one",
opts, opts.prefix,
opts.context,
); );
} }
@ -707,7 +755,8 @@ function createDictionaryConverter(name, ...dictionaries) {
throw makeException( throw makeException(
TypeError, TypeError,
"can not be converted to a dictionary", "can not be converted to a dictionary",
opts, opts.prefix,
opts.context,
); );
} }
const esDict = V; const esDict = V;
@ -741,7 +790,8 @@ function createDictionaryConverter(name, ...dictionaries) {
throw makeException( throw makeException(
TypeError, TypeError,
`can not be converted to '${name}' because '${key}' is required in '${name}'.`, `can not be converted to '${name}' because '${key}' is required in '${name}'.`,
opts, opts.prefix,
opts.context,
); );
} }
} }
@ -789,7 +839,8 @@ function createSequenceConverter(converter) {
throw makeException( throw makeException(
TypeError, TypeError,
"can not be converted to sequence.", "can not be converted to sequence.",
opts, opts.prefix,
opts.context,
); );
} }
const iter = V?.[SymbolIterator]?.(); const iter = V?.[SymbolIterator]?.();
@ -797,7 +848,8 @@ function createSequenceConverter(converter) {
throw makeException( throw makeException(
TypeError, TypeError,
"can not be converted to sequence.", "can not be converted to sequence.",
opts, opts.prefix,
opts.context,
); );
} }
const array = []; const array = [];
@ -807,7 +859,8 @@ function createSequenceConverter(converter) {
throw makeException( throw makeException(
TypeError, TypeError,
"can not be converted to sequence.", "can not be converted to sequence.",
opts, opts.prefix,
opts.context,
); );
} }
if (res.done === true) break; if (res.done === true) break;
@ -827,7 +880,8 @@ function createRecordConverter(keyConverter, valueConverter) {
throw makeException( throw makeException(
TypeError, TypeError,
"can not be converted to dictionary.", "can not be converted to dictionary.",
opts, opts.prefix,
opts.context,
); );
} }
const result = {}; const result = {};
@ -870,16 +924,17 @@ function invokeCallbackFunction(
args, args,
thisArg, thisArg,
returnValueConverter, returnValueConverter,
opts, prefix,
returnsPromise,
) { ) {
try { try {
const rv = ReflectApply(callable, thisArg, args); const rv = ReflectApply(callable, thisArg, args);
return returnValueConverter(rv, { return returnValueConverter(rv, {
prefix: opts.prefix, prefix,
context: "return value", context: "return value",
}); });
} catch (err) { } catch (err) {
if (opts.returnsPromise === true) { if (returnsPromise === true) {
return PromiseReject(err); return PromiseReject(err);
} }
throw err; throw err;
@ -891,7 +946,12 @@ const brand = Symbol("[[webidl.brand]]");
function createInterfaceConverter(name, prototype) { function createInterfaceConverter(name, prototype) {
return (V, opts) => { return (V, opts) => {
if (!ObjectPrototypeIsPrototypeOf(prototype, V) || V[brand] !== brand) { if (!ObjectPrototypeIsPrototypeOf(prototype, V) || V[brand] !== brand) {
throw makeException(TypeError, `is not of type ${name}.`, opts); throw makeException(
TypeError,
`is not of type ${name}.`,
opts.prefix,
opts.context,
);
} }
return V; return V;
}; };

View file

@ -5,7 +5,7 @@
/// <reference lib="esnext" /> /// <reference lib="esnext" />
declare module "ext:deno_webidl/00_webidl.js" { declare module "ext:deno_webidl/00_webidl.js" {
interface ConverterOpts { interface ValueConverterOpts {
/** /**
* The prefix for error messages created by this converter. * The prefix for error messages created by this converter.
* Examples: * Examples:
@ -13,8 +13,6 @@ declare module "ext:deno_webidl/00_webidl.js" {
* - `Failed to execute 'removeEventListener' on 'EventTarget'` * - `Failed to execute 'removeEventListener' on 'EventTarget'`
*/ */
prefix: string; prefix: string;
}
interface ValueConverterOpts extends ConverterOpts {
/** /**
* The context of this value error messages created by this converter. * The context of this value error messages created by this converter.
* Examples: * Examples:
@ -26,7 +24,8 @@ declare module "ext:deno_webidl/00_webidl.js" {
function makeException( function makeException(
ErrorType: any, ErrorType: any,
message: string, message: string,
opts: ValueConverterOpts, prefix: string,
context: string,
): any; ): any;
interface IntConverterOpts extends ValueConverterOpts { interface IntConverterOpts extends ValueConverterOpts {
/** /**
@ -261,7 +260,8 @@ declare module "ext:deno_webidl/00_webidl.js" {
args: any[], args: any[],
thisArg: any, thisArg: any,
returnValueConverter: (v: any, opts: ValueConverterOpts) => T, returnValueConverter: (v: any, opts: ValueConverterOpts) => T,
opts: ConverterOpts & { returnsPromise?: boolean }, prefix: string,
returnsPromise?: boolean,
): T; ): T;
/** /**