es6-proxy-polyfill
Version:
Proxy polyfill based on ES3 supports IE8, Node.js, etc.
9 lines (8 loc) • 4.29 kB
JavaScript
/**
* es6-proxy-polyfill
* @version 2.1.1
* @author Ambit-Tsai <ambit_tsai@qq.com>
* @license Apache-2.0
* @see {@link https://github.com/ambit-tsai/es6-proxy-polyfill#readme}
*/
var t="[[ProxyTarget]]",n="[[Get]]",r="[[Set]]",e="[[Call]]",o=Object.defineProperty,i=Object.defineProperties,u=Object.getPrototypeOf,c=Object.getOwnPropertyDescriptor,a=!!i&&s(i),f=a?Object.__proto__?u:function(t){return"function"==typeof t?t.__proto__||{}:u(t)}:function(t){return _isVbObject(t)&&_getVbInternalOf(t).__proto__||{}};function s(t){return"function"==typeof t&&/\[native code\]/.test(t.toString())}function p(t,n){if(this instanceof p)return l(new y(t,n));v("Constructor Proxy requires 'new'")}function l(f){var s=f[t];return"function"==typeof s?function(n){var r=n[t];function o(){return this instanceof o?n["[[Construct]]"](arguments,o):n[e](this,arguments)}if(o.prototype=r.prototype,a){var c=j(n),f=O(u(r),c);for(var s in g(o,f),c=x(r,n))d(o,s)&&delete c[s];i(o,c)}else P(o,r);return o}(f):s instanceof Array?function(e){var i,f,s=e[t];a&&((i=j(e)).concat.get=function(){var t=e[n]("concat",this);return t===Array.prototype.concat?t.bind(s):t},f=O(u(s),i));return(i=x(s,e)).length.set=function(n){var i=n-s.length;e[r]("length",n,this),i&&function(n,r,e){var i=e[t];if(n>0)for(var u=i.length,a=u-n;a<u;++a){var f=c(r,a);f?o(i,a,f):i[a]=undefined,f=w(i,a,e),o(r,a,f)}else for(var s=(a=i.length)-n;a<s;++a)delete r[a]}(i,this,e)},O(f,i)}(f):function(n){var r,e,o=n[t];a&&(r=j(n),e=O(u(o),r));return r=x(o,n),O(e,r)}(f)}function y(n,r){h(n)&&h(r)||v("Cannot create proxy with a non-object as target or handler"),"REVOKED"===(f(n).__PROXY__||f(r).__PROXY__)&&v("Cannot create proxy with a revoked proxy as target or handler"),this[t]=n,this["[[ProxyHandler]]"]=r}function _(t,n){t||v("Cannot perform '"+n+"' on a proxy that has been revoked")}function v(t){throw new TypeError(t)}function h(t){return!!t&&("object"==typeof t||"function"==typeof t)}function d(t,n){return Object.prototype.hasOwnProperty.call(t,n)}p.revocable=function(n,r){this instanceof p.revocable&&v("Proxy.revocable is not a constructor");var e=new y(n,r),o=l(e);return{proxy:o,revoke:function(){e[t]=undefined,e["[[ProxyHandler]]"]=undefined,a||(f(o).__PROXY__="REVOKED")}}},y.prototype[n]=function(n,r){var e=this["[[ProxyHandler]]"];return _(e,"get"),null==e.get?this[t][n]:"function"==typeof e.get?e.get(this[t],n,r):void v("Trap 'get' is not a function: "+e.get)},y.prototype[r]=function(n,r,e){var o=this["[[ProxyHandler]]"];if(_(o,"set"),null==o.set)this[t][n]=r;else{if("function"==typeof o.set){var i=o.set(this[t],n,r,e);return i||console.warn("Trap 'set' returned false for property '"+n+"'"),Boolean(i)}v("Trap 'set' is not a function: "+o.set)}},y.prototype[e]=function(n,r){var e=this["[[ProxyHandler]]"];return _(e,"apply"),null==e.apply?this[t].apply(n,r):"function"==typeof e.apply?e.apply(this[t],n,r):void v("Trap 'apply' is not a function: "+e.apply)},y.prototype["[[Construct]]"]=function(n,r){var e,o=this["[[ProxyHandler]]"];if(_(o,"construct"),null==o.construct?e=function(t,n){for(var r=[],e=0,o=n.length;e<o;++e)r.push("args["+e+"]");return new Function("Ctor","args","return new Ctor("+r.join(", ")+")")(t,n)}(this[t],n):"function"==typeof o.construct?e=o.construct(this[t],n,r):v("Trap 'construct' is not a function: "+o.construct),h(e))return e;v("Trap 'construct' returned non-object: "+e)};var b=Object.getOwnPropertyNames||function(t){var n=[];for(var r in t)d(t,r)&&n.push(r);return n},g=s(Object.setPrototypeOf)?Object.setPrototypeOf:Object.__proto__?function(t,n){return t.__proto__=n,t}:function(t,n){return o(t,"__proto__",{value:n})},O=a?Object.create:function(t,n){var r=i({},n);if(_isVbObject(r)){var e={__PROXY__:undefined};_getVbInternalOf(r).__proto__=e}return r},P=Object.assign||function(t,n){for(var r in n)d(n,r)&&(t[r]=n[r]);return t};function j(n){for(var r={},e=n[t];e=u(e);){var o=x(e,n);P(r,o)}return r.__PROXY__={get:function(){return n[t]?undefined:"REVOKED"}},r}function x(t,n){for(var r=b(t),e={},o=r.length-1;o>=0;--o)e[r[o]]=w(t,r[o],n);return e}function w(t,e,o){var i=c(t,e);return{get:function(){return o[n](e,this)},set:function(t){o[r](e,t,this)},enumerable:i.enumerable,configurable:i.configurable}}var C="undefined"==typeof Proxy?p:Proxy;export default C;