Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Only freeze empty array/dict with weakmap #361

Merged
merged 1 commit into from
Jan 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, core-js monkey patches toString to even emulate ^. Given that, it is unclear to me if we can detect the real thing any better then how you have written it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm, I did a ton of research around this before settling for it in ember itself. I just don't think there is a better way that isn't more brittle than this check.

})();

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