builtin_array.go 35 KB

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