Index: polyfill.js |
=================================================================== |
--- a/polyfill.js |
+++ b/polyfill.js |
@@ -69,48 +69,55 @@ |
let func = object[name]; |
if (!func) |
return; |
- |
- object[name] = function(...args) |
- { |
- let callStack = new Error().stack; |
- |
- if (typeof args[args.length - 1] == "function") |
- return func.apply(object, args); |
+ let descriptor = Reflect.getOwnPropertyDescriptor(object, name); |
Oleksandr
2017/10/27 13:12:37
We cannot use Object.getOwnPropertyDescriptor beca
Manish Jethani
2017/10/27 14:25:16
We can use Object.getOwnPropertyDescriptor here si
|
- // If the last argument is undefined, we drop it from the list assuming |
- // it stands for the optional callback. We must do this, because we have |
- // to replace it with our own callback. If we simply append our own |
- // callback to the list, it won't match the signature of the function and |
- // will cause an exception. |
- if (typeof args[args.length - 1] == "undefined") |
- args.pop(); |
- |
- return new Promise((resolve, reject) => |
- { |
- func.call(object, ...args, result => |
+ if (descriptor && descriptor.configurable) |
Oleksandr
2017/10/27 13:12:38
There isn't really an object in our list that does
Manish Jethani
2017/10/27 14:25:16
Yes, we should remove it.
If it's not configurabl
|
+ { |
+ Object.defineProperty(object, name, { |
Manish Jethani
2017/10/27 14:25:16
You could just do:
descriptor.value = (...args)
|
+ enumerable: true, |
+ configurable: true, |
+ writable: true, |
+ value: (...args) => |
{ |
- let error = browser.runtime.lastError; |
- if (error && !portClosedBeforeResponseError.test(error.message)) |
- { |
- // runtime.lastError is already an Error instance on Edge, while on |
- // Chrome it is a plain object with only a message property. |
- if (!(error instanceof Error)) |
- { |
- error = new Error(error.message); |
+ let callStack = new Error().stack; |
+ if (typeof args[args.length - 1] == "function") |
+ return func.apply(object, args); |
+ |
+ // If the last argument is undefined, we drop it from the list |
+ // assuming it stands for the optional callback. We must do this, |
+ // because we have to replace it with our own callback. If we simply |
+ // append our own callback to the list, it won't match the signature |
+ // of the function and will cause an exception. |
+ if (typeof args[args.length - 1] == "undefined") |
+ args.pop(); |
- // Add a more helpful stack trace. |
- error.stack = callStack; |
- } |
+ return new Promise((resolve, reject) => |
+ { |
+ func.call(object, ...args, result => |
+ { |
+ let error = browser.runtime.lastError; |
+ if (error && !portClosedBeforeResponseError.test(error.message)) |
+ { |
+ // runtime.lastError is already an Error instance on Edge, while |
+ // on Chrome it is a plain object with only a message property. |
+ if (!(error instanceof Error)) |
+ { |
+ error = new Error(error.message); |
- reject(error); |
- } |
- else |
- { |
- resolve(result); |
- } |
- }); |
- }); |
- }; |
+ // Add a more helpful stack trace. |
+ error.stack = callStack; |
+ } |
+ |
+ reject(error); |
+ } |
+ else |
+ { |
+ resolve(result); |
+ } |
+ }); |
+ }); |
+ }}); |
+ } |
} |
function shouldWrapAPIs() |