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

Commit

Permalink
Use eval for default generated getters, setters and methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Sep 5, 2013
1 parent 7d430a9 commit a75cc01
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ var ShadowDOMPolyfill = {};
var nativePrototypeTable = new SideTable();
var wrappers = Object.create(null);

// Don't test for eval if document has CSP securityPolicy object and we can
// see that eval is not supported. This avoids an error message in console
// even when the exception is caught
var hasEval = !('securityPolicy' in document) ||
document.securityPolicy.allowsEval;
if (hasEval) {
try {
var f = new Function('', 'return true;');
hasEval = f();
} catch (ex) {
}
}

function assert(b) {
if (!b)
throw new Error('Assertion failed');
Expand Down Expand Up @@ -90,6 +103,25 @@ var ShadowDOMPolyfill = {};
return /^on[a-z]+$/.test(name);
}

function getGetter(name) {
return hasEval ?
new Function('return this.impl.' + name) :
function() { return this.impl[name]; };
}

function getSetter(name) {
return hasEval ?
new Function('v', 'this.impl.' + name + ' = v') :
function(v) { this.impl[name] = v; };
}

function getMethod(name) {
return hasEval ?
new Function('return this.impl.' + name +
'.apply(this.impl, arguments)') :
function() { return this.impl[name].apply(this.impl, arguments); };
}

function installProperty(source, target, allowMethod) {
Object.getOwnPropertyNames(source).forEach(function(name) {
if (name in target)
Expand All @@ -110,29 +142,21 @@ var ShadowDOMPolyfill = {};
}
var getter, setter;
if (allowMethod && typeof descriptor.value === 'function') {
target[name] = function() {
return this.impl[name].apply(this.impl, arguments);
};
target[name] = getMethod(name);
return;
}

var isEvent = isEventHandlerName(name);
if (isEvent) {
if (isEvent)
getter = scope.getEventHandlerGetter(name);
} else {
getter = function() {
return this.impl[name];
};
}
else
getter = getGetter(name);

if (descriptor.writable || descriptor.set) {
if (isEvent) {
if (isEvent)
setter = scope.getEventHandlerSetter(name);
} else {
setter = function(value) {
this.impl[name] = value;
};
}
else
setter = getSetter(name);
}

Object.defineProperty(target, name, {
Expand Down

0 comments on commit a75cc01

Please sign in to comment.