builtin_array.go 39 KB

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