浏览代码

Do not fail getIterator if 'next' is set, but not a function, only fail in iterator step, as per the specification. Adjusted the error message.

Dmitry Panov 2 年之前
父节点
当前提交
996d4a0dec
共有 1 个文件被更改,包括 11 次插入4 次删除
  1. 11 4
      runtime.go

+ 11 - 4
runtime.go

@@ -2625,7 +2625,15 @@ func (r *Runtime) getIterator(obj Value, method func(FunctionCall) Value) *itera
 	iter := r.toObject(method(FunctionCall{
 		This: obj,
 	}))
-	next := toMethod(iter.self.getStr("next", nil))
+
+	var next func(FunctionCall) Value
+
+	if obj, ok := iter.self.getStr("next", nil).(*Object); ok {
+		if call, ok := obj.self.assertCallable(); ok {
+			next = call
+		}
+	}
+
 	return &iteratorRecord{
 		iterator: iter,
 		next:     next,
@@ -2636,10 +2644,9 @@ func (ir *iteratorRecord) iterate(step func(Value)) {
 	r := ir.iterator.runtime
 	for {
 		if ir.next == nil {
-			panic(r.NewTypeError("object null is not a function"))
+			panic(r.NewTypeError("iterator.next is missing or not a function"))
 		}
-		nextVal := ir.next(FunctionCall{This: ir.iterator})
-		res := r.toObject(nextVal)
+		res := r.toObject(ir.next(FunctionCall{This: ir.iterator}))
 		if nilSafe(res.self.getStr("done", nil)).ToBoolean() {
 			break
 		}