Reviewers: ihab.awad,

Description:
Adds MarkM's StringMap implementation and uses it instead of an object
to gather free vars in atLeastFreeVars

Please review this at http://codereview.appspot.com/5485073/

Affected files:
  M     build.xml
  A     src/com/google/caja/ses/StringMap.js
  M     src/com/google/caja/ses/atLeastFreeVarNames.js
  M     src/com/google/caja/ses/startSES.js


Index: src/com/google/caja/ses/StringMap.js
===================================================================
--- src/com/google/caja/ses/StringMap.js        (revision 0)
+++ src/com/google/caja/ses/StringMap.js        (revision 0)
@@ -0,0 +1,47 @@
+// Copyright (C) 2011 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @fileoverview Implements StringMap - a map api for strings.
+ *
+ * @author Mark S. Miller
+ * @requires cajaVM
+ * @provides StringMap
+ */
+
+function StringMap() {
+
+  function assertString(x) {
+    if ('string' !== typeof(x)) {
+      throw new TypeError('Not a string: ' + String(x));
+    }
+    return x;
+  }
+
+  var objAsMap = Object.create(null);
+  return Object.freeze({
+    get: function(key) {
+        return objAsMap[assertString(key) + '$'];
+      },
+    set: function(key, value) {
+        objAsMap[assertString(key) + '$'] = value;
+      },
+    has: function(key) {
+        return (assertString(key) + '$') in objAsMap;
+      },
+    'delete': function(key) {
+        return delete objAsMap[assertString(key) + '$'];
+      }
+  });
+}
Index: src/com/google/caja/ses/startSES.js
===================================================================
--- src/com/google/caja/ses/startSES.js (revision 4681)
+++ src/com/google/caja/ses/startSES.js (working copy)
@@ -378,7 +378,7 @@
      */
     function makeScopeObject(imports, freeNames) {
       var scopeObject = Object.create(null);
-      Object.keys(freeNames).forEach(function(name) {
+      freeNames.forEach(function(name) {
         var desc = Object.getOwnPropertyDescriptor(imports, name);
         if (!desc || desc.writable !== false || desc.configurable) {
           // If there is no own property, or it isn't a non-writable
Index: src/com/google/caja/ses/atLeastFreeVarNames.js
===================================================================
--- src/com/google/caja/ses/atLeastFreeVarNames.js      (revision 4681)
+++ src/com/google/caja/ses/atLeastFreeVarNames.js      (working copy)
@@ -91,7 +91,8 @@
     var regexp = SHOULD_MATCH_IDENTIFIER();
     // Once we decide this file can depends on ES5, the following line
     // should say "... = Object.create(null);" rather than "... = {};"
-    var result = {};
+    var result = [];
+    var found = StringMap();
     var a;
     while ((a = regexp.exec(programSrc))) {
       // Note that we could have avoided the while loop by doing
@@ -100,7 +101,11 @@
       // apparent identifiers, rather than the total number of
       // apparently unique identifiers.
       var name = a[0];
-      result[name] = true;
+
+      if (!found.has(name)) {
+        result.push(name);
+        found.set(name, true);
+      }
     }
     return result;
   };
Index: build.xml
===================================================================
--- build.xml   (revision 4681)
+++ build.xml   (working copy)
@@ -852,6 +852,7 @@
       <input file="${src.caja}/ses/logger.js"/>
       <input file="${src.caja}/ses/repairES5.js"/>
       <input file="${src.caja}/ses/WeakMap.js"/>
+      <input file="${src.caja}/ses/StringMap.js"/>
       <input file="${src.caja}/ses/whitelist.js"/>
       <input file="${src.caja}/ses/atLeastFreeVarNames.js"/>
       <input file="${src.caja}/ses/startSES.js"/>
@@ -865,6 +866,7 @@
       <input file="${src.caja}/ses/logger.js"/>
       <input file="${src.caja}/ses/repairES5.js"/>
       <input file="${src.caja}/ses/WeakMap.js"/>
+      <input file="${src.caja}/ses/StringMap.js"/>
       <input file="${src.caja}/ses/whitelist.js"/>
       <input file="${src.caja}/ses/atLeastFreeVarNames.js"/>
       <input file="${src.caja}/ses/startSES.js"/>
@@ -979,6 +981,7 @@
       <input file="${src.caja}/ses/logger.js"/>
       <input file="${src.caja}/ses/repairES5.js"/>
       <input file="${src.caja}/ses/WeakMap.js"/>
+      <input file="${src.caja}/ses/StringMap.js"/>
       <input file="${src.caja}/ses/whitelist.js"/>
       <input file="${src.caja}/ses/atLeastFreeVarNames.js"/>
       <input file="${src.caja}/ses/startSES.js"/>


Reply via email to