Skip to content

Commit

Permalink
fix closure minification of Math.*, and remove previous workaround co…
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken authored and Beuc committed Nov 17, 2018
1 parent 81eccb3 commit e3891c8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ flags.binary;
*/
Document.prototype.currentScript;

/**
* Don't minify Math.*
*/
/**
* @suppress {duplicate}
*/
var Math = {};
Math.abs = function() {};
Math.cos = function() {};
Math.sin = function() {};
Math.tan = function() {};
Math.acos = function() {};
Math.asin = function() {};
Math.atan = function() {};
Math.atan2 = function() {};
Math.exp = function() {};
Math.log = function() {};
Math.sqrt = function() {};
Math.ceil = function() {};
Math.floor = function() {};
Math.pow = function() {};
Math.imul = function() {};
Math.fround = function() {};
Math.round = function() {};
Math.min = function() {};
Math.max = function() {};
Math.clz32 = function() {};
Math.trunc = function() {};

/**
* SIMD.js support (not in upstream closure yet).
*/
Expand Down
26 changes: 11 additions & 15 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,32 +1422,30 @@ function writeAsciiToMemory(str, buffer, dontAddNull) {

#if POLYFILL_OLD_MATH_FUNCTIONS
// check for imul support, and also for correctness ( https://bugs.webkit.org/show_bug.cgi?id=126345 )
if (!Math['imul'] || Math['imul'](0xffffffff, 5) !== -5) Math['imul'] = function imul(a, b) {
if (!Math.imul || Math.imul(0xffffffff, 5) !== -5) Math.imul = function imul(a, b) {
var ah = a >>> 16;
var al = a & 0xffff;
var bh = b >>> 16;
var bl = b & 0xffff;
return (al*bl + ((ah*bl + al*bh) << 16))|0;
};
Math.imul = Math['imul'];

#if PRECISE_F32
#if PRECISE_F32 == 1
if (!Math['fround']) {
if (!Math.fround) {
var froundBuffer = new Float32Array(1);
Math['fround'] = function(x) { froundBuffer[0] = x; return froundBuffer[0] };
Math.fround = function(x) { froundBuffer[0] = x; return froundBuffer[0] };
}
#else // 2
if (!Math['fround']) Math['fround'] = function(x) { return x };
if (!Math.fround) Math.fround = function(x) { return x };
#endif
Math.fround = Math['fround'];
#else
#if SIMD
if (!Math['fround']) Math['fround'] = function(x) { return x };
if (!Math.fround) Math.fround = function(x) { return x };
#endif
#endif

if (!Math['clz32']) Math['clz32'] = function(x) {
if (!Math.clz32) Math.clz32 = function(x) {
var n = 32;
var y = x >> 16; if (y) { n -= 16; x = y; }
y = x >> 8; if (y) { n -= 8; x = y; }
Expand All @@ -1456,18 +1454,16 @@ if (!Math['clz32']) Math['clz32'] = function(x) {
y = x >> 1; if (y) return n - 2;
return n - x;
};
Math.clz32 = Math['clz32']

if (!Math['trunc']) Math['trunc'] = function(x) {
if (!Math.trunc) Math.trunc = function(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
};
Math.trunc = Math['trunc'];
#else // POLYFILL_OLD_MATH_FUNCTIONS
#if ASSERTIONS
assert(Math['imul'], 'This browser does not support Math.imul(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math['fround'], 'This browser does not support Math.fround(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math['clz32'], 'This browser does not support Math.clz32(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math['trunc'], 'This browser does not support Math.trunc(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math.imul, 'This browser does not support Math.imul(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math.fround, 'This browser does not support Math.fround(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math.clz32, 'This browser does not support Math.clz32(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
assert(Math.trunc, 'This browser does not support Math.trunc(), build with LEGACY_VM_SUPPORT or POLYFILL_OLD_MATH_FUNCTIONS to add in a polyfill');
#endif
#endif // LEGACY_VM_SUPPORT

Expand Down
5 changes: 5 additions & 0 deletions tests/webgl_create_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ int main()
if (vb2 != vb4) printf("Index 1: Generated VB: %d, read back VB: %d\n", vb2, vb4);
assert(vb2 == vb4);

// Test bug https://github.com/kripken/emscripten/issues/7472:
GLint enabled = 0;
glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
assert(enabled == 0);

// Test that deleting the context works.
res = emscripten_webgl_destroy_context(context);
assert(res == 0);
Expand Down

0 comments on commit e3891c8

Please sign in to comment.