builtin_array.go 40 KB

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