builtin_typedarrays.go 51 KB

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