Reviewers: ihab.awad,
Description:
* Warn when WeakMap is called as a function rather than a constructor.
Doing so is an error in ES6 spec drafts and Chrome Canary, so we
should not be more lenient when using emulated maps.
Fixes <https://code.google.com/p/google-caja/issues/detail?id=1832>.
* Use log-to-console.js in ES5/3 taming and guest frames; previously,
cajaVM.log would go nowhere in ES5/3 mode.
* Update log-to-console.js to pass linter.
Please review this at https://codereview.appspot.com/13348055/
Affected files (+56, -18 lines):
M build.xml
M src/com/google/caja/es53.js
M src/com/google/caja/log-to-console.js
M src/com/google/caja/ses/WeakMap.js
Index: src/com/google/caja/es53.js
===================================================================
--- src/com/google/caja/es53.js (revision 5594)
+++ src/com/google/caja/es53.js (working copy)
@@ -1518,7 +1518,18 @@
}
} else {
// No platform WeakMap; build our own.
- WeakMap = markFunc(function () { return newTable(true); });
+ var calledAsFunctionWarningDone = false;
+ WeakMap = markFunc(function () {
+ // Future ES6 WeakMap is currently (2013-09-10) expected to reject
+ // WeakMap() but we used to permit it and do it ourselves, so warn
only.
+ if (!(this instanceof WeakMap) && !calledAsFunctionWarningDone) {
+ calledAsFunctionWarningDone = true;
+ log('WeakMap should be invoked as new WeakMap(), not ' +
+ 'WeakMap(). This will be an error in the future.');
+ }
+
+ return newTable(true);
+ });
}
var registeredImports = [];
Index: src/com/google/caja/ses/WeakMap.js
===================================================================
--- src/com/google/caja/ses/WeakMap.js (revision 5594)
+++ src/com/google/caja/ses/WeakMap.js (working copy)
@@ -26,7 +26,7 @@
* is, which is why it is in the overrides list below.
*
* @author Mark S. Miller
- * @requires crypto, ArrayBuffer, Uint8Array, navigator
+ * @requires crypto, ArrayBuffer, Uint8Array, navigator, console
* @overrides WeakMap, ses, Proxy
* @overrides WeakMapModule
*/
@@ -354,12 +354,27 @@
return Object.freeze(func);
}
+ var calledAsFunctionWarningDone = false;
+ function calledAsFunctionWarning() {
+ // Future ES6 WeakMap is currently (2013-09-10) expected to reject
WeakMap()
+ // but we used to permit it and do it ourselves, so warn only.
+ if (!calledAsFunctionWarningDone && typeof console !== 'undefined') {
+ calledAsFunctionWarningDone = true;
+ console.warn('WeakMap should be invoked as new WeakMap(), not ' +
+ 'WeakMap(). This will be an error in the future.');
+ }
+ }
+
// Right now (12/25/2012) the histogram supports the current
// representation. We should check this occasionally, as a true
// constant time representation is easy.
// var histogram = [];
var OurWeakMap = function() {
+ if (!(this instanceof OurWeakMap)) {
+ calledAsFunctionWarning();
+ }
+
// We are currently (12/25/2012) never encountering any prematurely
// non-extensible keys.
var keys = []; // brute force for prematurely non-extensible keys.
@@ -503,6 +518,11 @@
// implementation which makes use of both as possible.
function DoubleWeakMap() {
+ // Be no more lenient than future ES6 WeakMap
+ if (!(this instanceof OurWeakMap)) {
+ calledAsFunctionWarning();
+ }
+
// Preferable, truly weak map.
var hmap = new HostWeakMap();
Index: src/com/google/caja/log-to-console.js
===================================================================
--- src/com/google/caja/log-to-console.js (revision 5594)
+++ src/com/google/caja/log-to-console.js (working copy)
@@ -13,23 +13,28 @@
// limitations under the License.
// .............................................................................
-// If this module is loaded after es53.js is loaded, and in an
-// environment (such as produced by turning on Firebug) where
-// <tt>console.log</tt> is a function, then it will register
-// a wrapper around <tt>console.log</tt> (or <tt>console.info</tt>
-// and <tt>console.error</tt> if available) using
-// <tt>___.setLogFunc()</tt>, so es53.js will log its diagnostics
-// to the Firebug console.
+/**
+ * @fileoverview
+ * If this module is loaded after es53.js is loaded, and in an
+ * environment (such as produced by turning on Firebug) where
+ * <tt>console.log</tt> is a function, then it will register
+ * a wrapper around <tt>console.log</tt> (or <tt>console.info</tt>
+ * and <tt>console.error</tt> if available) using
+ * <tt>___.setLogFunc()</tt>, so es53.js will log its diagnostics
+ * to the Firebug console.
+ *
+ * If you load triv-logger.js and log-to-console.js into the same
+ * system, the last one loaded wins.
+ *
+ * This module is written in Javascript, not Caja, and would be
+ * rejected by the Caja translator.
+ *
+ * @requires ___
+ */
-// If you load triv-logger.js and log-to-console.js into the same
-// system, the last one loaded wins.
+(function() {
+ var global = this;
-// This module is written in Javascript, not Caja, and would be
-// rejected by the Caja translator.
-
-
-(function(global) {
-
var console;
if (global.___ &&
@@ -54,4 +59,4 @@
___.setLogFunc(logToConsole);
}
-})(this);
+})();
Index: build.xml
===================================================================
--- build.xml (revision 5594)
+++ build.xml (working copy)
@@ -850,6 +850,7 @@
<input file="${src.caja}/plugin/caja-iframe-build-version.js"/>
<input file="${third_party}/js/json_sans_eval/json_sans_eval.js"/>
<input file="${src.caja}/es53.js"/>
+ <input file="${src.caja}/log-to-console.js"/>
<input file="${src.caja}/plugin/capture-cajoled-module.js"/>
<input file="${src.caja}/plugin/capture-domado.js"/>
<input file="${lib.caja}/plugin/domado.out.js" jslint="false"/>
@@ -882,6 +883,7 @@
<input file="${src.caja}/plugin/caja-iframe-build-version.js"/>
<input file="${third_party}/js/json_sans_eval/json_sans_eval.js"/>
<input file="${src.caja}/es53.js"/>
+ <input file="${src.caja}/log-to-console.js"/>
<input file="${src.caja}/cajita-promise.js"/>
<input file="${src.caja}/plugin/prepare-modules.js"/>
<input file="${src.caja}/plugin/load-module.js"/>
--
---
You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.