builtin_array.go 35 KB

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