builtin_typedarrays.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. ta.viewedArrayBuf.ensureNotDetached()
  373. copy(data[(offset+to)*elemSize:], data[(offset+from)*elemSize:(offset+final)*elemSize])
  374. }
  375. return call.This
  376. }
  377. panic(r.NewTypeError("Method TypedArray.prototype.copyWithin called on incompatible receiver %s", call.This.String()))
  378. }
  379. func (r *Runtime) typedArrayProto_entries(call FunctionCall) Value {
  380. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  381. ta.viewedArrayBuf.ensureNotDetached()
  382. return r.createArrayIterator(ta.val, iterationKindKeyValue)
  383. }
  384. panic(r.NewTypeError("Method TypedArray.prototype.entries called on incompatible receiver %s", call.This.String()))
  385. }
  386. func (r *Runtime) typedArrayProto_every(call FunctionCall) Value {
  387. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  388. ta.viewedArrayBuf.ensureNotDetached()
  389. callbackFn := r.toCallable(call.Argument(0))
  390. fc := FunctionCall{
  391. This: call.Argument(1),
  392. Arguments: []Value{nil, nil, call.This},
  393. }
  394. for k := 0; k < ta.length; k++ {
  395. ta.viewedArrayBuf.ensureNotDetached()
  396. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  397. fc.Arguments[1] = intToValue(int64(k))
  398. if !callbackFn(fc).ToBoolean() {
  399. return valueFalse
  400. }
  401. }
  402. return valueTrue
  403. }
  404. panic(r.NewTypeError("Method TypedArray.prototype.every called on incompatible receiver %s", call.This.String()))
  405. }
  406. func (r *Runtime) typedArrayProto_fill(call FunctionCall) Value {
  407. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  408. ta.viewedArrayBuf.ensureNotDetached()
  409. l := int64(ta.length)
  410. k := toIntStrict(relToIdx(call.Argument(1).ToInteger(), l))
  411. var relEnd int64
  412. if endArg := call.Argument(2); endArg != _undefined {
  413. relEnd = endArg.ToInteger()
  414. } else {
  415. relEnd = l
  416. }
  417. final := toIntStrict(relToIdx(relEnd, l))
  418. value := ta.typedArray.toRaw(call.Argument(0))
  419. ta.viewedArrayBuf.ensureNotDetached()
  420. for ; k < final; k++ {
  421. ta.typedArray.setRaw(ta.offset+k, value)
  422. }
  423. return call.This
  424. }
  425. panic(r.NewTypeError("Method TypedArray.prototype.fill called on incompatible receiver %s", call.This.String()))
  426. }
  427. func (r *Runtime) typedArrayProto_filter(call FunctionCall) Value {
  428. o := r.toObject(call.This)
  429. if ta, ok := o.self.(*typedArrayObject); ok {
  430. ta.viewedArrayBuf.ensureNotDetached()
  431. callbackFn := r.toCallable(call.Argument(0))
  432. fc := FunctionCall{
  433. This: call.Argument(1),
  434. Arguments: []Value{nil, nil, call.This},
  435. }
  436. buf := make([]byte, 0, ta.length*ta.elemSize)
  437. captured := 0
  438. for k := 0; k < ta.length; k++ {
  439. ta.viewedArrayBuf.ensureNotDetached()
  440. fc.Arguments[0] = ta.typedArray.get(k)
  441. fc.Arguments[1] = intToValue(int64(k))
  442. if callbackFn(fc).ToBoolean() {
  443. i := (ta.offset + k) * ta.elemSize
  444. buf = append(buf, ta.viewedArrayBuf.data[i:i+ta.elemSize]...)
  445. captured++
  446. }
  447. }
  448. c := r.speciesConstructorObj(o, ta.defaultCtor)
  449. ab := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)
  450. ab.data = buf
  451. kept := r.toConstructor(ta.defaultCtor)([]Value{ab.val}, ta.defaultCtor)
  452. if c == ta.defaultCtor {
  453. return kept
  454. } else {
  455. ret := r.typedArrayCreate(c, []Value{intToValue(int64(captured))})
  456. keptTa := kept.self.(*typedArrayObject)
  457. for i := 0; i < captured; i++ {
  458. ret.typedArray.set(i, keptTa.typedArray.get(i))
  459. }
  460. return ret.val
  461. }
  462. }
  463. panic(r.NewTypeError("Method TypedArray.prototype.filter called on incompatible receiver %s", call.This.String()))
  464. }
  465. func (r *Runtime) typedArrayProto_find(call FunctionCall) Value {
  466. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  467. ta.viewedArrayBuf.ensureNotDetached()
  468. predicate := r.toCallable(call.Argument(0))
  469. fc := FunctionCall{
  470. This: call.Argument(1),
  471. Arguments: []Value{nil, nil, call.This},
  472. }
  473. for k := 0; k < ta.length; k++ {
  474. ta.viewedArrayBuf.ensureNotDetached()
  475. val := ta.typedArray.get(ta.offset + k)
  476. fc.Arguments[0] = val
  477. fc.Arguments[1] = intToValue(int64(k))
  478. if predicate(fc).ToBoolean() {
  479. return val
  480. }
  481. }
  482. return _undefined
  483. }
  484. panic(r.NewTypeError("Method TypedArray.prototype.find called on incompatible receiver %s", call.This.String()))
  485. }
  486. func (r *Runtime) typedArrayProto_findIndex(call FunctionCall) Value {
  487. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  488. ta.viewedArrayBuf.ensureNotDetached()
  489. predicate := r.toCallable(call.Argument(0))
  490. fc := FunctionCall{
  491. This: call.Argument(1),
  492. Arguments: []Value{nil, nil, call.This},
  493. }
  494. for k := 0; k < ta.length; k++ {
  495. ta.viewedArrayBuf.ensureNotDetached()
  496. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  497. fc.Arguments[1] = intToValue(int64(k))
  498. if predicate(fc).ToBoolean() {
  499. return fc.Arguments[1]
  500. }
  501. }
  502. return intToValue(-1)
  503. }
  504. panic(r.NewTypeError("Method TypedArray.prototype.findIndex called on incompatible receiver %s", call.This.String()))
  505. }
  506. func (r *Runtime) typedArrayProto_forEach(call FunctionCall) Value {
  507. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  508. ta.viewedArrayBuf.ensureNotDetached()
  509. callbackFn := r.toCallable(call.Argument(0))
  510. fc := FunctionCall{
  511. This: call.Argument(1),
  512. Arguments: []Value{nil, nil, call.This},
  513. }
  514. for k := 0; k < ta.length; k++ {
  515. ta.viewedArrayBuf.ensureNotDetached()
  516. if val := ta.typedArray.get(k); val != nil {
  517. fc.Arguments[0] = val
  518. fc.Arguments[1] = intToValue(int64(k))
  519. callbackFn(fc)
  520. }
  521. }
  522. return _undefined
  523. }
  524. panic(r.NewTypeError("Method TypedArray.prototype.forEach called on incompatible receiver %s", call.This.String()))
  525. }
  526. func (r *Runtime) typedArrayProto_includes(call FunctionCall) Value {
  527. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  528. ta.viewedArrayBuf.ensureNotDetached()
  529. length := int64(ta.length)
  530. if length == 0 {
  531. return valueFalse
  532. }
  533. n := call.Argument(1).ToInteger()
  534. if n >= length {
  535. return valueFalse
  536. }
  537. if n < 0 {
  538. n = max(length+n, 0)
  539. }
  540. ta.viewedArrayBuf.ensureNotDetached()
  541. searchElement := call.Argument(0)
  542. if searchElement == _negativeZero {
  543. searchElement = _positiveZero
  544. }
  545. if ta.typedArray.typeMatch(searchElement) {
  546. se := ta.typedArray.toRaw(searchElement)
  547. for k := toIntStrict(n); k < ta.length; k++ {
  548. if ta.typedArray.getRaw(ta.offset+k) == se {
  549. return valueTrue
  550. }
  551. }
  552. }
  553. return valueFalse
  554. }
  555. panic(r.NewTypeError("Method TypedArray.prototype.includes called on incompatible receiver %s", call.This.String()))
  556. }
  557. func (r *Runtime) typedArrayProto_indexOf(call FunctionCall) Value {
  558. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  559. ta.viewedArrayBuf.ensureNotDetached()
  560. length := int64(ta.length)
  561. if length == 0 {
  562. return intToValue(-1)
  563. }
  564. n := call.Argument(1).ToInteger()
  565. if n >= length {
  566. return intToValue(-1)
  567. }
  568. if n < 0 {
  569. n = max(length+n, 0)
  570. }
  571. ta.viewedArrayBuf.ensureNotDetached()
  572. searchElement := call.Argument(0)
  573. if searchElement == _negativeZero {
  574. searchElement = _positiveZero
  575. }
  576. if !IsNaN(searchElement) && ta.typedArray.typeMatch(searchElement) {
  577. se := ta.typedArray.toRaw(searchElement)
  578. for k := toIntStrict(n); k < ta.length; k++ {
  579. if ta.typedArray.getRaw(ta.offset+k) == se {
  580. return intToValue(int64(k))
  581. }
  582. }
  583. }
  584. return intToValue(-1)
  585. }
  586. panic(r.NewTypeError("Method TypedArray.prototype.indexOf called on incompatible receiver %s", call.This.String()))
  587. }
  588. func (r *Runtime) typedArrayProto_join(call FunctionCall) Value {
  589. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  590. ta.viewedArrayBuf.ensureNotDetached()
  591. s := call.Argument(0)
  592. var sep valueString
  593. if s != _undefined {
  594. sep = s.toString()
  595. } else {
  596. sep = asciiString(",")
  597. }
  598. l := ta.length
  599. if l == 0 {
  600. return stringEmpty
  601. }
  602. var buf valueStringBuilder
  603. ta.viewedArrayBuf.ensureNotDetached()
  604. element0 := ta.typedArray.get(0)
  605. if element0 != nil && element0 != _undefined && element0 != _null {
  606. buf.WriteString(element0.toString())
  607. }
  608. for i := 1; i < l; i++ {
  609. ta.viewedArrayBuf.ensureNotDetached()
  610. buf.WriteString(sep)
  611. element := ta.typedArray.get(i)
  612. if element != nil && element != _undefined && element != _null {
  613. buf.WriteString(element.toString())
  614. }
  615. }
  616. return buf.String()
  617. }
  618. panic(r.NewTypeError("Method TypedArray.prototype.join called on incompatible receiver"))
  619. }
  620. func (r *Runtime) typedArrayProto_keys(call FunctionCall) Value {
  621. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  622. ta.viewedArrayBuf.ensureNotDetached()
  623. return r.createArrayIterator(ta.val, iterationKindKey)
  624. }
  625. panic(r.NewTypeError("Method TypedArray.prototype.keys called on incompatible receiver %s", call.This.String()))
  626. }
  627. func (r *Runtime) typedArrayProto_lastIndexOf(call FunctionCall) Value {
  628. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  629. ta.viewedArrayBuf.ensureNotDetached()
  630. length := int64(ta.length)
  631. if length == 0 {
  632. return intToValue(-1)
  633. }
  634. var fromIndex int64
  635. if len(call.Arguments) < 2 {
  636. fromIndex = length - 1
  637. } else {
  638. fromIndex = call.Argument(1).ToInteger()
  639. if fromIndex >= 0 {
  640. fromIndex = min(fromIndex, length-1)
  641. } else {
  642. fromIndex += length
  643. if fromIndex < 0 {
  644. fromIndex = -1 // prevent underflow in toIntStrict() on 32-bit platforms
  645. }
  646. }
  647. }
  648. ta.viewedArrayBuf.ensureNotDetached()
  649. searchElement := call.Argument(0)
  650. if searchElement == _negativeZero {
  651. searchElement = _positiveZero
  652. }
  653. if !IsNaN(searchElement) && ta.typedArray.typeMatch(searchElement) {
  654. se := ta.typedArray.toRaw(searchElement)
  655. for k := toIntStrict(fromIndex); k >= 0; k-- {
  656. if ta.typedArray.getRaw(ta.offset+k) == se {
  657. return intToValue(int64(k))
  658. }
  659. }
  660. }
  661. return intToValue(-1)
  662. }
  663. panic(r.NewTypeError("Method TypedArray.prototype.lastIndexOf called on incompatible receiver %s", call.This.String()))
  664. }
  665. func (r *Runtime) typedArrayProto_map(call FunctionCall) Value {
  666. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  667. ta.viewedArrayBuf.ensureNotDetached()
  668. callbackFn := r.toCallable(call.Argument(0))
  669. fc := FunctionCall{
  670. This: call.Argument(1),
  671. Arguments: []Value{nil, nil, call.This},
  672. }
  673. dst := r.typedArraySpeciesCreate(ta, []Value{intToValue(int64(ta.length))})
  674. for i := 0; i < ta.length; i++ {
  675. ta.viewedArrayBuf.ensureNotDetached()
  676. fc.Arguments[0] = ta.typedArray.get(ta.offset + i)
  677. fc.Arguments[1] = intToValue(int64(i))
  678. dst.typedArray.set(i, callbackFn(fc))
  679. }
  680. return dst.val
  681. }
  682. panic(r.NewTypeError("Method TypedArray.prototype.map called on incompatible receiver %s", call.This.String()))
  683. }
  684. func (r *Runtime) typedArrayProto_reduce(call FunctionCall) Value {
  685. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  686. ta.viewedArrayBuf.ensureNotDetached()
  687. callbackFn := r.toCallable(call.Argument(0))
  688. fc := FunctionCall{
  689. This: _undefined,
  690. Arguments: []Value{nil, nil, nil, call.This},
  691. }
  692. k := 0
  693. if len(call.Arguments) >= 2 {
  694. fc.Arguments[0] = call.Argument(1)
  695. } else {
  696. if ta.length > 0 {
  697. fc.Arguments[0] = ta.typedArray.get(ta.offset + 0)
  698. k = 1
  699. }
  700. }
  701. if fc.Arguments[0] == nil {
  702. panic(r.NewTypeError("Reduce of empty array with no initial value"))
  703. }
  704. for ; k < ta.length; k++ {
  705. ta.viewedArrayBuf.ensureNotDetached()
  706. idx := valueInt(k)
  707. fc.Arguments[1] = ta.typedArray.get(ta.offset + k)
  708. fc.Arguments[2] = idx
  709. fc.Arguments[0] = callbackFn(fc)
  710. }
  711. return fc.Arguments[0]
  712. }
  713. panic(r.NewTypeError("Method TypedArray.prototype.reduce called on incompatible receiver %s", call.This.String()))
  714. }
  715. func (r *Runtime) typedArrayProto_reduceRight(call FunctionCall) Value {
  716. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  717. ta.viewedArrayBuf.ensureNotDetached()
  718. callbackFn := r.toCallable(call.Argument(0))
  719. fc := FunctionCall{
  720. This: _undefined,
  721. Arguments: []Value{nil, nil, nil, call.This},
  722. }
  723. k := ta.length - 1
  724. if len(call.Arguments) >= 2 {
  725. fc.Arguments[0] = call.Argument(1)
  726. } else {
  727. if k >= 0 {
  728. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  729. k--
  730. }
  731. }
  732. if fc.Arguments[0] == nil {
  733. panic(r.NewTypeError("Reduce of empty array with no initial value"))
  734. }
  735. for ; k >= 0; k-- {
  736. ta.viewedArrayBuf.ensureNotDetached()
  737. idx := valueInt(k)
  738. fc.Arguments[1] = ta.typedArray.get(ta.offset + k)
  739. fc.Arguments[2] = idx
  740. fc.Arguments[0] = callbackFn(fc)
  741. }
  742. return fc.Arguments[0]
  743. }
  744. panic(r.NewTypeError("Method TypedArray.prototype.reduceRight called on incompatible receiver %s", call.This.String()))
  745. }
  746. func (r *Runtime) typedArrayProto_reverse(call FunctionCall) Value {
  747. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  748. ta.viewedArrayBuf.ensureNotDetached()
  749. l := ta.length
  750. middle := l / 2
  751. for lower := 0; lower != middle; lower++ {
  752. upper := l - lower - 1
  753. ta.typedArray.swap(ta.offset+lower, ta.offset+upper)
  754. }
  755. return call.This
  756. }
  757. panic(r.NewTypeError("Method TypedArray.prototype.reverse called on incompatible receiver %s", call.This.String()))
  758. }
  759. func (r *Runtime) typedArrayProto_set(call FunctionCall) Value {
  760. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  761. srcObj := call.Argument(0).ToObject(r)
  762. targetOffset := toIntStrict(call.Argument(1).ToInteger())
  763. if targetOffset < 0 {
  764. panic(r.newError(r.global.RangeError, "offset should be >= 0"))
  765. }
  766. ta.viewedArrayBuf.ensureNotDetached()
  767. targetLen := ta.length
  768. if src, ok := srcObj.self.(*typedArrayObject); ok {
  769. src.viewedArrayBuf.ensureNotDetached()
  770. srcLen := src.length
  771. if x := srcLen + targetOffset; x < 0 || x > targetLen {
  772. panic(r.newError(r.global.RangeError, "Source is too large"))
  773. }
  774. if src.defaultCtor == ta.defaultCtor {
  775. copy(ta.viewedArrayBuf.data[(ta.offset+targetOffset)*ta.elemSize:],
  776. src.viewedArrayBuf.data[src.offset*src.elemSize:(src.offset+srcLen)*src.elemSize])
  777. } else {
  778. curSrc := uintptr(unsafe.Pointer(&src.viewedArrayBuf.data[src.offset*src.elemSize]))
  779. endSrc := curSrc + uintptr(srcLen*src.elemSize)
  780. curDst := uintptr(unsafe.Pointer(&ta.viewedArrayBuf.data[(ta.offset+targetOffset)*ta.elemSize]))
  781. dstOffset := ta.offset + targetOffset
  782. srcOffset := src.offset
  783. if ta.elemSize == src.elemSize {
  784. if curDst <= curSrc || curDst >= endSrc {
  785. for i := 0; i < srcLen; i++ {
  786. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  787. }
  788. } else {
  789. for i := srcLen - 1; i >= 0; i-- {
  790. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  791. }
  792. }
  793. } else {
  794. x := int(curDst-curSrc) / (src.elemSize - ta.elemSize)
  795. if x < 0 {
  796. x = 0
  797. } else if x > srcLen {
  798. x = srcLen
  799. }
  800. if ta.elemSize < src.elemSize {
  801. for i := x; i < srcLen; i++ {
  802. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  803. }
  804. for i := x - 1; i >= 0; i-- {
  805. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  806. }
  807. } else {
  808. for i := 0; i < x; i++ {
  809. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  810. }
  811. for i := srcLen - 1; i >= x; i-- {
  812. ta.typedArray.set(dstOffset+i, src.typedArray.get(srcOffset+i))
  813. }
  814. }
  815. }
  816. }
  817. } else {
  818. targetLen := ta.length
  819. srcLen := toIntStrict(toLength(srcObj.self.getStr("length", nil)))
  820. if x := srcLen + targetOffset; x < 0 || x > targetLen {
  821. panic(r.newError(r.global.RangeError, "Source is too large"))
  822. }
  823. for i := 0; i < srcLen; i++ {
  824. val := nilSafe(srcObj.self.getIdx(valueInt(i), nil))
  825. ta.viewedArrayBuf.ensureNotDetached()
  826. ta.typedArray.set(targetOffset+i, val)
  827. }
  828. }
  829. return _undefined
  830. }
  831. panic(r.NewTypeError("Method TypedArray.prototype.set called on incompatible receiver %s", call.This.String()))
  832. }
  833. func (r *Runtime) typedArrayProto_slice(call FunctionCall) Value {
  834. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  835. ta.viewedArrayBuf.ensureNotDetached()
  836. length := int64(ta.length)
  837. start := toIntStrict(relToIdx(call.Argument(0).ToInteger(), length))
  838. var e int64
  839. if endArg := call.Argument(1); endArg != _undefined {
  840. e = endArg.ToInteger()
  841. } else {
  842. e = length
  843. }
  844. end := toIntStrict(relToIdx(e, length))
  845. count := end - start
  846. if count < 0 {
  847. count = 0
  848. }
  849. dst := r.typedArraySpeciesCreate(ta, []Value{intToValue(int64(count))})
  850. if dst.defaultCtor == ta.defaultCtor {
  851. if count > 0 {
  852. ta.viewedArrayBuf.ensureNotDetached()
  853. offset := ta.offset
  854. elemSize := ta.elemSize
  855. copy(dst.viewedArrayBuf.data, ta.viewedArrayBuf.data[(offset+start)*elemSize:(offset+start+count)*elemSize])
  856. }
  857. } else {
  858. for i := 0; i < count; i++ {
  859. ta.viewedArrayBuf.ensureNotDetached()
  860. dst.typedArray.set(i, ta.typedArray.get(ta.offset+start+i))
  861. }
  862. }
  863. return dst.val
  864. }
  865. panic(r.NewTypeError("Method TypedArray.prototype.slice called on incompatible receiver %s", call.This.String()))
  866. }
  867. func (r *Runtime) typedArrayProto_some(call FunctionCall) Value {
  868. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  869. ta.viewedArrayBuf.ensureNotDetached()
  870. callbackFn := r.toCallable(call.Argument(0))
  871. fc := FunctionCall{
  872. This: call.Argument(1),
  873. Arguments: []Value{nil, nil, call.This},
  874. }
  875. for k := 0; k < ta.length; k++ {
  876. ta.viewedArrayBuf.ensureNotDetached()
  877. fc.Arguments[0] = ta.typedArray.get(ta.offset + k)
  878. fc.Arguments[1] = intToValue(int64(k))
  879. if callbackFn(fc).ToBoolean() {
  880. return valueTrue
  881. }
  882. }
  883. return valueFalse
  884. }
  885. panic(r.NewTypeError("Method TypedArray.prototype.some called on incompatible receiver %s", call.This.String()))
  886. }
  887. func (r *Runtime) typedArrayProto_sort(call FunctionCall) Value {
  888. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  889. ta.viewedArrayBuf.ensureNotDetached()
  890. var compareFn func(FunctionCall) Value
  891. if arg := call.Argument(0); arg != _undefined {
  892. compareFn = r.toCallable(arg)
  893. }
  894. ctx := typedArraySortCtx{
  895. ta: ta,
  896. compare: compareFn,
  897. }
  898. sort.Stable(&ctx)
  899. return call.This
  900. }
  901. panic(r.NewTypeError("Method TypedArray.prototype.sort called on incompatible receiver %s", call.This.String()))
  902. }
  903. func (r *Runtime) typedArrayProto_subarray(call FunctionCall) Value {
  904. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  905. l := int64(ta.length)
  906. beginIdx := relToIdx(call.Argument(0).ToInteger(), l)
  907. var relEnd int64
  908. if endArg := call.Argument(1); endArg != _undefined {
  909. relEnd = endArg.ToInteger()
  910. } else {
  911. relEnd = l
  912. }
  913. endIdx := relToIdx(relEnd, l)
  914. newLen := max(endIdx-beginIdx, 0)
  915. return r.typedArraySpeciesCreate(ta, []Value{ta.viewedArrayBuf.val,
  916. intToValue((int64(ta.offset) + beginIdx) * int64(ta.elemSize)),
  917. intToValue(newLen),
  918. }).val
  919. }
  920. panic(r.NewTypeError("Method TypedArray.prototype.subarray called on incompatible receiver %s", call.This.String()))
  921. }
  922. func (r *Runtime) typedArrayProto_toLocaleString(call FunctionCall) Value {
  923. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  924. length := ta.length
  925. var buf valueStringBuilder
  926. for i := 0; i < length; i++ {
  927. ta.viewedArrayBuf.ensureNotDetached()
  928. if i > 0 {
  929. buf.WriteRune(',')
  930. }
  931. item := ta.typedArray.get(i)
  932. r.writeItemLocaleString(item, &buf)
  933. }
  934. return buf.String()
  935. }
  936. panic(r.NewTypeError("Method TypedArray.prototype.toLocaleString called on incompatible receiver %s", call.This.String()))
  937. }
  938. func (r *Runtime) typedArrayProto_values(call FunctionCall) Value {
  939. if ta, ok := r.toObject(call.This).self.(*typedArrayObject); ok {
  940. ta.viewedArrayBuf.ensureNotDetached()
  941. return r.createArrayIterator(ta.val, iterationKindValue)
  942. }
  943. panic(r.NewTypeError("Method TypedArray.prototype.values called on incompatible receiver %s", call.This.String()))
  944. }
  945. func (r *Runtime) typedArrayProto_toStringTag(call FunctionCall) Value {
  946. if obj, ok := call.This.(*Object); ok {
  947. if ta, ok := obj.self.(*typedArrayObject); ok {
  948. return ta.defaultCtor.self.getStr("name", nil)
  949. }
  950. }
  951. return _undefined
  952. }
  953. func (r *Runtime) newTypedArray([]Value, *Object) *Object {
  954. panic(r.NewTypeError("Abstract class TypedArray not directly constructable"))
  955. }
  956. func (r *Runtime) typedArray_from(call FunctionCall) Value {
  957. mapFn := call.Argument(1)
  958. if mapFn == _undefined {
  959. mapFn = nil
  960. }
  961. return r.typedArrayFrom(r.toObject(call.This), call.Argument(0).ToObject(r), mapFn, call.Argument(2))
  962. }
  963. func (r *Runtime) typedArray_of(call FunctionCall) Value {
  964. ta := r.typedArrayCreate(r.toObject(call.This), []Value{intToValue(int64(len(call.Arguments)))})
  965. for i, val := range call.Arguments {
  966. ta.typedArray.set(i, val)
  967. }
  968. return ta.val
  969. }
  970. func (r *Runtime) allocateTypedArray(newTarget *Object, length int, taCtor typedArrayObjectCtor) *Object {
  971. buf := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)
  972. ta := taCtor(buf, 0, length, r.getPrototypeFromCtor(newTarget, nil, r.global.TypedArrayPrototype))
  973. if length > 0 {
  974. buf.data = allocByteSlice(length * ta.elemSize)
  975. }
  976. return ta.val
  977. }
  978. func (r *Runtime) typedArraySpeciesCreate(ta *typedArrayObject, args []Value) *typedArrayObject {
  979. return r.typedArrayCreate(r.speciesConstructorObj(ta.val, ta.defaultCtor), args)
  980. }
  981. func (r *Runtime) typedArrayCreate(ctor *Object, args []Value) *typedArrayObject {
  982. o := r.toConstructor(ctor)(args, ctor)
  983. if ta, ok := o.self.(*typedArrayObject); ok {
  984. ta.viewedArrayBuf.ensureNotDetached()
  985. if len(args) == 1 {
  986. if l, ok := args[0].(valueInt); ok {
  987. if ta.length < int(l) {
  988. panic(r.NewTypeError("Derived TypedArray constructor created an array which was too small"))
  989. }
  990. }
  991. }
  992. return ta
  993. }
  994. panic(r.NewTypeError("Invalid TypedArray: %s", o))
  995. }
  996. func (r *Runtime) typedArrayFrom(ctor, items *Object, mapFn, thisValue Value) *Object {
  997. var mapFc func(call FunctionCall) Value
  998. if mapFn != nil {
  999. mapFc = r.toCallable(mapFn)
  1000. if thisValue == nil {
  1001. thisValue = _undefined
  1002. }
  1003. }
  1004. usingIter := toMethod(items.self.getSym(SymIterator, nil))
  1005. if usingIter != nil {
  1006. iter := r.getIterator(items, usingIter)
  1007. var values []Value
  1008. r.iterate(iter, func(item Value) {
  1009. values = append(values, item)
  1010. })
  1011. ta := r.typedArrayCreate(ctor, []Value{intToValue(int64(len(values)))})
  1012. if mapFc == nil {
  1013. for idx, val := range values {
  1014. ta.typedArray.set(idx, val)
  1015. }
  1016. } else {
  1017. fc := FunctionCall{
  1018. This: thisValue,
  1019. Arguments: []Value{nil, nil},
  1020. }
  1021. for idx, val := range values {
  1022. fc.Arguments[0], fc.Arguments[1] = val, intToValue(int64(idx))
  1023. val = mapFc(fc)
  1024. ta.typedArray.set(idx, val)
  1025. }
  1026. }
  1027. return ta.val
  1028. }
  1029. length := toIntStrict(toLength(items.self.getStr("length", nil)))
  1030. ta := r.typedArrayCreate(ctor, []Value{intToValue(int64(length))})
  1031. if mapFc == nil {
  1032. for i := 0; i < length; i++ {
  1033. ta.typedArray.set(i, nilSafe(items.self.getIdx(valueInt(i), nil)))
  1034. }
  1035. } else {
  1036. fc := FunctionCall{
  1037. This: thisValue,
  1038. Arguments: []Value{nil, nil},
  1039. }
  1040. for i := 0; i < length; i++ {
  1041. idx := valueInt(i)
  1042. fc.Arguments[0], fc.Arguments[1] = items.self.getIdx(idx, nil), idx
  1043. ta.typedArray.set(i, mapFc(fc))
  1044. }
  1045. }
  1046. return ta.val
  1047. }
  1048. func (r *Runtime) _newTypedArrayFromArrayBuffer(ab *arrayBufferObject, args []Value, newTarget *Object, taCtor typedArrayObjectCtor) *Object {
  1049. ta := taCtor(ab, 0, 0, r.getPrototypeFromCtor(newTarget, nil, r.global.TypedArrayPrototype))
  1050. var byteOffset int
  1051. if len(args) > 1 && args[1] != nil && args[1] != _undefined {
  1052. byteOffset = r.toIndex(args[1])
  1053. if byteOffset%ta.elemSize != 0 {
  1054. panic(r.newError(r.global.RangeError, "Start offset of %s should be a multiple of %d", newTarget.self.getStr("name", nil), ta.elemSize))
  1055. }
  1056. }
  1057. ab.ensureNotDetached()
  1058. var length int
  1059. if len(args) > 2 && args[2] != nil && args[2] != _undefined {
  1060. length = r.toIndex(args[2])
  1061. if byteOffset+length*ta.elemSize > len(ab.data) {
  1062. panic(r.newError(r.global.RangeError, "Invalid typed array length: %d", length))
  1063. }
  1064. } else {
  1065. if len(ab.data)%ta.elemSize != 0 {
  1066. panic(r.newError(r.global.RangeError, "Byte length of %s should be a multiple of %d", newTarget.self.getStr("name", nil), ta.elemSize))
  1067. }
  1068. length = (len(ab.data) - byteOffset) / ta.elemSize
  1069. }
  1070. ta.offset = byteOffset / ta.elemSize
  1071. ta.length = length
  1072. return ta.val
  1073. }
  1074. func (r *Runtime) _newTypedArrayFromTypedArray(src *typedArrayObject, newTarget *Object) *Object {
  1075. dst := r.typedArrayCreate(newTarget, []Value{_positiveZero})
  1076. src.viewedArrayBuf.ensureNotDetached()
  1077. l := src.length
  1078. dst.viewedArrayBuf.prototype = r.getPrototypeFromCtor(r.toObject(src.viewedArrayBuf.getStr("constructor", nil)), r.global.ArrayBuffer, r.global.ArrayBufferPrototype)
  1079. dst.viewedArrayBuf.data = allocByteSlice(toIntStrict(int64(l) * int64(dst.elemSize)))
  1080. if src.defaultCtor == dst.defaultCtor {
  1081. copy(dst.viewedArrayBuf.data, src.viewedArrayBuf.data[src.offset*src.elemSize:])
  1082. dst.length = src.length
  1083. return dst.val
  1084. }
  1085. dst.length = l
  1086. for i := 0; i < l; i++ {
  1087. dst.typedArray.set(i, src.typedArray.get(src.offset+i))
  1088. }
  1089. return dst.val
  1090. }
  1091. func (r *Runtime) _newTypedArray(args []Value, newTarget *Object, taCtor typedArrayObjectCtor) *Object {
  1092. if newTarget == nil {
  1093. panic(r.needNew("TypedArray"))
  1094. }
  1095. if len(args) > 0 {
  1096. if obj, ok := args[0].(*Object); ok {
  1097. switch o := obj.self.(type) {
  1098. case *arrayBufferObject:
  1099. return r._newTypedArrayFromArrayBuffer(o, args, newTarget, taCtor)
  1100. case *typedArrayObject:
  1101. return r._newTypedArrayFromTypedArray(o, newTarget)
  1102. default:
  1103. return r.typedArrayFrom(newTarget, obj, nil, nil)
  1104. }
  1105. }
  1106. }
  1107. var l int
  1108. if len(args) > 0 {
  1109. if arg0 := args[0]; arg0 != nil {
  1110. l = r.toIndex(arg0)
  1111. }
  1112. }
  1113. return r.allocateTypedArray(newTarget, l, taCtor)
  1114. }
  1115. func (r *Runtime) newUint8Array(args []Value, newTarget *Object) *Object {
  1116. return r._newTypedArray(args, newTarget, r.newUint8ArrayObject)
  1117. }
  1118. func (r *Runtime) newUint8ClampedArray(args []Value, newTarget *Object) *Object {
  1119. return r._newTypedArray(args, newTarget, r.newUint8ClampedArrayObject)
  1120. }
  1121. func (r *Runtime) newInt8Array(args []Value, newTarget *Object) *Object {
  1122. return r._newTypedArray(args, newTarget, r.newInt8ArrayObject)
  1123. }
  1124. func (r *Runtime) newUint16Array(args []Value, newTarget *Object) *Object {
  1125. return r._newTypedArray(args, newTarget, r.newUint16ArrayObject)
  1126. }
  1127. func (r *Runtime) newInt16Array(args []Value, newTarget *Object) *Object {
  1128. return r._newTypedArray(args, newTarget, r.newInt16ArrayObject)
  1129. }
  1130. func (r *Runtime) newUint32Array(args []Value, newTarget *Object) *Object {
  1131. return r._newTypedArray(args, newTarget, r.newUint32ArrayObject)
  1132. }
  1133. func (r *Runtime) newInt32Array(args []Value, newTarget *Object) *Object {
  1134. return r._newTypedArray(args, newTarget, r.newInt32ArrayObject)
  1135. }
  1136. func (r *Runtime) newFloat32Array(args []Value, newTarget *Object) *Object {
  1137. return r._newTypedArray(args, newTarget, r.newFloat32ArrayObject)
  1138. }
  1139. func (r *Runtime) newFloat64Array(args []Value, newTarget *Object) *Object {
  1140. return r._newTypedArray(args, newTarget, r.newFloat64ArrayObject)
  1141. }
  1142. func (r *Runtime) createArrayBufferProto(val *Object) objectImpl {
  1143. b := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  1144. byteLengthProp := &valueProperty{
  1145. accessor: true,
  1146. configurable: true,
  1147. getterFunc: r.newNativeFunc(r.arrayBufferProto_getByteLength, nil, "get byteLength", nil, 0),
  1148. }
  1149. b._put("byteLength", byteLengthProp)
  1150. b._putProp("constructor", r.global.ArrayBuffer, true, false, true)
  1151. b._putProp("slice", r.newNativeFunc(r.arrayBufferProto_slice, nil, "slice", nil, 2), true, false, true)
  1152. b._putSym(SymToStringTag, valueProp(asciiString("ArrayBuffer"), false, false, true))
  1153. return b
  1154. }
  1155. func (r *Runtime) createArrayBuffer(val *Object) objectImpl {
  1156. o := r.newNativeConstructOnly(val, r.builtin_newArrayBuffer, r.global.ArrayBufferPrototype, "ArrayBuffer", 1)
  1157. o._putProp("isView", r.newNativeFunc(r.arrayBuffer_isView, nil, "isView", nil, 1), true, false, true)
  1158. o._putSym(SymSpecies, &valueProperty{
  1159. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1160. accessor: true,
  1161. configurable: true,
  1162. })
  1163. return o
  1164. }
  1165. func (r *Runtime) createDataViewProto(val *Object) objectImpl {
  1166. b := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  1167. b._put("buffer", &valueProperty{
  1168. accessor: true,
  1169. configurable: true,
  1170. getterFunc: r.newNativeFunc(r.dataViewProto_getBuffer, nil, "get buffer", nil, 0),
  1171. })
  1172. b._put("byteLength", &valueProperty{
  1173. accessor: true,
  1174. configurable: true,
  1175. getterFunc: r.newNativeFunc(r.dataViewProto_getByteLen, nil, "get byteLength", nil, 0),
  1176. })
  1177. b._put("byteOffset", &valueProperty{
  1178. accessor: true,
  1179. configurable: true,
  1180. getterFunc: r.newNativeFunc(r.dataViewProto_getByteOffset, nil, "get byteOffset", nil, 0),
  1181. })
  1182. b._putProp("constructor", r.global.DataView, true, false, true)
  1183. b._putProp("getFloat32", r.newNativeFunc(r.dataViewProto_getFloat32, nil, "getFloat32", nil, 1), true, false, true)
  1184. b._putProp("getFloat64", r.newNativeFunc(r.dataViewProto_getFloat64, nil, "getFloat64", nil, 1), true, false, true)
  1185. b._putProp("getInt8", r.newNativeFunc(r.dataViewProto_getInt8, nil, "getInt8", nil, 1), true, false, true)
  1186. b._putProp("getInt16", r.newNativeFunc(r.dataViewProto_getInt16, nil, "getInt16", nil, 1), true, false, true)
  1187. b._putProp("getInt32", r.newNativeFunc(r.dataViewProto_getInt32, nil, "getInt32", nil, 1), true, false, true)
  1188. b._putProp("getUint8", r.newNativeFunc(r.dataViewProto_getUint8, nil, "getUint8", nil, 1), true, false, true)
  1189. b._putProp("getUint16", r.newNativeFunc(r.dataViewProto_getUint16, nil, "getUint16", nil, 1), true, false, true)
  1190. b._putProp("getUint32", r.newNativeFunc(r.dataViewProto_getUint32, nil, "getUint32", nil, 1), true, false, true)
  1191. b._putProp("setFloat32", r.newNativeFunc(r.dataViewProto_setFloat32, nil, "setFloat32", nil, 2), true, false, true)
  1192. b._putProp("setFloat64", r.newNativeFunc(r.dataViewProto_setFloat64, nil, "setFloat64", nil, 2), true, false, true)
  1193. b._putProp("setInt8", r.newNativeFunc(r.dataViewProto_setInt8, nil, "setInt8", nil, 2), true, false, true)
  1194. b._putProp("setInt16", r.newNativeFunc(r.dataViewProto_setInt16, nil, "setInt16", nil, 2), true, false, true)
  1195. b._putProp("setInt32", r.newNativeFunc(r.dataViewProto_setInt32, nil, "setInt32", nil, 2), true, false, true)
  1196. b._putProp("setUint8", r.newNativeFunc(r.dataViewProto_setUint8, nil, "setUint8", nil, 2), true, false, true)
  1197. b._putProp("setUint16", r.newNativeFunc(r.dataViewProto_setUint16, nil, "setUint16", nil, 2), true, false, true)
  1198. b._putProp("setUint32", r.newNativeFunc(r.dataViewProto_setUint32, nil, "setUint32", nil, 2), true, false, true)
  1199. b._putSym(SymToStringTag, valueProp(asciiString("DataView"), false, false, true))
  1200. return b
  1201. }
  1202. func (r *Runtime) createDataView(val *Object) objectImpl {
  1203. o := r.newNativeConstructOnly(val, r.newDataView, r.global.DataViewPrototype, "DataView", 3)
  1204. return o
  1205. }
  1206. func (r *Runtime) createTypedArrayProto(val *Object) objectImpl {
  1207. b := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
  1208. b._put("buffer", &valueProperty{
  1209. accessor: true,
  1210. configurable: true,
  1211. getterFunc: r.newNativeFunc(r.typedArrayProto_getBuffer, nil, "get buffer", nil, 0),
  1212. })
  1213. b._put("byteLength", &valueProperty{
  1214. accessor: true,
  1215. configurable: true,
  1216. getterFunc: r.newNativeFunc(r.typedArrayProto_getByteLen, nil, "get byteLength", nil, 0),
  1217. })
  1218. b._put("byteOffset", &valueProperty{
  1219. accessor: true,
  1220. configurable: true,
  1221. getterFunc: r.newNativeFunc(r.typedArrayProto_getByteOffset, nil, "get byteOffset", nil, 0),
  1222. })
  1223. b._putProp("constructor", r.global.TypedArray, true, false, true)
  1224. b._putProp("copyWithin", r.newNativeFunc(r.typedArrayProto_copyWithin, nil, "copyWithin", nil, 2), true, false, true)
  1225. b._putProp("entries", r.newNativeFunc(r.typedArrayProto_entries, nil, "entries", nil, 0), true, false, true)
  1226. b._putProp("every", r.newNativeFunc(r.typedArrayProto_every, nil, "every", nil, 1), true, false, true)
  1227. b._putProp("fill", r.newNativeFunc(r.typedArrayProto_fill, nil, "fill", nil, 1), true, false, true)
  1228. b._putProp("filter", r.newNativeFunc(r.typedArrayProto_filter, nil, "filter", nil, 1), true, false, true)
  1229. b._putProp("find", r.newNativeFunc(r.typedArrayProto_find, nil, "find", nil, 1), true, false, true)
  1230. b._putProp("findIndex", r.newNativeFunc(r.typedArrayProto_findIndex, nil, "findIndex", nil, 1), true, false, true)
  1231. b._putProp("forEach", r.newNativeFunc(r.typedArrayProto_forEach, nil, "forEach", nil, 1), true, false, true)
  1232. b._putProp("includes", r.newNativeFunc(r.typedArrayProto_includes, nil, "includes", nil, 1), true, false, true)
  1233. b._putProp("indexOf", r.newNativeFunc(r.typedArrayProto_indexOf, nil, "indexOf", nil, 1), true, false, true)
  1234. b._putProp("join", r.newNativeFunc(r.typedArrayProto_join, nil, "join", nil, 1), true, false, true)
  1235. b._putProp("keys", r.newNativeFunc(r.typedArrayProto_keys, nil, "keys", nil, 0), true, false, true)
  1236. b._putProp("lastIndexOf", r.newNativeFunc(r.typedArrayProto_lastIndexOf, nil, "lastIndexOf", nil, 1), true, false, true)
  1237. b._put("length", &valueProperty{
  1238. accessor: true,
  1239. configurable: true,
  1240. getterFunc: r.newNativeFunc(r.typedArrayProto_getLength, nil, "get length", nil, 0),
  1241. })
  1242. b._putProp("map", r.newNativeFunc(r.typedArrayProto_map, nil, "map", nil, 1), true, false, true)
  1243. b._putProp("reduce", r.newNativeFunc(r.typedArrayProto_reduce, nil, "reduce", nil, 1), true, false, true)
  1244. b._putProp("reduceRight", r.newNativeFunc(r.typedArrayProto_reduceRight, nil, "reduceRight", nil, 1), true, false, true)
  1245. b._putProp("reverse", r.newNativeFunc(r.typedArrayProto_reverse, nil, "reverse", nil, 0), true, false, true)
  1246. b._putProp("set", r.newNativeFunc(r.typedArrayProto_set, nil, "set", nil, 1), true, false, true)
  1247. b._putProp("slice", r.newNativeFunc(r.typedArrayProto_slice, nil, "slice", nil, 2), true, false, true)
  1248. b._putProp("some", r.newNativeFunc(r.typedArrayProto_some, nil, "some", nil, 1), true, false, true)
  1249. b._putProp("sort", r.newNativeFunc(r.typedArrayProto_sort, nil, "sort", nil, 1), true, false, true)
  1250. b._putProp("subarray", r.newNativeFunc(r.typedArrayProto_subarray, nil, "subarray", nil, 2), true, false, true)
  1251. b._putProp("toLocaleString", r.newNativeFunc(r.typedArrayProto_toLocaleString, nil, "toLocaleString", nil, 0), true, false, true)
  1252. b._putProp("toString", r.global.arrayToString, true, false, true)
  1253. valuesFunc := r.newNativeFunc(r.typedArrayProto_values, nil, "values", nil, 0)
  1254. b._putProp("values", valuesFunc, true, false, true)
  1255. b._putSym(SymIterator, valueProp(valuesFunc, true, false, true))
  1256. b._putSym(SymToStringTag, &valueProperty{
  1257. getterFunc: r.newNativeFunc(r.typedArrayProto_toStringTag, nil, "get [Symbol.toStringTag]", nil, 0),
  1258. accessor: true,
  1259. configurable: true,
  1260. })
  1261. return b
  1262. }
  1263. func (r *Runtime) createTypedArray(val *Object) objectImpl {
  1264. o := r.newNativeConstructOnly(val, r.newTypedArray, r.global.TypedArrayPrototype, "TypedArray", 0)
  1265. o._putProp("from", r.newNativeFunc(r.typedArray_from, nil, "from", nil, 1), true, false, true)
  1266. o._putProp("of", r.newNativeFunc(r.typedArray_of, nil, "of", nil, 0), true, false, true)
  1267. o._putSym(SymSpecies, &valueProperty{
  1268. getterFunc: r.newNativeFunc(r.returnThis, nil, "get [Symbol.species]", nil, 0),
  1269. accessor: true,
  1270. configurable: true,
  1271. })
  1272. return o
  1273. }
  1274. func (r *Runtime) addPrototype(ctor *Object, proto *Object) *baseObject {
  1275. p := r.newBaseObject(proto, classObject)
  1276. p._putProp("constructor", ctor, true, false, true)
  1277. ctor.self._putProp("prototype", p.val, false, false, false)
  1278. return p
  1279. }
  1280. func (r *Runtime) typedArrayCreator(ctor func(args []Value, newTarget *Object) *Object, name unistring.String, bytesPerElement int) func(val *Object) objectImpl {
  1281. return func(val *Object) objectImpl {
  1282. o := r.newNativeConstructOnly(val, ctor, nil, name, 3)
  1283. o.prototype = r.global.TypedArray
  1284. proto := r.addPrototype(o.val, r.global.TypedArrayPrototype)
  1285. bpe := intToValue(int64(bytesPerElement))
  1286. o._putProp("BYTES_PER_ELEMENT", bpe, false, false, false)
  1287. proto._putProp("BYTES_PER_ELEMENT", bpe, false, false, false)
  1288. return o
  1289. }
  1290. }
  1291. func (r *Runtime) initTypedArrays() {
  1292. r.global.ArrayBufferPrototype = r.newLazyObject(r.createArrayBufferProto)
  1293. r.global.ArrayBuffer = r.newLazyObject(r.createArrayBuffer)
  1294. r.addToGlobal("ArrayBuffer", r.global.ArrayBuffer)
  1295. r.global.DataViewPrototype = r.newLazyObject(r.createDataViewProto)
  1296. r.global.DataView = r.newLazyObject(r.createDataView)
  1297. r.addToGlobal("DataView", r.global.DataView)
  1298. r.global.TypedArrayPrototype = r.newLazyObject(r.createTypedArrayProto)
  1299. r.global.TypedArray = r.newLazyObject(r.createTypedArray)
  1300. r.global.Uint8Array = r.newLazyObject(r.typedArrayCreator(r.newUint8Array, "Uint8Array", 1))
  1301. r.addToGlobal("Uint8Array", r.global.Uint8Array)
  1302. r.global.Uint8ClampedArray = r.newLazyObject(r.typedArrayCreator(r.newUint8ClampedArray, "Uint8ClampedArray", 1))
  1303. r.addToGlobal("Uint8ClampedArray", r.global.Uint8ClampedArray)
  1304. r.global.Int8Array = r.newLazyObject(r.typedArrayCreator(r.newInt8Array, "Int8Array", 1))
  1305. r.addToGlobal("Int8Array", r.global.Int8Array)
  1306. r.global.Uint16Array = r.newLazyObject(r.typedArrayCreator(r.newUint16Array, "Uint16Array", 2))
  1307. r.addToGlobal("Uint16Array", r.global.Uint16Array)
  1308. r.global.Int16Array = r.newLazyObject(r.typedArrayCreator(r.newInt16Array, "Int16Array", 2))
  1309. r.addToGlobal("Int16Array", r.global.Int16Array)
  1310. r.global.Uint32Array = r.newLazyObject(r.typedArrayCreator(r.newUint32Array, "Uint32Array", 4))
  1311. r.addToGlobal("Uint32Array", r.global.Uint32Array)
  1312. r.global.Int32Array = r.newLazyObject(r.typedArrayCreator(r.newInt32Array, "Int32Array", 4))
  1313. r.addToGlobal("Int32Array", r.global.Int32Array)
  1314. r.global.Float32Array = r.newLazyObject(r.typedArrayCreator(r.newFloat32Array, "Float32Array", 4))
  1315. r.addToGlobal("Float32Array", r.global.Float32Array)
  1316. r.global.Float64Array = r.newLazyObject(r.typedArrayCreator(r.newFloat64Array, "Float64Array", 8))
  1317. r.addToGlobal("Float64Array", r.global.Float64Array)
  1318. }