typedarrays.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. a.viewedArrayBuf.ensureNotDetached()
  360. if 0 <= idx && idx < a.length {
  361. return a.typedArray.get(idx + a.offset)
  362. }
  363. return nil
  364. }
  365. func (a *typedArrayObject) getOwnPropStr(name unistring.String) Value {
  366. if idx, ok := strPropToInt(name); ok {
  367. v := a._getIdx(idx)
  368. if v != nil {
  369. return &valueProperty{
  370. value: v,
  371. writable: true,
  372. enumerable: true,
  373. }
  374. }
  375. return nil
  376. }
  377. return a.baseObject.getOwnPropStr(name)
  378. }
  379. func (a *typedArrayObject) getOwnPropIdx(idx valueInt) Value {
  380. v := a._getIdx(toIntStrict(int64(idx)))
  381. if v != nil {
  382. return &valueProperty{
  383. value: v,
  384. writable: true,
  385. enumerable: true,
  386. }
  387. }
  388. return nil
  389. }
  390. func (a *typedArrayObject) getStr(name unistring.String, receiver Value) Value {
  391. if idx, ok := strPropToInt(name); ok {
  392. prop := a._getIdx(idx)
  393. if prop == nil {
  394. if a.prototype != nil {
  395. if receiver == nil {
  396. return a.prototype.self.getStr(name, a.val)
  397. }
  398. return a.prototype.self.getStr(name, receiver)
  399. }
  400. }
  401. return prop
  402. }
  403. return a.baseObject.getStr(name, receiver)
  404. }
  405. func (a *typedArrayObject) getIdx(idx valueInt, receiver Value) Value {
  406. prop := a._getIdx(toIntStrict(int64(idx)))
  407. if prop == nil {
  408. if a.prototype != nil {
  409. if receiver == nil {
  410. return a.prototype.self.getIdx(idx, a.val)
  411. }
  412. return a.prototype.self.getIdx(idx, receiver)
  413. }
  414. }
  415. return prop
  416. }
  417. func (a *typedArrayObject) _putIdx(idx int, v Value, throw bool) bool {
  418. v = v.ToNumber()
  419. a.viewedArrayBuf.ensureNotDetached()
  420. if idx >= 0 && idx < a.length {
  421. a.typedArray.set(idx+a.offset, v)
  422. return true
  423. }
  424. // As far as I understand the specification this should throw, but neither V8 nor SpiderMonkey does
  425. return false
  426. }
  427. func (a *typedArrayObject) _hasIdx(idx int) bool {
  428. a.viewedArrayBuf.ensureNotDetached()
  429. return idx >= 0 && idx < a.length
  430. }
  431. func (a *typedArrayObject) setOwnStr(p unistring.String, v Value, throw bool) bool {
  432. if idx, ok := strPropToInt(p); ok {
  433. return a._putIdx(idx, v, throw)
  434. }
  435. return a.baseObject.setOwnStr(p, v, throw)
  436. }
  437. func (a *typedArrayObject) setOwnIdx(p valueInt, v Value, throw bool) bool {
  438. return a._putIdx(toIntStrict(int64(p)), v, throw)
  439. }
  440. func (a *typedArrayObject) setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool) {
  441. return a._setForeignStr(p, a.getOwnPropStr(p), v, receiver, throw)
  442. }
  443. func (a *typedArrayObject) setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool) {
  444. return a._setForeignIdx(p, trueValIfPresent(a.hasOwnPropertyIdx(p)), v, receiver, throw)
  445. }
  446. func (a *typedArrayObject) hasOwnPropertyStr(name unistring.String) bool {
  447. if idx, ok := strPropToInt(name); ok {
  448. a.viewedArrayBuf.ensureNotDetached()
  449. return idx < a.length
  450. }
  451. return a.baseObject.hasOwnPropertyStr(name)
  452. }
  453. func (a *typedArrayObject) hasOwnPropertyIdx(idx valueInt) bool {
  454. return a._hasIdx(toIntStrict(int64(idx)))
  455. }
  456. func (a *typedArrayObject) _defineIdxProperty(idx int, desc PropertyDescriptor, throw bool) bool {
  457. prop, ok := a._defineOwnProperty(unistring.String(strconv.Itoa(idx)), a.getOwnPropIdx(valueInt(idx)), desc, throw)
  458. if ok {
  459. return a._putIdx(idx, prop, throw)
  460. }
  461. return ok
  462. }
  463. func (a *typedArrayObject) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
  464. if idx, ok := strPropToInt(name); ok {
  465. return a._defineIdxProperty(idx, desc, throw)
  466. }
  467. return a.baseObject.defineOwnPropertyStr(name, desc, throw)
  468. }
  469. func (a *typedArrayObject) defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool {
  470. return a._defineIdxProperty(toIntStrict(int64(name)), desc, throw)
  471. }
  472. func (a *typedArrayObject) deleteStr(name unistring.String, throw bool) bool {
  473. if idx, ok := strPropToInt(name); ok {
  474. if idx < a.length {
  475. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.String())
  476. }
  477. }
  478. return a.baseObject.deleteStr(name, throw)
  479. }
  480. func (a *typedArrayObject) deleteIdx(idx valueInt, throw bool) bool {
  481. if idx >= 0 && int64(idx) < int64(a.length) {
  482. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.String())
  483. }
  484. return true
  485. }
  486. func (a *typedArrayObject) ownKeys(all bool, accum []Value) []Value {
  487. if accum == nil {
  488. accum = make([]Value, 0, a.length)
  489. }
  490. for i := 0; i < a.length; i++ {
  491. accum = append(accum, asciiString(strconv.Itoa(i)))
  492. }
  493. return a.baseObject.ownKeys(all, accum)
  494. }
  495. type typedArrayPropIter struct {
  496. a *typedArrayObject
  497. idx int
  498. }
  499. func (i *typedArrayPropIter) next() (propIterItem, iterNextFunc) {
  500. if i.idx < i.a.length {
  501. name := strconv.Itoa(i.idx)
  502. prop := i.a._getIdx(i.idx)
  503. i.idx++
  504. return propIterItem{name: unistring.String(name), value: prop}, i.next
  505. }
  506. return i.a.baseObject.enumerateOwnKeys()()
  507. }
  508. func (a *typedArrayObject) enumerateOwnKeys() iterNextFunc {
  509. return (&typedArrayPropIter{
  510. a: a,
  511. }).next
  512. }
  513. func (r *Runtime) _newTypedArrayObject(buf *arrayBufferObject, offset, length, elemSize int, defCtor *Object, arr typedArray, proto *Object) *typedArrayObject {
  514. o := &Object{runtime: r}
  515. a := &typedArrayObject{
  516. baseObject: baseObject{
  517. val: o,
  518. class: classObject,
  519. prototype: proto,
  520. extensible: true,
  521. },
  522. viewedArrayBuf: buf,
  523. offset: offset,
  524. length: length,
  525. elemSize: elemSize,
  526. defaultCtor: defCtor,
  527. typedArray: arr,
  528. }
  529. o.self = a
  530. a.init()
  531. return a
  532. }
  533. func (r *Runtime) newUint8ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  534. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Uint8Array, (*uint8Array)(&buf.data), proto)
  535. }
  536. func (r *Runtime) newUint8ClampedArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  537. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Uint8ClampedArray, (*uint8ClampedArray)(&buf.data), proto)
  538. }
  539. func (r *Runtime) newInt8ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  540. return r._newTypedArrayObject(buf, offset, length, 1, r.global.Int8Array, (*int8Array)(unsafe.Pointer(&buf.data)), proto)
  541. }
  542. func (r *Runtime) newUint16ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  543. return r._newTypedArrayObject(buf, offset, length, 2, r.global.Uint16Array, (*uint16Array)(unsafe.Pointer(&buf.data)), proto)
  544. }
  545. func (r *Runtime) newInt16ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  546. return r._newTypedArrayObject(buf, offset, length, 2, r.global.Int16Array, (*int16Array)(unsafe.Pointer(&buf.data)), proto)
  547. }
  548. func (r *Runtime) newUint32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  549. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Uint32Array, (*uint32Array)(unsafe.Pointer(&buf.data)), proto)
  550. }
  551. func (r *Runtime) newInt32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  552. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Int32Array, (*int32Array)(unsafe.Pointer(&buf.data)), proto)
  553. }
  554. func (r *Runtime) newFloat32ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  555. return r._newTypedArrayObject(buf, offset, length, 4, r.global.Float32Array, (*float32Array)(unsafe.Pointer(&buf.data)), proto)
  556. }
  557. func (r *Runtime) newFloat64ArrayObject(buf *arrayBufferObject, offset, length int, proto *Object) *typedArrayObject {
  558. return r._newTypedArrayObject(buf, offset, length, 8, r.global.Float64Array, (*float64Array)(unsafe.Pointer(&buf.data)), proto)
  559. }
  560. func (o *dataViewObject) getIdxAndByteOrder(idxVal, littleEndianVal Value, size int) (int, byteOrder) {
  561. getIdx := o.val.runtime.toIndex(idxVal)
  562. o.viewedArrayBuf.ensureNotDetached()
  563. if getIdx+size > o.byteLen {
  564. panic(o.val.runtime.newError(o.val.runtime.global.RangeError, "Index %d is out of bounds", getIdx))
  565. }
  566. getIdx += o.byteOffset
  567. var bo byteOrder
  568. if littleEndianVal != nil {
  569. if littleEndianVal.ToBoolean() {
  570. bo = littleEndian
  571. } else {
  572. bo = bigEndian
  573. }
  574. } else {
  575. bo = nativeEndian
  576. }
  577. return getIdx, bo
  578. }
  579. func (o *arrayBufferObject) ensureNotDetached() {
  580. if o.detached {
  581. panic(o.val.runtime.NewTypeError("ArrayBuffer is detached"))
  582. }
  583. }
  584. func (o *arrayBufferObject) getFloat32(idx int, byteOrder byteOrder) float32 {
  585. return math.Float32frombits(o.getUint32(idx, byteOrder))
  586. }
  587. func (o *arrayBufferObject) setFloat32(idx int, val float32, byteOrder byteOrder) {
  588. o.setUint32(idx, math.Float32bits(val), byteOrder)
  589. }
  590. func (o *arrayBufferObject) getFloat64(idx int, byteOrder byteOrder) float64 {
  591. return math.Float64frombits(o.getUint64(idx, byteOrder))
  592. }
  593. func (o *arrayBufferObject) setFloat64(idx int, val float64, byteOrder byteOrder) {
  594. o.setUint64(idx, math.Float64bits(val), byteOrder)
  595. }
  596. func (o *arrayBufferObject) getUint64(idx int, byteOrder byteOrder) uint64 {
  597. var b []byte
  598. if byteOrder == nativeEndian {
  599. b = o.data[idx : idx+8]
  600. } else {
  601. b = make([]byte, 8)
  602. d := o.data[idx : idx+8]
  603. 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]
  604. }
  605. return *((*uint64)(unsafe.Pointer(&b[0])))
  606. }
  607. func (o *arrayBufferObject) setUint64(idx int, val uint64, byteOrder byteOrder) {
  608. if byteOrder == nativeEndian {
  609. *(*uint64)(unsafe.Pointer(&o.data[idx])) = val
  610. } else {
  611. b := (*[8]byte)(unsafe.Pointer(&val))
  612. d := o.data[idx : idx+8]
  613. 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]
  614. }
  615. }
  616. func (o *arrayBufferObject) getUint32(idx int, byteOrder byteOrder) uint32 {
  617. var b []byte
  618. if byteOrder == nativeEndian {
  619. b = o.data[idx : idx+4]
  620. } else {
  621. b = make([]byte, 4)
  622. d := o.data[idx : idx+4]
  623. b[0], b[1], b[2], b[3] = d[3], d[2], d[1], d[0]
  624. }
  625. return *((*uint32)(unsafe.Pointer(&b[0])))
  626. }
  627. func (o *arrayBufferObject) setUint32(idx int, val uint32, byteOrder byteOrder) {
  628. if byteOrder == nativeEndian {
  629. *(*uint32)(unsafe.Pointer(&o.data[idx])) = val
  630. } else {
  631. b := (*[4]byte)(unsafe.Pointer(&val))
  632. d := o.data[idx : idx+4]
  633. d[0], d[1], d[2], d[3] = b[3], b[2], b[1], b[0]
  634. }
  635. }
  636. func (o *arrayBufferObject) getUint16(idx int, byteOrder byteOrder) uint16 {
  637. var b []byte
  638. if byteOrder == nativeEndian {
  639. b = o.data[idx : idx+2]
  640. } else {
  641. b = make([]byte, 2)
  642. d := o.data[idx : idx+2]
  643. b[0], b[1] = d[1], d[0]
  644. }
  645. return *((*uint16)(unsafe.Pointer(&b[0])))
  646. }
  647. func (o *arrayBufferObject) setUint16(idx int, val uint16, byteOrder byteOrder) {
  648. if byteOrder == nativeEndian {
  649. *(*uint16)(unsafe.Pointer(&o.data[idx])) = val
  650. } else {
  651. b := (*[2]byte)(unsafe.Pointer(&val))
  652. d := o.data[idx : idx+2]
  653. d[0], d[1] = b[1], b[0]
  654. }
  655. }
  656. func (o *arrayBufferObject) getUint8(idx int) uint8 {
  657. return o.data[idx]
  658. }
  659. func (o *arrayBufferObject) setUint8(idx int, val uint8) {
  660. o.data[idx] = val
  661. }
  662. func (o *arrayBufferObject) getInt32(idx int, byteOrder byteOrder) int32 {
  663. return int32(o.getUint32(idx, byteOrder))
  664. }
  665. func (o *arrayBufferObject) setInt32(idx int, val int32, byteOrder byteOrder) {
  666. o.setUint32(idx, uint32(val), byteOrder)
  667. }
  668. func (o *arrayBufferObject) getInt16(idx int, byteOrder byteOrder) int16 {
  669. return int16(o.getUint16(idx, byteOrder))
  670. }
  671. func (o *arrayBufferObject) setInt16(idx int, val int16, byteOrder byteOrder) {
  672. o.setUint16(idx, uint16(val), byteOrder)
  673. }
  674. func (o *arrayBufferObject) getInt8(idx int) int8 {
  675. return int8(o.data[idx])
  676. }
  677. func (o *arrayBufferObject) setInt8(idx int, val int8) {
  678. o.setUint8(idx, uint8(val))
  679. }
  680. func (o *arrayBufferObject) detach() {
  681. o.data = nil
  682. o.detached = true
  683. }
  684. func (o *arrayBufferObject) exportType() reflect.Type {
  685. return arrayBufferType
  686. }
  687. func (o *arrayBufferObject) export(*objectExportCtx) interface{} {
  688. return ArrayBuffer{
  689. buf: o,
  690. }
  691. }
  692. func (r *Runtime) _newArrayBuffer(proto *Object, o *Object) *arrayBufferObject {
  693. if o == nil {
  694. o = &Object{runtime: r}
  695. }
  696. b := &arrayBufferObject{
  697. baseObject: baseObject{
  698. class: classObject,
  699. val: o,
  700. prototype: proto,
  701. extensible: true,
  702. },
  703. }
  704. o.self = b
  705. b.init()
  706. return b
  707. }
  708. func init() {
  709. buf := [2]byte{}
  710. *(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xCAFE)
  711. switch buf {
  712. case [2]byte{0xFE, 0xCA}:
  713. nativeEndian = littleEndian
  714. case [2]byte{0xCA, 0xFE}:
  715. nativeEndian = bigEndian
  716. default:
  717. panic("Could not determine native endianness.")
  718. }
  719. }