builtin_array.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. package goja
  2. import (
  3. "math"
  4. "sort"
  5. "strings"
  6. )
  7. func (r *Runtime) newArray(prototype *Object) (a *arrayObject) {
  8. v := &Object{runtime: r}
  9. a = &arrayObject{}
  10. a.class = classArray
  11. a.val = v
  12. a.extensible = true
  13. v.self = a
  14. a.prototype = prototype
  15. a.init()
  16. return
  17. }
  18. func (r *Runtime) newArrayObject() *arrayObject {
  19. return r.newArray(r.global.ArrayPrototype)
  20. }
  21. func setArrayValues(a *arrayObject, values []Value) *arrayObject {
  22. a.values = values
  23. a.length = uint32(len(values))
  24. a.objCount = len(values)
  25. return a
  26. }
  27. func setArrayLength(a *arrayObject, l int64) *arrayObject {
  28. a.setOwnStr("length", intToValue(l), true)
  29. return a
  30. }
  31. func arraySpeciesCreate(obj *Object, size int64) *Object {
  32. if isArray(obj) {
  33. v := obj.self.getStr("constructor", nil)
  34. if constructObj, ok := v.(*Object); ok {
  35. v = constructObj.self.getSym(symSpecies, nil)
  36. if v == _null {
  37. v = nil
  38. }
  39. }
  40. if v != nil && v != _undefined {
  41. constructObj, _ := v.(*Object)
  42. if constructObj != nil {
  43. if constructor := constructObj.self.assertConstructor(); constructor != nil {
  44. return constructor([]Value{intToValue(size)}, constructObj)
  45. }
  46. }
  47. panic(obj.runtime.NewTypeError("Species is not a constructor"))
  48. }
  49. }
  50. return obj.runtime.newArrayLength(size)
  51. }
  52. func max(a, b int64) int64 {
  53. if a > b {
  54. return a
  55. }
  56. return b
  57. }
  58. func min(a, b int64) int64 {
  59. if a < b {
  60. return a
  61. }
  62. return b
  63. }
  64. func relToIdx(rel, l int64) int64 {
  65. if rel >= 0 {
  66. return min(rel, l)
  67. }
  68. return max(l+rel, 0)
  69. }
  70. func (r *Runtime) newArrayValues(values []Value) *Object {
  71. return setArrayValues(r.newArrayObject(), values).val
  72. }
  73. func (r *Runtime) newArrayLength(l int64) *Object {
  74. return setArrayLength(r.newArrayObject(), l).val
  75. }
  76. func (r *Runtime) builtin_newArray(args []Value, proto *Object) *Object {
  77. l := len(args)
  78. if l == 1 {
  79. if al, ok := args[0].(valueInt); ok {
  80. return setArrayLength(r.newArray(proto), int64(al)).val
  81. } else if f, ok := args[0].(valueFloat); ok {
  82. al := int64(f)
  83. if float64(al) == float64(f) {
  84. return r.newArrayLength(al)
  85. } else {
  86. panic(r.newError(r.global.RangeError, "Invalid array length"))
  87. }
  88. }
  89. return setArrayValues(r.newArray(proto), []Value{args[0]}).val
  90. } else {
  91. argsCopy := make([]Value, l)
  92. copy(argsCopy, args)
  93. return setArrayValues(r.newArray(proto), argsCopy).val
  94. }
  95. }
  96. func (r *Runtime) generic_push(obj *Object, call FunctionCall) Value {
  97. l := toLength(obj.self.getStr("length", nil))
  98. nl := l + int64(len(call.Arguments))
  99. if nl >= maxInt {
  100. r.typeErrorResult(true, "Invalid array length")
  101. panic("unreachable")
  102. }
  103. for i, arg := range call.Arguments {
  104. obj.self.setOwnIdx(valueInt(l+int64(i)), arg, true)
  105. }
  106. n := valueInt(nl)
  107. obj.self.setOwnStr("length", n, true)
  108. return n
  109. }
  110. func (r *Runtime) arrayproto_push(call FunctionCall) Value {
  111. obj := call.This.ToObject(r)
  112. return r.generic_push(obj, call)
  113. }
  114. func (r *Runtime) arrayproto_pop_generic(obj *Object) Value {
  115. l := toLength(obj.self.getStr("length", nil))
  116. if l == 0 {
  117. obj.self.setOwnStr("length", intToValue(0), true)
  118. return _undefined
  119. }
  120. idx := valueInt(l - 1)
  121. val := obj.self.getIdx(idx, nil)
  122. obj.self.deleteIdx(idx, true)
  123. obj.self.setOwnStr("length", idx, true)
  124. return val
  125. }
  126. func (r *Runtime) arrayproto_pop(call FunctionCall) Value {
  127. obj := call.This.ToObject(r)
  128. if a, ok := obj.self.(*arrayObject); ok {
  129. l := a.length
  130. if l > 0 {
  131. var val Value
  132. l--
  133. if l < uint32(len(a.values)) {
  134. val = a.values[l]
  135. }
  136. if val == nil {
  137. // optimisation bail-out
  138. return r.arrayproto_pop_generic(obj)
  139. }
  140. if _, ok := val.(*valueProperty); ok {
  141. // optimisation bail-out
  142. return r.arrayproto_pop_generic(obj)
  143. }
  144. //a._setLengthInt(l, false)
  145. a.values[l] = nil
  146. a.values = a.values[:l]
  147. a.length = l
  148. return val
  149. }
  150. return _undefined
  151. } else {
  152. return r.arrayproto_pop_generic(obj)
  153. }
  154. }
  155. func (r *Runtime) arrayproto_join(call FunctionCall) Value {
  156. o := call.This.ToObject(r)
  157. l := int(toLength(o.self.getStr("length", nil)))
  158. sep := ""
  159. if s := call.Argument(0); s != _undefined {
  160. sep = s.String()
  161. } else {
  162. sep = ","
  163. }
  164. if l == 0 {
  165. return stringEmpty
  166. }
  167. var buf strings.Builder
  168. element0 := o.self.getIdx(valueInt(0), nil)
  169. if element0 != nil && element0 != _undefined && element0 != _null {
  170. buf.WriteString(element0.String())
  171. }
  172. for i := 1; i < l; i++ {
  173. buf.WriteString(sep)
  174. element := o.self.getIdx(valueInt(int64(i)), nil)
  175. if element != nil && element != _undefined && element != _null {
  176. buf.WriteString(element.String())
  177. }
  178. }
  179. return newStringValue(buf.String())
  180. }
  181. func (r *Runtime) arrayproto_toString(call FunctionCall) Value {
  182. array := call.This.ToObject(r)
  183. f := array.self.getStr("join", nil)
  184. if fObj, ok := f.(*Object); ok {
  185. if fcall, ok := fObj.self.assertCallable(); ok {
  186. return fcall(FunctionCall{
  187. This: array,
  188. })
  189. }
  190. }
  191. return r.objectproto_toString(FunctionCall{
  192. This: array,
  193. })
  194. }
  195. func (r *Runtime) writeItemLocaleString(item Value, buf *strings.Builder) {
  196. if item != nil && item != _undefined && item != _null {
  197. if f, ok := r.getVStr(item, "toLocaleString").(*Object); ok {
  198. if c, ok := f.self.assertCallable(); ok {
  199. strVal := c(FunctionCall{
  200. This: item,
  201. })
  202. buf.WriteString(strVal.ToPrimitiveString().String())
  203. return
  204. }
  205. }
  206. r.typeErrorResult(true, "Property 'toLocaleString' of object %s is not a function", item)
  207. }
  208. }
  209. func (r *Runtime) arrayproto_toLocaleString(call FunctionCall) Value {
  210. array := call.This.ToObject(r)
  211. var buf strings.Builder
  212. if a := r.checkStdArrayObj(array); a != nil {
  213. for i, item := range a.values {
  214. if i > 0 {
  215. buf.WriteByte(',')
  216. }
  217. r.writeItemLocaleString(item, &buf)
  218. }
  219. } else {
  220. length := toLength(array.self.getStr("length", nil))
  221. for i := int64(0); i < length; i++ {
  222. if i > 0 {
  223. buf.WriteByte(',')
  224. }
  225. item := array.self.getIdx(valueInt(i), nil)
  226. r.writeItemLocaleString(item, &buf)
  227. }
  228. }
  229. return newStringValue(buf.String())
  230. }
  231. func isConcatSpreadable(obj *Object) bool {
  232. spreadable := obj.self.getSym(symIsConcatSpreadable, nil)
  233. if spreadable != nil && spreadable != _undefined {
  234. return spreadable.ToBoolean()
  235. }
  236. return isArray(obj)
  237. }
  238. func (r *Runtime) arrayproto_concat_append(a *Object, item Value) {
  239. aLength := toLength(a.self.getStr("length", nil))
  240. if obj, ok := item.(*Object); ok && isConcatSpreadable(obj) {
  241. length := toLength(obj.self.getStr("length", nil))
  242. for i := int64(0); i < length; i++ {
  243. v := obj.self.getIdx(valueInt(i), nil)
  244. if v != nil {
  245. createDataPropertyOrThrow(a, intToValue(aLength), v)
  246. }
  247. aLength++
  248. }
  249. } else {
  250. createDataPropertyOrThrow(a, intToValue(aLength), item)
  251. aLength++
  252. }
  253. a.self.setOwnStr("length", intToValue(aLength), true)
  254. }
  255. func (r *Runtime) arrayproto_concat(call FunctionCall) Value {
  256. obj := call.This.ToObject(r)
  257. a := arraySpeciesCreate(obj, 0)
  258. r.arrayproto_concat_append(a, call.This.ToObject(r))
  259. for _, item := range call.Arguments {
  260. r.arrayproto_concat_append(a, item)
  261. }
  262. return a
  263. }
  264. func (r *Runtime) arrayproto_slice(call FunctionCall) Value {
  265. o := call.This.ToObject(r)
  266. length := toLength(o.self.getStr("length", nil))
  267. start := relToIdx(call.Argument(0).ToInteger(), length)
  268. var end int64
  269. if endArg := call.Argument(1); endArg != _undefined {
  270. end = endArg.ToInteger()
  271. } else {
  272. end = length
  273. }
  274. end = relToIdx(end, length)
  275. count := end - start
  276. if count < 0 {
  277. count = 0
  278. }
  279. a := arraySpeciesCreate(o, count)
  280. if src := r.checkStdArrayObj(o); src != nil {
  281. if dst, ok := a.self.(*arrayObject); ok {
  282. values := make([]Value, count)
  283. copy(values, src.values[start:])
  284. setArrayValues(dst, values)
  285. return a
  286. }
  287. }
  288. n := int64(0)
  289. for start < end {
  290. p := o.self.getIdx(valueInt(start), nil)
  291. if p != nil {
  292. createDataPropertyOrThrow(a, valueInt(n), p)
  293. }
  294. start++
  295. n++
  296. }
  297. return a
  298. }
  299. func (r *Runtime) arrayproto_sort(call FunctionCall) Value {
  300. o := call.This.ToObject(r)
  301. var compareFn func(FunctionCall) Value
  302. if arg, ok := call.Argument(0).(*Object); ok {
  303. compareFn, _ = arg.self.assertCallable()
  304. }
  305. ctx := arraySortCtx{
  306. obj: o.self,
  307. compare: compareFn,
  308. }
  309. sort.Sort(&ctx)
  310. return o
  311. }
  312. func (r *Runtime) arrayproto_splice(call FunctionCall) Value {
  313. o := call.This.ToObject(r)
  314. length := toLength(o.self.getStr("length", nil))
  315. actualStart := relToIdx(call.Argument(0).ToInteger(), length)
  316. var actualDeleteCount int64
  317. switch len(call.Arguments) {
  318. case 0:
  319. case 1:
  320. actualDeleteCount = length - actualStart
  321. default:
  322. actualDeleteCount = min(max(call.Argument(1).ToInteger(), 0), length-actualStart)
  323. }
  324. a := arraySpeciesCreate(o, actualDeleteCount)
  325. itemCount := max(int64(len(call.Arguments)-2), 0)
  326. newLength := length - actualDeleteCount + itemCount
  327. if src := r.checkStdArrayObj(o); src != nil {
  328. if dst, ok := a.self.(*arrayObject); ok {
  329. values := make([]Value, actualDeleteCount)
  330. copy(values, src.values[actualStart:])
  331. setArrayValues(dst, values)
  332. } else {
  333. for k := int64(0); k < actualDeleteCount; k++ {
  334. createDataPropertyOrThrow(a, intToValue(k), src.values[k+actualStart])
  335. }
  336. }
  337. var values []Value
  338. if itemCount < actualDeleteCount {
  339. values = src.values
  340. copy(values[actualStart+itemCount:], values[actualStart+actualDeleteCount:])
  341. tail := values[newLength:]
  342. for k := range tail {
  343. tail[k] = nil
  344. }
  345. values = values[:newLength]
  346. } else if itemCount > actualDeleteCount {
  347. if int64(cap(src.values)) >= newLength {
  348. values = src.values[:newLength]
  349. copy(values[actualStart+itemCount:], values[actualStart+actualDeleteCount:length])
  350. } else {
  351. values = make([]Value, newLength)
  352. copy(values, src.values[:actualStart])
  353. copy(values[actualStart+itemCount:], src.values[actualStart+actualDeleteCount:])
  354. }
  355. } else {
  356. values = src.values
  357. }
  358. if itemCount > 0 {
  359. copy(values[actualStart:], call.Arguments[2:])
  360. }
  361. src.values = values
  362. src.objCount = len(values)
  363. } else {
  364. for k := int64(0); k < actualDeleteCount; k++ {
  365. from := valueInt(k + actualStart)
  366. if o.self.hasPropertyIdx(from) {
  367. createDataPropertyOrThrow(a, valueInt(k), o.self.getIdx(from, nil))
  368. }
  369. }
  370. if itemCount < actualDeleteCount {
  371. for k := actualStart; k < length-actualDeleteCount; k++ {
  372. from := valueInt(k + actualDeleteCount)
  373. to := valueInt(k + itemCount)
  374. if o.self.hasPropertyIdx(from) {
  375. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  376. } else {
  377. o.self.deleteIdx(to, true)
  378. }
  379. }
  380. for k := length; k > length-actualDeleteCount+itemCount; k-- {
  381. o.self.deleteIdx(valueInt(k-1), true)
  382. }
  383. } else if itemCount > actualDeleteCount {
  384. for k := length - actualDeleteCount; k > actualStart; k-- {
  385. from := valueInt(k + actualDeleteCount - 1)
  386. to := valueInt(k + itemCount - 1)
  387. if o.self.hasPropertyIdx(from) {
  388. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  389. } else {
  390. o.self.deleteIdx(to, true)
  391. }
  392. }
  393. }
  394. if itemCount > 0 {
  395. for i, item := range call.Arguments[2:] {
  396. o.self.setOwnIdx(valueInt(actualStart+int64(i)), item, true)
  397. }
  398. }
  399. }
  400. o.self.setOwnStr("length", intToValue(newLength), true)
  401. return a
  402. }
  403. func (r *Runtime) arrayproto_unshift(call FunctionCall) Value {
  404. o := call.This.ToObject(r)
  405. length := toLength(o.self.getStr("length", nil))
  406. argCount := int64(len(call.Arguments))
  407. newLen := intToValue(length + argCount)
  408. newSize := length + argCount
  409. if arr := r.checkStdArrayObj(o); arr != nil && newSize < math.MaxUint32 {
  410. if int64(cap(arr.values)) >= newSize {
  411. arr.values = arr.values[:newSize]
  412. copy(arr.values[argCount:], arr.values[:length])
  413. } else {
  414. values := make([]Value, newSize)
  415. copy(values[argCount:], arr.values)
  416. arr.values = values
  417. }
  418. copy(arr.values, call.Arguments)
  419. arr.objCount = int(arr.length)
  420. } else {
  421. for k := length - 1; k >= 0; k-- {
  422. from := valueInt(k)
  423. to := valueInt(k + argCount)
  424. if o.self.hasPropertyIdx(from) {
  425. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  426. } else {
  427. o.self.deleteIdx(to, true)
  428. }
  429. }
  430. for k, arg := range call.Arguments {
  431. o.self.setOwnIdx(valueInt(int64(k)), arg, true)
  432. }
  433. }
  434. o.self.setOwnStr("length", newLen, true)
  435. return newLen
  436. }
  437. func (r *Runtime) arrayproto_indexOf(call FunctionCall) Value {
  438. o := call.This.ToObject(r)
  439. length := toLength(o.self.getStr("length", nil))
  440. if length == 0 {
  441. return intToValue(-1)
  442. }
  443. n := call.Argument(1).ToInteger()
  444. if n >= length {
  445. return intToValue(-1)
  446. }
  447. if n < 0 {
  448. n = max(length+n, 0)
  449. }
  450. searchElement := call.Argument(0)
  451. if arr := r.checkStdArrayObj(o); arr != nil {
  452. for i, val := range arr.values[n:] {
  453. if searchElement.StrictEquals(val) {
  454. return intToValue(n + int64(i))
  455. }
  456. }
  457. return intToValue(-1)
  458. }
  459. for ; n < length; n++ {
  460. idx := valueInt(n)
  461. if val := o.self.getIdx(idx, nil); val != nil {
  462. if searchElement.StrictEquals(val) {
  463. return idx
  464. }
  465. }
  466. }
  467. return valueInt(-1)
  468. }
  469. func (r *Runtime) arrayproto_lastIndexOf(call FunctionCall) Value {
  470. o := call.This.ToObject(r)
  471. length := toLength(o.self.getStr("length", nil))
  472. if length == 0 {
  473. return valueInt(-1)
  474. }
  475. var fromIndex int64
  476. if len(call.Arguments) < 2 {
  477. fromIndex = length - 1
  478. } else {
  479. fromIndex = call.Argument(1).ToInteger()
  480. if fromIndex >= 0 {
  481. fromIndex = min(fromIndex, length-1)
  482. } else {
  483. fromIndex += length
  484. }
  485. }
  486. searchElement := call.Argument(0)
  487. if arr := r.checkStdArrayObj(o); arr != nil {
  488. vals := arr.values
  489. for k := fromIndex; k >= 0; k-- {
  490. if v := vals[k]; v != nil && searchElement.StrictEquals(v) {
  491. return intToValue(k)
  492. }
  493. }
  494. return intToValue(-1)
  495. }
  496. for k := fromIndex; k >= 0; k-- {
  497. idx := valueInt(k)
  498. if val := o.self.getIdx(idx, nil); val != nil {
  499. if searchElement.StrictEquals(val) {
  500. return idx
  501. }
  502. }
  503. }
  504. return intToValue(-1)
  505. }
  506. func (r *Runtime) arrayproto_every(call FunctionCall) Value {
  507. o := call.This.ToObject(r)
  508. length := toLength(o.self.getStr("length", nil))
  509. callbackFn := r.toCallable(call.Argument(0))
  510. fc := FunctionCall{
  511. This: call.Argument(1),
  512. Arguments: []Value{nil, nil, o},
  513. }
  514. for k := int64(0); k < length; k++ {
  515. idx := valueInt(k)
  516. if val := o.self.getIdx(idx, nil); val != nil {
  517. fc.Arguments[0] = val
  518. fc.Arguments[1] = idx
  519. if !callbackFn(fc).ToBoolean() {
  520. return valueFalse
  521. }
  522. }
  523. }
  524. return valueTrue
  525. }
  526. func (r *Runtime) arrayproto_some(call FunctionCall) Value {
  527. o := call.This.ToObject(r)
  528. length := toLength(o.self.getStr("length", nil))
  529. callbackFn := r.toCallable(call.Argument(0))
  530. fc := FunctionCall{
  531. This: call.Argument(1),
  532. Arguments: []Value{nil, nil, o},
  533. }
  534. for k := int64(0); k < length; k++ {
  535. idx := valueInt(k)
  536. if val := o.self.getIdx(idx, nil); val != nil {
  537. fc.Arguments[0] = val
  538. fc.Arguments[1] = idx
  539. if callbackFn(fc).ToBoolean() {
  540. return valueTrue
  541. }
  542. }
  543. }
  544. return valueFalse
  545. }
  546. func (r *Runtime) arrayproto_forEach(call FunctionCall) Value {
  547. o := call.This.ToObject(r)
  548. length := toLength(o.self.getStr("length", nil))
  549. callbackFn := r.toCallable(call.Argument(0))
  550. fc := FunctionCall{
  551. This: call.Argument(1),
  552. Arguments: []Value{nil, nil, o},
  553. }
  554. for k := int64(0); k < length; k++ {
  555. idx := valueInt(k)
  556. if val := o.self.getIdx(idx, nil); val != nil {
  557. fc.Arguments[0] = val
  558. fc.Arguments[1] = idx
  559. callbackFn(fc)
  560. }
  561. }
  562. return _undefined
  563. }
  564. func (r *Runtime) arrayproto_map(call FunctionCall) Value {
  565. o := call.This.ToObject(r)
  566. length := toLength(o.self.getStr("length", nil))
  567. callbackFn := r.toCallable(call.Argument(0))
  568. fc := FunctionCall{
  569. This: call.Argument(1),
  570. Arguments: []Value{nil, nil, o},
  571. }
  572. a := arraySpeciesCreate(o, length)
  573. if _, stdSrc := o.self.(*arrayObject); stdSrc {
  574. if arr, ok := a.self.(*arrayObject); ok {
  575. values := make([]Value, length)
  576. for k := int64(0); k < length; k++ {
  577. idx := valueInt(k)
  578. if val := o.self.getIdx(idx, nil); val != nil {
  579. fc.Arguments[0] = val
  580. fc.Arguments[1] = idx
  581. values[k] = callbackFn(fc)
  582. }
  583. }
  584. setArrayValues(arr, values)
  585. return a
  586. }
  587. }
  588. for k := int64(0); k < length; k++ {
  589. idx := valueInt(k)
  590. if val := o.self.getIdx(idx, nil); val != nil {
  591. fc.Arguments[0] = val
  592. fc.Arguments[1] = idx
  593. createDataPropertyOrThrow(a, idx, callbackFn(fc))
  594. }
  595. }
  596. return a
  597. }
  598. func (r *Runtime) arrayproto_filter(call FunctionCall) Value {
  599. o := call.This.ToObject(r)
  600. length := toLength(o.self.getStr("length", nil))
  601. callbackFn := call.Argument(0).ToObject(r)
  602. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  603. a := arraySpeciesCreate(o, 0)
  604. fc := FunctionCall{
  605. This: call.Argument(1),
  606. Arguments: []Value{nil, nil, o},
  607. }
  608. if _, stdSrc := o.self.(*arrayObject); stdSrc {
  609. if arr := r.checkStdArrayObj(a); arr != nil {
  610. var values []Value
  611. for k := int64(0); k < length; k++ {
  612. idx := valueInt(k)
  613. if val := o.self.getIdx(idx, nil); val != nil {
  614. fc.Arguments[0] = val
  615. fc.Arguments[1] = idx
  616. if callbackFn(fc).ToBoolean() {
  617. values = append(values, val)
  618. }
  619. }
  620. }
  621. setArrayValues(arr, values)
  622. return a
  623. }
  624. }
  625. to := int64(0)
  626. for k := int64(0); k < length; k++ {
  627. idx := valueInt(k)
  628. if val := o.self.getIdx(idx, nil); val != nil {
  629. fc.Arguments[0] = val
  630. fc.Arguments[1] = idx
  631. if callbackFn(fc).ToBoolean() {
  632. createDataPropertyOrThrow(a, intToValue(to), val)
  633. to++
  634. }
  635. }
  636. }
  637. return a
  638. } else {
  639. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  640. }
  641. panic("unreachable")
  642. }
  643. func (r *Runtime) arrayproto_reduce(call FunctionCall) Value {
  644. o := call.This.ToObject(r)
  645. length := toLength(o.self.getStr("length", nil))
  646. callbackFn := call.Argument(0).ToObject(r)
  647. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  648. fc := FunctionCall{
  649. This: _undefined,
  650. Arguments: []Value{nil, nil, nil, o},
  651. }
  652. var k int64
  653. if len(call.Arguments) >= 2 {
  654. fc.Arguments[0] = call.Argument(1)
  655. } else {
  656. for ; k < length; k++ {
  657. idx := valueInt(k)
  658. if val := o.self.getIdx(idx, nil); val != nil {
  659. fc.Arguments[0] = val
  660. break
  661. }
  662. }
  663. if fc.Arguments[0] == nil {
  664. r.typeErrorResult(true, "No initial value")
  665. panic("unreachable")
  666. }
  667. k++
  668. }
  669. for ; k < length; k++ {
  670. idx := valueInt(k)
  671. if val := o.self.getIdx(idx, nil); val != nil {
  672. fc.Arguments[1] = val
  673. fc.Arguments[2] = idx
  674. fc.Arguments[0] = callbackFn(fc)
  675. }
  676. }
  677. return fc.Arguments[0]
  678. } else {
  679. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  680. }
  681. panic("unreachable")
  682. }
  683. func (r *Runtime) arrayproto_reduceRight(call FunctionCall) Value {
  684. o := call.This.ToObject(r)
  685. length := toLength(o.self.getStr("length", nil))
  686. callbackFn := call.Argument(0).ToObject(r)
  687. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  688. fc := FunctionCall{
  689. This: _undefined,
  690. Arguments: []Value{nil, nil, nil, o},
  691. }
  692. k := length - 1
  693. if len(call.Arguments) >= 2 {
  694. fc.Arguments[0] = call.Argument(1)
  695. } else {
  696. for ; k >= 0; k-- {
  697. idx := valueInt(k)
  698. if val := o.self.getIdx(idx, nil); val != nil {
  699. fc.Arguments[0] = val
  700. break
  701. }
  702. }
  703. if fc.Arguments[0] == nil {
  704. r.typeErrorResult(true, "No initial value")
  705. panic("unreachable")
  706. }
  707. k--
  708. }
  709. for ; k >= 0; k-- {
  710. idx := valueInt(k)
  711. if val := o.self.getIdx(idx, nil); val != nil {
  712. fc.Arguments[1] = val
  713. fc.Arguments[2] = idx
  714. fc.Arguments[0] = callbackFn(fc)
  715. }
  716. }
  717. return fc.Arguments[0]
  718. } else {
  719. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  720. }
  721. panic("unreachable")
  722. }
  723. func arrayproto_reverse_generic_step(o *Object, lower, upper int64) {
  724. lowerP := valueInt(lower)
  725. upperP := valueInt(upper)
  726. lowerValue := o.self.getIdx(lowerP, nil)
  727. upperValue := o.self.getIdx(upperP, nil)
  728. if lowerValue != nil && upperValue != nil {
  729. o.self.setOwnIdx(lowerP, upperValue, true)
  730. o.self.setOwnIdx(upperP, lowerValue, true)
  731. } else if lowerValue == nil && upperValue != nil {
  732. o.self.setOwnIdx(lowerP, upperValue, true)
  733. o.self.deleteIdx(upperP, true)
  734. } else if lowerValue != nil && upperValue == nil {
  735. o.self.deleteIdx(lowerP, true)
  736. o.self.setOwnIdx(upperP, lowerValue, true)
  737. }
  738. }
  739. func (r *Runtime) arrayproto_reverse_generic(o *Object, start int64) {
  740. l := toLength(o.self.getStr("length", nil))
  741. middle := l / 2
  742. for lower := start; lower != middle; lower++ {
  743. arrayproto_reverse_generic_step(o, lower, l-lower-1)
  744. }
  745. }
  746. func (r *Runtime) arrayproto_reverse(call FunctionCall) Value {
  747. o := call.This.ToObject(r)
  748. if a := r.checkStdArrayObj(o); a != nil {
  749. l := len(a.values)
  750. middle := l / 2
  751. for lower := 0; lower != middle; lower++ {
  752. upper := l - lower - 1
  753. a.values[lower], a.values[upper] = a.values[upper], a.values[lower]
  754. }
  755. //TODO: go arrays
  756. } else {
  757. r.arrayproto_reverse_generic(o, 0)
  758. }
  759. return o
  760. }
  761. func (r *Runtime) arrayproto_shift(call FunctionCall) Value {
  762. o := call.This.ToObject(r)
  763. length := toLength(o.self.getStr("length", nil))
  764. if length == 0 {
  765. o.self.setOwnStr("length", intToValue(0), true)
  766. return _undefined
  767. }
  768. first := o.self.getIdx(valueInt(0), nil)
  769. for i := int64(1); i < length; i++ {
  770. v := o.self.getIdx(valueInt(i), nil)
  771. if v != nil {
  772. o.self.setOwnIdx(valueInt(i-1), v, true)
  773. } else {
  774. o.self.deleteIdx(valueInt(i-1), true)
  775. }
  776. }
  777. lv := valueInt(length - 1)
  778. o.self.deleteIdx(lv, true)
  779. o.self.setOwnStr("length", lv, true)
  780. return first
  781. }
  782. func (r *Runtime) arrayproto_values(call FunctionCall) Value {
  783. return r.createArrayIterator(call.This.ToObject(r), iterationKindValue)
  784. }
  785. func (r *Runtime) arrayproto_keys(call FunctionCall) Value {
  786. return r.createArrayIterator(call.This.ToObject(r), iterationKindKey)
  787. }
  788. func (r *Runtime) arrayproto_copyWithin(call FunctionCall) Value {
  789. o := call.This.ToObject(r)
  790. l := toLength(o.self.getStr("length", nil))
  791. var relEnd, dir int64
  792. to := relToIdx(call.Argument(0).ToInteger(), l)
  793. from := relToIdx(call.Argument(1).ToInteger(), l)
  794. if end := call.Argument(2); end != _undefined {
  795. relEnd = end.ToInteger()
  796. } else {
  797. relEnd = l
  798. }
  799. final := relToIdx(relEnd, l)
  800. count := min(final-from, l-to)
  801. if arr := r.checkStdArrayObj(o); arr != nil {
  802. if count > 0 {
  803. copy(arr.values[to:to+count], arr.values[from:from+count])
  804. }
  805. return o
  806. }
  807. if from < to && to < from+count {
  808. dir = -1
  809. from = from + count - 1
  810. to = to + count - 1
  811. } else {
  812. dir = 1
  813. }
  814. for count > 0 {
  815. if o.self.hasPropertyIdx(valueInt(from)) {
  816. o.self.setOwnIdx(valueInt(to), o.self.getIdx(valueInt(from), nil), true)
  817. } else {
  818. o.self.deleteIdx(valueInt(to), true)
  819. }
  820. from += dir
  821. to += dir
  822. count--
  823. }
  824. return o
  825. }
  826. func (r *Runtime) arrayproto_entries(call FunctionCall) Value {
  827. return r.createArrayIterator(call.This.ToObject(r), iterationKindKeyValue)
  828. }
  829. func (r *Runtime) arrayproto_fill(call FunctionCall) Value {
  830. o := call.This.ToObject(r)
  831. l := toLength(o.self.getStr("length", nil))
  832. k := relToIdx(call.Argument(1).ToInteger(), l)
  833. var relEnd int64
  834. if endArg := call.Argument(2); endArg != _undefined {
  835. relEnd = endArg.ToInteger()
  836. } else {
  837. relEnd = l
  838. }
  839. final := relToIdx(relEnd, l)
  840. value := call.Argument(0)
  841. if arr := r.checkStdArrayObj(o); arr != nil {
  842. for ; k < final; k++ {
  843. arr.values[k] = value
  844. }
  845. } else {
  846. for ; k < final; k++ {
  847. o.self.setOwnIdx(valueInt(k), value, true)
  848. }
  849. }
  850. return o
  851. }
  852. func (r *Runtime) arrayproto_find(call FunctionCall) Value {
  853. o := call.This.ToObject(r)
  854. l := toLength(o.self.getStr("length", nil))
  855. predicate := r.toCallable(call.Argument(0))
  856. fc := FunctionCall{
  857. This: call.Argument(1),
  858. Arguments: []Value{nil, nil, o},
  859. }
  860. for k := int64(0); k < l; k++ {
  861. idx := valueInt(k)
  862. kValue := o.self.getIdx(idx, nil)
  863. fc.Arguments[0], fc.Arguments[1] = kValue, idx
  864. if predicate(fc).ToBoolean() {
  865. return kValue
  866. }
  867. }
  868. return _undefined
  869. }
  870. func (r *Runtime) arrayproto_findIndex(call FunctionCall) Value {
  871. o := call.This.ToObject(r)
  872. l := toLength(o.self.getStr("length", nil))
  873. predicate := r.toCallable(call.Argument(0))
  874. fc := FunctionCall{
  875. This: call.Argument(1),
  876. Arguments: []Value{nil, nil, o},
  877. }
  878. for k := int64(0); k < l; k++ {
  879. idx := valueInt(k)
  880. kValue := o.self.getIdx(idx, nil)
  881. fc.Arguments[0], fc.Arguments[1] = kValue, idx
  882. if predicate(fc).ToBoolean() {
  883. return idx
  884. }
  885. }
  886. return valueInt(-1)
  887. }
  888. func (r *Runtime) checkStdArrayObj(obj *Object) *arrayObject {
  889. if arr, ok := obj.self.(*arrayObject); ok &&
  890. arr.propValueCount == 0 &&
  891. arr.length == uint32(len(arr.values)) &&
  892. uint32(arr.objCount) == arr.length {
  893. return arr
  894. }
  895. return nil
  896. }
  897. func (r *Runtime) checkStdArray(v Value) *arrayObject {
  898. if obj, ok := v.(*Object); ok {
  899. return r.checkStdArrayObj(obj)
  900. }
  901. return nil
  902. }
  903. func (r *Runtime) checkStdArrayIter(v Value) *arrayObject {
  904. if arr := r.checkStdArray(v); arr != nil &&
  905. arr.getSym(symIterator, nil) == r.global.arrayValues {
  906. return arr
  907. }
  908. return nil
  909. }
  910. func (r *Runtime) array_from(call FunctionCall) Value {
  911. var mapFn func(FunctionCall) Value
  912. if mapFnArg := call.Argument(1); mapFnArg != _undefined {
  913. if mapFnObj, ok := mapFnArg.(*Object); ok {
  914. if fn, ok := mapFnObj.self.assertCallable(); ok {
  915. mapFn = fn
  916. }
  917. }
  918. if mapFn == nil {
  919. panic(r.NewTypeError("%s is not a function", mapFnArg))
  920. }
  921. }
  922. t := call.Argument(2)
  923. items := call.Argument(0)
  924. if mapFn == nil && call.This == r.global.Array { // mapFn may mutate the array
  925. if arr := r.checkStdArrayIter(items); arr != nil {
  926. items := make([]Value, len(arr.values))
  927. copy(items, arr.values)
  928. return r.newArrayValues(items)
  929. }
  930. }
  931. var ctor func(args []Value, newTarget *Object) *Object
  932. if call.This != r.global.Array {
  933. if o, ok := call.This.(*Object); ok {
  934. if c := o.self.assertConstructor(); c != nil {
  935. ctor = c
  936. }
  937. }
  938. }
  939. var arr *Object
  940. if usingIterator := toMethod(r.getV(items, symIterator)); usingIterator != nil {
  941. if ctor != nil {
  942. arr = ctor([]Value{}, nil)
  943. } else {
  944. arr = r.newArrayValues(nil)
  945. }
  946. iter := r.getIterator(items, usingIterator)
  947. if mapFn == nil {
  948. if a := r.checkStdArrayObj(arr); a != nil {
  949. var values []Value
  950. r.iterate(iter, func(val Value) {
  951. values = append(values, val)
  952. })
  953. setArrayValues(a, values)
  954. return arr
  955. }
  956. }
  957. k := int64(0)
  958. r.iterate(iter, func(val Value) {
  959. if mapFn != nil {
  960. val = mapFn(FunctionCall{This: t, Arguments: []Value{val, intToValue(k)}})
  961. }
  962. createDataPropertyOrThrow(arr, intToValue(k), val)
  963. k++
  964. })
  965. arr.self.setOwnStr("length", intToValue(k), true)
  966. } else {
  967. arrayLike := items.ToObject(r)
  968. l := toLength(arrayLike.self.getStr("length", nil))
  969. if ctor != nil {
  970. arr = ctor([]Value{intToValue(l)}, nil)
  971. } else {
  972. arr = r.newArrayValues(nil)
  973. }
  974. if mapFn == nil {
  975. if a := r.checkStdArrayObj(arr); a != nil {
  976. values := make([]Value, l)
  977. for k := int64(0); k < l; k++ {
  978. values[k] = nilSafe(arrayLike.self.getIdx(valueInt(k), nil))
  979. }
  980. setArrayValues(a, values)
  981. return arr
  982. }
  983. }
  984. for k := int64(0); k < l; k++ {
  985. idx := valueInt(k)
  986. item := arrayLike.self.getIdx(idx, nil)
  987. if mapFn != nil {
  988. item = mapFn(FunctionCall{This: t, Arguments: []Value{item, idx}})
  989. } else {
  990. item = nilSafe(item)
  991. }
  992. createDataPropertyOrThrow(arr, idx, item)
  993. }
  994. arr.self.setOwnStr("length", intToValue(l), true)
  995. }
  996. return arr
  997. }
  998. func (r *Runtime) array_isArray(call FunctionCall) Value {
  999. if o, ok := call.Argument(0).(*Object); ok {
  1000. if isArray(o) {
  1001. return valueTrue
  1002. }
  1003. }
  1004. return valueFalse
  1005. }
  1006. func (r *Runtime) array_of(call FunctionCall) Value {
  1007. var ctor func(args []Value, newTarget *Object) *Object
  1008. if call.This != r.global.Array {
  1009. if o, ok := call.This.(*Object); ok {
  1010. if c := o.self.assertConstructor(); c != nil {
  1011. ctor = c
  1012. }
  1013. }
  1014. }
  1015. if ctor == nil {
  1016. values := make([]Value, len(call.Arguments))
  1017. copy(values, call.Arguments)
  1018. return r.newArrayValues(values)
  1019. }
  1020. l := intToValue(int64(len(call.Arguments)))
  1021. arr := ctor([]Value{l}, nil)
  1022. for i, val := range call.Arguments {
  1023. createDataPropertyOrThrow(arr, intToValue(int64(i)), val)
  1024. }
  1025. arr.self.setOwnStr("length", l, true)
  1026. return arr
  1027. }
  1028. func (r *Runtime) arrayIterProto_next(call FunctionCall) Value {
  1029. thisObj := r.toObject(call.This)
  1030. if iter, ok := thisObj.self.(*arrayIterObject); ok {
  1031. return iter.next()
  1032. }
  1033. panic(r.NewTypeError("Method Array Iterator.prototype.next called on incompatible receiver %s", thisObj.String()))
  1034. }
  1035. func (r *Runtime) createArrayProto(val *Object) objectImpl {
  1036. o := &arrayObject{
  1037. baseObject: baseObject{
  1038. class: classArray,
  1039. val: val,
  1040. extensible: true,
  1041. prototype: r.global.ObjectPrototype,
  1042. },
  1043. }
  1044. o.init()
  1045. o._putProp("constructor", r.global.Array, true, false, true)
  1046. o._putProp("concat", r.newNativeFunc(r.arrayproto_concat, nil, "concat", nil, 1), true, false, true)
  1047. o._putProp("copyWithin", r.newNativeFunc(r.arrayproto_copyWithin, nil, "copyWithin", nil, 2), true, false, true)
  1048. o._putProp("entries", r.newNativeFunc(r.arrayproto_entries, nil, "entries", nil, 0), true, false, true)
  1049. o._putProp("every", r.newNativeFunc(r.arrayproto_every, nil, "every", nil, 1), true, false, true)
  1050. o._putProp("fill", r.newNativeFunc(r.arrayproto_fill, nil, "fill", nil, 1), true, false, true)
  1051. o._putProp("filter", r.newNativeFunc(r.arrayproto_filter, nil, "filter", nil, 1), true, false, true)
  1052. o._putProp("find", r.newNativeFunc(r.arrayproto_find, nil, "find", nil, 1), true, false, true)
  1053. o._putProp("findIndex", r.newNativeFunc(r.arrayproto_findIndex, nil, "findIndex", nil, 1), true, false, true)
  1054. o._putProp("forEach", r.newNativeFunc(r.arrayproto_forEach, nil, "forEach", nil, 1), true, false, true)
  1055. o._putProp("indexOf", r.newNativeFunc(r.arrayproto_indexOf, nil, "indexOf", nil, 1), true, false, true)
  1056. o._putProp("join", r.newNativeFunc(r.arrayproto_join, nil, "join", nil, 1), true, false, true)
  1057. o._putProp("keys", r.newNativeFunc(r.arrayproto_keys, nil, "keys", nil, 0), true, false, true)
  1058. o._putProp("lastIndexOf", r.newNativeFunc(r.arrayproto_lastIndexOf, nil, "lastIndexOf", nil, 1), true, false, true)
  1059. o._putProp("map", r.newNativeFunc(r.arrayproto_map, nil, "map", nil, 1), true, false, true)
  1060. o._putProp("pop", r.newNativeFunc(r.arrayproto_pop, nil, "pop", nil, 0), true, false, true)
  1061. o._putProp("push", r.newNativeFunc(r.arrayproto_push, nil, "push", nil, 1), true, false, true)
  1062. o._putProp("reduce", r.newNativeFunc(r.arrayproto_reduce, nil, "reduce", nil, 1), true, false, true)
  1063. o._putProp("reduceRight", r.newNativeFunc(r.arrayproto_reduceRight, nil, "reduceRight", nil, 1), true, false, true)
  1064. o._putProp("reverse", r.newNativeFunc(r.arrayproto_reverse, nil, "reverse", nil, 0), true, false, true)
  1065. o._putProp("shift", r.newNativeFunc(r.arrayproto_shift, nil, "shift", nil, 0), true, false, true)
  1066. o._putProp("slice", r.newNativeFunc(r.arrayproto_slice, nil, "slice", nil, 2), true, false, true)
  1067. o._putProp("some", r.newNativeFunc(r.arrayproto_some, nil, "some", nil, 1), true, false, true)
  1068. o._putProp("sort", r.newNativeFunc(r.arrayproto_sort, nil, "sort", nil, 1), true, false, true)
  1069. o._putProp("splice", r.newNativeFunc(r.arrayproto_splice, nil, "splice", nil, 2), true, false, true)
  1070. o._putProp("toLocaleString", r.newNativeFunc(r.arrayproto_toLocaleString, nil, "toLocaleString", nil, 0), true, false, true)
  1071. o._putProp("toString", r.newNativeFunc(r.arrayproto_toString, nil, "toString", nil, 0), true, false, true)
  1072. o._putProp("unshift", r.newNativeFunc(r.arrayproto_unshift, nil, "unshift", nil, 1), true, false, true)
  1073. valuesFunc := r.newNativeFunc(r.arrayproto_values, nil, "values", nil, 0)
  1074. r.global.arrayValues = valuesFunc
  1075. o._putProp("values", valuesFunc, true, false, true)
  1076. o._putSym(symIterator, valueProp(valuesFunc, true, false, true))
  1077. bl := r.NewObject()
  1078. bl.self.setOwnStr("copyWithin", valueTrue, true)
  1079. bl.self.setOwnStr("entries", valueTrue, true)
  1080. bl.self.setOwnStr("fill", valueTrue, true)
  1081. bl.self.setOwnStr("find", valueTrue, true)
  1082. bl.self.setOwnStr("findIndex", valueTrue, true)
  1083. bl.self.setOwnStr("keys", valueTrue, true)
  1084. bl.self.setOwnStr("values", valueTrue, true)
  1085. o._putSym(symUnscopables, valueProp(bl, false, false, true))
  1086. return o
  1087. }
  1088. func (r *Runtime) createArray(val *Object) objectImpl {
  1089. o := r.newNativeFuncConstructObj(val, r.builtin_newArray, "Array", r.global.ArrayPrototype, 1)
  1090. o._putProp("from", r.newNativeFunc(r.array_from, nil, "from", nil, 1), true, false, true)
  1091. o._putProp("isArray", r.newNativeFunc(r.array_isArray, nil, "isArray", nil, 1), true, false, true)
  1092. o._putProp("of", r.newNativeFunc(r.array_of, nil, "of", nil, 0), true, false, true)
  1093. o._putSym(symSpecies, &valueProperty{
  1094. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1095. accessor: true,
  1096. configurable: true,
  1097. })
  1098. return o
  1099. }
  1100. func (r *Runtime) createArrayIterProto(val *Object) objectImpl {
  1101. o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)
  1102. o._putProp("next", r.newNativeFunc(r.arrayIterProto_next, nil, "next", nil, 0), true, false, true)
  1103. o._putSym(symToStringTag, valueProp(asciiString(classArrayIterator), false, false, true))
  1104. return o
  1105. }
  1106. func (r *Runtime) initArray() {
  1107. r.global.ArrayIteratorPrototype = r.newLazyObject(r.createArrayIterProto)
  1108. //r.global.ArrayPrototype = r.newArray(r.global.ObjectPrototype).val
  1109. //o := r.global.ArrayPrototype.self
  1110. r.global.ArrayPrototype = r.newLazyObject(r.createArrayProto)
  1111. //r.global.Array = r.newNativeFuncConstruct(r.builtin_newArray, "Array", r.global.ArrayPrototype, 1)
  1112. //o = r.global.Array.self
  1113. //o._putProp("isArray", r.newNativeFunc(r.array_isArray, nil, "isArray", nil, 1), true, false, true)
  1114. r.global.Array = r.newLazyObject(r.createArray)
  1115. r.addToGlobal("Array", r.global.Array)
  1116. }
  1117. type sortable interface {
  1118. sortLen() int64
  1119. sortGet(int64) Value
  1120. swap(int64, int64)
  1121. }
  1122. type arraySortCtx struct {
  1123. obj sortable
  1124. compare func(FunctionCall) Value
  1125. }
  1126. func (ctx *arraySortCtx) sortCompare(x, y Value) int {
  1127. if x == nil && y == nil {
  1128. return 0
  1129. }
  1130. if x == nil {
  1131. return 1
  1132. }
  1133. if y == nil {
  1134. return -1
  1135. }
  1136. if x == _undefined && y == _undefined {
  1137. return 0
  1138. }
  1139. if x == _undefined {
  1140. return 1
  1141. }
  1142. if y == _undefined {
  1143. return -1
  1144. }
  1145. if ctx.compare != nil {
  1146. return int(ctx.compare(FunctionCall{
  1147. This: _undefined,
  1148. Arguments: []Value{x, y},
  1149. }).ToInteger())
  1150. }
  1151. return strings.Compare(x.String(), y.String())
  1152. }
  1153. // sort.Interface
  1154. func (a *arraySortCtx) Len() int {
  1155. return int(a.obj.sortLen())
  1156. }
  1157. func (a *arraySortCtx) Less(j, k int) bool {
  1158. return a.sortCompare(a.obj.sortGet(int64(j)), a.obj.sortGet(int64(k))) < 0
  1159. }
  1160. func (a *arraySortCtx) Swap(j, k int) {
  1161. a.obj.swap(int64(j), int64(k))
  1162. }