builtin_array.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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
  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. if aLength+length >= maxInt {
  242. panic(r.NewTypeError("Invalid array length"))
  243. }
  244. for i := int64(0); i < length; i++ {
  245. v := obj.self.getIdx(valueInt(i), nil)
  246. if v != nil {
  247. createDataPropertyOrThrow(a, intToValue(aLength), v)
  248. }
  249. aLength++
  250. }
  251. } else {
  252. createDataPropertyOrThrow(a, intToValue(aLength), item)
  253. aLength++
  254. }
  255. a.self.setOwnStr("length", intToValue(aLength), true)
  256. }
  257. func (r *Runtime) arrayproto_concat(call FunctionCall) Value {
  258. obj := call.This.ToObject(r)
  259. a := arraySpeciesCreate(obj, 0)
  260. r.arrayproto_concat_append(a, call.This.ToObject(r))
  261. for _, item := range call.Arguments {
  262. r.arrayproto_concat_append(a, item)
  263. }
  264. return a
  265. }
  266. func (r *Runtime) arrayproto_slice(call FunctionCall) Value {
  267. o := call.This.ToObject(r)
  268. length := toLength(o.self.getStr("length", nil))
  269. start := relToIdx(call.Argument(0).ToInteger(), length)
  270. var end int64
  271. if endArg := call.Argument(1); endArg != _undefined {
  272. end = endArg.ToInteger()
  273. } else {
  274. end = length
  275. }
  276. end = relToIdx(end, length)
  277. count := end - start
  278. if count < 0 {
  279. count = 0
  280. }
  281. a := arraySpeciesCreate(o, count)
  282. if src := r.checkStdArrayObj(o); src != nil {
  283. if dst, ok := a.self.(*arrayObject); ok {
  284. values := make([]Value, count)
  285. copy(values, src.values[start:])
  286. setArrayValues(dst, values)
  287. return a
  288. }
  289. }
  290. n := int64(0)
  291. for start < end {
  292. p := o.self.getIdx(valueInt(start), nil)
  293. if p != nil {
  294. createDataPropertyOrThrow(a, valueInt(n), p)
  295. }
  296. start++
  297. n++
  298. }
  299. return a
  300. }
  301. func (r *Runtime) arrayproto_sort(call FunctionCall) Value {
  302. o := call.This.ToObject(r)
  303. var compareFn func(FunctionCall) Value
  304. arg := call.Argument(0)
  305. if arg != _undefined {
  306. if arg, ok := call.Argument(0).(*Object); ok {
  307. compareFn, _ = arg.self.assertCallable()
  308. }
  309. if compareFn == nil {
  310. panic(r.NewTypeError("The comparison function must be either a function or undefined"))
  311. }
  312. }
  313. ctx := arraySortCtx{
  314. obj: o.self,
  315. compare: compareFn,
  316. }
  317. sort.Stable(&ctx)
  318. return o
  319. }
  320. func (r *Runtime) arrayproto_splice(call FunctionCall) Value {
  321. o := call.This.ToObject(r)
  322. length := toLength(o.self.getStr("length", nil))
  323. actualStart := relToIdx(call.Argument(0).ToInteger(), length)
  324. var actualDeleteCount int64
  325. switch len(call.Arguments) {
  326. case 0:
  327. case 1:
  328. actualDeleteCount = length - actualStart
  329. default:
  330. actualDeleteCount = min(max(call.Argument(1).ToInteger(), 0), length-actualStart)
  331. }
  332. a := arraySpeciesCreate(o, actualDeleteCount)
  333. itemCount := max(int64(len(call.Arguments)-2), 0)
  334. newLength := length - actualDeleteCount + itemCount
  335. if src := r.checkStdArrayObj(o); src != nil {
  336. if dst, ok := a.self.(*arrayObject); ok {
  337. values := make([]Value, actualDeleteCount)
  338. copy(values, src.values[actualStart:])
  339. setArrayValues(dst, values)
  340. } else {
  341. for k := int64(0); k < actualDeleteCount; k++ {
  342. createDataPropertyOrThrow(a, intToValue(k), src.values[k+actualStart])
  343. }
  344. }
  345. var values []Value
  346. if itemCount < actualDeleteCount {
  347. values = src.values
  348. copy(values[actualStart+itemCount:], values[actualStart+actualDeleteCount:])
  349. tail := values[newLength:]
  350. for k := range tail {
  351. tail[k] = nil
  352. }
  353. values = values[:newLength]
  354. } else if itemCount > actualDeleteCount {
  355. if int64(cap(src.values)) >= newLength {
  356. values = src.values[:newLength]
  357. copy(values[actualStart+itemCount:], values[actualStart+actualDeleteCount:length])
  358. } else {
  359. values = make([]Value, newLength)
  360. copy(values, src.values[:actualStart])
  361. copy(values[actualStart+itemCount:], src.values[actualStart+actualDeleteCount:])
  362. }
  363. } else {
  364. values = src.values
  365. }
  366. if itemCount > 0 {
  367. copy(values[actualStart:], call.Arguments[2:])
  368. }
  369. src.values = values
  370. src.objCount = len(values)
  371. } else {
  372. for k := int64(0); k < actualDeleteCount; k++ {
  373. from := valueInt(k + actualStart)
  374. if o.self.hasPropertyIdx(from) {
  375. createDataPropertyOrThrow(a, valueInt(k), o.self.getIdx(from, nil))
  376. }
  377. }
  378. if itemCount < actualDeleteCount {
  379. for k := actualStart; k < length-actualDeleteCount; k++ {
  380. from := valueInt(k + actualDeleteCount)
  381. to := valueInt(k + itemCount)
  382. if o.self.hasPropertyIdx(from) {
  383. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  384. } else {
  385. o.self.deleteIdx(to, true)
  386. }
  387. }
  388. for k := length; k > length-actualDeleteCount+itemCount; k-- {
  389. o.self.deleteIdx(valueInt(k-1), true)
  390. }
  391. } else if itemCount > actualDeleteCount {
  392. for k := length - actualDeleteCount; k > actualStart; k-- {
  393. from := valueInt(k + actualDeleteCount - 1)
  394. to := valueInt(k + itemCount - 1)
  395. if o.self.hasPropertyIdx(from) {
  396. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  397. } else {
  398. o.self.deleteIdx(to, true)
  399. }
  400. }
  401. }
  402. if itemCount > 0 {
  403. for i, item := range call.Arguments[2:] {
  404. o.self.setOwnIdx(valueInt(actualStart+int64(i)), item, true)
  405. }
  406. }
  407. }
  408. o.self.setOwnStr("length", intToValue(newLength), true)
  409. return a
  410. }
  411. func (r *Runtime) arrayproto_unshift(call FunctionCall) Value {
  412. o := call.This.ToObject(r)
  413. length := toLength(o.self.getStr("length", nil))
  414. argCount := int64(len(call.Arguments))
  415. newLen := intToValue(length + argCount)
  416. newSize := length + argCount
  417. if arr := r.checkStdArrayObj(o); arr != nil && newSize < math.MaxUint32 {
  418. if int64(cap(arr.values)) >= newSize {
  419. arr.values = arr.values[:newSize]
  420. copy(arr.values[argCount:], arr.values[:length])
  421. } else {
  422. values := make([]Value, newSize)
  423. copy(values[argCount:], arr.values)
  424. arr.values = values
  425. }
  426. copy(arr.values, call.Arguments)
  427. arr.objCount = int(arr.length)
  428. } else {
  429. for k := length - 1; k >= 0; k-- {
  430. from := valueInt(k)
  431. to := valueInt(k + argCount)
  432. if o.self.hasPropertyIdx(from) {
  433. o.self.setOwnIdx(to, o.self.getIdx(from, nil), true)
  434. } else {
  435. o.self.deleteIdx(to, true)
  436. }
  437. }
  438. for k, arg := range call.Arguments {
  439. o.self.setOwnIdx(valueInt(int64(k)), arg, true)
  440. }
  441. }
  442. o.self.setOwnStr("length", newLen, true)
  443. return newLen
  444. }
  445. func (r *Runtime) arrayproto_indexOf(call FunctionCall) Value {
  446. o := call.This.ToObject(r)
  447. length := toLength(o.self.getStr("length", nil))
  448. if length == 0 {
  449. return intToValue(-1)
  450. }
  451. n := call.Argument(1).ToInteger()
  452. if n >= length {
  453. return intToValue(-1)
  454. }
  455. if n < 0 {
  456. n = max(length+n, 0)
  457. }
  458. searchElement := call.Argument(0)
  459. if arr := r.checkStdArrayObj(o); arr != nil {
  460. for i, val := range arr.values[n:] {
  461. if searchElement.StrictEquals(val) {
  462. return intToValue(n + int64(i))
  463. }
  464. }
  465. return intToValue(-1)
  466. }
  467. for ; n < length; n++ {
  468. idx := valueInt(n)
  469. if o.self.hasPropertyIdx(idx) {
  470. if val := o.self.getIdx(idx, nil); val != nil {
  471. if searchElement.StrictEquals(val) {
  472. return idx
  473. }
  474. }
  475. }
  476. }
  477. return intToValue(-1)
  478. }
  479. func (r *Runtime) arrayproto_includes(call FunctionCall) Value {
  480. o := call.This.ToObject(r)
  481. length := toLength(o.self.getStr("length", nil))
  482. if length == 0 {
  483. return valueFalse
  484. }
  485. n := call.Argument(1).ToInteger()
  486. if n >= length {
  487. return valueFalse
  488. }
  489. if n < 0 {
  490. n = max(length+n, 0)
  491. }
  492. searchElement := call.Argument(0)
  493. if searchElement == _negativeZero {
  494. searchElement = _positiveZero
  495. }
  496. if arr := r.checkStdArrayObj(o); arr != nil {
  497. for _, val := range arr.values[n:] {
  498. if searchElement.SameAs(val) {
  499. return valueTrue
  500. }
  501. }
  502. return valueFalse
  503. }
  504. for ; n < length; n++ {
  505. idx := valueInt(n)
  506. val := nilSafe(o.self.getIdx(idx, nil))
  507. if searchElement.SameAs(val) {
  508. return valueTrue
  509. }
  510. }
  511. return valueFalse
  512. }
  513. func (r *Runtime) arrayproto_lastIndexOf(call FunctionCall) Value {
  514. o := call.This.ToObject(r)
  515. length := toLength(o.self.getStr("length", nil))
  516. if length == 0 {
  517. return intToValue(-1)
  518. }
  519. var fromIndex int64
  520. if len(call.Arguments) < 2 {
  521. fromIndex = length - 1
  522. } else {
  523. fromIndex = call.Argument(1).ToInteger()
  524. if fromIndex >= 0 {
  525. fromIndex = min(fromIndex, length-1)
  526. } else {
  527. fromIndex += length
  528. }
  529. }
  530. searchElement := call.Argument(0)
  531. if arr := r.checkStdArrayObj(o); arr != nil {
  532. vals := arr.values
  533. for k := fromIndex; k >= 0; k-- {
  534. if v := vals[k]; v != nil && searchElement.StrictEquals(v) {
  535. return intToValue(k)
  536. }
  537. }
  538. return intToValue(-1)
  539. }
  540. for k := fromIndex; k >= 0; k-- {
  541. idx := valueInt(k)
  542. if o.self.hasPropertyIdx(idx) {
  543. if val := o.self.getIdx(idx, nil); val != nil {
  544. if searchElement.StrictEquals(val) {
  545. return idx
  546. }
  547. }
  548. }
  549. }
  550. return intToValue(-1)
  551. }
  552. func (r *Runtime) arrayproto_every(call FunctionCall) Value {
  553. o := call.This.ToObject(r)
  554. length := toLength(o.self.getStr("length", nil))
  555. callbackFn := r.toCallable(call.Argument(0))
  556. fc := FunctionCall{
  557. This: call.Argument(1),
  558. Arguments: []Value{nil, nil, o},
  559. }
  560. for k := int64(0); k < length; k++ {
  561. idx := valueInt(k)
  562. if val := o.self.getIdx(idx, nil); val != nil {
  563. fc.Arguments[0] = val
  564. fc.Arguments[1] = idx
  565. if !callbackFn(fc).ToBoolean() {
  566. return valueFalse
  567. }
  568. }
  569. }
  570. return valueTrue
  571. }
  572. func (r *Runtime) arrayproto_some(call FunctionCall) Value {
  573. o := call.This.ToObject(r)
  574. length := toLength(o.self.getStr("length", nil))
  575. callbackFn := r.toCallable(call.Argument(0))
  576. fc := FunctionCall{
  577. This: call.Argument(1),
  578. Arguments: []Value{nil, nil, o},
  579. }
  580. for k := int64(0); k < length; k++ {
  581. idx := valueInt(k)
  582. if val := o.self.getIdx(idx, nil); val != nil {
  583. fc.Arguments[0] = val
  584. fc.Arguments[1] = idx
  585. if callbackFn(fc).ToBoolean() {
  586. return valueTrue
  587. }
  588. }
  589. }
  590. return valueFalse
  591. }
  592. func (r *Runtime) arrayproto_forEach(call FunctionCall) Value {
  593. o := call.This.ToObject(r)
  594. length := toLength(o.self.getStr("length", nil))
  595. callbackFn := r.toCallable(call.Argument(0))
  596. fc := FunctionCall{
  597. This: call.Argument(1),
  598. Arguments: []Value{nil, nil, o},
  599. }
  600. for k := int64(0); k < length; k++ {
  601. idx := valueInt(k)
  602. if val := o.self.getIdx(idx, nil); val != nil {
  603. fc.Arguments[0] = val
  604. fc.Arguments[1] = idx
  605. callbackFn(fc)
  606. }
  607. }
  608. return _undefined
  609. }
  610. func (r *Runtime) arrayproto_map(call FunctionCall) Value {
  611. o := call.This.ToObject(r)
  612. length := toLength(o.self.getStr("length", nil))
  613. callbackFn := r.toCallable(call.Argument(0))
  614. fc := FunctionCall{
  615. This: call.Argument(1),
  616. Arguments: []Value{nil, nil, o},
  617. }
  618. a := arraySpeciesCreate(o, length)
  619. if _, stdSrc := o.self.(*arrayObject); stdSrc {
  620. if arr, ok := a.self.(*arrayObject); ok {
  621. values := make([]Value, length)
  622. for k := int64(0); k < length; k++ {
  623. idx := valueInt(k)
  624. if val := o.self.getIdx(idx, nil); val != nil {
  625. fc.Arguments[0] = val
  626. fc.Arguments[1] = idx
  627. values[k] = callbackFn(fc)
  628. }
  629. }
  630. setArrayValues(arr, values)
  631. return a
  632. }
  633. }
  634. for k := int64(0); k < length; k++ {
  635. idx := valueInt(k)
  636. if val := o.self.getIdx(idx, nil); val != nil {
  637. fc.Arguments[0] = val
  638. fc.Arguments[1] = idx
  639. createDataPropertyOrThrow(a, idx, callbackFn(fc))
  640. }
  641. }
  642. return a
  643. }
  644. func (r *Runtime) arrayproto_filter(call FunctionCall) Value {
  645. o := call.This.ToObject(r)
  646. length := toLength(o.self.getStr("length", nil))
  647. callbackFn := call.Argument(0).ToObject(r)
  648. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  649. a := arraySpeciesCreate(o, 0)
  650. fc := FunctionCall{
  651. This: call.Argument(1),
  652. Arguments: []Value{nil, nil, o},
  653. }
  654. if _, stdSrc := o.self.(*arrayObject); stdSrc {
  655. if arr := r.checkStdArrayObj(a); arr != nil {
  656. var values []Value
  657. for k := int64(0); k < length; k++ {
  658. idx := valueInt(k)
  659. if val := o.self.getIdx(idx, nil); val != nil {
  660. fc.Arguments[0] = val
  661. fc.Arguments[1] = idx
  662. if callbackFn(fc).ToBoolean() {
  663. values = append(values, val)
  664. }
  665. }
  666. }
  667. setArrayValues(arr, values)
  668. return a
  669. }
  670. }
  671. to := int64(0)
  672. for k := int64(0); k < length; k++ {
  673. idx := valueInt(k)
  674. if val := o.self.getIdx(idx, nil); val != nil {
  675. fc.Arguments[0] = val
  676. fc.Arguments[1] = idx
  677. if callbackFn(fc).ToBoolean() {
  678. createDataPropertyOrThrow(a, intToValue(to), val)
  679. to++
  680. }
  681. }
  682. }
  683. return a
  684. } else {
  685. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  686. }
  687. panic("unreachable")
  688. }
  689. func (r *Runtime) arrayproto_reduce(call FunctionCall) Value {
  690. o := call.This.ToObject(r)
  691. length := toLength(o.self.getStr("length", nil))
  692. callbackFn := call.Argument(0).ToObject(r)
  693. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  694. fc := FunctionCall{
  695. This: _undefined,
  696. Arguments: []Value{nil, nil, nil, o},
  697. }
  698. var k int64
  699. if len(call.Arguments) >= 2 {
  700. fc.Arguments[0] = call.Argument(1)
  701. } else {
  702. for ; k < length; k++ {
  703. idx := valueInt(k)
  704. if val := o.self.getIdx(idx, nil); val != nil {
  705. fc.Arguments[0] = val
  706. break
  707. }
  708. }
  709. if fc.Arguments[0] == nil {
  710. r.typeErrorResult(true, "No initial value")
  711. panic("unreachable")
  712. }
  713. k++
  714. }
  715. for ; k < length; k++ {
  716. idx := valueInt(k)
  717. if val := o.self.getIdx(idx, nil); val != nil {
  718. fc.Arguments[1] = val
  719. fc.Arguments[2] = idx
  720. fc.Arguments[0] = callbackFn(fc)
  721. }
  722. }
  723. return fc.Arguments[0]
  724. } else {
  725. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  726. }
  727. panic("unreachable")
  728. }
  729. func (r *Runtime) arrayproto_reduceRight(call FunctionCall) Value {
  730. o := call.This.ToObject(r)
  731. length := toLength(o.self.getStr("length", nil))
  732. callbackFn := call.Argument(0).ToObject(r)
  733. if callbackFn, ok := callbackFn.self.assertCallable(); ok {
  734. fc := FunctionCall{
  735. This: _undefined,
  736. Arguments: []Value{nil, nil, nil, o},
  737. }
  738. k := length - 1
  739. if len(call.Arguments) >= 2 {
  740. fc.Arguments[0] = call.Argument(1)
  741. } else {
  742. for ; k >= 0; k-- {
  743. idx := valueInt(k)
  744. if val := o.self.getIdx(idx, nil); val != nil {
  745. fc.Arguments[0] = val
  746. break
  747. }
  748. }
  749. if fc.Arguments[0] == nil {
  750. r.typeErrorResult(true, "No initial value")
  751. panic("unreachable")
  752. }
  753. k--
  754. }
  755. for ; k >= 0; k-- {
  756. idx := valueInt(k)
  757. if val := o.self.getIdx(idx, nil); val != nil {
  758. fc.Arguments[1] = val
  759. fc.Arguments[2] = idx
  760. fc.Arguments[0] = callbackFn(fc)
  761. }
  762. }
  763. return fc.Arguments[0]
  764. } else {
  765. r.typeErrorResult(true, "%s is not a function", call.Argument(0))
  766. }
  767. panic("unreachable")
  768. }
  769. func arrayproto_reverse_generic_step(o *Object, lower, upper int64) {
  770. lowerP := valueInt(lower)
  771. upperP := valueInt(upper)
  772. lowerValue := o.self.getIdx(lowerP, nil)
  773. upperValue := o.self.getIdx(upperP, nil)
  774. if lowerValue != nil && upperValue != nil {
  775. o.self.setOwnIdx(lowerP, upperValue, true)
  776. o.self.setOwnIdx(upperP, lowerValue, true)
  777. } else if lowerValue == nil && upperValue != nil {
  778. o.self.setOwnIdx(lowerP, upperValue, true)
  779. o.self.deleteIdx(upperP, true)
  780. } else if lowerValue != nil && upperValue == nil {
  781. o.self.deleteIdx(lowerP, true)
  782. o.self.setOwnIdx(upperP, lowerValue, true)
  783. }
  784. }
  785. func (r *Runtime) arrayproto_reverse_generic(o *Object, start int64) {
  786. l := toLength(o.self.getStr("length", nil))
  787. middle := l / 2
  788. for lower := start; lower != middle; lower++ {
  789. arrayproto_reverse_generic_step(o, lower, l-lower-1)
  790. }
  791. }
  792. func (r *Runtime) arrayproto_reverse(call FunctionCall) Value {
  793. o := call.This.ToObject(r)
  794. if a := r.checkStdArrayObj(o); a != nil {
  795. l := len(a.values)
  796. middle := l / 2
  797. for lower := 0; lower != middle; lower++ {
  798. upper := l - lower - 1
  799. a.values[lower], a.values[upper] = a.values[upper], a.values[lower]
  800. }
  801. //TODO: go arrays
  802. } else {
  803. r.arrayproto_reverse_generic(o, 0)
  804. }
  805. return o
  806. }
  807. func (r *Runtime) arrayproto_shift(call FunctionCall) Value {
  808. o := call.This.ToObject(r)
  809. length := toLength(o.self.getStr("length", nil))
  810. if length == 0 {
  811. o.self.setOwnStr("length", intToValue(0), true)
  812. return _undefined
  813. }
  814. first := o.self.getIdx(valueInt(0), nil)
  815. for i := int64(1); i < length; i++ {
  816. v := o.self.getIdx(valueInt(i), nil)
  817. if v != nil {
  818. o.self.setOwnIdx(valueInt(i-1), v, true)
  819. } else {
  820. o.self.deleteIdx(valueInt(i-1), true)
  821. }
  822. }
  823. lv := valueInt(length - 1)
  824. o.self.deleteIdx(lv, true)
  825. o.self.setOwnStr("length", lv, true)
  826. return first
  827. }
  828. func (r *Runtime) arrayproto_values(call FunctionCall) Value {
  829. return r.createArrayIterator(call.This.ToObject(r), iterationKindValue)
  830. }
  831. func (r *Runtime) arrayproto_keys(call FunctionCall) Value {
  832. return r.createArrayIterator(call.This.ToObject(r), iterationKindKey)
  833. }
  834. func (r *Runtime) arrayproto_copyWithin(call FunctionCall) Value {
  835. o := call.This.ToObject(r)
  836. l := toLength(o.self.getStr("length", nil))
  837. var relEnd, dir int64
  838. to := relToIdx(call.Argument(0).ToInteger(), l)
  839. from := relToIdx(call.Argument(1).ToInteger(), l)
  840. if end := call.Argument(2); end != _undefined {
  841. relEnd = end.ToInteger()
  842. } else {
  843. relEnd = l
  844. }
  845. final := relToIdx(relEnd, l)
  846. count := min(final-from, l-to)
  847. if arr := r.checkStdArrayObj(o); arr != nil {
  848. if count > 0 {
  849. copy(arr.values[to:to+count], arr.values[from:from+count])
  850. }
  851. return o
  852. }
  853. if from < to && to < from+count {
  854. dir = -1
  855. from = from + count - 1
  856. to = to + count - 1
  857. } else {
  858. dir = 1
  859. }
  860. for count > 0 {
  861. if o.self.hasPropertyIdx(valueInt(from)) {
  862. o.self.setOwnIdx(valueInt(to), o.self.getIdx(valueInt(from), nil), true)
  863. } else {
  864. o.self.deleteIdx(valueInt(to), true)
  865. }
  866. from += dir
  867. to += dir
  868. count--
  869. }
  870. return o
  871. }
  872. func (r *Runtime) arrayproto_entries(call FunctionCall) Value {
  873. return r.createArrayIterator(call.This.ToObject(r), iterationKindKeyValue)
  874. }
  875. func (r *Runtime) arrayproto_fill(call FunctionCall) Value {
  876. o := call.This.ToObject(r)
  877. l := toLength(o.self.getStr("length", nil))
  878. k := relToIdx(call.Argument(1).ToInteger(), l)
  879. var relEnd int64
  880. if endArg := call.Argument(2); endArg != _undefined {
  881. relEnd = endArg.ToInteger()
  882. } else {
  883. relEnd = l
  884. }
  885. final := relToIdx(relEnd, l)
  886. value := call.Argument(0)
  887. if arr := r.checkStdArrayObj(o); arr != nil {
  888. for ; k < final; k++ {
  889. arr.values[k] = value
  890. }
  891. } else {
  892. for ; k < final; k++ {
  893. o.self.setOwnIdx(valueInt(k), value, true)
  894. }
  895. }
  896. return o
  897. }
  898. func (r *Runtime) arrayproto_find(call FunctionCall) Value {
  899. o := call.This.ToObject(r)
  900. l := toLength(o.self.getStr("length", nil))
  901. predicate := r.toCallable(call.Argument(0))
  902. fc := FunctionCall{
  903. This: call.Argument(1),
  904. Arguments: []Value{nil, nil, o},
  905. }
  906. for k := int64(0); k < l; k++ {
  907. idx := valueInt(k)
  908. kValue := o.self.getIdx(idx, nil)
  909. fc.Arguments[0], fc.Arguments[1] = kValue, idx
  910. if predicate(fc).ToBoolean() {
  911. return kValue
  912. }
  913. }
  914. return _undefined
  915. }
  916. func (r *Runtime) arrayproto_findIndex(call FunctionCall) Value {
  917. o := call.This.ToObject(r)
  918. l := toLength(o.self.getStr("length", nil))
  919. predicate := r.toCallable(call.Argument(0))
  920. fc := FunctionCall{
  921. This: call.Argument(1),
  922. Arguments: []Value{nil, nil, o},
  923. }
  924. for k := int64(0); k < l; k++ {
  925. idx := valueInt(k)
  926. kValue := o.self.getIdx(idx, nil)
  927. fc.Arguments[0], fc.Arguments[1] = kValue, idx
  928. if predicate(fc).ToBoolean() {
  929. return idx
  930. }
  931. }
  932. return intToValue(-1)
  933. }
  934. func (r *Runtime) arrayproto_flat(call FunctionCall) Value {
  935. o := call.This.ToObject(r)
  936. l := toLength(o.self.getStr("length", nil))
  937. depthNum := int64(1)
  938. if len(call.Arguments) > 0 {
  939. depthNum = call.Argument(0).ToInteger()
  940. }
  941. a := arraySpeciesCreate(o, 0)
  942. r.flattenIntoArray(a, o, l, 0, depthNum, nil, nil)
  943. return a
  944. }
  945. func (r *Runtime) flattenIntoArray(target, source *Object, sourceLen, start, depth int64, mapperFunction func(FunctionCall) Value, thisArg Value) int64 {
  946. targetIndex, sourceIndex := start, int64(0)
  947. for sourceIndex < sourceLen {
  948. p := intToValue(sourceIndex)
  949. if source.hasProperty(p.toString()) {
  950. element := source.get(p, source)
  951. if mapperFunction != nil {
  952. element = mapperFunction(FunctionCall{
  953. This: thisArg,
  954. Arguments: []Value{element, p, source},
  955. })
  956. }
  957. var elementArray *Object
  958. if depth > 0 {
  959. if elementObj, ok := element.(*Object); ok && isArray(elementObj) {
  960. elementArray = elementObj
  961. }
  962. }
  963. if elementArray != nil {
  964. elementLen := toLength(elementArray.self.getStr("length", nil))
  965. targetIndex = r.flattenIntoArray(target, elementArray, elementLen, targetIndex, depth-1, nil, nil)
  966. } else {
  967. if targetIndex >= maxInt-1 {
  968. panic(r.NewTypeError("Invalid array length"))
  969. }
  970. createDataPropertyOrThrow(target, intToValue(targetIndex), element)
  971. targetIndex++
  972. }
  973. }
  974. sourceIndex++
  975. }
  976. return targetIndex
  977. }
  978. func (r *Runtime) arrayproto_flatMap(call FunctionCall) Value {
  979. o := call.This.ToObject(r)
  980. l := toLength(o.self.getStr("length", nil))
  981. callbackFn := r.toCallable(call.Argument(0))
  982. thisArg := Undefined()
  983. if len(call.Arguments) > 1 {
  984. thisArg = call.Argument(1)
  985. }
  986. a := arraySpeciesCreate(o, 0)
  987. r.flattenIntoArray(a, o, l, 0, 1, callbackFn, thisArg)
  988. return a
  989. }
  990. func (r *Runtime) checkStdArrayObj(obj *Object) *arrayObject {
  991. if arr, ok := obj.self.(*arrayObject); ok &&
  992. arr.propValueCount == 0 &&
  993. arr.length == uint32(len(arr.values)) &&
  994. uint32(arr.objCount) == arr.length {
  995. return arr
  996. }
  997. return nil
  998. }
  999. func (r *Runtime) checkStdArray(v Value) *arrayObject {
  1000. if obj, ok := v.(*Object); ok {
  1001. return r.checkStdArrayObj(obj)
  1002. }
  1003. return nil
  1004. }
  1005. func (r *Runtime) checkStdArrayIter(v Value) *arrayObject {
  1006. if arr := r.checkStdArray(v); arr != nil &&
  1007. arr.getSym(SymIterator, nil) == r.global.arrayValues {
  1008. return arr
  1009. }
  1010. return nil
  1011. }
  1012. func (r *Runtime) array_from(call FunctionCall) Value {
  1013. var mapFn func(FunctionCall) Value
  1014. if mapFnArg := call.Argument(1); mapFnArg != _undefined {
  1015. if mapFnObj, ok := mapFnArg.(*Object); ok {
  1016. if fn, ok := mapFnObj.self.assertCallable(); ok {
  1017. mapFn = fn
  1018. }
  1019. }
  1020. if mapFn == nil {
  1021. panic(r.NewTypeError("%s is not a function", mapFnArg))
  1022. }
  1023. }
  1024. t := call.Argument(2)
  1025. items := call.Argument(0)
  1026. if mapFn == nil && call.This == r.global.Array { // mapFn may mutate the array
  1027. if arr := r.checkStdArrayIter(items); arr != nil {
  1028. items := make([]Value, len(arr.values))
  1029. copy(items, arr.values)
  1030. return r.newArrayValues(items)
  1031. }
  1032. }
  1033. var ctor func(args []Value, newTarget *Object) *Object
  1034. if call.This != r.global.Array {
  1035. if o, ok := call.This.(*Object); ok {
  1036. if c := o.self.assertConstructor(); c != nil {
  1037. ctor = c
  1038. }
  1039. }
  1040. }
  1041. var arr *Object
  1042. if usingIterator := toMethod(r.getV(items, SymIterator)); usingIterator != nil {
  1043. if ctor != nil {
  1044. arr = ctor([]Value{}, nil)
  1045. } else {
  1046. arr = r.newArrayValues(nil)
  1047. }
  1048. iter := r.getIterator(items, usingIterator)
  1049. if mapFn == nil {
  1050. if a := r.checkStdArrayObj(arr); a != nil {
  1051. var values []Value
  1052. r.iterate(iter, func(val Value) {
  1053. values = append(values, val)
  1054. })
  1055. setArrayValues(a, values)
  1056. return arr
  1057. }
  1058. }
  1059. k := int64(0)
  1060. r.iterate(iter, func(val Value) {
  1061. if mapFn != nil {
  1062. val = mapFn(FunctionCall{This: t, Arguments: []Value{val, intToValue(k)}})
  1063. }
  1064. createDataPropertyOrThrow(arr, intToValue(k), val)
  1065. k++
  1066. })
  1067. arr.self.setOwnStr("length", intToValue(k), true)
  1068. } else {
  1069. arrayLike := items.ToObject(r)
  1070. l := toLength(arrayLike.self.getStr("length", nil))
  1071. if ctor != nil {
  1072. arr = ctor([]Value{intToValue(l)}, nil)
  1073. } else {
  1074. arr = r.newArrayValues(nil)
  1075. }
  1076. if mapFn == nil {
  1077. if a := r.checkStdArrayObj(arr); a != nil {
  1078. values := make([]Value, l)
  1079. for k := int64(0); k < l; k++ {
  1080. values[k] = nilSafe(arrayLike.self.getIdx(valueInt(k), nil))
  1081. }
  1082. setArrayValues(a, values)
  1083. return arr
  1084. }
  1085. }
  1086. for k := int64(0); k < l; k++ {
  1087. idx := valueInt(k)
  1088. item := arrayLike.self.getIdx(idx, nil)
  1089. if mapFn != nil {
  1090. item = mapFn(FunctionCall{This: t, Arguments: []Value{item, idx}})
  1091. } else {
  1092. item = nilSafe(item)
  1093. }
  1094. createDataPropertyOrThrow(arr, idx, item)
  1095. }
  1096. arr.self.setOwnStr("length", intToValue(l), true)
  1097. }
  1098. return arr
  1099. }
  1100. func (r *Runtime) array_isArray(call FunctionCall) Value {
  1101. if o, ok := call.Argument(0).(*Object); ok {
  1102. if isArray(o) {
  1103. return valueTrue
  1104. }
  1105. }
  1106. return valueFalse
  1107. }
  1108. func (r *Runtime) array_of(call FunctionCall) Value {
  1109. var ctor func(args []Value, newTarget *Object) *Object
  1110. if call.This != r.global.Array {
  1111. if o, ok := call.This.(*Object); ok {
  1112. if c := o.self.assertConstructor(); c != nil {
  1113. ctor = c
  1114. }
  1115. }
  1116. }
  1117. if ctor == nil {
  1118. values := make([]Value, len(call.Arguments))
  1119. copy(values, call.Arguments)
  1120. return r.newArrayValues(values)
  1121. }
  1122. l := intToValue(int64(len(call.Arguments)))
  1123. arr := ctor([]Value{l}, nil)
  1124. for i, val := range call.Arguments {
  1125. createDataPropertyOrThrow(arr, intToValue(int64(i)), val)
  1126. }
  1127. arr.self.setOwnStr("length", l, true)
  1128. return arr
  1129. }
  1130. func (r *Runtime) arrayIterProto_next(call FunctionCall) Value {
  1131. thisObj := r.toObject(call.This)
  1132. if iter, ok := thisObj.self.(*arrayIterObject); ok {
  1133. return iter.next()
  1134. }
  1135. panic(r.NewTypeError("Method Array Iterator.prototype.next called on incompatible receiver %s", thisObj.String()))
  1136. }
  1137. func (r *Runtime) createArrayProto(val *Object) objectImpl {
  1138. o := &arrayObject{
  1139. baseObject: baseObject{
  1140. class: classArray,
  1141. val: val,
  1142. extensible: true,
  1143. prototype: r.global.ObjectPrototype,
  1144. },
  1145. }
  1146. o.init()
  1147. o._putProp("constructor", r.global.Array, true, false, true)
  1148. o._putProp("concat", r.newNativeFunc(r.arrayproto_concat, nil, "concat", nil, 1), true, false, true)
  1149. o._putProp("copyWithin", r.newNativeFunc(r.arrayproto_copyWithin, nil, "copyWithin", nil, 2), true, false, true)
  1150. o._putProp("entries", r.newNativeFunc(r.arrayproto_entries, nil, "entries", nil, 0), true, false, true)
  1151. o._putProp("every", r.newNativeFunc(r.arrayproto_every, nil, "every", nil, 1), true, false, true)
  1152. o._putProp("fill", r.newNativeFunc(r.arrayproto_fill, nil, "fill", nil, 1), true, false, true)
  1153. o._putProp("filter", r.newNativeFunc(r.arrayproto_filter, nil, "filter", nil, 1), true, false, true)
  1154. o._putProp("find", r.newNativeFunc(r.arrayproto_find, nil, "find", nil, 1), true, false, true)
  1155. o._putProp("findIndex", r.newNativeFunc(r.arrayproto_findIndex, nil, "findIndex", nil, 1), true, false, true)
  1156. o._putProp("flat", r.newNativeFunc(r.arrayproto_flat, nil, "flat", nil, 0), true, false, true)
  1157. o._putProp("flatMap", r.newNativeFunc(r.arrayproto_flatMap, nil, "flatMap", nil, 1), true, false, true)
  1158. o._putProp("forEach", r.newNativeFunc(r.arrayproto_forEach, nil, "forEach", nil, 1), true, false, true)
  1159. o._putProp("includes", r.newNativeFunc(r.arrayproto_includes, nil, "includes", nil, 1), true, false, true)
  1160. o._putProp("indexOf", r.newNativeFunc(r.arrayproto_indexOf, nil, "indexOf", nil, 1), true, false, true)
  1161. o._putProp("join", r.newNativeFunc(r.arrayproto_join, nil, "join", nil, 1), true, false, true)
  1162. o._putProp("keys", r.newNativeFunc(r.arrayproto_keys, nil, "keys", nil, 0), true, false, true)
  1163. o._putProp("lastIndexOf", r.newNativeFunc(r.arrayproto_lastIndexOf, nil, "lastIndexOf", nil, 1), true, false, true)
  1164. o._putProp("map", r.newNativeFunc(r.arrayproto_map, nil, "map", nil, 1), true, false, true)
  1165. o._putProp("pop", r.newNativeFunc(r.arrayproto_pop, nil, "pop", nil, 0), true, false, true)
  1166. o._putProp("push", r.newNativeFunc(r.arrayproto_push, nil, "push", nil, 1), true, false, true)
  1167. o._putProp("reduce", r.newNativeFunc(r.arrayproto_reduce, nil, "reduce", nil, 1), true, false, true)
  1168. o._putProp("reduceRight", r.newNativeFunc(r.arrayproto_reduceRight, nil, "reduceRight", nil, 1), true, false, true)
  1169. o._putProp("reverse", r.newNativeFunc(r.arrayproto_reverse, nil, "reverse", nil, 0), true, false, true)
  1170. o._putProp("shift", r.newNativeFunc(r.arrayproto_shift, nil, "shift", nil, 0), true, false, true)
  1171. o._putProp("slice", r.newNativeFunc(r.arrayproto_slice, nil, "slice", nil, 2), true, false, true)
  1172. o._putProp("some", r.newNativeFunc(r.arrayproto_some, nil, "some", nil, 1), true, false, true)
  1173. o._putProp("sort", r.newNativeFunc(r.arrayproto_sort, nil, "sort", nil, 1), true, false, true)
  1174. o._putProp("splice", r.newNativeFunc(r.arrayproto_splice, nil, "splice", nil, 2), true, false, true)
  1175. o._putProp("toLocaleString", r.newNativeFunc(r.arrayproto_toLocaleString, nil, "toLocaleString", nil, 0), true, false, true)
  1176. o._putProp("toString", r.global.arrayToString, true, false, true)
  1177. o._putProp("unshift", r.newNativeFunc(r.arrayproto_unshift, nil, "unshift", nil, 1), true, false, true)
  1178. valuesFunc := r.newNativeFunc(r.arrayproto_values, nil, "values", nil, 0)
  1179. r.global.arrayValues = valuesFunc
  1180. o._putProp("values", valuesFunc, true, false, true)
  1181. o._putSym(SymIterator, valueProp(valuesFunc, true, false, true))
  1182. bl := r.newBaseObject(nil, classObject)
  1183. bl.setOwnStr("copyWithin", valueTrue, true)
  1184. bl.setOwnStr("entries", valueTrue, true)
  1185. bl.setOwnStr("fill", valueTrue, true)
  1186. bl.setOwnStr("find", valueTrue, true)
  1187. bl.setOwnStr("findIndex", valueTrue, true)
  1188. bl.setOwnStr("flat", valueTrue, true)
  1189. bl.setOwnStr("flatMap", valueTrue, true)
  1190. bl.setOwnStr("includes", valueTrue, true)
  1191. bl.setOwnStr("keys", valueTrue, true)
  1192. bl.setOwnStr("values", valueTrue, true)
  1193. o._putSym(SymUnscopables, valueProp(bl.val, false, false, true))
  1194. return o
  1195. }
  1196. func (r *Runtime) createArray(val *Object) objectImpl {
  1197. o := r.newNativeFuncConstructObj(val, r.builtin_newArray, "Array", r.global.ArrayPrototype, 1)
  1198. o._putProp("from", r.newNativeFunc(r.array_from, nil, "from", nil, 1), true, false, true)
  1199. o._putProp("isArray", r.newNativeFunc(r.array_isArray, nil, "isArray", nil, 1), true, false, true)
  1200. o._putProp("of", r.newNativeFunc(r.array_of, nil, "of", nil, 0), true, false, true)
  1201. o._putSym(SymSpecies, &valueProperty{
  1202. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1203. accessor: true,
  1204. configurable: true,
  1205. })
  1206. return o
  1207. }
  1208. func (r *Runtime) createArrayIterProto(val *Object) objectImpl {
  1209. o := newBaseObjectObj(val, r.global.IteratorPrototype, classObject)
  1210. o._putProp("next", r.newNativeFunc(r.arrayIterProto_next, nil, "next", nil, 0), true, false, true)
  1211. o._putSym(SymToStringTag, valueProp(asciiString(classArrayIterator), false, false, true))
  1212. return o
  1213. }
  1214. func (r *Runtime) initArray() {
  1215. r.global.arrayToString = r.newNativeFunc(r.arrayproto_toString, nil, "toString", nil, 0)
  1216. r.global.ArrayIteratorPrototype = r.newLazyObject(r.createArrayIterProto)
  1217. //r.global.ArrayPrototype = r.newArray(r.global.ObjectPrototype).val
  1218. //o := r.global.ArrayPrototype.self
  1219. r.global.ArrayPrototype = r.newLazyObject(r.createArrayProto)
  1220. //r.global.Array = r.newNativeFuncConstruct(r.builtin_newArray, "Array", r.global.ArrayPrototype, 1)
  1221. //o = r.global.Array.self
  1222. //o._putProp("isArray", r.newNativeFunc(r.array_isArray, nil, "isArray", nil, 1), true, false, true)
  1223. r.global.Array = r.newLazyObject(r.createArray)
  1224. r.addToGlobal("Array", r.global.Array)
  1225. }
  1226. type sortable interface {
  1227. sortLen() int64
  1228. sortGet(int64) Value
  1229. swap(int64, int64)
  1230. }
  1231. type arraySortCtx struct {
  1232. obj sortable
  1233. compare func(FunctionCall) Value
  1234. }
  1235. func (a *arraySortCtx) sortCompare(x, y Value) int {
  1236. if x == nil && y == nil {
  1237. return 0
  1238. }
  1239. if x == nil {
  1240. return 1
  1241. }
  1242. if y == nil {
  1243. return -1
  1244. }
  1245. if x == _undefined && y == _undefined {
  1246. return 0
  1247. }
  1248. if x == _undefined {
  1249. return 1
  1250. }
  1251. if y == _undefined {
  1252. return -1
  1253. }
  1254. if a.compare != nil {
  1255. f := a.compare(FunctionCall{
  1256. This: _undefined,
  1257. Arguments: []Value{x, y},
  1258. }).ToFloat()
  1259. if f > 0 {
  1260. return 1
  1261. }
  1262. if f < 0 {
  1263. return -1
  1264. }
  1265. if math.Signbit(f) {
  1266. return -1
  1267. }
  1268. return 0
  1269. }
  1270. return x.toString().compareTo(y.toString())
  1271. }
  1272. // sort.Interface
  1273. func (a *arraySortCtx) Len() int {
  1274. return int(a.obj.sortLen())
  1275. }
  1276. func (a *arraySortCtx) Less(j, k int) bool {
  1277. return a.sortCompare(a.obj.sortGet(int64(j)), a.obj.sortGet(int64(k))) < 0
  1278. }
  1279. func (a *arraySortCtx) Swap(j, k int) {
  1280. a.obj.swap(int64(j), int64(k))
  1281. }