builtin_array.go 38 KB

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