Skip to content

Commit

Permalink
Only freeze helper args if weakmap present
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Dec 12, 2016
1 parent 2f4e846 commit a39c9c1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
24 changes: 17 additions & 7 deletions packages/ember-glimmer/lib/utils/references.js
@@ -1,4 +1,8 @@
import { symbol, EmptyObject } from 'ember-utils';
import {
HAS_NATIVE_WEAKMAP,
symbol,
EmptyObject
} from 'ember-utils';
import {
get,
set,
Expand Down Expand Up @@ -289,8 +293,10 @@ export class SimpleHelperReference extends CachedReference {
let namedValue = named.value();

runInDebug(() => {
Object.freeze(positionalValue);
Object.freeze(namedValue);
if (HAS_NATIVE_WEAKMAP) {
Object.freeze(positionalValue);
Object.freeze(namedValue);
}
});

let result = helper(positionalValue, namedValue);
Expand Down Expand Up @@ -324,8 +330,10 @@ export class SimpleHelperReference extends CachedReference {
let namedValue = named.value();

runInDebug(() => {
Object.freeze(positionalValue);
Object.freeze(namedValue);
if (HAS_NATIVE_WEAKMAP) {
Object.freeze(positionalValue);
Object.freeze(namedValue);
}
});

return helper(positionalValue, namedValue);
Expand Down Expand Up @@ -354,8 +362,10 @@ export class ClassBasedHelperReference extends CachedReference {
let namedValue = named.value();

runInDebug(() => {
Object.freeze(positionalValue);
Object.freeze(namedValue);
if (HAS_NATIVE_WEAKMAP) {
Object.freeze(positionalValue);
Object.freeze(namedValue);
}
});

return instance.compute(positionalValue, namedValue);
Expand Down
Expand Up @@ -2,10 +2,8 @@
import { RenderingTest, moduleFor } from '../../utils/test-case';
import { makeBoundHelper } from '../../utils/helpers';
import { runDestroy } from 'internal-test-helpers';
import {
HAS_NATIVE_WEAKMAP,
set
} from 'ember-metal';
import { set } from 'ember-metal';
import { HAS_NATIVE_WEAKMAP } from 'ember-utils';

let assert = QUnit.assert;

Expand Down
18 changes: 6 additions & 12 deletions packages/ember-metal/lib/meta.js
Expand Up @@ -2,7 +2,12 @@
// Remove "use strict"; from transpiled module until
// https://bugs.webkit.org/show_bug.cgi?id=138038 is fixed

import { EmptyObject, lookupDescriptor, symbol } from 'ember-utils';
import {
HAS_NATIVE_WEAKMAP,
EmptyObject,
lookupDescriptor,
symbol
} from 'ember-utils';
import isEnabled from './features';
import { protoMethods as listenerMethods } from './meta_listeners';
import { runInDebug, assert } from './debug';
Expand Down Expand Up @@ -460,17 +465,6 @@ if (isEnabled('mandatory-setter')) {
};
}

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]';
})();

let setMeta, peekMeta;

// choose the one appropriate for given platform
Expand Down
1 change: 1 addition & 0 deletions packages/ember-utils/lib/index.js
Expand Up @@ -30,3 +30,4 @@ export { default as makeArray } from './make-array';
export { default as applyStr } from './apply-str';
export { default as NAME_KEY } from './name';
export { default as toString } from './to-string';
export { HAS_NATIVE_WEAKMAP } from './weak-map-utils';
10 changes: 10 additions & 0 deletions packages/ember-utils/lib/weak-map-utils.js
@@ -0,0 +1,10 @@
export 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]';
})();

0 comments on commit a39c9c1

Please sign in to comment.