gba/IodineGBA/includes/TypedArrayShim.js

187 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-07-11 23:11:25 -04:00
"use strict";
/*
2019-04-23 19:59:17 -04:00
Copyright (C) 2012-2015 Grant Galitz
2015-07-11 23:11:25 -04:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2019-04-23 19:59:17 -04:00
2015-07-11 23:11:25 -04:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2019-04-23 19:59:17 -04:00
2015-07-11 23:11:25 -04:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function getInt8Array(size_t) {
try {
return new Int8Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
function getUint8Array(size_t) {
try {
return new Uint8Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
2019-04-23 19:59:17 -04:00
function getUint8View(typed_array) {
try {
return new Uint8Array(typed_array.buffer);
}
catch (error) {
return null;
}
}
function getSharedUint8Array(size_t) {
try {
//Compatibility for older Firefox Nightlies:
return new SharedUint8Array(size_t);
}
catch (error) {
return new Uint8Array(new SharedArrayBuffer(size_t));
}
}
2015-07-11 23:11:25 -04:00
function getInt16Array(size_t) {
try {
return new Int16Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
function getUint16Array(size_t) {
try {
return new Uint16Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
function getUint16View(typed_array) {
try {
return new Uint16Array(typed_array.buffer);
}
catch (error) {
return null;
}
}
function getInt32Array(size_t) {
try {
return new Int32Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
function getInt32View(typed_array) {
try {
return new Int32Array(typed_array.buffer);
}
catch (error) {
return null;
}
}
function getInt32ViewCustom(typed_array, start, end) {
try {
2019-04-23 19:59:17 -04:00
typed_array = getInt32View(typed_array);
return typed_array.subarray(start, end);
}
catch (error) {
try {
//Nightly Firefox 4 used to have the subarray function named as slice:
return typed_array.slice(start, end);
}
catch (error) {
return null;
}
}
}
function getSharedInt32Array(size_t) {
try {
//Compatibility for older Firefox Nightlies:
return new SharedInt32Array(size_t);
}
catch (error) {
return new Int32Array(new SharedArrayBuffer(size_t << 2));
}
}
function getUint8ViewCustom(typed_array, start, end) {
try {
typed_array = getUint8View(typed_array);
2015-07-11 23:11:25 -04:00
return typed_array.subarray(start, end);
}
catch (error) {
try {
//Nightly Firefox 4 used to have the subarray function named as slice:
return typed_array.slice(start, end);
}
catch (error) {
return null;
}
}
}
function getUint32Array(size_t) {
try {
return new Uint32Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
2019-04-23 19:59:17 -04:00
function getSharedUint32Array(size_t) {
try {
//Compatibility for older Firefox Nightlies:
return new SharedUint32Array(size_t);
}
catch (error) {
return new Uint32Array(new SharedArrayBuffer(size_t << 2));
}
}
2015-07-11 23:11:25 -04:00
function getFloat32Array(size_t) {
try {
return new Float32Array(size_t);
}
catch (error) {
return getArray(size_t);
}
}
2019-04-23 19:59:17 -04:00
function getSharedFloat32Array(size_t) {
try {
//Compatibility for older Firefox Nightlies:
return new SharedFloat32Array(size_t);
}
catch (error) {
return new Float32Array(new SharedArrayBuffer(size_t << 2));
}
}
2015-07-11 23:11:25 -04:00
function getArray(size_t) {
var genericArray = [];
for (var size_index = 0; size_index < size_t; ++size_index) {
genericArray[size_index] = 0;
}
return genericArray;
}
var __VIEWS_SUPPORTED__ = getUint16View(getInt32Array(1)) !== null;
var __LITTLE_ENDIAN__ = (function () {
if (__VIEWS_SUPPORTED__) {
var test = getInt32Array(1);
test[0] = 1;
var test2 = getUint16View(test);
if (test2[0] == 1) {
return true;
}
}
return false;
})();
2019-04-23 19:59:17 -04:00
if (typeof Atomics == "object") {
if (typeof Atomics.futexWait == "function" && typeof Atomics.wait == "undefined") {
//Polyfill in deprecated call names:
Atomics.wait = Atomics.futexWait;
2019-12-04 03:28:50 -05:00
Atomics.notify = Atomics.futexWake;
}
else if (typeof Atomics.wake == "function" && typeof Atomics.notify == "undefined") {
//Polyfill in deprecated call names:
Atomics.notify = Atomics.wake;
2019-04-23 19:59:17 -04:00
}
}