UNPKG

legacy-es6-proxy-polyfill

Version:

Proxy polyfill based on ES3 supports IE8, Node.js, etc.

9 lines (8 loc) 4.1 kB
/** * 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,n="[[ProxyTarget]]",r="[[ProxyHandler]]",e="[[Get]]",o="[[Set]]",i="[[Call]]",c="[[Construct]]",a="__proto__",u="__PROXY__",f="REVOKED",s=Object.defineProperty,p=Object.defineProperties,y=Object.getPrototypeOf,v=Object.getOwnPropertyDescriptor,h=!!p&&b(p),l=h?Object[a]?y:function(t){return"function"==typeof t?t[a]||{}:y(t)}:function(t){return _isVbObject(t)&&_getVbInternalOf(t)[a]||{}};function b(t){return"function"==typeof t&&/\[native code\]/.test(t.toString())}function g(t,n){if(this instanceof g)return O(new d(t,n));P("Constructor Proxy requires 'new'")}function O(r){var a,u=r[n];return a="function"==typeof u?function(t){var r=t[n];function e(){return this instanceof e?t[c](arguments,e):t[i](this,arguments)}if(e.prototype=r.prototype,h){var o=m(t),a=T(y(r),o);for(var u in C(e,a),o=k(r,t))x(e,u)&&delete o[u];p(e,o)}else V(e,r);return e}(r):u instanceof Array?function(r){var i,c,a=r[n];h&&((i=m(r)).concat.get=function(){var t=r[e]("concat",this);return t===Array.prototype.concat?t.bind(a):t},c=T(y(a),i));return i=k(a,r),i.length.set=function(e){var i=e-a.length;r[o]("length",e,this),i&&function(r,e,o){var i=o[n];if(r>0)for(var c=i.length,a=c-r;a<c;++a){var u=v(e,a);u?s(i,a,u):i[a]=t,u=E(i,a,o),s(e,a,u)}else for(var f=(a=i.length)-r;a<f;++a)delete e[a]}(i,this,r)},T(c,i)}(r):function(t){var r,e,o=t[n];h&&(r=m(t),e=T(y(o),r));return r=k(o,t),T(e,r)}(r),a}function d(t,e){w(t)&&w(e)||P("Cannot create proxy with a non-object as target or handler"),(l(t)[u]||l(e)[u])===f&&P("Cannot create proxy with a revoked proxy as target or handler"),this[n]=t,this[r]=e}function j(t,n){t||P("Cannot perform '"+n+"' on a proxy that has been revoked")}function P(t){throw new TypeError(t)}function w(t){return!!t&&("object"==typeof t||"function"==typeof t)}function x(t,n){return Object.prototype.hasOwnProperty.call(t,n)}g.revocable=function(e,o){this instanceof g.revocable&&P("Proxy.revocable is not a constructor");var i=new d(e,o),c=O(i);return{proxy:c,revoke:function(){i[n]=t,i[r]=t,h||(l(c)[u]=f)}}},d.prototype[e]=function(e,o){var i=this[r];return j(i,"get"),i.get==t?this[n][e]:"function"==typeof i.get?i.get(this[n],e,o):void P("Trap 'get' is not a function: "+i.get)},d.prototype[o]=function(e,o,i){var c=this[r];if(j(c,"set"),c.set==t)this[n][e]=o;else{if("function"==typeof c.set){var a=c.set(this[n],e,o,i);return a||console.warn("Trap 'set' returned false for property '"+e+"'"),Boolean(a)}P("Trap 'set' is not a function: "+c.set)}},d.prototype[i]=function(e,o){var i=this[r];return j(i,"apply"),i.apply==t?this[n].apply(e,o):"function"==typeof i.apply?i.apply(this[n],e,o):void P("Trap 'apply' is not a function: "+i.apply)},d.prototype[c]=function(e,o){var i,c=this[r];if(j(c,"construct"),c.construct==t?i=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[n],e):"function"==typeof c.construct?i=c.construct(this[n],e,o):P("Trap 'construct' is not a function: "+c.construct),w(i))return i;P("Trap 'construct' returned non-object: "+i)};var _=Object.getOwnPropertyNames||function(t){var n=[];for(var r in t)x(t,r)&&n.push(r);return n},C=b(Object.setPrototypeOf)?Object.setPrototypeOf:Object[a]?function(t,n){return t[a]=n,t}:function(t,n){return s(t,a,{value:n})},T=h?Object.create:function(n,r){var e=p({},r);if(_isVbObject(e)){var o={};o[u]=t,_getVbInternalOf(e)[a]=o}return e},V=Object.assign||function(t,n){for(var r in n)x(n,r)&&(t[r]=n[r]);return t};function m(r){for(var e={},o=r[n];o=y(o);){var i=k(o,r);V(e,i)}return e[u]={get:function(){return r[n]?t:f}},e}function k(t,n){for(var r=_(t),e={},o=r.length-1;o>=0;--o)e[r[o]]=E(t,r[o],n);return e}function E(t,n,r){var i=v(t,n);return{get:function(){return r[e](n,this)},set:function(t){r[o](n,t,this)},enumerable:i.enumerable,configurable:i.configurable}}var A="undefined"==typeof Proxy?g:Proxy;export{A as default};