builtin_typedarrays.go 68 KB

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