summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-30 22:04:35 +0000
committerarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-30 22:04:35 +0000
commitaaf260fe5efef2e29e0a038c8e9ce1c699b6c4cb (patch)
tree68852831c0b734d74d173153c42bbeb46144f64a /chrome/browser
parent95edc39cdd3c55e2d62f818a830a86b209018137 (diff)
downloadchromium_src-aaf260fe5efef2e29e0a038c8e9ce1c699b6c4cb.zip
chromium_src-aaf260fe5efef2e29e0a038c8e9ce1c699b6c4cb.tar.gz
chromium_src-aaf260fe5efef2e29e0a038c8e9ce1c699b6c4cb.tar.bz2
Cr.js: Allow cr.define to export getters and setters.
There was a bug in the export code for exporting properties since it was just doing a simple [[Get]] which breaks cr.doc as well as the option to have singletons created lazily by the getter. BUG=None TEST=Options and Bookmarks should still work. Review URL: http://codereview.chromium.org/3031035 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54393 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/resources/shared/js/cr.js10
-rw-r--r--chrome/browser/resources/shared/js/cr_test.html27
2 files changed, 35 insertions, 2 deletions
diff --git a/chrome/browser/resources/shared/js/cr.js b/chrome/browser/resources/shared/js/cr.js
index 3c3a37e..77bc40d 100644
--- a/chrome/browser/resources/shared/js/cr.js
+++ b/chrome/browser/resources/shared/js/cr.js
@@ -300,8 +300,14 @@ const cr = (function() {
function define(name, fun) {
var obj = exportPath(name);
var exports = fun();
- for (var key in exports) {
- obj[key] = exports[key];
+ for (var propertyName in exports) {
+ // Maybe we should check the prototype chain here? The current usage
+ // pattern is always using an object literal so we only care about own
+ // properties.
+ var propertyDescriptor = Object.getOwnPropertyDescriptor(exports,
+ propertyName);
+ if (propertyDescriptor)
+ Object.defineProperty(obj, propertyName, propertyDescriptor);
}
}
diff --git a/chrome/browser/resources/shared/js/cr_test.html b/chrome/browser/resources/shared/js/cr_test.html
index 43c3628..b20281c 100644
--- a/chrome/browser/resources/shared/js/cr_test.html
+++ b/chrome/browser/resources/shared/js/cr_test.html
@@ -244,6 +244,33 @@ function testAddSingletonGetter() {
x, z);
}
+function testWithDoc() {
+ var d = {};
+
+ assertEquals(document, cr.doc);
+
+ cr.withDoc(d, function() {
+ assertEquals(d, cr.doc);
+ });
+
+ assertEquals(document, cr.doc);
+}
+
+function testDefineWithGetter() {
+ var v = 0;
+ cr.define('foo', function() {
+ return {
+ get v() {
+ return v;
+ }
+ }
+ });
+
+ assertEquals(0, foo.v);
+
+ v = 1;
+ assertEquals(1, foo.v);
+}
</script>