builtin_typedarrays.go 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "sort"
  6. "sync"
  7. "unsafe"
  8. "github.com/dop251/goja/unistring"
  9. )
  10. type typedArraySortCtx struct {
  11. ta *typedArrayObject
  12. compare func(FunctionCall) Value
  13. needValidate bool
  14. }
  15. func (ctx *typedArraySortCtx) Len() int {
  16. return ctx.ta.length
  17. }
  18. func (ctx *typedArraySortCtx) Less(i, j int) bool {
  19. if ctx.needValidate {
  20. ctx.ta.viewedArrayBuf.ensureNotDetached(true)
  21. ctx.needValidate = false
  22. }
  23. offset := ctx.ta.offset
  24. if ctx.compare != nil {
  25. x := ctx.ta.typedArray.get(offset + i)
  26. y := ctx.ta.typedArray.get(offset + j)
  27. res := ctx.compare(FunctionCall{
  28. This: _undefined,
  29. Arguments: []Value{x, y},
  30. }).ToNumber()
  31. ctx.needValidate = true
  32. if i, ok := res.(valueInt); ok {
  33. return i < 0
  34. }
  35. f := res.ToFloat()
  36. if f < 0 {
  37. return true
  38. }
  39. if f > 0 {
  40. return false
  41. }
  42. if math.Signbit(f) {
  43. return true
  44. }
  45. return false
  46. }
  47. return ctx.ta.typedArray.less(offset+i, offset+j)
  48. }
  49. func (ctx *typedArraySortCtx) Swap(i, j int) {
  50. if ctx.needValidate {
  51. ctx.ta.viewedArrayBuf.ensureNotDetached(true)
  52. ctx.needValidate = false
  53. }
  54. offset := ctx.ta.offset
  55. ctx.ta.typedArray.swap(offset+i, offset+j)
  56. }
  57. func allocByteSlice(size int) (b []byte) {
  58. defer func() {
  59. if x := recover(); x != nil {
  60. panic(rangeError(fmt.Sprintf("Buffer size is too large: %d", size)))
  61. }
  62. }()
  63. if size < 0 {
  64. panic(rangeError(fmt.Sprintf("Invalid buffer size: %d", size)))
  65. }
  66. b = make([]byte, size)
  67. return
  68. }
  69. func (r *Runtime) builtin_newArrayBuffer(args []Value, newTarget *Object) *Object {
  70. if newTarget == nil {
  71. panic(r.needNew("ArrayBuffer"))
  72. }
  73. b := r._newArrayBuffer(r.getPrototypeFromCtor(newTarget, r.getArrayBuffer(), r.getArrayBufferPrototype()), nil)
  74. if len(args) > 0 {
  75. b.data = allocByteSlice(r.toIndex(args[0]))
  76. }
  77. return b.val
  78. }
  79. func (r *Runtime) arrayBufferProto_getByteLength(call FunctionCall) Value {
  80. o := r.toObject(call.This)
  81. if b, ok := o.self.(*arrayBufferObject); ok {
  82. if b.ensureNotDetached(false) {
  83. return intToValue(int64(len(b.data)))
  84. }
  85. return intToValue(0)
  86. }
  87. panic(r.NewTypeError("Object is not ArrayBuffer: %s", o))
  88. }
  89. func (r *Runtime) arrayBufferProto_slice(call FunctionCall) Value {
  90. o := r.toObject(call.This)
  91. if b, ok := o.self.(*arrayBufferObject); ok {
  92. l := int64(len(b.data))
  93. start := relToIdx(call.Argument(0).ToInteger(), l)
  94. var stop int64
  95. if arg := call.Argument(1); arg != _undefined {
  96. stop = arg.ToInteger()
  97. } else {
  98. stop = l
  99. }
  100. stop = relToIdx(stop, l)
  101. newLen := max(stop-start, 0)
  102. ret := r.speciesConstructor(o, r.getArrayBuffer())([]Value{intToValue(newLen)}, nil)
  103. if ab, ok := ret.self.(*arrayBufferObject); ok {
  104. if newLen > 0 {
  105. b.ensureNotDetached(true)
  106. if ret == o {
  107. panic(r.NewTypeError("Species constructor returned the same ArrayBuffer"))
  108. }
  109. if int64(len(ab.data)) < newLen {
  110. panic(r.NewTypeError("Species constructor returned an ArrayBuffer that is too small: %d", len(ab.data)))
  111. }
  112. ab.ensureNotDetached(true)
  113. copy(ab.data, b.data[start:stop])
  114. }
  115. return ret
  116. }
  117. panic(r.NewTypeError("Species constructor did not return an ArrayBuffer: %s", ret.String()))
  118. }
  119. panic(r.NewTypeError("Object is not ArrayBuffer: %s", o))
  120. }
  121. func (r *Runtime) arrayBuffer_isView(call FunctionCall) Value {
  122. if o, ok := call.Argument(0).(*Object); ok {
  123. if _, ok := o.self.(*dataViewObject); ok {
  124. return valueTrue
  125. }
  126. if _, ok := o.self.(*typedArrayObject); ok {
  127. return valueTrue
  128. }
  129. }
  130. return valueFalse
  131. }
  132. func (r *Runtime) newDataView(args []Value, newTarget *Object) *Object {
  133. if newTarget == nil {
  134. panic(r.needNew("DataView"))
  135. }
  136. proto := r.getPrototypeFromCtor(newTarget, r.getDataView(), r.getDataViewPrototype())
  137. var bufArg Value
  138. if len(args) > 0 {
  139. bufArg = args[0]
  140. }
  141. var buffer *arrayBufferObject
  142. if o, ok := bufArg.(*Object); ok {
  143. if b, ok := o.self.(*arrayBufferObject); ok {
  144. buffer = b
  145. }
  146. }
  147. if buffer == nil {
  148. panic(r.NewTypeError("First argument to DataView constructor must be an ArrayBuffer"))
  149. }
  150. var byteOffset, byteLen int
  151. if len(args) > 1 {
  152. offsetArg := nilSafe(args[1])
  153. byteOffset = r.toIndex(offsetArg)
  154. buffer.ensureNotDetached(true)
  155. if byteOffset > len(buffer.data) {
  156. panic(r.newError(r.getRangeError(), "Start offset %s is outside the bounds of the buffer", offsetArg.String()))
  157. }
  158. }
  159. if len(args) > 2 && args[2] != nil && args[2] != _undefined {
  160. byteLen = r.toIndex(args[2])
  161. if byteOffset+byteLen > len(buffer.data) {
  162. panic(r.newError(r.getRangeError(), "Invalid DataView length %d", byteLen))
  163. }
  164. } else {
  165. byteLen = len(buffer.data) - byteOffset
  166. }
  167. o := &Object{runtime: r}
  168. b := &dataViewObject{
  169. baseObject: baseObject{
  170. class: classObject,
  171. val: o,
  172. prototype: proto,
  173. extensible: true,
  174. },
  175. viewedArrayBuf: buffer,
  176. byteOffset: byteOffset,
  177. byteLen: byteLen,
  178. }
  179. o.self = b
  180. b.init()
  181. return o
  182. }
  183. func (r *Runtime) dataViewProto_getBuffer(call FunctionCall) Value {
  184. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  185. return dv.viewedArrayBuf.val
  186. }
  187. panic(r.NewTypeError("Method get DataView.prototype.buffer called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  188. }
  189. func (r *Runtime) dataViewProto_getByteLen(call FunctionCall) Value {
  190. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  191. dv.viewedArrayBuf.ensureNotDetached(true)
  192. return intToValue(int64(dv.byteLen))
  193. }
  194. panic(r.NewTypeError("Method get DataView.prototype.byteLength called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  195. }
  196. func (r *Runtime) dataViewProto_getByteOffset(call FunctionCall) Value {
  197. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  198. dv.viewedArrayBuf.ensureNotDetached(true)
  199. return intToValue(int64(dv.byteOffset))
  200. }
  201. panic(r.NewTypeError("Method get DataView.prototype.byteOffset called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  202. }
  203. func (r *Runtime) dataViewProto_getFloat32(call FunctionCall) Value {
  204. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  205. return floatToValue(float64(dv.viewedArrayBuf.getFloat32(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 4))))
  206. }
  207. panic(r.NewTypeError("Method DataView.prototype.getFloat32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  208. }
  209. func (r *Runtime) dataViewProto_getFloat64(call FunctionCall) Value {
  210. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  211. return floatToValue(dv.viewedArrayBuf.getFloat64(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 8)))
  212. }
  213. panic(r.NewTypeError("Method DataView.prototype.getFloat64 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  214. }
  215. func (r *Runtime) dataViewProto_getInt8(call FunctionCall) Value {
  216. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  217. idx, _ := dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 1)
  218. return intToValue(int64(dv.viewedArrayBuf.getInt8(idx)))
  219. }
  220. panic(r.NewTypeError("Method DataView.prototype.getInt8 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  221. }
  222. func (r *Runtime) dataViewProto_getInt16(call FunctionCall) Value {
  223. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  224. return intToValue(int64(dv.viewedArrayBuf.getInt16(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 2))))
  225. }
  226. panic(r.NewTypeError("Method DataView.prototype.getInt16 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  227. }
  228. func (r *Runtime) dataViewProto_getInt32(call FunctionCall) Value {
  229. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  230. return intToValue(int64(dv.viewedArrayBuf.getInt32(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 4))))
  231. }
  232. panic(r.NewTypeError("Method DataView.prototype.getInt32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  233. }
  234. func (r *Runtime) dataViewProto_getUint8(call FunctionCall) Value {
  235. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  236. idx, _ := dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 1)
  237. return intToValue(int64(dv.viewedArrayBuf.getUint8(idx)))
  238. }
  239. panic(r.NewTypeError("Method DataView.prototype.getUint8 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  240. }
  241. func (r *Runtime) dataViewProto_getUint16(call FunctionCall) Value {
  242. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  243. return intToValue(int64(dv.viewedArrayBuf.getUint16(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 2))))
  244. }
  245. panic(r.NewTypeError("Method DataView.prototype.getUint16 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  246. }
  247. func (r *Runtime) dataViewProto_getUint32(call FunctionCall) Value {
  248. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  249. return intToValue(int64(dv.viewedArrayBuf.getUint32(dv.getIdxAndByteOrder(r.toIndex(call.Argument(0)), call.Argument(1), 4))))
  250. }
  251. panic(r.NewTypeError("Method DataView.prototype.getUint32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  252. }
  253. func (r *Runtime) dataViewProto_setFloat32(call FunctionCall) Value {
  254. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  255. idxVal := r.toIndex(call.Argument(0))
  256. val := toFloat32(call.Argument(1))
  257. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 4)
  258. dv.viewedArrayBuf.setFloat32(idx, val, bo)
  259. return _undefined
  260. }
  261. panic(r.NewTypeError("Method DataView.prototype.setFloat32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  262. }
  263. func (r *Runtime) dataViewProto_setFloat64(call FunctionCall) Value {
  264. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  265. idxVal := r.toIndex(call.Argument(0))
  266. val := call.Argument(1).ToFloat()
  267. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 8)
  268. dv.viewedArrayBuf.setFloat64(idx, val, bo)
  269. return _undefined
  270. }
  271. panic(r.NewTypeError("Method DataView.prototype.setFloat64 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  272. }
  273. func (r *Runtime) dataViewProto_setInt8(call FunctionCall) Value {
  274. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  275. idxVal := r.toIndex(call.Argument(0))
  276. val := toInt8(call.Argument(1))
  277. idx, _ := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 1)
  278. dv.viewedArrayBuf.setInt8(idx, val)
  279. return _undefined
  280. }
  281. panic(r.NewTypeError("Method DataView.prototype.setInt8 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  282. }
  283. func (r *Runtime) dataViewProto_setInt16(call FunctionCall) Value {
  284. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  285. idxVal := r.toIndex(call.Argument(0))
  286. val := toInt16(call.Argument(1))
  287. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 2)
  288. dv.viewedArrayBuf.setInt16(idx, val, bo)
  289. return _undefined
  290. }
  291. panic(r.NewTypeError("Method DataView.prototype.setInt16 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  292. }
  293. func (r *Runtime) dataViewProto_setInt32(call FunctionCall) Value {
  294. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  295. idxVal := r.toIndex(call.Argument(0))
  296. val := toInt32(call.Argument(1))
  297. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 4)
  298. dv.viewedArrayBuf.setInt32(idx, val, bo)
  299. return _undefined
  300. }
  301. panic(r.NewTypeError("Method DataView.prototype.setInt32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  302. }
  303. func (r *Runtime) dataViewProto_setUint8(call FunctionCall) Value {
  304. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  305. idxVal := r.toIndex(call.Argument(0))
  306. val := toUint8(call.Argument(1))
  307. idx, _ := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 1)
  308. dv.viewedArrayBuf.setUint8(idx, val)
  309. return _undefined
  310. }
  311. panic(r.NewTypeError("Method DataView.prototype.setUint8 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  312. }
  313. func (r *Runtime) dataViewProto_setUint16(call FunctionCall) Value {
  314. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  315. idxVal := r.toIndex(call.Argument(0))
  316. val := toUint16(call.Argument(1))
  317. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 2)
  318. dv.viewedArrayBuf.setUint16(idx, val, bo)
  319. return _undefined
  320. }
  321. panic(r.NewTypeError("Method DataView.prototype.setUint16 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  322. }
  323. func (r *Runtime) dataViewProto_setUint32(call FunctionCall) Value {
  324. if dv, ok := r.toObject(call.This).self.(*dataViewObject); ok {
  325. idxVal := r.toIndex(call.Argument(0))
  326. val := toUint32(call.Argument(1))
  327. idx, bo := dv.getIdxAndByteOrder(idxVal, call.Argument(2), 4)
  328. dv.viewedArrayBuf.setUint32(idx, val, bo)
  329. return _undefined
  330. }
  331. panic(r.NewTypeError("Method DataView.prototype.setUint32 called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  332. }
  333. func (r *Runtime) typedArrayProto_getBuffer(call FunctionCall) Value {
  334. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  335. return ta.viewedArrayBuf.val
  336. }
  337. panic(r.NewTypeError("Method get TypedArray.prototype.buffer called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  338. }
  339. func (r *Runtime) typedArrayProto_getByteLen(call FunctionCall) Value {
  340. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  341. if ta.viewedArrayBuf.data == nil {
  342. return _positiveZero
  343. }
  344. return intToValue(int64(ta.length) * int64(ta.elemSize))
  345. }
  346. panic(r.NewTypeError("Method get TypedArray.prototype.byteLength called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  347. }
  348. func (r *Runtime) typedArrayProto_getLength(call FunctionCall) Value {
  349. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  350. if ta.viewedArrayBuf.data == nil {
  351. return _positiveZero
  352. }
  353. return intToValue(int64(ta.length))
  354. }
  355. panic(r.NewTypeError("Method get TypedArray.prototype.length called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  356. }
  357. func (r *Runtime) typedArrayProto_getByteOffset(call FunctionCall) Value {
  358. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  359. if ta.viewedArrayBuf.data == nil {
  360. return _positiveZero
  361. }
  362. return intToValue(int64(ta.offset) * int64(ta.elemSize))
  363. }
  364. panic(r.NewTypeError("Method get TypedArray.prototype.byteOffset called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  365. }
  366. func (r *Runtime) typedArrayProto_copyWithin(call FunctionCall) Value {
  367. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  368. ta.viewedArrayBuf.ensureNotDetached(true)
  369. l := int64(ta.length)
  370. var relEnd int64
  371. to := toIntStrict(relToIdx(call.Argument(0).ToInteger(), l))
  372. from := toIntStrict(relToIdx(call.Argument(1).ToInteger(), l))
  373. if end := call.Argument(2); end != _undefined {
  374. relEnd = end.ToInteger()
  375. } else {
  376. relEnd = l
  377. }
  378. final := toIntStrict(relToIdx(relEnd, l))
  379. data := ta.viewedArrayBuf.data
  380. offset := ta.offset
  381. elemSize := ta.elemSize
  382. if final > from {
  383. ta.viewedArrayBuf.ensureNotDetached(true)
  384. copy(data[(offset+to)*elemSize:], data[(offset+from)*elemSize:(offset+final)*elemSize])
  385. }
  386. return call.This
  387. }
  388. panic(r.NewTypeError("Method TypedArray.prototype.copyWithin called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  389. }
  390. func (r *Runtime) typedArrayProto_entries(call FunctionCall) Value {
  391. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  392. ta.viewedArrayBuf.ensureNotDetached(true)
  393. return r.createArrayIterator(ta.val, iterationKindKeyValue)
  394. }
  395. panic(r.NewTypeError("Method TypedArray.prototype.entries called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  396. }
  397. func (r *Runtime) typedArrayProto_every(call FunctionCall) Value {
  398. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  399. ta.viewedArrayBuf.ensureNotDetached(true)
  400. callbackFn := r.toCallable(call.Argument(0))
  401. fc := FunctionCall{
  402. This: call.Argument(1),
  403. Arguments: []Value{nil, nil, call.This},
  404. }
  405. for k := 0; k < ta.length; k++ {
  406. if ta.isValidIntegerIndex(k) {
  407. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  408. } else {
  409. fc.Arguments[0] = _undefined
  410. }
  411. fc.Arguments[1] = intToValue(int64(k))
  412. if !callbackFn(fc).ToBoolean() {
  413. return valueFalse
  414. }
  415. }
  416. return valueTrue
  417. }
  418. panic(r.NewTypeError("Method TypedArray.prototype.every called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  419. }
  420. func (r *Runtime) typedArrayProto_fill(call FunctionCall) Value {
  421. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  422. ta.viewedArrayBuf.ensureNotDetached(true)
  423. l := int64(ta.length)
  424. k := toIntStrict(relToIdx(call.Argument(1).ToInteger(), l))
  425. var relEnd int64
  426. if endArg := call.Argument(2); endArg != _undefined {
  427. relEnd = endArg.ToInteger()
  428. } else {
  429. relEnd = l
  430. }
  431. final := toIntStrict(relToIdx(relEnd, l))
  432. value := ta.typedArray.toRaw(call.Argument(0))
  433. ta.viewedArrayBuf.ensureNotDetached(true)
  434. for ; k < final; k++ {
  435. ta.typedArray.setRaw(ta.offset+k, value)
  436. }
  437. return call.This
  438. }
  439. panic(r.NewTypeError("Method TypedArray.prototype.fill called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  440. }
  441. func (r *Runtime) typedArrayProto_filter(call FunctionCall) Value {
  442. o := r.toObject(call.This)
  443. if ta, ok := o.self.(*typedArrayObject); ok {
  444. ta.viewedArrayBuf.ensureNotDetached(true)
  445. callbackFn := r.toCallable(call.Argument(0))
  446. fc := FunctionCall{
  447. This: call.Argument(1),
  448. Arguments: []Value{nil, nil, call.This},
  449. }
  450. buf := make([]byte, 0, ta.length*ta.elemSize)
  451. captured := 0
  452. rawVal := make([]byte, ta.elemSize)
  453. for k := 0; k < ta.length; k++ {
  454. if ta.isValidIntegerIndex(k) {
  455. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  456. i := (ta.offset + k) * ta.elemSize
  457. copy(rawVal, ta.viewedArrayBuf.data[i:])
  458. } else {
  459. fc.Arguments[0] = _undefined
  460. for i := range rawVal {
  461. rawVal[i] = 0
  462. }
  463. }
  464. fc.Arguments[1] = intToValue(int64(k))
  465. if callbackFn(fc).ToBoolean() {
  466. buf = append(buf, rawVal...)
  467. captured++
  468. }
  469. }
  470. c := r.speciesConstructorObj(o, ta.defaultCtor)
  471. ab := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)
  472. ab.data = buf
  473. kept := r.toConstructor(ta.defaultCtor)([]Value{ab.val}, ta.defaultCtor)
  474. if c == ta.defaultCtor {
  475. return kept
  476. } else {
  477. ret := r.typedArrayCreate(c, intToValue(int64(captured)))
  478. keptTa := kept.self.(*typedArrayObject)
  479. for i := 0; i < captured; i++ {
  480. ret.typedArray.set(i, keptTa.typedArray.get(keptTa.offset+i))
  481. }
  482. return ret.val
  483. }
  484. }
  485. panic(r.NewTypeError("Method TypedArray.prototype.filter called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  486. }
  487. func (r *Runtime) typedArrayProto_find(call FunctionCall) Value {
  488. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  489. ta.viewedArrayBuf.ensureNotDetached(true)
  490. predicate := r.toCallable(call.Argument(0))
  491. fc := FunctionCall{
  492. This: call.Argument(1),
  493. Arguments: []Value{nil, nil, call.This},
  494. }
  495. for k := 0; k < ta.length; k++ {
  496. var val Value
  497. if ta.isValidIntegerIndex(k) {
  498. val = ta.typedArray.get(ta.offset + k)
  499. }
  500. fc.Arguments[0] = val
  501. fc.Arguments[1] = intToValue(int64(k))
  502. if predicate(fc).ToBoolean() {
  503. return val
  504. }
  505. }
  506. return _undefined
  507. }
  508. panic(r.NewTypeError("Method TypedArray.prototype.find called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  509. }
  510. func (r *Runtime) typedArrayProto_findIndex(call FunctionCall) Value {
  511. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  512. ta.viewedArrayBuf.ensureNotDetached(true)
  513. predicate := r.toCallable(call.Argument(0))
  514. fc := FunctionCall{
  515. This: call.Argument(1),
  516. Arguments: []Value{nil, nil, call.This},
  517. }
  518. for k := 0; k < ta.length; k++ {
  519. if ta.isValidIntegerIndex(k) {
  520. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  521. } else {
  522. fc.Arguments[0] = _undefined
  523. }
  524. fc.Arguments[1] = intToValue(int64(k))
  525. if predicate(fc).ToBoolean() {
  526. return fc.Arguments[1]
  527. }
  528. }
  529. return intToValue(-1)
  530. }
  531. panic(r.NewTypeError("Method TypedArray.prototype.findIndex called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  532. }
  533. func (r *Runtime) typedArrayProto_findLast(call FunctionCall) Value {
  534. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  535. ta.viewedArrayBuf.ensureNotDetached(true)
  536. predicate := r.toCallable(call.Argument(0))
  537. fc := FunctionCall{
  538. This: call.Argument(1),
  539. Arguments: []Value{nil, nil, call.This},
  540. }
  541. for k := ta.length - 1; k >= 0; k-- {
  542. var val Value
  543. if ta.isValidIntegerIndex(k) {
  544. val = ta.typedArray.get(ta.offset + k)
  545. }
  546. fc.Arguments[0] = val
  547. fc.Arguments[1] = intToValue(int64(k))
  548. if predicate(fc).ToBoolean() {
  549. return val
  550. }
  551. }
  552. return _undefined
  553. }
  554. panic(r.NewTypeError("Method TypedArray.prototype.findLast called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  555. }
  556. func (r *Runtime) typedArrayProto_findLastIndex(call FunctionCall) Value {
  557. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  558. ta.viewedArrayBuf.ensureNotDetached(true)
  559. predicate := r.toCallable(call.Argument(0))
  560. fc := FunctionCall{
  561. This: call.Argument(1),
  562. Arguments: []Value{nil, nil, call.This},
  563. }
  564. for k := ta.length - 1; k >= 0; k-- {
  565. if ta.isValidIntegerIndex(k) {
  566. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  567. } else {
  568. fc.Arguments[0] = _undefined
  569. }
  570. fc.Arguments[1] = intToValue(int64(k))
  571. if predicate(fc).ToBoolean() {
  572. return fc.Arguments[1]
  573. }
  574. }
  575. return intToValue(-1)
  576. }
  577. panic(r.NewTypeError("Method TypedArray.prototype.findLastIndex called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  578. }
  579. func (r *Runtime) typedArrayProto_forEach(call FunctionCall) Value {
  580. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  581. ta.viewedArrayBuf.ensureNotDetached(true)
  582. callbackFn := r.toCallable(call.Argument(0))
  583. fc := FunctionCall{
  584. This: call.Argument(1),
  585. Arguments: []Value{nil, nil, call.This},
  586. }
  587. for k := 0; k < ta.length; k++ {
  588. var val Value
  589. if ta.isValidIntegerIndex(k) {
  590. val = ta.typedArray.get(ta.offset + k)
  591. }
  592. fc.Arguments[0] = val
  593. fc.Arguments[1] = intToValue(int64(k))
  594. callbackFn(fc)
  595. }
  596. return _undefined
  597. }
  598. panic(r.NewTypeError("Method TypedArray.prototype.forEach called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  599. }
  600. func (r *Runtime) typedArrayProto_includes(call FunctionCall) Value {
  601. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  602. ta.viewedArrayBuf.ensureNotDetached(true)
  603. length := int64(ta.length)
  604. if length == 0 {
  605. return valueFalse
  606. }
  607. n := call.Argument(1).ToInteger()
  608. if n >= length {
  609. return valueFalse
  610. }
  611. if n < 0 {
  612. n = max(length+n, 0)
  613. }
  614. searchElement := call.Argument(0)
  615. if searchElement == _negativeZero {
  616. searchElement = _positiveZero
  617. }
  618. startIdx := toIntStrict(n)
  619. if !ta.viewedArrayBuf.ensureNotDetached(false) {
  620. if searchElement == _undefined && startIdx < ta.length {
  621. return valueTrue
  622. }
  623. return valueFalse
  624. }
  625. if ta.typedArray.typeMatch(searchElement) {
  626. se := ta.typedArray.toRaw(searchElement)
  627. for k := startIdx; k < ta.length; k++ {
  628. if ta.typedArray.getRaw(ta.offset+k) == se {
  629. return valueTrue
  630. }
  631. }
  632. }
  633. return valueFalse
  634. }
  635. panic(r.NewTypeError("Method TypedArray.prototype.includes called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  636. }
  637. func (r *Runtime) typedArrayProto_at(call FunctionCall) Value {
  638. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  639. ta.viewedArrayBuf.ensureNotDetached(true)
  640. idx := call.Argument(0).ToInteger()
  641. length := int64(ta.length)
  642. if idx < 0 {
  643. idx = length + idx
  644. }
  645. if idx >= length || idx < 0 {
  646. return _undefined
  647. }
  648. if ta.viewedArrayBuf.ensureNotDetached(false) {
  649. return ta.typedArray.get(ta.offset + int(idx))
  650. }
  651. return _undefined
  652. }
  653. panic(r.NewTypeError("Method TypedArray.prototype.at called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  654. }
  655. func (r *Runtime) typedArrayProto_indexOf(call FunctionCall) Value {
  656. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  657. ta.viewedArrayBuf.ensureNotDetached(true)
  658. length := int64(ta.length)
  659. if length == 0 {
  660. return intToValue(-1)
  661. }
  662. n := call.Argument(1).ToInteger()
  663. if n >= length {
  664. return intToValue(-1)
  665. }
  666. if n < 0 {
  667. n = max(length+n, 0)
  668. }
  669. if ta.viewedArrayBuf.ensureNotDetached(false) {
  670. searchElement := call.Argument(0)
  671. if searchElement == _negativeZero {
  672. searchElement = _positiveZero
  673. }
  674. if !IsNaN(searchElement) && ta.typedArray.typeMatch(searchElement) {
  675. se := ta.typedArray.toRaw(searchElement)
  676. for k := toIntStrict(n); k < ta.length; k++ {
  677. if ta.typedArray.getRaw(ta.offset+k) == se {
  678. return intToValue(int64(k))
  679. }
  680. }
  681. }
  682. }
  683. return intToValue(-1)
  684. }
  685. panic(r.NewTypeError("Method TypedArray.prototype.indexOf called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  686. }
  687. func (r *Runtime) typedArrayProto_join(call FunctionCall) Value {
  688. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  689. ta.viewedArrayBuf.ensureNotDetached(true)
  690. s := call.Argument(0)
  691. var sep String
  692. if s != _undefined {
  693. sep = s.toString()
  694. } else {
  695. sep = asciiString(",")
  696. }
  697. l := ta.length
  698. if l == 0 {
  699. return stringEmpty
  700. }
  701. var buf StringBuilder
  702. var element0 Value
  703. if ta.isValidIntegerIndex(0) {
  704. element0 = ta.typedArray.get(ta.offset + 0)
  705. }
  706. if element0 != nil && element0 != _undefined && element0 != _null {
  707. buf.WriteString(element0.toString())
  708. }
  709. for i := 1; i < l; i++ {
  710. buf.WriteString(sep)
  711. if ta.isValidIntegerIndex(i) {
  712. element := ta.typedArray.get(ta.offset + i)
  713. if element != nil && element != _undefined && element != _null {
  714. buf.WriteString(element.toString())
  715. }
  716. }
  717. }
  718. return buf.String()
  719. }
  720. panic(r.NewTypeError("Method TypedArray.prototype.join called on incompatible receiver"))
  721. }
  722. func (r *Runtime) typedArrayProto_keys(call FunctionCall) Value {
  723. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  724. ta.viewedArrayBuf.ensureNotDetached(true)
  725. return r.createArrayIterator(ta.val, iterationKindKey)
  726. }
  727. panic(r.NewTypeError("Method TypedArray.prototype.keys called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  728. }
  729. func (r *Runtime) typedArrayProto_lastIndexOf(call FunctionCall) Value {
  730. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  731. ta.viewedArrayBuf.ensureNotDetached(true)
  732. length := int64(ta.length)
  733. if length == 0 {
  734. return intToValue(-1)
  735. }
  736. var fromIndex int64
  737. if len(call.Arguments) < 2 {
  738. fromIndex = length - 1
  739. } else {
  740. fromIndex = call.Argument(1).ToInteger()
  741. if fromIndex >= 0 {
  742. fromIndex = min(fromIndex, length-1)
  743. } else {
  744. fromIndex += length
  745. if fromIndex < 0 {
  746. fromIndex = -1 // prevent underflow in toIntStrict() on 32-bit platforms
  747. }
  748. }
  749. }
  750. if ta.viewedArrayBuf.ensureNotDetached(false) {
  751. searchElement := call.Argument(0)
  752. if searchElement == _negativeZero {
  753. searchElement = _positiveZero
  754. }
  755. if !IsNaN(searchElement) && ta.typedArray.typeMatch(searchElement) {
  756. se := ta.typedArray.toRaw(searchElement)
  757. for k := toIntStrict(fromIndex); k >= 0; k-- {
  758. if ta.typedArray.getRaw(ta.offset+k) == se {
  759. return intToValue(int64(k))
  760. }
  761. }
  762. }
  763. }
  764. return intToValue(-1)
  765. }
  766. panic(r.NewTypeError("Method TypedArray.prototype.lastIndexOf called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  767. }
  768. func (r *Runtime) typedArrayProto_map(call FunctionCall) Value {
  769. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  770. ta.viewedArrayBuf.ensureNotDetached(true)
  771. callbackFn := r.toCallable(call.Argument(0))
  772. fc := FunctionCall{
  773. This: call.Argument(1),
  774. Arguments: []Value{nil, nil, call.This},
  775. }
  776. dst := r.typedArraySpeciesCreate(ta, []Value{intToValue(int64(ta.length))})
  777. for i := 0; i < ta.length; i++ {
  778. if ta.isValidIntegerIndex(i) {
  779. fc.Arguments[0] = ta.typedArray.get(ta.offset + i)
  780. } else {
  781. fc.Arguments[0] = _undefined
  782. }
  783. fc.Arguments[1] = intToValue(int64(i))
  784. dst.typedArray.set(i, callbackFn(fc))
  785. }
  786. return dst.val
  787. }
  788. panic(r.NewTypeError("Method TypedArray.prototype.map called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  789. }
  790. func (r *Runtime) typedArrayProto_reduce(call FunctionCall) Value {
  791. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  792. ta.viewedArrayBuf.ensureNotDetached(true)
  793. callbackFn := r.toCallable(call.Argument(0))
  794. fc := FunctionCall{
  795. This: _undefined,
  796. Arguments: []Value{nil, nil, nil, call.This},
  797. }
  798. k := 0
  799. if len(call.Arguments) >= 2 {
  800. fc.Arguments[0] = call.Argument(1)
  801. } else {
  802. if ta.length > 0 {
  803. fc.Arguments[0] = ta.typedArray.get(ta.offset + 0)
  804. k = 1
  805. }
  806. }
  807. if fc.Arguments[0] == nil {
  808. panic(r.NewTypeError("Reduce of empty array with no initial value"))
  809. }
  810. for ; k < ta.length; k++ {
  811. if ta.isValidIntegerIndex(k) {
  812. fc.Arguments[1] = ta.typedArray.get(ta.offset + k)
  813. } else {
  814. fc.Arguments[1] = _undefined
  815. }
  816. idx := valueInt(k)
  817. fc.Arguments[2] = idx
  818. fc.Arguments[0] = callbackFn(fc)
  819. }
  820. return fc.Arguments[0]
  821. }
  822. panic(r.NewTypeError("Method TypedArray.prototype.reduce called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  823. }
  824. func (r *Runtime) typedArrayProto_reduceRight(call FunctionCall) Value {
  825. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  826. ta.viewedArrayBuf.ensureNotDetached(true)
  827. callbackFn := r.toCallable(call.Argument(0))
  828. fc := FunctionCall{
  829. This: _undefined,
  830. Arguments: []Value{nil, nil, nil, call.This},
  831. }
  832. k := ta.length - 1
  833. if len(call.Arguments) >= 2 {
  834. fc.Arguments[0] = call.Argument(1)
  835. } else {
  836. if k >= 0 {
  837. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  838. k--
  839. }
  840. }
  841. if fc.Arguments[0] == nil {
  842. panic(r.NewTypeError("Reduce of empty array with no initial value"))
  843. }
  844. for ; k >= 0; k-- {
  845. if ta.isValidIntegerIndex(k) {
  846. fc.Arguments[1] = ta.typedArray.get(ta.offset + k)
  847. } else {
  848. fc.Arguments[1] = _undefined
  849. }
  850. idx := valueInt(k)
  851. fc.Arguments[2] = idx
  852. fc.Arguments[0] = callbackFn(fc)
  853. }
  854. return fc.Arguments[0]
  855. }
  856. panic(r.NewTypeError("Method TypedArray.prototype.reduceRight called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  857. }
  858. func (r *Runtime) typedArrayProto_reverse(call FunctionCall) Value {
  859. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  860. ta.viewedArrayBuf.ensureNotDetached(true)
  861. l := ta.length
  862. middle := l / 2
  863. for lower := 0; lower != middle; lower++ {
  864. upper := l - lower - 1
  865. ta.typedArray.swap(ta.offset+lower, ta.offset+upper)
  866. }
  867. return call.This
  868. }
  869. panic(r.NewTypeError("Method TypedArray.prototype.reverse called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  870. }
  871. func (r *Runtime) typedArrayProto_set(call FunctionCall) Value {
  872. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  873. srcObj := call.Argument(0).ToObject(r)
  874. targetOffset := toIntStrict(call.Argument(1).ToInteger())
  875. if targetOffset < 0 {
  876. panic(r.newError(r.getRangeError(), "offset should be >= 0"))
  877. }
  878. ta.viewedArrayBuf.ensureNotDetached(true)
  879. targetLen := ta.length
  880. if src, ok := srcObj.self.(*typedArrayObject); ok {
  881. src.viewedArrayBuf.ensureNotDetached(true)
  882. srcLen := src.length
  883. if x := srcLen + targetOffset; x < 0 || x > targetLen {
  884. panic(r.newError(r.getRangeError(), "Source is too large"))
  885. }
  886. if src.defaultCtor == ta.defaultCtor {
  887. copy(ta.viewedArrayBuf.data[(ta.offset+targetOffset)*ta.elemSize:],
  888. src.viewedArrayBuf.data[src.offset*src.elemSize:(src.offset+srcLen)*src.elemSize])
  889. } else {
  890. curSrc := uintptr(unsafe.Pointer(&src.viewedArrayBuf.data[src.offset*src.elemSize]))
  891. endSrc := curSrc + uintptr(srcLen*src.elemSize)
  892. curDst := uintptr(unsafe.Pointer(&ta.viewedArrayBuf.data[(ta.offset+targetOffset)*ta.elemSize]))
  893. dstOffset := ta.offset + targetOffset
  894. srcOffset := src.offset
  895. if ta.elemSize == src.elemSize {
  896. if curDst <= curSrc || curDst >= endSrc {
  897. for i := 0; i < srcLen; i++ {
  898. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  899. }
  900. } else {
  901. for i := srcLen - 1; i >= 0; i-- {
  902. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  903. }
  904. }
  905. } else {
  906. x := int(curDst-curSrc) / (src.elemSize - ta.elemSize)
  907. if x < 0 {
  908. x = 0
  909. } else if x > srcLen {
  910. x = srcLen
  911. }
  912. if ta.elemSize < src.elemSize {
  913. for i := x; i < srcLen; i++ {
  914. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  915. }
  916. for i := x - 1; i >= 0; i-- {
  917. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  918. }
  919. } else {
  920. for i := 0; i < x; i++ {
  921. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  922. }
  923. for i := srcLen - 1; i >= x; i-- {
  924. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  925. }
  926. }
  927. }
  928. }
  929. } else {
  930. targetLen := ta.length
  931. srcLen := toIntStrict(toLength(srcObj.self.getStr("length", nil)))
  932. if x := srcLen + targetOffset; x < 0 || x > targetLen {
  933. panic(r.newError(r.getRangeError(), "Source is too large"))
  934. }
  935. for i := 0; i < srcLen; i++ {
  936. val := nilSafe(srcObj.self.getIdx(valueInt(i), nil))
  937. ta.viewedArrayBuf.ensureNotDetached(true)
  938. if ta.isValidIntegerIndex(i) {
  939. ta.typedArray.set(targetOffset+i, val)
  940. }
  941. }
  942. }
  943. return _undefined
  944. }
  945. panic(r.NewTypeError("Method TypedArray.prototype.set called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  946. }
  947. func (r *Runtime) typedArrayProto_slice(call FunctionCall) Value {
  948. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  949. ta.viewedArrayBuf.ensureNotDetached(true)
  950. length := int64(ta.length)
  951. start := toIntStrict(relToIdx(call.Argument(0).ToInteger(), length))
  952. var e int64
  953. if endArg := call.Argument(1); endArg != _undefined {
  954. e = endArg.ToInteger()
  955. } else {
  956. e = length
  957. }
  958. end := toIntStrict(relToIdx(e, length))
  959. count := end - start
  960. if count < 0 {
  961. count = 0
  962. }
  963. dst := r.typedArraySpeciesCreate(ta, []Value{intToValue(int64(count))})
  964. if dst.defaultCtor == ta.defaultCtor {
  965. if count > 0 {
  966. ta.viewedArrayBuf.ensureNotDetached(true)
  967. offset := ta.offset
  968. elemSize := ta.elemSize
  969. copy(dst.viewedArrayBuf.data, ta.viewedArrayBuf.data[(offset+start)*elemSize:(offset+start+count)*elemSize])
  970. }
  971. } else {
  972. for i := 0; i < count; i++ {
  973. ta.viewedArrayBuf.ensureNotDetached(true)
  974. dst.typedArray.set(i, ta.typedArray.get(ta.offset+start+i))
  975. }
  976. }
  977. return dst.val
  978. }
  979. panic(r.NewTypeError("Method TypedArray.prototype.slice called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  980. }
  981. func (r *Runtime) typedArrayProto_some(call FunctionCall) Value {
  982. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  983. ta.viewedArrayBuf.ensureNotDetached(true)
  984. callbackFn := r.toCallable(call.Argument(0))
  985. fc := FunctionCall{
  986. This: call.Argument(1),
  987. Arguments: []Value{nil, nil, call.This},
  988. }
  989. for k := 0; k < ta.length; k++ {
  990. if ta.isValidIntegerIndex(k) {
  991. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  992. } else {
  993. fc.Arguments[0] = _undefined
  994. }
  995. fc.Arguments[1] = intToValue(int64(k))
  996. if callbackFn(fc).ToBoolean() {
  997. return valueTrue
  998. }
  999. }
  1000. return valueFalse
  1001. }
  1002. panic(r.NewTypeError("Method TypedArray.prototype.some called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  1003. }
  1004. func (r *Runtime) typedArrayProto_sort(call FunctionCall) Value {
  1005. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  1006. ta.viewedArrayBuf.ensureNotDetached(true)
  1007. var compareFn func(FunctionCall) Value
  1008. if arg := call.Argument(0); arg != _undefined {
  1009. compareFn = r.toCallable(arg)
  1010. }
  1011. ctx := typedArraySortCtx{
  1012. ta: ta,
  1013. compare: compareFn,
  1014. }
  1015. sort.Stable(&ctx)
  1016. return call.This
  1017. }
  1018. panic(r.NewTypeError("Method TypedArray.prototype.sort called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  1019. }
  1020. func (r *Runtime) typedArrayProto_subarray(call FunctionCall) Value {
  1021. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  1022. l := int64(ta.length)
  1023. beginIdx := relToIdx(call.Argument(0).ToInteger(), l)
  1024. var relEnd int64
  1025. if endArg := call.Argument(1); endArg != _undefined {
  1026. relEnd = endArg.ToInteger()
  1027. } else {
  1028. relEnd = l
  1029. }
  1030. endIdx := relToIdx(relEnd, l)
  1031. newLen := max(endIdx-beginIdx, 0)
  1032. return r.typedArraySpeciesCreate(ta, []Value{ta.viewedArrayBuf.val,
  1033. intToValue((int64(ta.offset) + beginIdx) * int64(ta.elemSize)),
  1034. intToValue(newLen),
  1035. }).val
  1036. }
  1037. panic(r.NewTypeError("Method TypedArray.prototype.subarray called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  1038. }
  1039. func (r *Runtime) typedArrayProto_toLocaleString(call FunctionCall) Value {
  1040. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  1041. length := ta.length
  1042. var buf StringBuilder
  1043. for i := 0; i < length; i++ {
  1044. ta.viewedArrayBuf.ensureNotDetached(true)
  1045. if i > 0 {
  1046. buf.WriteRune(',')
  1047. }
  1048. item := ta.typedArray.get(ta.offset + i)
  1049. r.writeItemLocaleString(item, &buf)
  1050. }
  1051. return buf.String()
  1052. }
  1053. panic(r.NewTypeError("Method TypedArray.prototype.toLocaleString called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  1054. }
  1055. func (r *Runtime) typedArrayProto_values(call FunctionCall) Value {
  1056. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  1057. ta.viewedArrayBuf.ensureNotDetached(true)
  1058. return r.createArrayIterator(ta.val, iterationKindValue)
  1059. }
  1060. panic(r.NewTypeError("Method TypedArray.prototype.values called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: call.This})))
  1061. }
  1062. func (r *Runtime) typedArrayProto_toStringTag(call FunctionCall) Value {
  1063. if obj, ok := call.This.(*Object); ok {
  1064. if ta, ok := obj.self.(*typedArrayObject); ok {
  1065. return nilSafe(ta.defaultCtor.self.getStr("name", nil))
  1066. }
  1067. }
  1068. return _undefined
  1069. }
  1070. func (r *Runtime) newTypedArray([]Value, *Object) *Object {
  1071. panic(r.NewTypeError("Abstract class TypedArray not directly constructable"))
  1072. }
  1073. func (r *Runtime) typedArray_from(call FunctionCall) Value {
  1074. c := r.toObject(call.This)
  1075. var mapFc func(call FunctionCall) Value
  1076. thisValue := call.Argument(2)
  1077. if mapFn := call.Argument(1); mapFn != _undefined {
  1078. mapFc = r.toCallable(mapFn)
  1079. }
  1080. source := r.toObject(call.Argument(0))
  1081. usingIter := toMethod(source.self.getSym(SymIterator, nil))
  1082. if usingIter != nil {
  1083. values := r.iterableToList(source, usingIter)
  1084. ta := r.typedArrayCreate(c, intToValue(int64(len(values))))
  1085. if mapFc == nil {
  1086. for idx, val := range values {
  1087. ta.typedArray.set(idx, val)
  1088. }
  1089. } else {
  1090. fc := FunctionCall{
  1091. This: thisValue,
  1092. Arguments: []Value{nil, nil},
  1093. }
  1094. for idx, val := range values {
  1095. fc.Arguments[0], fc.Arguments[1] = val, intToValue(int64(idx))
  1096. val = mapFc(fc)
  1097. ta.typedArray.set(idx, val)
  1098. }
  1099. }
  1100. return ta.val
  1101. }
  1102. length := toIntStrict(toLength(source.self.getStr("length", nil)))
  1103. ta := r.typedArrayCreate(c, intToValue(int64(length)))
  1104. if mapFc == nil {
  1105. for i := 0; i < length; i++ {
  1106. ta.typedArray.set(i, nilSafe(source.self.getIdx(valueInt(i), nil)))
  1107. }
  1108. } else {
  1109. fc := FunctionCall{
  1110. This: thisValue,
  1111. Arguments: []Value{nil, nil},
  1112. }
  1113. for i := 0; i < length; i++ {
  1114. idx := valueInt(i)
  1115. fc.Arguments[0], fc.Arguments[1] = source.self.getIdx(idx, nil), idx
  1116. ta.typedArray.set(i, mapFc(fc))
  1117. }
  1118. }
  1119. return ta.val
  1120. }
  1121. func (r *Runtime) typedArray_of(call FunctionCall) Value {
  1122. ta := r.typedArrayCreate(r.toObject(call.This), intToValue(int64(len(call.Arguments))))
  1123. for i, val := range call.Arguments {
  1124. ta.typedArray.set(i, val)
  1125. }
  1126. return ta.val
  1127. }
  1128. func (r *Runtime) allocateTypedArray(newTarget *Object, length int, taCtor typedArrayObjectCtor, proto *Object) *typedArrayObject {
  1129. buf := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)
  1130. ta := taCtor(buf, 0, length, r.getPrototypeFromCtor(newTarget, nil, proto))
  1131. if length > 0 {
  1132. buf.data = allocByteSlice(length * ta.elemSize)
  1133. }
  1134. return ta
  1135. }
  1136. func (r *Runtime) typedArraySpeciesCreate(ta *typedArrayObject, args []Value) *typedArrayObject {
  1137. return r.typedArrayCreate(r.speciesConstructorObj(ta.val, ta.defaultCtor), args...)
  1138. }
  1139. func (r *Runtime) typedArrayCreate(ctor *Object, args ...Value) *typedArrayObject {
  1140. o := r.toConstructor(ctor)(args, ctor)
  1141. if ta, ok := o.self.(*typedArrayObject); ok {
  1142. ta.viewedArrayBuf.ensureNotDetached(true)
  1143. if len(args) == 1 {
  1144. if l, ok := args[0].(valueInt); ok {
  1145. if ta.length < int(l) {
  1146. panic(r.NewTypeError("Derived TypedArray constructor created an array which was too small"))
  1147. }
  1148. }
  1149. }
  1150. return ta
  1151. }
  1152. panic(r.NewTypeError("Invalid TypedArray: %s", o))
  1153. }
  1154. func (r *Runtime) typedArrayFrom(ctor, items *Object, mapFn, thisValue Value, taCtor typedArrayObjectCtor, proto *Object) *Object {
  1155. var mapFc func(call FunctionCall) Value
  1156. if mapFn != nil {
  1157. mapFc = r.toCallable(mapFn)
  1158. if thisValue == nil {
  1159. thisValue = _undefined
  1160. }
  1161. }
  1162. usingIter := toMethod(items.self.getSym(SymIterator, nil))
  1163. if usingIter != nil {
  1164. values := r.iterableToList(items, usingIter)
  1165. ta := r.allocateTypedArray(ctor, len(values), taCtor, proto)
  1166. if mapFc == nil {
  1167. for idx, val := range values {
  1168. ta.typedArray.set(idx, val)
  1169. }
  1170. } else {
  1171. fc := FunctionCall{
  1172. This: thisValue,
  1173. Arguments: []Value{nil, nil},
  1174. }
  1175. for idx, val := range values {
  1176. fc.Arguments[0], fc.Arguments[1] = val, intToValue(int64(idx))
  1177. val = mapFc(fc)
  1178. ta.typedArray.set(idx, val)
  1179. }
  1180. }
  1181. return ta.val
  1182. }
  1183. length := toIntStrict(toLength(items.self.getStr("length", nil)))
  1184. ta := r.allocateTypedArray(ctor, length, taCtor, proto)
  1185. if mapFc == nil {
  1186. for i := 0; i < length; i++ {
  1187. ta.typedArray.set(i, nilSafe(items.self.getIdx(valueInt(i), nil)))
  1188. }
  1189. } else {
  1190. fc := FunctionCall{
  1191. This: thisValue,
  1192. Arguments: []Value{nil, nil},
  1193. }
  1194. for i := 0; i < length; i++ {
  1195. idx := valueInt(i)
  1196. fc.Arguments[0], fc.Arguments[1] = items.self.getIdx(idx, nil), idx
  1197. ta.typedArray.set(i, mapFc(fc))
  1198. }
  1199. }
  1200. return ta.val
  1201. }
  1202. func (r *Runtime) _newTypedArrayFromArrayBuffer(ab *arrayBufferObject, args []Value, newTarget *Object, taCtor typedArrayObjectCtor, proto *Object) *Object {
  1203. ta := taCtor(ab, 0, 0, r.getPrototypeFromCtor(newTarget, nil, proto))
  1204. var byteOffset int
  1205. if len(args) > 1 && args[1] != nil && args[1] != _undefined {
  1206. byteOffset = r.toIndex(args[1])
  1207. if byteOffset%ta.elemSize != 0 {
  1208. panic(r.newError(r.getRangeError(), "Start offset of %s should be a multiple of %d", newTarget.self.getStr("name", nil), ta.elemSize))
  1209. }
  1210. }
  1211. var length int
  1212. if len(args) > 2 && args[2] != nil && args[2] != _undefined {
  1213. length = r.toIndex(args[2])
  1214. ab.ensureNotDetached(true)
  1215. if byteOffset+length*ta.elemSize > len(ab.data) {
  1216. panic(r.newError(r.getRangeError(), "Invalid typed array length: %d", length))
  1217. }
  1218. } else {
  1219. ab.ensureNotDetached(true)
  1220. if len(ab.data)%ta.elemSize != 0 {
  1221. panic(r.newError(r.getRangeError(), "Byte length of %s should be a multiple of %d", newTarget.self.getStr("name", nil), ta.elemSize))
  1222. }
  1223. length = (len(ab.data) - byteOffset) / ta.elemSize
  1224. if length < 0 {
  1225. panic(r.newError(r.getRangeError(), "Start offset %d is outside the bounds of the buffer", byteOffset))
  1226. }
  1227. }
  1228. ta.offset = byteOffset / ta.elemSize
  1229. ta.length = length
  1230. return ta.val
  1231. }
  1232. func (r *Runtime) _newTypedArrayFromTypedArray(src *typedArrayObject, newTarget *Object, taCtor typedArrayObjectCtor, proto *Object) *Object {
  1233. dst := r.allocateTypedArray(newTarget, 0, taCtor, proto)
  1234. src.viewedArrayBuf.ensureNotDetached(true)
  1235. l := src.length
  1236. arrayBuffer := r.getArrayBuffer()
  1237. dst.viewedArrayBuf.prototype = r.getPrototypeFromCtor(r.speciesConstructorObj(src.viewedArrayBuf.val, arrayBuffer), arrayBuffer, r.getArrayBufferPrototype())
  1238. dst.viewedArrayBuf.data = allocByteSlice(toIntStrict(int64(l) * int64(dst.elemSize)))
  1239. src.viewedArrayBuf.ensureNotDetached(true)
  1240. if src.defaultCtor == dst.defaultCtor {
  1241. copy(dst.viewedArrayBuf.data, src.viewedArrayBuf.data[src.offset*src.elemSize:])
  1242. dst.length = src.length
  1243. return dst.val
  1244. }
  1245. dst.length = l
  1246. for i := 0; i < l; i++ {
  1247. dst.typedArray.set(i, src.typedArray.get(src.offset+i))
  1248. }
  1249. return dst.val
  1250. }
  1251. func (r *Runtime) _newTypedArray(args []Value, newTarget *Object, taCtor typedArrayObjectCtor, proto *Object) *Object {
  1252. if newTarget == nil {
  1253. panic(r.needNew("TypedArray"))
  1254. }
  1255. if len(args) > 0 {
  1256. if obj, ok := args[0].(*Object); ok {
  1257. switch o := obj.self.(type) {
  1258. case *arrayBufferObject:
  1259. return r._newTypedArrayFromArrayBuffer(o, args, newTarget, taCtor, proto)
  1260. case *typedArrayObject:
  1261. return r._newTypedArrayFromTypedArray(o, newTarget, taCtor, proto)
  1262. default:
  1263. return r.typedArrayFrom(newTarget, obj, nil, nil, taCtor, proto)
  1264. }
  1265. }
  1266. }
  1267. var l int
  1268. if len(args) > 0 {
  1269. if arg0 := args[0]; arg0 != nil {
  1270. l = r.toIndex(arg0)
  1271. }
  1272. }
  1273. return r.allocateTypedArray(newTarget, l, taCtor, proto).val
  1274. }
  1275. func (r *Runtime) newUint8Array(args []Value, newTarget, proto *Object) *Object {
  1276. return r._newTypedArray(args, newTarget, r.newUint8ArrayObject, proto)
  1277. }
  1278. func (r *Runtime) newUint8ClampedArray(args []Value, newTarget, proto *Object) *Object {
  1279. return r._newTypedArray(args, newTarget, r.newUint8ClampedArrayObject, proto)
  1280. }
  1281. func (r *Runtime) newInt8Array(args []Value, newTarget, proto *Object) *Object {
  1282. return r._newTypedArray(args, newTarget, r.newInt8ArrayObject, proto)
  1283. }
  1284. func (r *Runtime) newUint16Array(args []Value, newTarget, proto *Object) *Object {
  1285. return r._newTypedArray(args, newTarget, r.newUint16ArrayObject, proto)
  1286. }
  1287. func (r *Runtime) newInt16Array(args []Value, newTarget, proto *Object) *Object {
  1288. return r._newTypedArray(args, newTarget, r.newInt16ArrayObject, proto)
  1289. }
  1290. func (r *Runtime) newUint32Array(args []Value, newTarget, proto *Object) *Object {
  1291. return r._newTypedArray(args, newTarget, r.newUint32ArrayObject, proto)
  1292. }
  1293. func (r *Runtime) newInt32Array(args []Value, newTarget, proto *Object) *Object {
  1294. return r._newTypedArray(args, newTarget, r.newInt32ArrayObject, proto)
  1295. }
  1296. func (r *Runtime) newFloat32Array(args []Value, newTarget, proto *Object) *Object {
  1297. return r._newTypedArray(args, newTarget, r.newFloat32ArrayObject, proto)
  1298. }
  1299. func (r *Runtime) newFloat64Array(args []Value, newTarget, proto *Object) *Object {
  1300. return r._newTypedArray(args, newTarget, r.newFloat64ArrayObject, proto)
  1301. }
  1302. func (r *Runtime) createArrayBufferProto(val *Object) objectImpl {
  1303. b := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  1304. byteLengthProp := &valueProperty{
  1305. accessor: true,
  1306. configurable: true,
  1307. getterFunc: r.newNativeFunc(r.arrayBufferProto_getByteLength, "get byteLength", 0),
  1308. }
  1309. b._put("byteLength", byteLengthProp)
  1310. b._putProp("constructor", r.getArrayBuffer(), true, false, true)
  1311. b._putProp("slice", r.newNativeFunc(r.arrayBufferProto_slice, "slice", 2), true, false, true)
  1312. b._putSym(SymToStringTag, valueProp(asciiString("ArrayBuffer"), false, false, true))
  1313. return b
  1314. }
  1315. func (r *Runtime) createArrayBuffer(val *Object) objectImpl {
  1316. o := r.newNativeConstructOnly(val, r.builtin_newArrayBuffer, r.getArrayBufferPrototype(), "ArrayBuffer", 1)
  1317. o._putProp("isView", r.newNativeFunc(r.arrayBuffer_isView, "isView", 1), true, false, true)
  1318. r.putSpeciesReturnThis(o)
  1319. return o
  1320. }
  1321. func (r *Runtime) createDataView(val *Object) objectImpl {
  1322. o := r.newNativeConstructOnly(val, r.newDataView, r.getDataViewPrototype(), "DataView", 1)
  1323. return o
  1324. }
  1325. func (r *Runtime) createTypedArray(val *Object) objectImpl {
  1326. o := r.newNativeConstructOnly(val, r.newTypedArray, r.getTypedArrayPrototype(), "TypedArray", 0)
  1327. o._putProp("from", r.newNativeFunc(r.typedArray_from, "from", 1), true, false, true)
  1328. o._putProp("of", r.newNativeFunc(r.typedArray_of, "of", 0), true, false, true)
  1329. r.putSpeciesReturnThis(o)
  1330. return o
  1331. }
  1332. func (r *Runtime) getTypedArray() *Object {
  1333. ret := r.global.TypedArray
  1334. if ret == nil {
  1335. ret = &Object{runtime: r}
  1336. r.global.TypedArray = ret
  1337. r.createTypedArray(ret)
  1338. }
  1339. return ret
  1340. }
  1341. func (r *Runtime) createTypedArrayCtor(val *Object, ctor func(args []Value, newTarget, proto *Object) *Object, name unistring.String, bytesPerElement int) {
  1342. p := r.newBaseObject(r.getTypedArrayPrototype(), classObject)
  1343. o := r.newNativeConstructOnly(val, func(args []Value, newTarget *Object) *Object {
  1344. return ctor(args, newTarget, p.val)
  1345. }, p.val, name, 3)
  1346. p._putProp("constructor", o.val, true, false, true)
  1347. o.prototype = r.getTypedArray()
  1348. bpe := intToValue(int64(bytesPerElement))
  1349. o._putProp("BYTES_PER_ELEMENT", bpe, false, false, false)
  1350. p._putProp("BYTES_PER_ELEMENT", bpe, false, false, false)
  1351. }
  1352. func addTypedArrays(t *objectTemplate) {
  1353. t.putStr("ArrayBuffer", func(r *Runtime) Value { return valueProp(r.getArrayBuffer(), true, false, true) })
  1354. t.putStr("DataView", func(r *Runtime) Value { return valueProp(r.getDataView(), true, false, true) })
  1355. t.putStr("Uint8Array", func(r *Runtime) Value { return valueProp(r.getUint8Array(), true, false, true) })
  1356. t.putStr("Uint8ClampedArray", func(r *Runtime) Value { return valueProp(r.getUint8ClampedArray(), true, false, true) })
  1357. t.putStr("Int8Array", func(r *Runtime) Value { return valueProp(r.getInt8Array(), true, false, true) })
  1358. t.putStr("Uint16Array", func(r *Runtime) Value { return valueProp(r.getUint16Array(), true, false, true) })
  1359. t.putStr("Int16Array", func(r *Runtime) Value { return valueProp(r.getInt16Array(), true, false, true) })
  1360. t.putStr("Uint32Array", func(r *Runtime) Value { return valueProp(r.getUint32Array(), true, false, true) })
  1361. t.putStr("Int32Array", func(r *Runtime) Value { return valueProp(r.getInt32Array(), true, false, true) })
  1362. t.putStr("Float32Array", func(r *Runtime) Value { return valueProp(r.getFloat32Array(), true, false, true) })
  1363. t.putStr("Float64Array", func(r *Runtime) Value { return valueProp(r.getFloat64Array(), true, false, true) })
  1364. }
  1365. func createTypedArrayProtoTemplate() *objectTemplate {
  1366. t := newObjectTemplate()
  1367. t.protoFactory = func(r *Runtime) *Object {
  1368. return r.global.ObjectPrototype
  1369. }
  1370. t.putStr("buffer", func(r *Runtime) Value {
  1371. return &valueProperty{
  1372. accessor: true,
  1373. configurable: true,
  1374. getterFunc: r.newNativeFunc(r.typedArrayProto_getBuffer, "get buffer", 0),
  1375. }
  1376. })
  1377. t.putStr("byteLength", func(r *Runtime) Value {
  1378. return &valueProperty{
  1379. accessor: true,
  1380. configurable: true,
  1381. getterFunc: r.newNativeFunc(r.typedArrayProto_getByteLen, "get byteLength", 0),
  1382. }
  1383. })
  1384. t.putStr("byteOffset", func(r *Runtime) Value {
  1385. return &valueProperty{
  1386. accessor: true,
  1387. configurable: true,
  1388. getterFunc: r.newNativeFunc(r.typedArrayProto_getByteOffset, "get byteOffset", 0),
  1389. }
  1390. })
  1391. t.putStr("at", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_at, "at", 1) })
  1392. t.putStr("constructor", func(r *Runtime) Value { return valueProp(r.getTypedArray(), true, false, true) })
  1393. t.putStr("copyWithin", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_copyWithin, "copyWithin", 2) })
  1394. t.putStr("entries", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_entries, "entries", 0) })
  1395. t.putStr("every", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_every, "every", 1) })
  1396. t.putStr("fill", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_fill, "fill", 1) })
  1397. t.putStr("filter", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_filter, "filter", 1) })
  1398. t.putStr("find", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_find, "find", 1) })
  1399. t.putStr("findIndex", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_findIndex, "findIndex", 1) })
  1400. t.putStr("findLast", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_findLast, "findLast", 1) })
  1401. t.putStr("findLastIndex", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_findLastIndex, "findLastIndex", 1) })
  1402. t.putStr("forEach", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_forEach, "forEach", 1) })
  1403. t.putStr("includes", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_includes, "includes", 1) })
  1404. t.putStr("indexOf", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_indexOf, "indexOf", 1) })
  1405. t.putStr("join", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_join, "join", 1) })
  1406. t.putStr("keys", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_keys, "keys", 0) })
  1407. t.putStr("lastIndexOf", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_lastIndexOf, "lastIndexOf", 1) })
  1408. t.putStr("length", func(r *Runtime) Value {
  1409. return &valueProperty{
  1410. accessor: true,
  1411. configurable: true,
  1412. getterFunc: r.newNativeFunc(r.typedArrayProto_getLength, "get length", 0),
  1413. }
  1414. })
  1415. t.putStr("map", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_map, "map", 1) })
  1416. t.putStr("reduce", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_reduce, "reduce", 1) })
  1417. t.putStr("reduceRight", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_reduceRight, "reduceRight", 1) })
  1418. t.putStr("reverse", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_reverse, "reverse", 0) })
  1419. t.putStr("set", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_set, "set", 1) })
  1420. t.putStr("slice", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_slice, "slice", 2) })
  1421. t.putStr("some", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_some, "some", 1) })
  1422. t.putStr("sort", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_sort, "sort", 1) })
  1423. t.putStr("subarray", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_subarray, "subarray", 2) })
  1424. t.putStr("toLocaleString", func(r *Runtime) Value { return r.methodProp(r.typedArrayProto_toLocaleString, "toLocaleString", 0) })
  1425. t.putStr("toString", func(r *Runtime) Value { return valueProp(r.getArrayToString(), true, false, true) })
  1426. t.putStr("values", func(r *Runtime) Value { return valueProp(r.getTypedArrayValues(), true, false, true) })
  1427. t.putSym(SymIterator, func(r *Runtime) Value { return valueProp(r.getTypedArrayValues(), true, false, true) })
  1428. t.putSym(SymToStringTag, func(r *Runtime) Value {
  1429. return &valueProperty{
  1430. getterFunc: r.newNativeFunc(r.typedArrayProto_toStringTag, "get [Symbol.toStringTag]", 0),
  1431. accessor: true,
  1432. configurable: true,
  1433. }
  1434. })
  1435. return t
  1436. }
  1437. func (r *Runtime) getTypedArrayValues() *Object {
  1438. ret := r.global.typedArrayValues
  1439. if ret == nil {
  1440. ret = r.newNativeFunc(r.typedArrayProto_values, "values", 0)
  1441. r.global.typedArrayValues = ret
  1442. }
  1443. return ret
  1444. }
  1445. var typedArrayProtoTemplate *objectTemplate
  1446. var typedArrayProtoTemplateOnce sync.Once
  1447. func getTypedArrayProtoTemplate() *objectTemplate {
  1448. typedArrayProtoTemplateOnce.Do(func() {
  1449. typedArrayProtoTemplate = createTypedArrayProtoTemplate()
  1450. })
  1451. return typedArrayProtoTemplate
  1452. }
  1453. func (r *Runtime) getTypedArrayPrototype() *Object {
  1454. ret := r.global.TypedArrayPrototype
  1455. if ret == nil {
  1456. ret = &Object{runtime: r}
  1457. r.global.TypedArrayPrototype = ret
  1458. r.newTemplatedObject(getTypedArrayProtoTemplate(), ret)
  1459. }
  1460. return ret
  1461. }
  1462. func (r *Runtime) getUint8Array() *Object {
  1463. ret := r.global.Uint8Array
  1464. if ret == nil {
  1465. ret = &Object{runtime: r}
  1466. r.global.Uint8Array = ret
  1467. r.createTypedArrayCtor(ret, r.newUint8Array, "Uint8Array", 1)
  1468. }
  1469. return ret
  1470. }
  1471. func (r *Runtime) getUint8ClampedArray() *Object {
  1472. ret := r.global.Uint8ClampedArray
  1473. if ret == nil {
  1474. ret = &Object{runtime: r}
  1475. r.global.Uint8ClampedArray = ret
  1476. r.createTypedArrayCtor(ret, r.newUint8ClampedArray, "Uint8ClampedArray", 1)
  1477. }
  1478. return ret
  1479. }
  1480. func (r *Runtime) getInt8Array() *Object {
  1481. ret := r.global.Int8Array
  1482. if ret == nil {
  1483. ret = &Object{runtime: r}
  1484. r.global.Int8Array = ret
  1485. r.createTypedArrayCtor(ret, r.newInt8Array, "Int8Array", 1)
  1486. }
  1487. return ret
  1488. }
  1489. func (r *Runtime) getUint16Array() *Object {
  1490. ret := r.global.Uint16Array
  1491. if ret == nil {
  1492. ret = &Object{runtime: r}
  1493. r.global.Uint16Array = ret
  1494. r.createTypedArrayCtor(ret, r.newUint16Array, "Uint16Array", 2)
  1495. }
  1496. return ret
  1497. }
  1498. func (r *Runtime) getInt16Array() *Object {
  1499. ret := r.global.Int16Array
  1500. if ret == nil {
  1501. ret = &Object{runtime: r}
  1502. r.global.Int16Array = ret
  1503. r.createTypedArrayCtor(ret, r.newInt16Array, "Int16Array", 2)
  1504. }
  1505. return ret
  1506. }
  1507. func (r *Runtime) getUint32Array() *Object {
  1508. ret := r.global.Uint32Array
  1509. if ret == nil {
  1510. ret = &Object{runtime: r}
  1511. r.global.Uint32Array = ret
  1512. r.createTypedArrayCtor(ret, r.newUint32Array, "Uint32Array", 4)
  1513. }
  1514. return ret
  1515. }
  1516. func (r *Runtime) getInt32Array() *Object {
  1517. ret := r.global.Int32Array
  1518. if ret == nil {
  1519. ret = &Object{runtime: r}
  1520. r.global.Int32Array = ret
  1521. r.createTypedArrayCtor(ret, r.newInt32Array, "Int32Array", 4)
  1522. }
  1523. return ret
  1524. }
  1525. func (r *Runtime) getFloat32Array() *Object {
  1526. ret := r.global.Float32Array
  1527. if ret == nil {
  1528. ret = &Object{runtime: r}
  1529. r.global.Float32Array = ret
  1530. r.createTypedArrayCtor(ret, r.newFloat32Array, "Float32Array", 4)
  1531. }
  1532. return ret
  1533. }
  1534. func (r *Runtime) getFloat64Array() *Object {
  1535. ret := r.global.Float64Array
  1536. if ret == nil {
  1537. ret = &Object{runtime: r}
  1538. r.global.Float64Array = ret
  1539. r.createTypedArrayCtor(ret, r.newFloat64Array, "Float64Array", 8)
  1540. }
  1541. return ret
  1542. }
  1543. func createDataViewProtoTemplate() *objectTemplate {
  1544. t := newObjectTemplate()
  1545. t.protoFactory = func(r *Runtime) *Object {
  1546. return r.global.ObjectPrototype
  1547. }
  1548. t.putStr("buffer", func(r *Runtime) Value {
  1549. return &valueProperty{
  1550. accessor: true,
  1551. configurable: true,
  1552. getterFunc: r.newNativeFunc(r.dataViewProto_getBuffer, "get buffer", 0),
  1553. }
  1554. })
  1555. t.putStr("byteLength", func(r *Runtime) Value {
  1556. return &valueProperty{
  1557. accessor: true,
  1558. configurable: true,
  1559. getterFunc: r.newNativeFunc(r.dataViewProto_getByteLen, "get byteLength", 0),
  1560. }
  1561. })
  1562. t.putStr("byteOffset", func(r *Runtime) Value {
  1563. return &valueProperty{
  1564. accessor: true,
  1565. configurable: true,
  1566. getterFunc: r.newNativeFunc(r.dataViewProto_getByteOffset, "get byteOffset", 0),
  1567. }
  1568. })
  1569. t.putStr("constructor", func(r *Runtime) Value { return valueProp(r.getDataView(), true, false, true) })
  1570. t.putStr("getFloat32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getFloat32, "getFloat32", 1) })
  1571. t.putStr("getFloat64", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getFloat64, "getFloat64", 1) })
  1572. t.putStr("getInt8", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getInt8, "getInt8", 1) })
  1573. t.putStr("getInt16", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getInt16, "getInt16", 1) })
  1574. t.putStr("getInt32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getInt32, "getInt32", 1) })
  1575. t.putStr("getUint8", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getUint8, "getUint8", 1) })
  1576. t.putStr("getUint16", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getUint16, "getUint16", 1) })
  1577. t.putStr("getUint32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_getUint32, "getUint32", 1) })
  1578. t.putStr("setFloat32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setFloat32, "setFloat32", 2) })
  1579. t.putStr("setFloat64", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setFloat64, "setFloat64", 2) })
  1580. t.putStr("setInt8", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setInt8, "setInt8", 2) })
  1581. t.putStr("setInt16", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setInt16, "setInt16", 2) })
  1582. t.putStr("setInt32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setInt32, "setInt32", 2) })
  1583. t.putStr("setUint8", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setUint8, "setUint8", 2) })
  1584. t.putStr("setUint16", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setUint16, "setUint16", 2) })
  1585. t.putStr("setUint32", func(r *Runtime) Value { return r.methodProp(r.dataViewProto_setUint32, "setUint32", 2) })
  1586. t.putSym(SymToStringTag, func(r *Runtime) Value { return valueProp(asciiString("DataView"), false, false, true) })
  1587. return t
  1588. }
  1589. var dataViewProtoTemplate *objectTemplate
  1590. var dataViewProtoTemplateOnce sync.Once
  1591. func getDataViewProtoTemplate() *objectTemplate {
  1592. dataViewProtoTemplateOnce.Do(func() {
  1593. dataViewProtoTemplate = createDataViewProtoTemplate()
  1594. })
  1595. return dataViewProtoTemplate
  1596. }
  1597. func (r *Runtime) getDataViewPrototype() *Object {
  1598. ret := r.global.DataViewPrototype
  1599. if ret == nil {
  1600. ret = &Object{runtime: r}
  1601. r.global.DataViewPrototype = ret
  1602. r.newTemplatedObject(getDataViewProtoTemplate(), ret)
  1603. }
  1604. return ret
  1605. }
  1606. func (r *Runtime) getDataView() *Object {
  1607. ret := r.global.DataView
  1608. if ret == nil {
  1609. ret = &Object{runtime: r}
  1610. r.global.DataView = ret
  1611. ret.self = r.createDataView(ret)
  1612. }
  1613. return ret
  1614. }
  1615. func (r *Runtime) getArrayBufferPrototype() *Object {
  1616. ret := r.global.ArrayBufferPrototype
  1617. if ret == nil {
  1618. ret = &Object{runtime: r}
  1619. r.global.ArrayBufferPrototype = ret
  1620. ret.self = r.createArrayBufferProto(ret)
  1621. }
  1622. return ret
  1623. }
  1624. func (r *Runtime) getArrayBuffer() *Object {
  1625. ret := r.global.ArrayBuffer
  1626. if ret == nil {
  1627. ret = &Object{runtime: r}
  1628. r.global.ArrayBuffer = ret
  1629. ret.self = r.createArrayBuffer(ret)
  1630. }
  1631. return ret
  1632. }