Array iterator's next() reads [[ArrayLikeNextIndex]] after reading length

オープン
#1,957 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c, javascript
領域
backend, compilers

調査の方向性

Start with js_array_iterator_next in quickjs.c, especially the js_get_length32 call and the later read of it->idx. Compare the order with the %ArrayIteratorPrototype%.next steps cited in the issue, then run the reproduction with qjs sample.js; done when it prints 0 0 and the iterator behavior matches the specified order.

索引モデルが issue の本文から書いたものです。

説明

Reproduction
// sample.js
var iterator;
var nested = false;
var nestedKey;

var arrayLike = {
  get length() {
    if (!nested) {
      nested = true;
      // nested next() while the outer next() is reading length
      nestedKey = iterator.next().value;
    }
    return 2;
  }
};

iterator = Array.prototype.keys.call(arrayLike);
var outerKey = iterator.next().value;
print(nestedKey + " " + outerKey);
Expected

V8 (16.0.102):

$ v8 sample.js
0 0
Actual (QuickJS-ng)
$ qjs sample.js
0 1
Spec

%ArrayIteratorPrototype%.next reads the iterator's index before it reads the length:

6. Let index be iteratorObj.[[ArrayLikeNextIndex]].
...
9.a. Let length be ? LengthOfArrayLike(array).
...
11. Set iteratorObj.[[ArrayLikeNextIndex]] to index + 1.

The outer iterator.next() stores [[ArrayLikeNextIndex]] (0) in index (step 6) and then reads length (step 9.a, through LengthOfArrayLike). The length getter calls iterator.next() once more. This nested call also stores 0 in its own index, returns 0, and sets [[ArrayLikeNextIndex]] to 1. When it returns, the outer call continues with the index it stored earlier, so it also returns 0.

QuickJS-ng reads [[ArrayLikeNextIndex]] after reading length, so the outer call sees the 1 written by the nested call and returns 1.

In js_array_iterator_next, QuickJS-ng gets the length with js_get_length32 first and reads it->idx after that:

if (js_get_length32(ctx, &len, it->obj)) {
    ...
}
...
idx = it->idx;
...
it->idx = idx + 1;

JavaScriptCore, SpiderMonkey and GraalJS agree with V8. XS has the same behavior (Moddable-OpenSource/moddable#1837).

Versions
  • QuickJS-ng: 0.18.0
  • V8: 16.0.102
主要言語
C
スター
3.9k
フォーク
389
平均マージ
2日 2時間
マージ済み PR(30日)
19

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

quickjs-ng/quickjs のほかの issue

quickjs-ng/quickjs の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。