builtin_array.go 40 KB

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