Skip to content

Commit 8e889b6

Browse files
committed
Fixing #8 as well as preventing future problems with SharedArrayBuffers
1 parent 0fe97e8 commit 8e889b6

17 files changed

+205
-117
lines changed

EncoderDecoderTogether.min.js

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EncoderDecoderTogether.min.js.map

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EncoderDecoderTogether.src.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
var clz32 = Math.clz32 || function(x) {return 31 - log(x >>> 0) / LN2 | 0};
66
var fromCharCode = String.fromCharCode;
77
var Object_prototype_toString = ({}).toString;
8+
var NativeSharedArrayBuffer = window["SharedArrayBuffer"];
9+
var sharedArrayBufferString = NativeSharedArrayBuffer ? Object_prototype_toString.call(NativeSharedArrayBuffer) : "";
810
var NativeUint8Array = window.Uint8Array;
911
var patchedU8Array = NativeUint8Array || Array;
10-
var ArrayBufferString = Object_prototype_toString.call((window.ArrayBuffer || patchedU8Array).prototype);
12+
var arrayBufferString = Object_prototype_toString.call((NativeUint8Array ? ArrayBuffer : patchedU8Array).prototype);
1113
function decoderReplacer(encoded){
1214
var codePoint = encoded.charCodeAt(0) << 24;
1315
var leadingOnes = clz32(~codePoint)|0;
@@ -32,9 +34,10 @@
3234
return result;
3335
}
3436
function TextDecoder(){};
35-
TextDecoder.prototype.decode = function(inputArrayOrBuffer){
37+
TextDecoder["prototype"]["decode"] = function(inputArrayOrBuffer){
3638
var buffer = (inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer;
37-
if (Object_prototype_toString.call(buffer) !== ArrayBufferString)
39+
var asObjectString = Object_prototype_toString.call(buffer);
40+
if (asObjectString !== arrayBufferString && asObjectString !== sharedArrayBufferString)
3841
throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
3942
var inputAs8 = NativeUint8Array ? new patchedU8Array(buffer) : buffer;
4043
var resultingString = "";
@@ -74,7 +77,7 @@
7477
);
7578
}
7679
function TextEncoder(){};
77-
TextEncoder.prototype.encode = function(inputString){
80+
TextEncoder["prototype"]["encode"] = function(inputString){
7881
// 0xc0 => 0b11000000; 0xff => 0b11111111; 0xc0-0xff => 0b11xxxxxx
7982
// 0x80 => 0b10000000; 0xbf => 0b10111111; 0x80-0xbf => 0b10xxxxxx
8083
var encodedString = inputString === void 0 ? "" : ("" + inputString).replace(/[\x80-\uD7ff\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g, encoderReplacer);

NodeJS/EncoderAndDecoderNodeJS.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)