Skip to content

Add bind() polyfill to plugin functions #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion source/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,59 @@ AdapterJS.addEvent = function(elem, evnt, func) {
}
};

AdapterJS.recursivePolyfillBind = function(obj) {
if (!obj) {
return;
}

// these objects cause infinite recursion in IE; ignore them
var recursionBlacklist = ['attachEvent()', 'detachEvent()', 'getLastException()'];
if (obj.value && recursionBlacklist.indexOf(obj.value.trim()) > -1) {
return;
}

var pluginProperties = Object.keys(obj);
for (var i = 0; i < pluginProperties.length; i++) {
var currentProperty = obj[pluginProperties[i]];

if ((typeof currentProperty === 'function' || (currentProperty && currentProperty.call)) && !currentProperty.bind) {
// with the NPAPI/ActiveX JS functions you have to bind the bindPolyfill function to the NPAPI function and
// then call the bound function with the obj because the function must be called on its parent object
currentProperty.bind = bindPolyfill.bind(currentProperty)(obj);
} else if (typeof currentProperty === 'object') {
AdapterJS.recursivePolyfillBind(currentProperty);
}
}
}

// adapted from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill
function bindPolyfill(oThis) {
// NPAPI/ActiveX JS functions are objects with a call property
if (typeof this !== 'function' && !(this && this.call)) {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();

return fBound;
};

AdapterJS.renderNotificationBar = function (message, buttonText, buttonCallback) {
// only inject once the page is ready
if (document.readyState !== 'interactive' && document.readyState !== 'complete') {
Expand Down Expand Up @@ -1133,7 +1186,11 @@ if (['webkit', 'moz', 'ms', 'AppleWebKit'].indexOf(AdapterJS.webrtcDetectedType)
if (iceServers) {
servers.iceServers = iceServers;
}
return AdapterJS.WebRTCPlugin.plugin.PeerConnection(servers);

// polyfill plugin functions with bind()
var peerConnection = AdapterJS.WebRTCPlugin.plugin.PeerConnection(servers);
AdapterJS.recursivePolyfillBind(peerConnection);
return peerConnection;
} else {
var mandatory = (constraints && constraints.mandatory) ?
constraints.mandatory : null;
Expand Down