builtin_array.go 38 KB

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