Skip to content

Commit 2984b0a

Browse files
authored
Use polyfil rather for Object.assign (#15822)
This gives a small code size saving and it makes our codebase more readable. The reason for this change is that I ran into an issue where I was trying to use `objAssign` in more places and it became apparent that MINIMAL_RUNTIME didn't define the `objAssign` helper. Also, move the defintion of the polyfill inside of `#if POLYFILL` so it can be disabled using `-sNO_POLYFILL`. See #15823 #15938
1 parent a41876c commit 2984b0a

33 files changed

+68
-44
lines changed

src/polyfill/objassign.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Object.assign polyfill from:
2+
// https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/js/es6/util/assign.js
3+
4+
#if !POLYFILL
5+
assert(false, "this file should never be included unless POLYFILL is set");
6+
#endif
7+
8+
/**
9+
* Equivalent to the Object.assign() method, but guaranteed to be available for use in code
10+
* generated by the compiler.
11+
*
12+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
13+
*
14+
* Copies values of all enumerable own properties from one or more
15+
* sources to the given target object, and returns the target.
16+
*
17+
* @final
18+
* @param {!Object} target The target object onto which to copy.
19+
* @param {...?Object} var_args The source objects.
20+
* @return {!Object} The target object is returned.
21+
*/
22+
if (Object.assign === 'undefined') {
23+
Object.assign = function(target, source) {
24+
for (var i = 1; i < arguments.length; i++) {
25+
var source = arguments[i];
26+
if (!source) continue;
27+
for (var key in source) {
28+
if (source.hasOwnProperty(key)) target[key] = source[key];
29+
}
30+
}
31+
return target;
32+
};
33+
}

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ function instantiateSync(file, info) {
990990
// This does not work with nested objects that has prototypes, but it suffices for WasmSourceMap and WasmOffsetConverter.
991991
function resetPrototype(constructor, attrs) {
992992
var object = Object.create(constructor.prototype);
993-
return objAssign(object, attrs);
993+
return Object.assign(object, attrs);
994994
}
995995
#endif
996996

src/shell.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,12 @@ var Module = typeof {{{ EXPORT_NAME }}} !== 'undefined' ? {{{ EXPORT_NAME }}} :
4343
// modularize which creates a Promise for when the module is ready.
4444
#include "polyfill/promise.js"
4545
#endif
46-
#endif
4746

4847
// See https://caniuse.com/mdn-javascript_builtins_object_assign
4948
#if MIN_CHROME_VERSION < 45 || MIN_EDGE_VERSION < 12 || MIN_FIREFOX_VERSION < 34 || MIN_IE_VERSION != TARGET_NOT_SUPPORTED || MIN_SAFARI_VERSION < 90000
50-
function objAssign(target, source) {
51-
for (var key in source) {
52-
if (source.hasOwnProperty(key)) {
53-
target[key] = source[key];
54-
}
55-
}
56-
return target;
57-
}
58-
#else
59-
var objAssign = Object.assign;
49+
#include "polyfill/objassign.js"
6050
#endif
51+
#endif // POLYFILL
6152

6253
#if MODULARIZE
6354
// Set up the promise that indicates the Module is initialized
@@ -80,7 +71,7 @@ Module['ready'] = new Promise(function(resolve, reject) {
8071
// we collect those properties and reapply _after_ we configure
8172
// the current environment's defaults to avoid having to be so
8273
// defensive during initialization.
83-
var moduleOverrides = objAssign({}, Module);
74+
var moduleOverrides = Object.assign({}, Module);
8475

8576
var arguments_ = [];
8677
var thisProgram = './this.program';
@@ -420,7 +411,7 @@ if (ENVIRONMENT_IS_NODE) {
420411
#endif
421412

422413
// Merge back in the overrides
423-
objAssign(Module, moduleOverrides);
414+
Object.assign(Module, moduleOverrides);
424415
// Free the object hierarchy contained in the overrides, this lets the GC
425416
// reclaim data used e.g. in memoryInitializerRequest, which is a large typed array.
426417
moduleOverrides = null;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
96809
1+
96785
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
96809
1+
96785
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
96707
1+
96683
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
110263
1+
110239
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
111250
1+
111226
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
125520
1+
125497
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58390
1+
58367

0 commit comments

Comments
 (0)