Skip to content

Commit

Permalink
[BUGFIX] Only freeze empty array/dict with weakmap
Browse files Browse the repository at this point in the history
When arrays and objects are frozen in JavaScript, it is impossible to
attach meta-data (like Ember's own `meta`) to them without using a
WeakMap. Ember does adopt the WeakMap strategy in browsers that support
it, however there are still supported environments (IE9, IE10) where
`Object.freeze` is supported but WeakMap is not. But not freezing these
empty arrays and object if WeakMap is missing, legacy meta-data
strategies are permitted on those instances.

See:

* emberjs/ember.js#14264
* emberjs/ember.js#14244
  • Loading branch information
mixonic committed Dec 17, 2016
1 parent fe53956 commit 6954ade
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/glimmer-runtime/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Dict, dict } from 'glimmer-util';

export const EMPTY_ARRAY = Object.freeze([]);
export const EMPTY_DICT: Dict<any> = Object.freeze(dict<any>());
const HAS_NATIVE_WEAKMAP = (function() {
// detect if `WeakMap` is even present
let hasWeakMap = typeof WeakMap === 'function';
if (!hasWeakMap) { return false; }

let instance = new WeakMap();
// use `Object`'s `.toString` directly to prevent us from detecting
// polyfills as native weakmaps
return Object.prototype.toString.call(instance) === '[object WeakMap]';
})();

export const EMPTY_ARRAY = HAS_NATIVE_WEAKMAP ? Object.freeze([]) : [];
export const EMPTY_DICT: Dict<any> = HAS_NATIVE_WEAKMAP ? Object.freeze(dict<any>()) : dict<any>();

export interface EnumerableCallback<T> {
(item: T): void;
Expand Down

0 comments on commit 6954ade

Please sign in to comment.