builtin_array.go 37 KB

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