builtin_array.go 35 KB

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