summaryrefslogtreecommitdiff
path: root/ext/webidl
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /ext/webidl
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'ext/webidl')
-rw-r--r--ext/webidl/00_webidl.js126
-rw-r--r--ext/webidl/Cargo.toml2
-rw-r--r--ext/webidl/internal.d.ts26
3 files changed, 153 insertions, 1 deletions
diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js
index 1d05aae5f..eb18cbcc3 100644
--- a/ext/webidl/00_webidl.js
+++ b/ext/webidl/00_webidl.js
@@ -26,6 +26,7 @@ const {
Float32Array,
Float64Array,
FunctionPrototypeBind,
+ FunctionPrototypeCall,
Int16Array,
Int32Array,
Int8Array,
@@ -77,6 +78,7 @@ const {
StringPrototypeToWellFormed,
Symbol,
SymbolIterator,
+ SymbolAsyncIterator,
SymbolToStringTag,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetSymbolToStringTag,
@@ -920,6 +922,127 @@ function createSequenceConverter(converter) {
};
}
+function isAsyncIterable(obj) {
+ if (obj[SymbolAsyncIterator] === undefined) {
+ if (obj[SymbolIterator] === undefined) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+const AsyncIterable = Symbol("[[asyncIterable]]");
+
+function createAsyncIterableConverter(converter) {
+ return function (
+ V,
+ prefix = undefined,
+ context = undefined,
+ opts = { __proto__: null },
+ ) {
+ if (type(V) !== "Object") {
+ throw makeException(
+ TypeError,
+ "can not be converted to async iterable.",
+ prefix,
+ context,
+ );
+ }
+
+ let isAsync = true;
+ let method = V[SymbolAsyncIterator];
+ if (method === undefined) {
+ method = V[SymbolIterator];
+
+ if (method === undefined) {
+ throw makeException(
+ TypeError,
+ "is not iterable.",
+ prefix,
+ context,
+ );
+ }
+
+ isAsync = false;
+ }
+
+ return {
+ value: V,
+ [AsyncIterable]: AsyncIterable,
+ open(context) {
+ const iter = FunctionPrototypeCall(method, V);
+ if (type(iter) !== "Object") {
+ throw new TypeError(
+ `${context} could not be iterated because iterator method did not return object, but ${
+ type(iter)
+ }.`,
+ );
+ }
+
+ let asyncIterator = iter;
+
+ if (!isAsync) {
+ asyncIterator = {
+ // deno-lint-ignore require-await
+ async next() {
+ // deno-lint-ignore prefer-primordials
+ return iter.next();
+ },
+ };
+ }
+
+ return {
+ async next() {
+ // deno-lint-ignore prefer-primordials
+ const iterResult = await asyncIterator.next();
+ if (type(iterResult) !== "Object") {
+ throw TypeError(
+ `${context} failed to iterate next value because the next() method did not return an object, but ${
+ type(iterResult)
+ }.`,
+ );
+ }
+
+ if (iterResult.done) {
+ return { done: true };
+ }
+
+ const iterValue = converter(
+ iterResult.value,
+ `${context} failed to iterate next value`,
+ `The value returned from the next() method`,
+ opts,
+ );
+
+ return { done: false, value: iterValue };
+ },
+ async return(reason) {
+ if (asyncIterator.return === undefined) {
+ return undefined;
+ }
+
+ // deno-lint-ignore prefer-primordials
+ const returnPromiseResult = await asyncIterator.return(reason);
+ if (type(returnPromiseResult) !== "Object") {
+ throw TypeError(
+ `${context} failed to close iterator because the return() method did not return an object, but ${
+ type(returnPromiseResult)
+ }.`,
+ );
+ }
+
+ return undefined;
+ },
+ [SymbolAsyncIterator]() {
+ return this;
+ },
+ };
+ },
+ };
+ };
+}
+
function createRecordConverter(keyConverter, valueConverter) {
return (V, prefix, context, opts) => {
if (type(V) !== "Object") {
@@ -1302,9 +1425,11 @@ function setlike(obj, objPrototype, readonly) {
export {
assertBranded,
+ AsyncIterable,
brand,
configureInterface,
converters,
+ createAsyncIterableConverter,
createBranded,
createDictionaryConverter,
createEnumConverter,
@@ -1315,6 +1440,7 @@ export {
createSequenceConverter,
illegalConstructor,
invokeCallbackFunction,
+ isAsyncIterable,
makeException,
mixinPairIterable,
requiredArguments,
diff --git a/ext/webidl/Cargo.toml b/ext/webidl/Cargo.toml
index 8a25be366..8c3f6f612 100644
--- a/ext/webidl/Cargo.toml
+++ b/ext/webidl/Cargo.toml
@@ -2,7 +2,7 @@
[package]
name = "deno_webidl"
-version = "0.171.0"
+version = "0.177.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
diff --git a/ext/webidl/internal.d.ts b/ext/webidl/internal.d.ts
index 1ce45463e..375d548d3 100644
--- a/ext/webidl/internal.d.ts
+++ b/ext/webidl/internal.d.ts
@@ -439,6 +439,27 @@ declare module "ext:deno_webidl/00_webidl.js" {
) => T[];
/**
+ * Create a converter that converts an async iterable of the inner type.
+ */
+ function createAsyncIterableConverter<V, T>(
+ converter: (
+ v: V,
+ prefix?: string,
+ context?: string,
+ opts?: any,
+ ) => T,
+ ): (
+ v: any,
+ prefix?: string,
+ context?: string,
+ opts?: any,
+ ) => ConvertedAsyncIterable<V, T>;
+
+ interface ConvertedAsyncIterable<V, T> extends AsyncIterableIterator<T> {
+ value: V;
+ }
+
+ /**
* Create a converter that converts a Promise of the inner type.
*/
function createPromiseConverter<T>(
@@ -559,4 +580,9 @@ declare module "ext:deno_webidl/00_webidl.js" {
| "Symbol"
| "BigInt"
| "Object";
+
+ /**
+ * Check whether a value is an async iterable.
+ */
+ function isAsyncIterable(v: any): boolean;
}