From f089e5c8467047a1571ecbbfc5556a7e619c6b1d Mon Sep 17 00:00:00 2001 From: adamk Date: Tue, 7 Apr 2015 12:00:34 -0700 Subject: [PATCH] Simplify collections.js now that it's wrapped in an IIFE Also wrap templates.js in an IIFE to avoid unnecessary pollution of the builtins object. Review URL: https://codereview.chromium.org/1067903004 Cr-Commit-Position: refs/heads/master@{#27638} --- src/collection.js | 92 +++++++++++++++++++++---------------------------------- src/templates.js | 20 +++++++++--- 2 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/collection.js b/src/collection.js index 537dbd6..928670c 100644 --- a/src/collection.js +++ b/src/collection.js @@ -12,13 +12,10 @@ var $Set = global.Set; var $Map = global.Map; -// Used by harmony-templates.js -var $MapGet; -var $MapSet; - - (function() { +%CheckIsBootstrapping(); + function HashToEntry(table, hash, numBuckets) { var bucket = ORDERED_HASH_TABLE_HASH_TO_BUCKET(hash, numBuckets); @@ -238,31 +235,23 @@ function SetForEach(f, receiver) { } -// ------------------------------------------------------------------- +%SetCode($Set, SetConstructor); +%FunctionSetPrototype($Set, new $Object()); +%AddNamedProperty($Set.prototype, "constructor", $Set, DONT_ENUM); +%AddNamedProperty( + $Set.prototype, symbolToStringTag, "Set", DONT_ENUM | READ_ONLY); -function SetUpSet() { - %CheckIsBootstrapping(); - - %SetCode($Set, SetConstructor); - %FunctionSetPrototype($Set, new $Object()); - %AddNamedProperty($Set.prototype, "constructor", $Set, DONT_ENUM); - %AddNamedProperty( - $Set.prototype, symbolToStringTag, "Set", DONT_ENUM | READ_ONLY); - - %FunctionSetLength(SetForEach, 1); - - // Set up the non-enumerable functions on the Set prototype object. - InstallGetter($Set.prototype, "size", SetGetSize); - InstallFunctions($Set.prototype, DONT_ENUM, $Array( - "add", SetAdd, - "has", SetHas, - "delete", SetDelete, - "clear", SetClearJS, - "forEach", SetForEach - )); -} +%FunctionSetLength(SetForEach, 1); -SetUpSet(); +// Set up the non-enumerable functions on the Set prototype object. +InstallGetter($Set.prototype, "size", SetGetSize); +InstallFunctions($Set.prototype, DONT_ENUM, $Array( + "add", SetAdd, + "has", SetHas, + "delete", SetDelete, + "clear", SetClearJS, + "forEach", SetForEach +)); // ------------------------------------------------------------------- @@ -434,34 +423,23 @@ function MapForEach(f, receiver) { } -// ------------------------------------------------------------------- - -function SetUpMap() { - %CheckIsBootstrapping(); - - %SetCode($Map, MapConstructor); - %FunctionSetPrototype($Map, new $Object()); - %AddNamedProperty($Map.prototype, "constructor", $Map, DONT_ENUM); - %AddNamedProperty( - $Map.prototype, symbolToStringTag, "Map", DONT_ENUM | READ_ONLY); - - %FunctionSetLength(MapForEach, 1); - - // Set up the non-enumerable functions on the Map prototype object. - InstallGetter($Map.prototype, "size", MapGetSize); - InstallFunctions($Map.prototype, DONT_ENUM, $Array( - "get", MapGet, - "set", MapSet, - "has", MapHas, - "delete", MapDelete, - "clear", MapClearJS, - "forEach", MapForEach - )); - - $MapGet = MapGet; - $MapSet = MapSet; -} - -SetUpMap(); +%SetCode($Map, MapConstructor); +%FunctionSetPrototype($Map, new $Object()); +%AddNamedProperty($Map.prototype, "constructor", $Map, DONT_ENUM); +%AddNamedProperty( + $Map.prototype, symbolToStringTag, "Map", DONT_ENUM | READ_ONLY); + +%FunctionSetLength(MapForEach, 1); + +// Set up the non-enumerable functions on the Map prototype object. +InstallGetter($Map.prototype, "size", MapGetSize); +InstallFunctions($Map.prototype, DONT_ENUM, $Array( + "get", MapGet, + "set", MapSet, + "has", MapHas, + "delete", MapDelete, + "clear", MapClearJS, + "forEach", MapForEach +)); })(); diff --git a/src/templates.js b/src/templates.js index feb5b06..6dae189 100644 --- a/src/templates.js +++ b/src/templates.js @@ -4,7 +4,17 @@ "use strict"; +// Called from a desugaring in the parser. +var GetTemplateCallSite; + +(function() { + +%CheckIsBootstrapping(); + var callSiteCache = new $Map; +var mapGetFn = $Map.prototype.get; +var mapSetFn = $Map.prototype.set; + function SameCallSiteElements(rawStrings, other) { var length = rawStrings.length; @@ -21,7 +31,7 @@ function SameCallSiteElements(rawStrings, other) { function GetCachedCallSite(siteObj, hash) { - var obj = %_CallFunction(callSiteCache, hash, $MapGet); + var obj = %_CallFunction(callSiteCache, hash, mapGetFn); if (IS_UNDEFINED(obj)) return; @@ -33,13 +43,13 @@ function GetCachedCallSite(siteObj, hash) { function SetCachedCallSite(siteObj, hash) { - var obj = %_CallFunction(callSiteCache, hash, $MapGet); + var obj = %_CallFunction(callSiteCache, hash, mapGetFn); var array; if (IS_UNDEFINED(obj)) { array = new InternalArray(1); array[0] = siteObj; - %_CallFunction(callSiteCache, hash, array, $MapSet); + %_CallFunction(callSiteCache, hash, array, mapSetFn); } else { obj.push(siteObj); } @@ -48,7 +58,7 @@ function SetCachedCallSite(siteObj, hash) { } -function GetTemplateCallSite(siteObj, rawStrings, hash) { +GetTemplateCallSite = function(siteObj, rawStrings, hash) { var cached = GetCachedCallSite(rawStrings, hash); if (!IS_UNDEFINED(cached)) return cached; @@ -58,3 +68,5 @@ function GetTemplateCallSite(siteObj, rawStrings, hash) { return SetCachedCallSite(%ObjectFreeze(siteObj), hash); } + +})(); -- 2.7.4