From 50ac9a16dfeff549a8b3e1131cbb960085e55560 Mon Sep 17 00:00:00 2001 From: James Brock Date: Tue, 21 Sep 2021 13:28:48 +0900 Subject: [PATCH] Execute the IE TypedArray polyfill directly Because the polyFill() function gets dead-code-eliminated by `purs bundle` in purs 0.14.4. --- CHANGELOG.md | 4 ++++ src/Data/ArrayBuffer/Typed.js | 23 +++++++++++++---------- src/Data/ArrayBuffer/Typed.purs | 1 + 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df7523..ba125a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +@jamesdbrock Bugfix for purs bundle 0.14.4. https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + ## v11.0.0 Jorge Acereda has graciously donated this package to __purescript-contrib__. diff --git a/src/Data/ArrayBuffer/Typed.js b/src/Data/ArrayBuffer/Typed.js index cf2d733..ef0a4fc 100644 --- a/src/Data/ArrayBuffer/Typed.js +++ b/src/Data/ArrayBuffer/Typed.js @@ -1,24 +1,27 @@ "use strict"; - - -// Lightweight polyfill for ie - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill -function polyFill () { - var typedArrayTypes = +// Lightweight polyfill for IE +// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Methods_Polyfill +// +// Can we trust that this FFI code gets called exactly once? I think so, but +// I can't find a reference for that. +{ + // var is not block scoped, but there is no way to scope this block inside + // a function in such a way that the function will not be dead-code-eliminated. + // https://github.com/purescript-contrib/purescript-arraybuffer/issues/34 + var _typedArrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array , Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array ]; - for (var k in typedArrayTypes) { + for (var k in _typedArrayTypes) { for (var v in Array.prototype) { - if (Array.prototype.hasOwnProperty(v) && !typedArrayTypes[k].prototype.hasOwnProperty(v)) - typedArrayTypes[k].prototype[v] = Array.prototype[v]; + if (Array.prototype.hasOwnProperty(v) && !_typedArrayTypes[k].prototype.hasOwnProperty(v)) + _typedArrayTypes[k].prototype[v] = Array.prototype[v]; } } }; -polyFill(); - // module Data.ArrayBuffer.Typed exports.buffer = function buffer (v) { diff --git a/src/Data/ArrayBuffer/Typed.purs b/src/Data/ArrayBuffer/Typed.purs index b7828a6..271248d 100644 --- a/src/Data/ArrayBuffer/Typed.purs +++ b/src/Data/ArrayBuffer/Typed.purs @@ -401,3 +401,4 @@ compare a b = Prelude.compare <$> toArray a <*> toArray b -- | Equality test for typed arrays. eq :: forall a t. TypedArray a t => Eq t => ArrayView a -> ArrayView a -> Effect Boolean eq a b = Prelude.eq <$> toArray a <*> toArray b +