typedarrays.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. package goja
  2. import (
  3. "math"
  4. "reflect"
  5. "strconv"
  6. "unsafe"
  7. "github.com/dop251/goja/unistring"
  8. )
  9. type byteOrder bool
  10. const (
  11. bigEndian byteOrder = false
  12. littleEndian byteOrder = true
  13. )
  14. var (
  15. nativeEndian byteOrder
  16. arrayBufferType = reflect.TypeOf(ArrayBuffer{})
  17. )
  18. type typedArrayObjectCtor func(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject
  19. type arrayBufferObject struct {
  20. baseObject
  21. detached bool
  22. data []byte
  23. }
  24. // ArrayBuffer is a Go wrapper around ECMAScript ArrayBuffer. Calling Runtime.ToValue() on it
  25. // returns the underlying ArrayBuffer. Calling Export() on an ECMAScript ArrayBuffer returns a wrapper.
  26. // Use Runtime.NewArrayBuffer([]byte) to create one.
  27. type ArrayBuffer struct {
  28. buf *arrayBufferObject
  29. }
  30. type dataViewObject struct {
  31. baseObject
  32. viewedArrayBuf *arrayBufferObject
  33. byteLen, byteOffset int
  34. }
  35. type typedArray interface {
  36. toRaw(Value) uint64
  37. get(idx int) Value
  38. set(idx int, value Value)
  39. getRaw(idx int) uint64
  40. setRaw(idx int, raw uint64)
  41. less(i, j int) bool
  42. swap(i, j int)
  43. typeMatch(v Value) bool
  44. }
  45. type uint8Array []uint8
  46. type uint8ClampedArray []uint8
  47. type int8Array []int8
  48. type uint16Array []uint16
  49. type int16Array []int16
  50. type uint32Array []uint32
  51. type int32Array []int32
  52. type float32Array []float32
  53. type float64Array []float64
  54. type typedArrayObject struct {
  55. baseObject
  56. viewedArrayBuf *arrayBufferObject
  57. defaultCtor *Object
  58. length, offset int
  59. elemSize int
  60. typedArray typedArray
  61. }
  62. func (a ArrayBuffer) toValue(r *Runtime) Value {
  63. if a.buf == nil {
  64. return _null
  65. }
  66. v := a.buf.val
  67. if v.runtime != r {
  68. panic(r.NewTypeError("Illegal runtime transition of an ArrayBuffer"))
  69. }
  70. return v
  71. }
  72. // Bytes returns the underlying []byte for this ArrayBuffer.
  73. // For detached ArrayBuffers returns nil.
  74. func (a ArrayBuffer) Bytes() []byte {
  75. return a.buf.data
  76. }
  77. // Detach the ArrayBuffer. After this, the underlying []byte becomes unreferenced and any attempt
  78. // to use this ArrayBuffer results in a TypeError.
  79. // Returns false if it was already detached, true otherwise.
  80. // Note, this method may only be called from the goroutine that 'owns' the Runtime, it may not
  81. // be called concurrently.
  82. func (a ArrayBuffer) Detach() bool {
  83. if a.buf.detached {
  84. return false
  85. }
  86. a.buf.detach()
  87. return true
  88. }
  89. // Detached returns true if the ArrayBuffer is detached.
  90. func (a ArrayBuffer) Detached() bool {
  91. return a.buf.detached
  92. }
  93. func (r *Runtime) NewArrayBuffer(data []byte) ArrayBuffer {
  94. buf := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)
  95. buf.data = data
  96. return ArrayBuffer{
  97. buf: buf,
  98. }
  99. }
  100. func (a *uint8Array) get(idx int) Value {
  101. return intToValue(int64((*a)[idx]))
  102. }
  103. func (a *uint8Array) getRaw(idx int) uint64 {
  104. return uint64((*a)[idx])
  105. }
  106. func (a *uint8Array) set(idx int, value Value) {
  107. (*a)[idx] = toUint8(value)
  108. }
  109. func (a *uint8Array) toRaw(v Value) uint64 {
  110. return uint64(toUint8(v))
  111. }
  112. func (a *uint8Array) setRaw(idx int, v uint64) {
  113. (*a)[idx] = uint8(v)
  114. }
  115. func (a *uint8Array) less(i, j int) bool {
  116. return (*a)[i] < (*a)[j]
  117. }
  118. func (a *uint8Array) swap(i, j int) {
  119. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  120. }
  121. func (a *uint8Array) typeMatch(v Value) bool {
  122. if i, ok := v.(valueInt); ok {
  123. return i >= 0 && i <= 255
  124. }
  125. return false
  126. }
  127. func (a *uint8ClampedArray) get(idx int) Value {
  128. return intToValue(int64((*a)[idx]))
  129. }
  130. func (a *uint8ClampedArray) getRaw(idx int) uint64 {
  131. return uint64((*a)[idx])
  132. }
  133. func (a *uint8ClampedArray) set(idx int, value Value) {
  134. (*a)[idx] = toUint8Clamp(value)
  135. }
  136. func (a *uint8ClampedArray) toRaw(v Value) uint64 {
  137. return uint64(toUint8Clamp(v))
  138. }
  139. func (a *uint8ClampedArray) setRaw(idx int, v uint64) {
  140. (*a)[idx] = uint8(v)
  141. }
  142. func (a *uint8ClampedArray) less(i, j int) bool {
  143. return (*a)[i] < (*a)[j]
  144. }
  145. func (a *uint8ClampedArray) swap(i, j int) {
  146. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  147. }
  148. func (a *uint8ClampedArray) typeMatch(v Value) bool {
  149. if i, ok := v.(valueInt); ok {
  150. return i >= 0 && i <= 255
  151. }
  152. return false
  153. }
  154. func (a *int8Array) get(idx int) Value {
  155. return intToValue(int64((*a)[idx]))
  156. }
  157. func (a *int8Array) getRaw(idx int) uint64 {
  158. return uint64((*a)[idx])
  159. }
  160. func (a *int8Array) set(idx int, value Value) {
  161. (*a)[idx] = toInt8(value)
  162. }
  163. func (a *int8Array) toRaw(v Value) uint64 {
  164. return uint64(toInt8(v))
  165. }
  166. func (a *int8Array) setRaw(idx int, v uint64) {
  167. (*a)[idx] = int8(v)
  168. }
  169. func (a *int8Array) less(i, j int) bool {
  170. return (*a)[i] < (*a)[j]
  171. }
  172. func (a *int8Array) swap(i, j int) {
  173. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  174. }
  175. func (a *int8Array) typeMatch(v Value) bool {
  176. if i, ok := v.(valueInt); ok {
  177. return i >= math.MinInt8 && i <= math.MaxInt8
  178. }
  179. return false
  180. }
  181. func (a *uint16Array) get(idx int) Value {
  182. return intToValue(int64((*a)[idx]))
  183. }
  184. func (a *uint16Array) getRaw(idx int) uint64 {
  185. return uint64((*a)[idx])
  186. }
  187. func (a *uint16Array) set(idx int, value Value) {
  188. (*a)[idx] = toUint16(value)
  189. }
  190. func (a *uint16Array) toRaw(v Value) uint64 {
  191. return uint64(toUint16(v))
  192. }
  193. func (a *uint16Array) setRaw(idx int, v uint64) {
  194. (*a)[idx] = uint16(v)
  195. }
  196. func (a *uint16Array) less(i, j int) bool {
  197. return (*a)[i] < (*a)[j]
  198. }
  199. func (a *uint16Array) swap(i, j int) {
  200. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  201. }
  202. func (a *uint16Array) typeMatch(v Value) bool {
  203. if i, ok := v.(valueInt); ok {
  204. return i >= 0 && i <= math.MaxUint16
  205. }
  206. return false
  207. }
  208. func (a *int16Array) get(idx int) Value {
  209. return intToValue(int64((*a)[idx]))
  210. }
  211. func (a *int16Array) getRaw(idx int) uint64 {
  212. return uint64((*a)[idx])
  213. }
  214. func (a *int16Array) set(idx int, value Value) {
  215. (*a)[idx] = toInt16(value)
  216. }
  217. func (a *int16Array) toRaw(v Value) uint64 {
  218. return uint64(toInt16(v))
  219. }
  220. func (a *int16Array) setRaw(idx int, v uint64) {
  221. (*a)[idx] = int16(v)
  222. }
  223. func (a *int16Array) less(i, j int) bool {
  224. return (*a)[i] < (*a)[j]
  225. }
  226. func (a *int16Array) swap(i, j int) {
  227. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  228. }
  229. func (a *int16Array) typeMatch(v Value) bool {
  230. if i, ok := v.(valueInt); ok {
  231. return i >= math.MinInt16 && i <= math.MaxInt16
  232. }
  233. return false
  234. }
  235. func (a *uint32Array) get(idx int) Value {
  236. return intToValue(int64((*a)[idx]))
  237. }
  238. func (a *uint32Array) getRaw(idx int) uint64 {
  239. return uint64((*a)[idx])
  240. }
  241. func (a *uint32Array) set(idx int, value Value) {
  242. (*a)[idx] = toUint32(value)
  243. }
  244. func (a *uint32Array) toRaw(v Value) uint64 {
  245. return uint64(toUint32(v))
  246. }
  247. func (a *uint32Array) setRaw(idx int, v uint64) {
  248. (*a)[idx] = uint32(v)
  249. }
  250. func (a *uint32Array) less(i, j int) bool {
  251. return (*a)[i] < (*a)[j]
  252. }
  253. func (a *uint32Array) swap(i, j int) {
  254. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  255. }
  256. func (a *uint32Array) typeMatch(v Value) bool {
  257. if i, ok := v.(valueInt); ok {
  258. return i >= 0 && i <= math.MaxUint32
  259. }
  260. return false
  261. }
  262. func (a *int32Array) get(idx int) Value {
  263. return intToValue(int64((*a)[idx]))
  264. }
  265. func (a *int32Array) getRaw(idx int) uint64 {
  266. return uint64((*a)[idx])
  267. }
  268. func (a *int32Array) set(idx int, value Value) {
  269. (*a)[idx] = toInt32(value)
  270. }
  271. func (a *int32Array) toRaw(v Value) uint64 {
  272. return uint64(toInt32(v))
  273. }
  274. func (a *int32Array) setRaw(idx int, v uint64) {
  275. (*a)[idx] = int32(v)
  276. }
  277. func (a *int32Array) less(i, j int) bool {
  278. return (*a)[i] < (*a)[j]
  279. }
  280. func (a *int32Array) swap(i, j int) {
  281. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  282. }
  283. func (a *int32Array) typeMatch(v Value) bool {
  284. if i, ok := v.(valueInt); ok {
  285. return i >= math.MinInt32 && i <= math.MaxInt32
  286. }
  287. return false
  288. }
  289. func (a *float32Array) get(idx int) Value {
  290. return floatToValue(float64((*a)[idx]))
  291. }
  292. func (a *float32Array) getRaw(idx int) uint64 {
  293. return uint64(math.Float32bits((*a)[idx]))
  294. }
  295. func (a *float32Array) set(idx int, value Value) {
  296. (*a)[idx] = toFloat32(value)
  297. }
  298. func (a *float32Array) toRaw(v Value) uint64 {
  299. return uint64(math.Float32bits(toFloat32(v)))
  300. }
  301. func (a *float32Array) setRaw(idx int, v uint64) {
  302. (*a)[idx] = math.Float32frombits(uint32(v))
  303. }
  304. func typedFloatLess(x, y float64) bool {
  305. xNan := math.IsNaN(x)
  306. yNan := math.IsNaN(y)
  307. if yNan {
  308. return !xNan
  309. } else if xNan {
  310. return false
  311. }
  312. if x == 0 && y == 0 { // handle neg zero
  313. return math.Signbit(x)
  314. }
  315. return x < y
  316. }
  317. func (a *float32Array) less(i, j int) bool {
  318. return typedFloatLess(float64((*a)[i]), float64((*a)[j]))
  319. }
  320. func (a *float32Array) swap(i, j int) {
  321. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  322. }
  323. func (a *float32Array) typeMatch(v Value) bool {
  324. switch v.(type) {
  325. case valueInt, valueFloat:
  326. return true
  327. }
  328. return false
  329. }
  330. func (a *float64Array) get(idx int) Value {
  331. return floatToValue((*a)[idx])
  332. }
  333. func (a *float64Array) getRaw(idx int) uint64 {
  334. return math.Float64bits((*a)[idx])
  335. }
  336. func (a *float64Array) set(idx int, value Value) {
  337. (*a)[idx] = value.ToFloat()
  338. }
  339. func (a *float64Array) toRaw(v Value) uint64 {
  340. return math.Float64bits(v.ToFloat())
  341. }
  342. func (a *float64Array) setRaw(idx int, v uint64) {
  343. (*a)[idx] = math.Float64frombits(v)
  344. }
  345. func (a *float64Array) less(i, j int) bool {
  346. return typedFloatLess((*a)[i], (*a)[j])
  347. }
  348. func (a *float64Array) swap(i, j int) {
  349. (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
  350. }
  351. func (a *float64Array) typeMatch(v Value) bool {
  352. switch v.(type) {
  353. case valueInt, valueFloat:
  354. return true
  355. }
  356. return false
  357. }
  358. func (a *typedArrayObject) _getIdx(idx int) Value {
  359. if 0 <= idx && idx < a.length {
  360. if !a.viewedArrayBuf.ensureNotDetached(false) {
  361. return nil
  362. }
  363. return a.typedArray.get(idx + a.offset)
  364. }
  365. return nil
  366. }
  367. func (a *typedArrayObject) getOwnPropStr(name unistring.String) Value {
  368. idx, ok := strToIntNum(name)
  369. if ok {
  370. v := a._getIdx(idx)
  371. if v != nil {
  372. return &valueProperty{
  373. value: v,
  374. writable: true,
  375. enumerable: true,
  376. configurable: true,
  377. }
  378. }
  379. return nil
  380. }
  381. if idx == 0 {
  382. return nil
  383. }
  384. return a.baseObject.getOwnPropStr(name)
  385. }
  386. func (a *typedArrayObject) getOwnPropIdx(idx valueInt) Value {
  387. v := a._getIdx(toIntClamp(int64(idx)))
  388. if v != nil {
  389. return &valueProperty{
  390. value: v,
  391. writable: true,
  392. enumerable: true,
  393. configurable: true,
  394. }
  395. }
  396. return nil
  397. }
  398. func (a *typedArrayObject) getStr(name unistring.String, receiver Value) Value {
  399. idx, ok := strToIntNum(name)
  400. if ok {
  401. return a._getIdx(idx)
  402. }
  403. if idx == 0 {
  404. return nil
  405. }
  406. return a.baseObject.getStr(name, receiver)
  407. }
  408. func (a *typedArrayObject) getIdx(idx valueInt, receiver Value) Value {
  409. return a._getIdx(toIntClamp(int64(idx)))
  410. }
  411. func (a *typedArrayObject) isValidIntegerIndex(idx int) bool {
  412. if a.viewedArrayBuf.ensureNotDetached(false) {
  413. if idx >= 0 && idx < a.length {
  414. return true
  415. }
  416. }
  417. return false
  418. }
  419. func (a *typedArrayObject) _putIdx(idx int, v Value) {
  420. v = v.ToNumber()
  421. if a.isValidIntegerIndex(idx) {
  422. a.typedArray.set(idx+a.offset, v)
  423. }
  424. }
  425. func (a *typedArrayObject) _hasIdx(idx int) bool {
  426. return a.isValidIntegerIndex(idx)
  427. }
  428. func (a *typedArrayObject) setOwnStr(p unistring.String, v Value, throw bool) bool {
  429. idx, ok := strToIntNum(p)
  430. if ok {
  431. a._putIdx(idx, v)
  432. return true
  433. }
  434. if idx == 0 {
  435. v.ToNumber() // make sure it throws
  436. return true
  437. }
  438. return a.baseObject.setOwnStr(p, v, throw)
  439. }
  440. func (a *typedArrayObject) setOwnIdx(p valueInt, v Value, throw bool) bool {
  441. a._putIdx(toIntClamp(int64(p)), v)
  442. return true
  443. }
  444. func (a *typedArrayObject) setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool) {
  445. return a._setForeignStr(p, a.getOwnPropStr(p), v, receiver, throw)
  446. }
  447. func (a *typedArrayObject) setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool) {
  448. return a._setForeignIdx(p, trueValIfPresent(a.hasOwnPropertyIdx(p)), v, receiver, throw)
  449. }
  450. func (a *typedArrayObject) hasOwnPropertyStr(name unistring.String) bool {
  451. idx, ok := strToIntNum(name)
  452. if ok {
  453. return a._hasIdx(idx)
  454. }
  455. if idx == 0 {
  456. return false
  457. }
  458. return a.baseObject.hasOwnPropertyStr(name)
  459. }
  460. func (a *typedArrayObject) hasOwnPropertyIdx(idx valueInt) bool {
  461. return a._hasIdx(toIntClamp(int64(idx)))
  462. }
  463. func (a *typedArrayObject) hasPropertyStr(name unistring.String) bool {
  464. idx, ok := strToIntNum(name)
  465. if ok {
  466. return a._hasIdx(idx)
  467. }
  468. if idx == 0 {
  469. return false
  470. }
  471. return a.baseObject.hasPropertyStr(name)
  472. }
  473. func (a *typedArrayObject) hasPropertyIdx(idx valueInt) bool {
  474. return a.hasOwnPropertyIdx(idx)
  475. }
  476. func (a *typedArrayObject) _defineIdxProperty(idx int, desc PropertyDescriptor, throw bool) bool {
  477. if desc.Configurable == FLAG_FALSE || desc.Enumerable == FLAG_FALSE || desc.IsAccessor() || desc.Writable == FLAG_FALSE {
  478. a.val.runtime.typeErrorResult(throw, "Cannot redefine property: %d", idx)
  479. return false
  480. }
  481. _, ok := a._defineOwnProperty(unistring.String(strconv.Itoa(idx)), a.getOwnPropIdx(valueInt(idx)), desc, throw)
  482. if ok {
  483. if !a.isValidIntegerIndex(idx) {
  484. a.val.runtime.typeErrorResult(throw, "Invalid typed array index")
  485. return false
  486. }
  487. a._putIdx(idx, desc.Value)
  488. return true
  489. }
  490. return ok
  491. }
  492. func (a *typedArrayObject) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
  493. idx, ok := strToIntNum(name)
  494. if ok {
  495. return a._defineIdxProperty(idx, desc, throw)
  496. }
  497. if idx == 0 {
  498. a.viewedArrayBuf.ensureNotDetached(throw)
  499. a.val.runtime.typeErrorResult(throw, "Invalid typed array index")
  500. return false
  501. }
  502. return a.baseObject.defineOwnPropertyStr(name, desc, throw)
  503. }
  504. func (a *typedArrayObject) defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool {
  505. return a._defineIdxProperty(toIntClamp(int64(name)), desc, throw)
  506. }
  507. func (a *typedArrayObject) deleteStr(name unistring.String, throw bool) bool {
  508. idx, ok := strToIntNum(name)
  509. if ok {
  510. if a.isValidIntegerIndex(idx) {
  511. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.String())
  512. return false
  513. }
  514. return true
  515. }
  516. if idx == 0 {
  517. return true
  518. }
  519. return a.baseObject.deleteStr(name, throw)
  520. }
  521. func (a *typedArrayObject) deleteIdx(idx valueInt, throw bool) bool {
  522. if a.viewedArrayBuf.ensureNotDetached(false) && idx >= 0 && int64(idx) < int64(a.length) {
  523. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.String())
  524. return false
  525. }
  526. return true
  527. }
  528. func (a *typedArrayObject) stringKeys(all bool, accum []Value) []Value {
  529. if accum == nil {
  530. accum = make([]Value, 0, a.length)
  531. }
  532. for i := 0; i < a.length; i++ {
  533. accum = append(accum, asciiString(strconv.Itoa(i)))
  534. }
  535. return a.baseObject.stringKeys(all, accum)
  536. }
  537. type typedArrayPropIter struct {
  538. a *typedArrayObject
  539. idx int
  540. }
  541. func (i *typedArrayPropIter) next() (propIterItem, iterNextFunc) {
  542. if i.idx < i.a.length {
  543. name := strconv.Itoa(i.idx)
  544. prop := i.a._getIdx(i.idx)
  545. i.idx++
  546. return propIterItem{name: asciiString(name), value: prop}, i.next
  547. }
  548. return i.a.baseObject.iterateStringKeys()()
  549. }
  550. func (a *typedArrayObject) iterateStringKeys() iterNextFunc {
  551. return (&typedArrayPropIter{
  552. a: a,
  553. }).next
  554. }
  555. func (r *Runtime) _newTypedArrayObject(buf *arrayBufferObject, offset, length, elemSize int, defCtor *Object, arr typedArray, proto *Object) *typedArrayObject {
  556. o := &Object{runtime: r}
  557. a := &typedArrayObject{
  558. baseObject: baseObject{
  559. val: o,
  560. class: classObject,
  561. prototype: proto,
  562. extensible: true,
  563. },
  564. viewedArrayBuf: buf,
  565. offset: offset,
  566. length: length,
  567. elemSize: elemSize,
  568. defaultCtor: defCtor,
  569. typedArray: arr,
  570. }
  571. o.self = a
  572. a.init()
  573. return a
  574. }
  575. func (r *Runtime) newUint8ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  576. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Uint8Array, (*uint8Array)(&buf.data), proto)
  577. }
  578. func (r *Runtime) newUint8ClampedArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  579. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Uint8ClampedArray, (*uint8ClampedArray)(&buf.data), proto)
  580. }
  581. func (r *Runtime) newInt8ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  582. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Int8Array, (*int8Array)(unsafe.Pointer(&buf.data)), proto)
  583. }
  584. func (r *Runtime) newUint16ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  585. return r._newTypedArrayObject(buf, offset, length, 2, r.global.Uint16Array, (*uint16Array)(unsafe.Pointer(&buf.data)), proto)
  586. }
  587. func (r *Runtime) newInt16ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  588. return r._newTypedArrayObject(buf, offset, length, 2, r.global.Int16Array, (*int16Array)(unsafe.Pointer(&buf.data)), proto)
  589. }
  590. func (r *Runtime) newUint32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  591. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Uint32Array, (*uint32Array)(unsafe.Pointer(&buf.data)), proto)
  592. }
  593. func (r *Runtime) newInt32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  594. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Int32Array, (*int32Array)(unsafe.Pointer(&buf.data)), proto)
  595. }
  596. func (r *Runtime) newFloat32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  597. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Float32Array, (*float32Array)(unsafe.Pointer(&buf.data)), proto)
  598. }
  599. func (r *Runtime) newFloat64ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  600. return r._newTypedArrayObject(buf, offset, length, 8, r.global.Float64Array, (*float64Array)(unsafe.Pointer(&buf.data)), proto)
  601. }
  602. func (o *dataViewObject) getIdxAndByteOrder(getIdx int, littleEndianVal Value, size int) (int, byteOrder) {
  603. o.viewedArrayBuf.ensureNotDetached(true)
  604. if getIdx+size > o.byteLen {
  605. panic(o.val.runtime.newError(o.val.runtime.global.RangeError, "Index %d is out of bounds", getIdx))
  606. }
  607. getIdx += o.byteOffset
  608. var bo byteOrder
  609. if littleEndianVal != nil {
  610. if littleEndianVal.ToBoolean() {
  611. bo = littleEndian
  612. } else {
  613. bo = bigEndian
  614. }
  615. } else {
  616. bo = nativeEndian
  617. }
  618. return getIdx, bo
  619. }
  620. func (o *arrayBufferObject) ensureNotDetached(throw bool) bool {
  621. if o.detached {
  622. o.val.runtime.typeErrorResult(throw, "ArrayBuffer is detached")
  623. return false
  624. }
  625. return true
  626. }
  627. func (o *arrayBufferObject) getFloat32(idx int, byteOrder byteOrder) float32 {
  628. return math.Float32frombits(o.getUint32(idx, byteOrder))
  629. }
  630. func (o *arrayBufferObject) setFloat32(idx int, val float32, byteOrder byteOrder) {
  631. o.setUint32(idx, math.Float32bits(val), byteOrder)
  632. }
  633. func (o *arrayBufferObject) getFloat64(idx int, byteOrder byteOrder) float64 {
  634. return math.Float64frombits(o.getUint64(idx, byteOrder))
  635. }
  636. func (o *arrayBufferObject) setFloat64(idx int, val float64, byteOrder byteOrder) {
  637. o.setUint64(idx, math.Float64bits(val), byteOrder)
  638. }
  639. func (o *arrayBufferObject) getUint64(idx int, byteOrder byteOrder) uint64 {
  640. var b []byte
  641. if byteOrder == nativeEndian {
  642. b = o.data[idx : idx+8]
  643. } else {
  644. b = make([]byte, 8)
  645. d := o.data[idx : idx+8]
  646. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = d[7], d[6], d[5], d[4], d[3], d[2], d[1], d[0]
  647. }
  648. return *((*uint64)(unsafe.Pointer(&b[0])))
  649. }
  650. func (o *arrayBufferObject) setUint64(idx int, val uint64, byteOrder byteOrder) {
  651. if byteOrder == nativeEndian {
  652. *(*uint64)(unsafe.Pointer(&o.data[idx])) = val
  653. } else {
  654. b := (*[8]byte)(unsafe.Pointer(&val))
  655. d := o.data[idx : idx+8]
  656. d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7] = b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0]
  657. }
  658. }
  659. func (o *arrayBufferObject) getUint32(idx int, byteOrder byteOrder) uint32 {
  660. var b []byte
  661. if byteOrder == nativeEndian {
  662. b = o.data[idx : idx+4]
  663. } else {
  664. b = make([]byte, 4)
  665. d := o.data[idx : idx+4]
  666. b[0], b[1], b[2], b[3] = d[3], d[2], d[1], d[0]
  667. }
  668. return *((*uint32)(unsafe.Pointer(&b[0])))
  669. }
  670. func (o *arrayBufferObject) setUint32(idx int, val uint32, byteOrder byteOrder) {
  671. o.ensureNotDetached(true)
  672. if byteOrder == nativeEndian {
  673. *(*uint32)(unsafe.Pointer(&o.data[idx])) = val
  674. } else {
  675. b := (*[4]byte)(unsafe.Pointer(&val))
  676. d := o.data[idx : idx+4]
  677. d[0], d[1], d[2], d[3] = b[3], b[2], b[1], b[0]
  678. }
  679. }
  680. func (o *arrayBufferObject) getUint16(idx int, byteOrder byteOrder) uint16 {
  681. var b []byte
  682. if byteOrder == nativeEndian {
  683. b = o.data[idx : idx+2]
  684. } else {
  685. b = make([]byte, 2)
  686. d := o.data[idx : idx+2]
  687. b[0], b[1] = d[1], d[0]
  688. }
  689. return *((*uint16)(unsafe.Pointer(&b[0])))
  690. }
  691. func (o *arrayBufferObject) setUint16(idx int, val uint16, byteOrder byteOrder) {
  692. if byteOrder == nativeEndian {
  693. *(*uint16)(unsafe.Pointer(&o.data[idx])) = val
  694. } else {
  695. b := (*[2]byte)(unsafe.Pointer(&val))
  696. d := o.data[idx : idx+2]
  697. d[0], d[1] = b[1], b[0]
  698. }
  699. }
  700. func (o *arrayBufferObject) getUint8(idx int) uint8 {
  701. return o.data[idx]
  702. }
  703. func (o *arrayBufferObject) setUint8(idx int, val uint8) {
  704. o.data[idx] = val
  705. }
  706. func (o *arrayBufferObject) getInt32(idx int, byteOrder byteOrder) int32 {
  707. return int32(o.getUint32(idx, byteOrder))
  708. }
  709. func (o *arrayBufferObject) setInt32(idx int, val int32, byteOrder byteOrder) {
  710. o.setUint32(idx, uint32(val), byteOrder)
  711. }
  712. func (o *arrayBufferObject) getInt16(idx int, byteOrder byteOrder) int16 {
  713. return int16(o.getUint16(idx, byteOrder))
  714. }
  715. func (o *arrayBufferObject) setInt16(idx int, val int16, byteOrder byteOrder) {
  716. o.setUint16(idx, uint16(val), byteOrder)
  717. }
  718. func (o *arrayBufferObject) getInt8(idx int) int8 {
  719. return int8(o.data[idx])
  720. }
  721. func (o *arrayBufferObject) setInt8(idx int, val int8) {
  722. o.setUint8(idx, uint8(val))
  723. }
  724. func (o *arrayBufferObject) detach() {
  725. o.data = nil
  726. o.detached = true
  727. }
  728. func (o *arrayBufferObject) exportType() reflect.Type {
  729. return arrayBufferType
  730. }
  731. func (o *arrayBufferObject) export(*objectExportCtx) interface{} {
  732. return ArrayBuffer{
  733. buf: o,
  734. }
  735. }
  736. func (r *Runtime) _newArrayBuffer(proto *Object, o *Object) *arrayBufferObject {
  737. if o == nil {
  738. o = &Object{runtime: r}
  739. }
  740. b := &arrayBufferObject{
  741. baseObject: baseObject{
  742. class: classObject,
  743. val: o,
  744. prototype: proto,
  745. extensible: true,
  746. },
  747. }
  748. o.self = b
  749. b.init()
  750. return b
  751. }
  752. func init() {
  753. buf := [2]byte{}
  754. *(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xCAFE)
  755. switch buf {
  756. case [2]byte{0xFE, 0xCA}:
  757. nativeEndian = littleEndian
  758. case [2]byte{0xCA, 0xFE}:
  759. nativeEndian = bigEndian
  760. default:
  761. panic("Could not determine native endianness.")
  762. }
  763. }