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

Method arrayproto_sort

builtin_array.go:377–429  ·  view source on GitHub ↗
(call FunctionCall)

Source from the content-addressed store, hash-verified

375}
376
377func (r *Runtime) arrayproto_sort(call FunctionCall) Value {
378 o := call.This.ToObject(r)
379
380 var compareFn func(FunctionCall) Value
381 arg := call.Argument(0)
382 if arg != _undefined {
383 if arg, ok := call.Argument(0).(*Object); ok {
384 compareFn, _ = arg.self.assertCallable()
385 }
386 if compareFn == nil {
387 panic(r.NewTypeError("The comparison function must be either a function or undefined"))
388 }
389 }
390
391 var s sortable
392 if r.checkStdArrayObj(o) != nil {
393 s = o.self
394 } else if _, ok := o.self.(reflectValueWrapper); ok {
395 s = o.self
396 }
397
398 if s != nil {
399 ctx := arraySortCtx{
400 obj: s,
401 compare: compareFn,
402 }
403
404 sort.Stable(&ctx)
405 } else {
406 length := toLength(o.self.getStr("length", nil))
407 a := make([]Value, 0, length)
408 for i := int64(0); i < length; i++ {
409 idx := valueInt(i)
410 if o.self.hasPropertyIdx(idx) {
411 a = append(a, nilSafe(o.self.getIdx(idx, nil)))
412 }
413 }
414 ar := r.newArrayValues(a)
415 ctx := arraySortCtx{
416 obj: ar.self,
417 compare: compareFn,
418 }
419
420 sort.Stable(&ctx)
421 for i := 0; i < len(a); i++ {
422 o.self.setOwnIdx(valueInt(i), a[i], true)
423 }
424 for i := int64(len(a)); i < length; i++ {
425 o.self.deleteIdx(valueInt(i), true)
426 }
427 }
428 return o
429}
430
431func (r *Runtime) arrayproto_splice(call FunctionCall) Value {
432 o := call.This.ToObject(r)

Callers

nothing calls this directly

Calls 14

NewTypeErrorMethod · 0.95
checkStdArrayObjMethod · 0.95
newArrayValuesMethod · 0.95
toLengthFunction · 0.85
valueIntTypeAlias · 0.85
nilSafeFunction · 0.85
ToObjectMethod · 0.65
assertCallableMethod · 0.65
getStrMethod · 0.65
hasPropertyIdxMethod · 0.65
getIdxMethod · 0.65
setOwnIdxMethod · 0.65

Tested by

no test coverage detected