typedarrays.go 25 KB

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