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

Method arrayproto_toSorted

builtin_array.go:1321–1359  ·  view source on GitHub ↗
(call FunctionCall)

Source from the content-addressed store, hash-verified

1319}
1320
1321func (r *Runtime) arrayproto_toSorted(call FunctionCall) Value {
1322 var compareFn func(FunctionCall) Value
1323 arg := call.Argument(0)
1324 if arg != _undefined {
1325 if arg, ok := arg.(*Object); ok {
1326 compareFn, _ = arg.self.assertCallable()
1327 }
1328 if compareFn == nil {
1329 panic(r.NewTypeError("The comparison function must be either a function or undefined"))
1330 }
1331 }
1332
1333 o := call.This.ToObject(r)
1334 length := toLength(o.self.getStr("length", nil))
1335 if length >= math.MaxUint32 {
1336 panic(r.newError(r.getRangeError(), "Invalid array length"))
1337 }
1338 var a []Value
1339
1340 if src := r.checkStdArrayObj(o); src != nil {
1341 a = make([]Value, length)
1342 copy(a, src.values)
1343 } else {
1344 a = make([]Value, 0, length)
1345 for i := int64(0); i < length; i++ {
1346 idx := valueInt(i)
1347 a = append(a, nilSafe(o.self.getIdx(idx, nil)))
1348 }
1349 }
1350
1351 ar := r.newArrayValues(a)
1352 ctx := arraySortCtx{
1353 obj: ar.self,
1354 compare: compareFn,
1355 }
1356
1357 sort.Stable(&ctx)
1358 return ar
1359}
1360
1361func (r *Runtime) arrayproto_toSpliced(call FunctionCall) Value {
1362 o := call.This.ToObject(r)

Callers

nothing calls this directly

Calls 13

NewTypeErrorMethod · 0.95
newErrorMethod · 0.95
getRangeErrorMethod · 0.95
checkStdArrayObjMethod · 0.95
newArrayValuesMethod · 0.95
toLengthFunction · 0.85
valueIntTypeAlias · 0.85
nilSafeFunction · 0.85
assertCallableMethod · 0.65
ToObjectMethod · 0.65
getStrMethod · 0.65
getIdxMethod · 0.65

Tested by

no test coverage detected