From acc0dd92730d0d13246ba272a23fca611c0746b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 7 Oct 2024 10:25:04 +0200 Subject: [PATCH] Implement proper Symbol.toStringTag for iterators --- quickjs.c | 32 ++++++++++++++++++++++++++++++++ test262_errors.txt | 12 ------------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/quickjs.c b/quickjs.c index d168fa2..e156a92 100644 --- a/quickjs.c +++ b/quickjs.c @@ -39991,6 +39991,37 @@ static JSValue js_iterator_proto_iterator(JSContext *ctx, JSValue this_val, return js_dup(this_val); } +static JSValue js_iterator_proto_get_toStringTag(JSContext *ctx, JSValue this_val) +{ + return JS_AtomToString(ctx, JS_ATOM_Iterator); +} + +static JSValue js_iterator_proto_set_toStringTag(JSContext *ctx, JSValue this_val, JSValue val) +{ + JSObject *p; + int res; + + if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT) + return JS_ThrowTypeError(ctx, + "set Iterator.prototype[Symbol.toStringTag] called on non-object"); + p = JS_VALUE_GET_OBJ(this_val); + if (p->class_id == JS_CLASS_ITERATOR) { + // This is home. + return JS_ThrowTypeError(ctx, "Cannot assign to read only property"); + } + res = JS_GetOwnProperty(ctx, NULL, this_val, JS_ATOM_Symbol_toStringTag); + if (res < 0) + return JS_EXCEPTION; + if (res) { + if (JS_SetProperty(ctx, this_val, JS_ATOM_Symbol_toStringTag, js_dup(val)) < 0) + return JS_EXCEPTION; + } else { + if (JS_DefinePropertyValue(ctx, this_val, JS_ATOM_Symbol_toStringTag, js_dup(val), JS_PROP_C_W_E) < 0) + return JS_EXCEPTION; + } + return JS_UNDEFINED; +} + static const JSCFunctionListEntry js_iterator_funcs[] = { JS_CFUNC_DEF("from", 1, js_iterator_from ), }; @@ -40008,6 +40039,7 @@ static const JSCFunctionListEntry js_iterator_proto_funcs[] = { JS_CFUNC_DEF("take", 1, js_iterator_proto_take ), JS_CFUNC_DEF("toArray", 0, js_iterator_proto_toArray ), JS_CFUNC_DEF("[Symbol.iterator]", 0, js_iterator_proto_iterator ), + JS_CGETSET_DEF("[Symbol.toStringTag]", js_iterator_proto_get_toStringTag, js_iterator_proto_set_toStringTag), }; static const JSCFunctionListEntry js_array_proto_funcs[] = { diff --git a/test262_errors.txt b/test262_errors.txt index f7d054a..89eb9d3 100644 --- a/test262_errors.txt +++ b/test262_errors.txt @@ -20,10 +20,6 @@ test262/test/built-ins/AsyncFromSyncIteratorPrototype/throw/throw-undefined-retu test262/test/built-ins/AsyncFromSyncIteratorPrototype/throw/throw-undefined-return-object.js:66: strict mode: TypeError: $DONE() not called test262/test/built-ins/Iterator/prototype/Symbol.iterator/prop-desc.js:15: Test262Error: obj should have an own property Symbol(Symbol.iterator) test262/test/built-ins/Iterator/prototype/Symbol.iterator/prop-desc.js:15: strict mode: Test262Error: obj should have an own property Symbol(Symbol.iterator) -test262/test/built-ins/Iterator/prototype/Symbol.toStringTag/prop-desc.js:11: TypeError: cannot read property 'get' of undefined -test262/test/built-ins/Iterator/prototype/Symbol.toStringTag/prop-desc.js:11: strict mode: TypeError: cannot read property 'get' of undefined -test262/test/built-ins/Iterator/prototype/Symbol.toStringTag/weird-setter.js:20: TypeError: Cannot convert undefined or null to object -test262/test/built-ins/Iterator/prototype/Symbol.toStringTag/weird-setter.js:20: strict mode: TypeError: Cannot convert undefined or null to object test262/test/built-ins/Iterator/prototype/constructor/prop-desc.js:10: Test262Error: Expected SameValue(«undefined», «function») to be true test262/test/built-ins/Iterator/prototype/constructor/prop-desc.js:10: strict mode: Test262Error: Expected SameValue(«undefined», «function») to be true test262/test/built-ins/Iterator/prototype/constructor/weird-setter.js:23: TypeError: cannot read property 'call' of undefined @@ -566,14 +562,6 @@ test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed-in-par test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed-in-parallel.js:19: strict mode: InternalError: TODO implement Iterator.prototype.take test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed.js:21: InternalError: TODO implement Iterator.prototype.take test262/test/built-ins/Iterator/prototype/take/underlying-iterator-closed.js:21: strict mode: InternalError: TODO implement Iterator.prototype.take -test262/test/built-ins/Object/prototype/toString/symbol-tag-array-builtin.js:24: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-array-builtin.js:24: strict mode: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-map-builtin.js:20: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-map-builtin.js:20: strict mode: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-set-builtin.js:20: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-set-builtin.js:20: strict mode: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-string-builtin.js:24: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true -test262/test/built-ins/Object/prototype/toString/symbol-tag-string-builtin.js:24: strict mode: Test262Error: Expected SameValue(«[object Object]», «[object Iterator]») to be true test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: Test262Error: `\p{Alphabetic}` should match U+02EBF0 (`𮯰`) test262/test/built-ins/RegExp/property-escapes/generated/Alphabetic.js:16: strict mode: Test262Error: `\p{Alphabetic}` should match U+02EBF0 (`𮯰`) test262/test/built-ins/RegExp/property-escapes/generated/Assigned.js:16: Test262Error: `\p{Assigned}` should match U+002FFC (`⿼`)