Revision: 4682
Author:   jasvir
Date:     Wed Dec 14 14:11:00 2011
Log: Fixes a bug in atLeastFreeVars which was using objects as maps and failing on __proto__ and friends.
http://codereview.appspot.com/5485073

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

[email protected]

http://code.google.com/p/google-caja/source/detail?r=4682

Added:
 /trunk/src/com/google/caja/ses/StringMap.js
Modified:
 /trunk/build.xml
 /trunk/src/com/google/caja/ses/atLeastFreeVarNames.js
 /trunk/src/com/google/caja/ses/startSES.js

=======================================
--- /dev/null
+++ /trunk/src/com/google/caja/ses/StringMap.js Wed Dec 14 14:11:00 2011
@@ -0,0 +1,55 @@
+// 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
+ * @author Jasvir Nagra
+ * @requires cajaVM
+ * @provides StringMap
+ */
+
+function StringMap() {
+
+  function assertString(x) {
+    if ('string' !== typeof(x)) {
+      throw new TypeError('Not a string: ' + String(x));
+    }
+    return x;
+  }
+
+  var def;
+  if ('undefined' !== typeof cajaVM) {
+    def = cajaVM.def;
+  } else {
+    def = Object.freeze;
+  }
+
+  var objAsMap = Object.create(null);
+  return def({
+    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) + '$'];
+      }
+  });
+}
=======================================
--- /trunk/build.xml    Tue Dec 13 15:05:45 2011
+++ /trunk/build.xml    Wed Dec 14 14:11:00 2011
@@ -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"/>
=======================================
--- /trunk/src/com/google/caja/ses/atLeastFreeVarNames.js Fri Oct 21 18:37:00 2011 +++ /trunk/src/com/google/caja/ses/atLeastFreeVarNames.js Wed Dec 14 14:11:00 2011
@@ -21,6 +21,7 @@
  * anticipated ES6.
  *
  * @author Mark S. Miller
+ * @requires StringMap
  * @overrides ses, atLeastFreeVarNamesModule
  */
 var ses;
@@ -91,7 +92,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 +102,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;
   };
=======================================
--- /trunk/src/com/google/caja/ses/startSES.js  Tue Nov  8 10:55:13 2011
+++ /trunk/src/com/google/caja/ses/startSES.js  Wed Dec 14 14:11:00 2011
@@ -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

Reply via email to