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 release] Only freeze params, hash when WeakMap available #14649

Merged
merged 1 commit into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions packages/ember-glimmer/lib/utils/references.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RenderingTest, moduleFor } from '../../utils/test-case';
import { makeBoundHelper } from '../../utils/helpers';
import { runDestroy } from 'internal-test-helpers';
import { set } from 'ember-metal';
import { HAS_NATIVE_WEAKMAP } from 'ember-utils';

let assert = QUnit.assert;

Expand Down Expand Up @@ -174,6 +175,30 @@ moduleFor('Helpers test: custom helpers', class extends RenderingTest {
assert.strictEqual(destroyCount, 0, 'destroy is not called on recomputation');
}

['@test helper params can be returned']() {
this.registerHelper('hello-world', values => {
return values;
});

this.render('{{#each (hello-world model) as |item|}}({{item}}){{/each}}', {
model: ['bob']
});

this.assertText('(bob)');
}

['@test helper hash can be returned']() {
this.registerHelper('hello-world', (_, hash) => {
return hash.model;
});

this.render(`{{get (hello-world model=model) 'name'}}`, {
model: { name: 'bob' }
});

this.assertText('bob');
}

['@test simple helper is called for param changes']() {
let computeCount = 0;

Expand Down Expand Up @@ -602,7 +627,7 @@ let addingPropertyToFrozenObjectThrows = (() => {
}
})();

if (!EmberDev.runningProdBuild && (
if (!EmberDev.runningProdBuild && HAS_NATIVE_WEAKMAP && (
pushingIntoFrozenArrayThrows ||
assigningExistingFrozenPropertyThrows ||
addingPropertyToFrozenObjectThrows
Expand Down
18 changes: 6 additions & 12 deletions packages/ember-metal/lib/meta.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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]';
})();