From 852721fd7471246862dd355f7fa8b494bd023230 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Wed, 11 Sep 2013 12:53:24 -0400 Subject: [PATCH] Replace SideTable with WeakMap --- build.json | 2 +- conf/karma.conf.js | 1 + shadowdom.js | 2 +- src/ShadowRenderer.js | 12 ++++----- src/sidetable.js | 40 ----------------------------- src/wrappers.js | 4 +-- src/wrappers/Document.js | 2 +- src/wrappers/Element.js | 2 +- src/wrappers/HTMLTemplateElement.js | 4 +-- src/wrappers/ShadowRoot.js | 4 +-- src/wrappers/events.js | 22 ++++++++-------- 11 files changed, 28 insertions(+), 67 deletions(-) delete mode 100644 src/sidetable.js diff --git a/build.json b/build.json index f2cd87e..3b6fbc3 100644 --- a/build.json +++ b/build.json @@ -1,6 +1,6 @@ [ "../observe-js/src/observe.js", - "src/sidetable.js", + "../WeakMap/weakmap.js", "src/wrappers.js", "src/wrappers/events.js", "src/wrappers/NodeList.js", diff --git a/conf/karma.conf.js b/conf/karma.conf.js index ad5a580..d27221c 100644 --- a/conf/karma.conf.js +++ b/conf/karma.conf.js @@ -7,6 +7,7 @@ module.exports = function(karma) { // list of files / patterns to load in the browser files: [ 'observe-js/src/observe.js', + 'WeakMap/weakmap.js', 'ShadowDOM/tools/test/mocha-htmltest.js', 'ShadowDOM/conf/mocha.conf.js', 'ShadowDOM/node_modules/chai/chai.js', diff --git a/shadowdom.js b/shadowdom.js index 3ceef66..98c76b4 100644 --- a/shadowdom.js +++ b/shadowdom.js @@ -16,7 +16,7 @@ [ '../observe-js/src/observe.js', - 'src/sidetable.js', + '../WeakMap/weakmap.js', 'src/wrappers.js', 'src/wrappers/events.js', 'src/wrappers/NodeList.js', diff --git a/src/ShadowRenderer.js b/src/ShadowRenderer.js index 8de3fae..620390c 100644 --- a/src/ShadowRenderer.js +++ b/src/ShadowRenderer.js @@ -97,11 +97,11 @@ parentNode.removeChild(node); } - var distributedChildNodesTable = new SideTable(); - var eventParentsTable = new SideTable(); - var insertionParentTable = new SideTable(); - var rendererForHostTable = new SideTable(); - var shadowDOMRendererTable = new SideTable(); + var distributedChildNodesTable = new WeakMap(); + var eventParentsTable = new WeakMap(); + var insertionParentTable = new WeakMap(); + var rendererForHostTable = new WeakMap(); + var shadowDOMRendererTable = new WeakMap(); function distributeChildToInsertionPoint(child, insertionPoint) { getDistributedChildNodes(insertionPoint).push(child); @@ -283,7 +283,7 @@ var newChildren = this.childNodes; // plain array of real nodes. var oldChildren = getChildNodesSnapshot(unwrap(nodeWrapper)); - var added = opt_added || new SideTable(); + var added = opt_added || new WeakMap(); var splices = spliceDiff.calculateSplices(newChildren, oldChildren); diff --git a/src/sidetable.js b/src/sidetable.js deleted file mode 100644 index 8e783b5..0000000 --- a/src/sidetable.js +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2012 The Polymer Authors. All rights reserved. - * Use of this source code is goverened by a BSD-style - * license that can be found in the LICENSE file. - */ - -// SideTable is a weak map where possible. If WeakMap is not available the -// association is stored as an expando property. -var SideTable; -// TODO(arv): WeakMap does not allow for Node etc to be keys in Firefox -if (typeof WeakMap !== 'undefined' && navigator.userAgent.indexOf('Firefox/') < 0) { - SideTable = WeakMap; -} else { - (function() { - var defineProperty = Object.defineProperty; - var counter = Date.now() % 1e9; - - SideTable = function() { - this.name = '__st' + (Math.random() * 1e9 >>> 0) + (counter++ + '__'); - }; - - SideTable.prototype = { - set: function(key, value) { - var entry = key[this.name]; - if (entry && entry[0] === key) - entry[1] = value; - else - defineProperty(key, this.name, {value: [key, value], writable: true}); - }, - get: function(key) { - var entry; - return (entry = key[this.name]) && entry[0] === key ? - entry[1] : undefined; - }, - delete: function(key) { - this.set(key, undefined); - } - } - })(); -} diff --git a/src/wrappers.js b/src/wrappers.js index 19dc6f3..c3795c6 100644 --- a/src/wrappers.js +++ b/src/wrappers.js @@ -7,8 +7,8 @@ var ShadowDOMPolyfill = {}; (function(scope) { 'use strict'; - var constructorTable = new SideTable(); - var nativePrototypeTable = new SideTable(); + var constructorTable = new WeakMap(); + var nativePrototypeTable = new WeakMap(); var wrappers = Object.create(null); // Don't test for eval if document has CSP securityPolicy object and we can diff --git a/src/wrappers/Document.js b/src/wrappers/Document.js index 2e09c76..1390058 100644 --- a/src/wrappers/Document.js +++ b/src/wrappers/Document.js @@ -21,7 +21,7 @@ var wrapEventTargetMethods = scope.wrapEventTargetMethods; var wrapNodeList = scope.wrapNodeList; - var implementationTable = new SideTable(); + var implementationTable = new WeakMap(); function Document(node) { Node.call(this, node); diff --git a/src/wrappers/Element.js b/src/wrappers/Element.js index 039ff20..02797f5 100644 --- a/src/wrappers/Element.js +++ b/src/wrappers/Element.js @@ -16,7 +16,7 @@ var registerWrapper = scope.registerWrapper; var wrappers = scope.wrappers; - var shadowRootTable = new SideTable(); + var shadowRootTable = new WeakMap(); var OriginalElement = window.Element; diff --git a/src/wrappers/HTMLTemplateElement.js b/src/wrappers/HTMLTemplateElement.js index 740abf5..fb58b47 100644 --- a/src/wrappers/HTMLTemplateElement.js +++ b/src/wrappers/HTMLTemplateElement.js @@ -13,8 +13,8 @@ var unwrap = scope.unwrap; var wrap = scope.wrap; - var contentTable = new SideTable(); - var templateContentsOwnerTable = new SideTable(); + var contentTable = new WeakMap(); + var templateContentsOwnerTable = new WeakMap(); // http://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/templates/index.html#dfn-template-contents-owner function getTemplateContentsOwner(doc) { diff --git a/src/wrappers/ShadowRoot.js b/src/wrappers/ShadowRoot.js index b2e9556..75b7c78 100644 --- a/src/wrappers/ShadowRoot.js +++ b/src/wrappers/ShadowRoot.js @@ -13,8 +13,8 @@ var setInnerHTML = scope.setInnerHTML; var unwrap = scope.unwrap; - var shadowHostTable = new SideTable(); - var nextOlderShadowTreeTable = new SideTable(); + var shadowHostTable = new WeakMap(); + var nextOlderShadowTreeTable = new WeakMap(); function ShadowRoot(hostWrapper) { var node = unwrap(hostWrapper.impl.ownerDocument.createDocumentFragment()); diff --git a/src/wrappers/events.js b/src/wrappers/events.js index f5edc8a..c24bd33 100644 --- a/src/wrappers/events.js +++ b/src/wrappers/events.js @@ -12,17 +12,17 @@ var wrap = scope.wrap; var wrappers = scope.wrappers; - var wrappedFuns = new SideTable(); - var listenersTable = new SideTable(); - var handledEventsTable = new SideTable(); - var targetTable = new SideTable(); - var currentTargetTable = new SideTable(); - var relatedTargetTable = new SideTable(); - var eventPhaseTable = new SideTable(); - var stopPropagationTable = new SideTable(); - var stopImmediatePropagationTable = new SideTable(); - var eventHandlersTable = new SideTable(); - var eventPathTable = new SideTable(); + var wrappedFuns = new WeakMap(); + var listenersTable = new WeakMap(); + var handledEventsTable = new WeakMap(); + var targetTable = new WeakMap(); + var currentTargetTable = new WeakMap(); + var relatedTargetTable = new WeakMap(); + var eventPhaseTable = new WeakMap(); + var stopPropagationTable = new WeakMap(); + var stopImmediatePropagationTable = new WeakMap(); + var eventHandlersTable = new WeakMap(); + var eventPathTable = new WeakMap(); function isShadowRoot(node) { return node instanceof wrappers.ShadowRoot;