Revision: 5595
Author: [email protected]
Date: Tue Sep 10 23:47:04 2013 UTC
Log: Warn on calling WeakMap as a function; ES5/3 log to console by
default.
https://codereview.appspot.com/13348055
* 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.
[email protected]
http://code.google.com/p/google-caja/source/detail?r=5595
Modified:
/trunk/build.xml
/trunk/src/com/google/caja/es53.js
/trunk/src/com/google/caja/log-to-console.js
/trunk/src/com/google/caja/ses/WeakMap.js
=======================================
--- /trunk/build.xml Wed Aug 28 18:04:38 2013 UTC
+++ /trunk/build.xml Tue Sep 10 23:47:04 2013 UTC
@@ -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"/>
=======================================
--- /trunk/src/com/google/caja/es53.js Wed Sep 4 04:54:27 2013 UTC
+++ /trunk/src/com/google/caja/es53.js Tue Sep 10 23:47:04 2013 UTC
@@ -1518,7 +1518,19 @@
}
} 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.
+ // The instanceof is an approximate test for use of new ...()
vs. ...().
+ 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 = [];
=======================================
--- /trunk/src/com/google/caja/log-to-console.js Tue Jun 5 22:50:03 2012
UTC
+++ /trunk/src/com/google/caja/log-to-console.js Tue Sep 10 23:47:04 2013
UTC
@@ -13,22 +13,27 @@
// 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.
-
-// 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.
-
+/**
+ * @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 ___
+ */
-(function(global) {
+(function() {
+ var global = this;
var console;
@@ -54,4 +59,4 @@
___.setLogFunc(logToConsole);
}
-})(this);
+})();
=======================================
--- /trunk/src/com/google/caja/ses/WeakMap.js Sat Jul 13 15:35:24 2013 UTC
+++ /trunk/src/com/google/caja/ses/WeakMap.js Tue Sep 10 23:47:04 2013 UTC
@@ -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
*/
@@ -353,6 +353,17 @@
func.prototype = null;
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
@@ -360,6 +371,10 @@
// var histogram = [];
var OurWeakMap = function() {
+ if (!(this instanceof OurWeakMap)) { // approximate test for new ...()
+ 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,10 @@
// implementation which makes use of both as possible.
function DoubleWeakMap() {
+ if (!(this instanceof OurWeakMap)) { // approximate test for
new ...()
+ calledAsFunctionWarning();
+ }
+
// Preferable, truly weak map.
var hmap = new HostWeakMap();
--
---
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.