Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit a75cc01

Browse files
committed
Use eval for default generated getters, setters and methods.
1 parent 7d430a9 commit a75cc01

1 file changed

Lines changed: 39 additions & 15 deletions

File tree

src/wrappers.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ var ShadowDOMPolyfill = {};
1111
var nativePrototypeTable = new SideTable();
1212
var wrappers = Object.create(null);
1313

14+
// Don't test for eval if document has CSP securityPolicy object and we can
15+
// see that eval is not supported. This avoids an error message in console
16+
// even when the exception is caught
17+
var hasEval = !('securityPolicy' in document) ||
18+
document.securityPolicy.allowsEval;
19+
if (hasEval) {
20+
try {
21+
var f = new Function('', 'return true;');
22+
hasEval = f();
23+
} catch (ex) {
24+
}
25+
}
26+
1427
function assert(b) {
1528
if (!b)
1629
throw new Error('Assertion failed');
@@ -90,6 +103,25 @@ var ShadowDOMPolyfill = {};
90103
return /^on[a-z]+$/.test(name);
91104
}
92105

106+
function getGetter(name) {
107+
return hasEval ?
108+
new Function('return this.impl.' + name) :
109+
function() { return this.impl[name]; };
110+
}
111+
112+
function getSetter(name) {
113+
return hasEval ?
114+
new Function('v', 'this.impl.' + name + ' = v') :
115+
function(v) { this.impl[name] = v; };
116+
}
117+
118+
function getMethod(name) {
119+
return hasEval ?
120+
new Function('return this.impl.' + name +
121+
'.apply(this.impl, arguments)') :
122+
function() { return this.impl[name].apply(this.impl, arguments); };
123+
}
124+
93125
function installProperty(source, target, allowMethod) {
94126
Object.getOwnPropertyNames(source).forEach(function(name) {
95127
if (name in target)
@@ -110,29 +142,21 @@ var ShadowDOMPolyfill = {};
110142
}
111143
var getter, setter;
112144
if (allowMethod && typeof descriptor.value === 'function') {
113-
target[name] = function() {
114-
return this.impl[name].apply(this.impl, arguments);
115-
};
145+
target[name] = getMethod(name);
116146
return;
117147
}
118148

119149
var isEvent = isEventHandlerName(name);
120-
if (isEvent) {
150+
if (isEvent)
121151
getter = scope.getEventHandlerGetter(name);
122-
} else {
123-
getter = function() {
124-
return this.impl[name];
125-
};
126-
}
152+
else
153+
getter = getGetter(name);
127154

128155
if (descriptor.writable || descriptor.set) {
129-
if (isEvent) {
156+
if (isEvent)
130157
setter = scope.getEventHandlerSetter(name);
131-
} else {
132-
setter = function(value) {
133-
this.impl[name] = value;
134-
};
135-
}
158+
else
159+
setter = getSetter(name);
136160
}
137161

138162
Object.defineProperty(target, name, {

0 commit comments

Comments
 (0)