MCPcopy Create free account
hub / github.com/dop251/goja / array_from

Method array_from

builtin_array.go:1467–1556  ·  view source on GitHub ↗
(call FunctionCall)

Source from the content-addressed store, hash-verified

1465}
1466
1467func (r *Runtime) array_from(call FunctionCall) Value {
1468 var mapFn func(FunctionCall) Value
1469 if mapFnArg := call.Argument(1); mapFnArg != _undefined {
1470 if mapFnObj, ok := mapFnArg.(*Object); ok {
1471 if fn, ok := mapFnObj.self.assertCallable(); ok {
1472 mapFn = fn
1473 }
1474 }
1475 if mapFn == nil {
1476 panic(r.NewTypeError("%s is not a function", mapFnArg))
1477 }
1478 }
1479 t := call.Argument(2)
1480 items := call.Argument(0)
1481 if mapFn == nil && call.This == r.global.Array { // mapFn may mutate the array
1482 if arr := r.checkStdArrayIter(items); arr != nil {
1483 items := make([]Value, len(arr.values))
1484 copy(items, arr.values)
1485 return r.newArrayValues(items)
1486 }
1487 }
1488
1489 var ctor func(args []Value, newTarget *Object) *Object
1490 if call.This != r.global.Array {
1491 if o, ok := call.This.(*Object); ok {
1492 if c := o.self.assertConstructor(); c != nil {
1493 ctor = c
1494 }
1495 }
1496 }
1497 var arr *Object
1498 if usingIterator := toMethod(r.getV(items, SymIterator)); usingIterator != nil {
1499 if ctor != nil {
1500 arr = ctor([]Value{}, nil)
1501 } else {
1502 arr = r.newArrayValues(nil)
1503 }
1504 iter := r.getIterator(items, usingIterator)
1505 if mapFn == nil {
1506 if a := r.checkStdArrayObjWithProto(arr); a != nil {
1507 var values []Value
1508 iter.iterate(func(val Value) {
1509 values = append(values, val)
1510 })
1511 setArrayValues(a, values)
1512 return arr
1513 }
1514 }
1515 k := int64(0)
1516 iter.iterate(func(val Value) {
1517 if mapFn != nil {
1518 val = mapFn(FunctionCall{This: t, Arguments: []Value{val, intToValue(k)}})
1519 }
1520 createDataPropertyOrThrow(arr, intToValue(k), val)
1521 k++
1522 })
1523 arr.self.setOwnStr("length", intToValue(k), true)
1524 } else {

Callers

nothing calls this directly

Calls 15

NewTypeErrorMethod · 0.95
checkStdArrayIterMethod · 0.95
newArrayValuesMethod · 0.95
getVMethod · 0.95
getIteratorMethod · 0.95
toMethodFunction · 0.85
setArrayValuesFunction · 0.85
intToValueFunction · 0.85
toLengthFunction · 0.85
nilSafeFunction · 0.85

Tested by

no test coverage detected