11 lines
11 KiB
JavaScript
11 lines
11 KiB
JavaScript
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?module.exports=b():"function"==typeof define&&define.amd?define(b):(a=a||self,a.Realm=b())})(this,function(){'use strict';function a(a,b=void 0){const c=`please report internal shim error: ${a}`;console.error(c),b&&(console.error(`${b}`),console.error(`${b.stack}`));debugger;throw c}function b(b,c){b||a(c)}function c(a){let b=`'use strict'; (${a})`;return b=b.replace(/\(0,\s*_[0-9a-fA-F]{3}\u200D\.e\)/g,"(0, eval)"),b=b.replace(/_[0-9a-fA-F]{3}\u200D\.g\./g,""),b=b.replace(/cov_[^+]+\+\+[;,]/g,""),b}function d(a,b){const{callAndWrapError:c}=a,{initRootRealm:d,initCompartment:e,getRealmGlobal:f,realmEvaluate:g}=b,{create:h,defineProperties:i}=Object;class j{constructor(){throw new TypeError("Realm is not a constructor")}static makeRootRealm(b={}){const e=h(j.prototype);return c(d,[a,e,b]),e}static makeCompartment(b={}){const d=h(j.prototype);return c(e,[a,d,b]),d}get global(){return c(f,[this])}evaluate(a,b,d={}){return c(g,[this,a,b,d])}}return i(j,{toString:{value:()=>"function Realm() { [shim code] }",writable:!1,enumerable:!1,configurable:!0}}),i(j.prototype,{toString:{value:()=>"[object Realm]",writable:!1,enumerable:!1,configurable:!0}}),j}function e(a){function c(c,e,f,g){for(const h of c){const c=G(a,h);c&&(b("value"in c,`unexpected accessor on global property: ${h}`),d[h]={value:c.value,writable:e,enumerable:f,configurable:g})}}const d={};return c(V,!1,!1,!1),c(W,!1,!1,!1),c(X,!0,!1,!0),d}function f(){function a(a){if(a===void 0||null===a)throw new TypeError(`can't convert undefined or null to object`);return Object(a)}function b(a){return"symbol"==typeof a?a:`${a}`}function c(a,b){if("function"!=typeof a)throw TypeError(`invalid ${b} usage`);return a}const{defineProperty:d,defineProperties:e,getOwnPropertyDescriptor:f,getPrototypeOf:g,prototype:h}=Object;try{(0,h.__lookupGetter__)("x")}catch(a){return}e(h,{__defineGetter__:{value:function(b,e){const f=a(this);d(f,b,{get:c(e,"getter"),enumerable:!0,configurable:!0})}},__defineSetter__:{value:function(b,e){const f=a(this);d(f,b,{set:c(e,"setter"),enumerable:!0,configurable:!0})}},__lookupGetter__:{value:function(c){let d=a(this);c=b(c);let e;for(;d&&!(e=f(d,c));)d=g(d);return e&&e.get}},__lookupSetter__:{value:function(c){let d=a(this);c=b(c);let e;for(;d&&!(e=f(d,c));)d=g(d);return e&&e.set}}})}function g(){function a(a,e){let f;try{f=(0,eval)(e)}catch(a){if(a instanceof SyntaxError)return;throw a}const g=c(f),h=function(){throw new TypeError("Not available")};b(h,{name:{value:a}}),b(g,{constructor:{value:h}}),b(h,{prototype:{value:g}}),h!==Function.prototype.constructor&&d(h,Function.prototype.constructor)}const{defineProperties:b,getPrototypeOf:c,setPrototypeOf:d}=Object;a("Function","(function(){})"),a("GeneratorFunction","(function*(){})"),a("AsyncFunction","(async function(){})"),a("AsyncGeneratorFunction","(async function*(){})")}function h(){const a=new Function("try {return this===global}catch(e){return false}")();if(!a)return;const b=require("vm"),c=b.runInNewContext(Z);return c}function i(){if("undefined"!=typeof document){const a=document.createElement("iframe");a.style.display="none",document.body.appendChild(a);const b=a.contentWindow.eval(Y);return b}}function j(a,b=[]){const c=e(a),d=a.eval,f=a.Function,g=d(B)();return E({unsafeGlobal:a,sharedGlobalDescs:c,unsafeEval:d,unsafeFunction:f,callAndWrapError:g,allShims:b})}function k(a){const b=$(),c=j(b,a),{unsafeEval:d}=c;return d(_)(),d(aa)(),c}function l(a,b={}){const c=I(a),d=P(c,c=>{if(c in b)return!1;if("eval"===c||ca.has(c)||!T(ba,c))return!1;const d=G(a,c);return!1===d.configurable&&!1===d.writable&&O(d,"value")});return d}function m(a){const b=a.search(ha);if(-1!==b){const c=a.slice(0,b).split("\n").length;throw new SyntaxError(`possible html comment syntax rejected around line ${c}`)}}function n(a){const b=a.search(ia);if(-1!==b){const c=a.slice(0,b).split("\n").length;throw new SyntaxError(`possible import expression rejected around line ${c}`)}}function o(a){const b=a.search(ja);if(-1!==b){const c=a.slice(0,b).split("\n").length;throw new SyntaxError(`possible direct eval expression rejected around line ${c}`)}}function p(a){m(a),n(a),o(a)}function q(a){return 0===a.length?"":`const {${R(a,",")}} = this;`}function r(a,b){const{unsafeFunction:c}=a,d=q(b);return c(`
|
|
with (arguments[0]) {
|
|
${d}
|
|
return function() {
|
|
'use strict';
|
|
return eval(arguments[0]);
|
|
};
|
|
}
|
|
`)}function s(b,c,d,e){const{unsafeEval:f}=b,g=f(ga);return function(h={},i={}){const j=i.transforms||[],k=S(j,d||[],[ka]);return function(d){let i={src:d,endowments:h};i=g(i,k);const j=l(c,i.endowments),m=l(i.endowments),n=S(j,m),o=r(b,n),p=f(da)(b,c,i.endowments,e),q=Proxy.revocable({},p),s=q.proxy,t=L(o,c,[s]);p.useUnsafeEvaluator=!0;let u;try{return L(t,c,[i.src])}catch(a){throw u=a,a}finally{p.useUnsafeEvaluator&&(q.revoke(),a("handler did not revoke useUnsafeEvaluator",u))}}}}function t(a,c){const{unsafeEval:d,unsafeFunction:e}=a,f=d(ea)(a,c);return b(J(f).constructor!==Function,"hide Function"),b(J(f).constructor!==e,"hide unsafeFunction"),f}function u(a){return(b,c,d={})=>a(c,d)(b)}function v(a,c){const{unsafeGlobal:d,unsafeEval:e,unsafeFunction:f}=a,g=e(fa)(a,function(...a){const b=`${Q(a)||""}`;let e=`${R(a,",")}`;if(!T(/^[\w\s,]*$/,e))throw new SyntaxError("shim limitation: Function arg must be simple ASCII identifiers, possibly separated by commas: no default values, pattern matches, or non-ASCII parameter names");if(new f(b),U(e,")"))throw new d.SyntaxError("shim limitation: Function arg string contains parenthesis");0<e.length&&(e+="\n/*``*/");const g=`(function(${e}){\n${b}\n})`;return c(g)});return b(J(g).constructor!==Function,"hide Function"),b(J(g).constructor!==f,"hide unsafeFunction"),g}function w(a){return b(Object(a)===a,"bad object, not a Realm instance"),b(la.has(a),"Realm instance has no record"),la.get(a)}function x(a,c){b(Object(a)===a,"bad object, not a Realm instance"),b(!la.has(a),"Realm instance already has a record"),la.set(a,c)}function y(a,b,c){F(a,{eval:{value:b,writable:!0,configurable:!0},Function:{value:c,writable:!0,configurable:!0}})}function z(a,b,c){const{sharedGlobalDescs:d,unsafeGlobal:e}=a,f=D(e.Object.prototype,d),g=s(a,f,b,c),h=g(),i=t(a,h),j=v(a,h),k=u(g);y(f,i,j);const l=E({safeGlobal:f,safeEval:i,safeEvalWhichTakesEndowments:k,safeFunction:j});return l}const A=c(d),B=c(function(){const{getPrototypeOf:a}=Object,{apply:b}=Reflect,c=a=>(c,...d)=>b(a,c,d),d=c(Map.prototype.get),e=c(Set.prototype.has),f=new Map([["EvalError",EvalError],["RangeError",RangeError],["ReferenceError",ReferenceError],["SyntaxError",SyntaxError],["TypeError",TypeError],["URIError",URIError]]),g=new Set([EvalError.prototype,RangeError.prototype,ReferenceError.prototype,SyntaxError.prototype,TypeError.prototype,URIError.prototype,Error.prototype]);return function(c,h){try{return b(c,void 0,h)}catch(b){if(Object(b)!==b)throw b;if(e(g,a(b)))throw b;let c,h,i;try{c=`${b.name}`,h=`${b.message}`,i=`${b.stack||h}`}catch(a){throw new Error("unknown error")}const j=d(f,c)||Error;try{throw new j(h)}catch(a){throw a.stack=i,a}}}}),{assign:C,create:D,freeze:E,defineProperties:F,getOwnPropertyDescriptor:G,getOwnPropertyDescriptors:H,getOwnPropertyNames:I,getPrototypeOf:J,setPrototypeOf:K}=Object,{apply:L,ownKeys:M}=Reflect,N=a=>(b,...c)=>L(a,b,c),O=N(Object.prototype.hasOwnProperty),P=N(Array.prototype.filter),Q=N(Array.prototype.pop),R=N(Array.prototype.join),S=N(Array.prototype.concat),T=N(RegExp.prototype.test),U=N(String.prototype.includes),V=["Infinity","NaN","undefined"],W=["isFinite","isNaN","parseFloat","parseInt","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","Array","ArrayBuffer","Boolean","DataView","EvalError","Float32Array","Float64Array","Int8Array","Int16Array","Int32Array","Map","Number","Object","RangeError","ReferenceError","Set","String","Symbol","SyntaxError","TypeError","Uint8Array","Uint8ClampedArray","Uint16Array","Uint32Array","URIError","WeakMap","WeakSet","JSON","Math","Reflect","escape","unescape"],X=["Date","Error","Promise","Proxy","RegExp","Intl"],Y="'use strict'; this",Z=`(0, eval)("'use strict'; this")`,$=()=>{const a=i(),b=h();if(!a&&!b||a&&b)throw new Error("unexpected platform, unable to create Realm");return a||b},_=c(f),aa=c(g),ba=/^[a-zA-Z_$][\w$]*$/,ca=new Set(["await","break","case","catch","class","const","continue","debugger","default","delete","do","else","export","extends","finally","for","function","if","import","in","instanceof","new","return","super","switch","this","throw","try","typeof","var","void","while","with","yield","let","static","enum","implements","package","protected","interface","private","public","await","null","true","false","this","arguments"]),da=c(function(a,b,c={},d=!1){const{unsafeGlobal:e,unsafeEval:f}=a,{freeze:g,getOwnPropertyDescriptor:h}=Object,{get:i,set:j}=Reflect,k=new Proxy(g({}),{get(a,b){throw new TypeError(`unexpected scope handler trap called: ${b+""}`)}});return{__proto__:k,useUnsafeEvaluator:!1,get(a,d){return"symbol"==typeof d?void 0:"eval"===d&&!0===this.useUnsafeEvaluator?(this.useUnsafeEvaluator=!1,f):d in c?i(c,d,b):i(b,d)},set(a,d,e){if(d in c){const a=h(c,d);return"value"in a?j(c,d,e):j(c,d,e,b)}return j(b,d,e)},has(a,f){return!!d||!!("eval"===f||f in c||f in b||f in e)},getPrototypeOf(){return null}}}),ea=c(function(a,b){const{callAndWrapError:c}=a,{defineProperties:d}=Object,e={eval(){return c(b,arguments)}}.eval;return d(e,{toString:{value:()=>`function ${"eval"}() { [shim code] }`,writable:!1,enumerable:!1,configurable:!0}}),e}),fa=c(function(a,b){const{callAndWrapError:c,unsafeFunction:d}=a,{defineProperties:e}=Object,f=function(){return c(b,arguments)};return e(f,{prototype:{value:d.prototype},toString:{value:()=>"function Function() { [shim code] }",writable:!1,enumerable:!1,configurable:!0}}),f}),ga=c(function(a,b){const{create:c,getOwnPropertyDescriptors:d}=Object,{apply:e}=Reflect,f=(a=>(b,...c)=>e(a,b,c))(Array.prototype.reduce);return a={src:`${a.src}`,endowments:c(null,d(a.endowments))},a=f(b,(a,b)=>b.rewrite?b.rewrite(a):a,a),a={src:`${a.src}`,endowments:c(null,d(a.endowments))},a}),ha=/(?:<!--|-->)/,ia=/\bimport\s*(?:\(|\/[/*])/,ja=/\beval\s*(?:\(|\/[/*])/,ka={rewrite(a){return p(a.src),a}},la=new WeakMap,ma={initRootRealm:function(a,b,c){const{shims:d,transforms:e,sloppyGlobals:f}=c,g=S(a.allShims,d),h=k(g),{unsafeEval:i}=h,j=i(A)(h,ma);h.sharedGlobalDescs.Realm={value:j,writable:!0,configurable:!0};const l=z(h,e,f),{safeEvalWhichTakesEndowments:m}=l;for(const d of g)m(d);x(b,l)},initCompartment:function(a,b,c={}){const{transforms:d,sloppyGlobals:e}=c,f=z(a,d,e);x(b,f)},getRealmGlobal:function(a){const{safeGlobal:b}=w(a);return b},realmEvaluate:function(a,b,c={},d={}){const{safeEvalWhichTakesEndowments:e}=w(a);return e(b,c,d)}},na=function(){const a=eval,b=a(Y);return f(),g(),j(b)}(),oa=d(na,ma);return oa});
|
|
//# sourceMappingURL=realms-shim.umd.min.js.map
|