@huolala-tech/eval5
Version:
A JavaScript vm written in JavaScript. Forked from eval5
3 lines (2 loc) • 25 kB
JavaScript
var e=function(t,r){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},e(t,r)};function t(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}var r=function(){return r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var n=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(Error),a=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(SyntaxError),o=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(ReferenceError),i=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(TypeError),c=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(n),s=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(a),u=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return t(r,e),r}(o),l={UnknownError:[3001,"%0",c],ExecutionTimeOutError:[3002,"Script execution timed out after %0ms",c],NodeTypeSyntaxError:[1001,"Unknown node type: %0",u],BinaryOperatorSyntaxError:[1002,"Unknown binary operator: %0",u],LogicalOperatorSyntaxError:[1003,"Unknown logical operator: %0",u],UnaryOperatorSyntaxError:[1004,"Unknown unary operator: %0",u],UpdateOperatorSyntaxError:[1005,"Unknown update operator: %0",u],ObjectStructureSyntaxError:[1006,"Unknown object structure: %0",u],AssignmentExpressionSyntaxError:[1007,"Unknown assignment expression: %0",u],VariableTypeSyntaxError:[1008,"Unknown variable type: %0",u],ParamTypeSyntaxError:[1009,"Unknown param type: %0",u],AssignmentTypeSyntaxError:[1010,"Unknown assignment type: %0",u],FunctionUndefinedReferenceError:[2001,"%0 is not a function",o],VariableUndefinedReferenceError:[2002,"%0 is not defined",o],IsNotConstructor:[2003,"%0 is not a constructor",i]};function p(e,t){Object.defineProperty(e,"name",{value:t,writable:!1,enumerable:!1,configurable:!0})}var f=Object.prototype.hasOwnProperty,h=Symbol("Break"),d=Symbol("Continue"),y=Symbol("DefaultCase"),m=Symbol("EmptyStatementReturn"),b=Symbol("WithScopeName"),S=Symbol("SuperScopeName"),v=Symbol("RootScopeName"),E=Symbol("GlobalScopeName");function g(e){return"function"==typeof e}var x=function(e){this.value=e},C=function(e){this.value=e},w=function(e){this.value=e},k=function(e,t,r){void 0===t&&(t=null),this.name=r,this.parent=t,this.data=e,this.labelStack=[]};function H(){}var I={NaN:NaN,Infinity:1/0,undefined:void 0,Object:Object,Array:Array,String:String,Boolean:Boolean,Number:Number,Date:Date,RegExp:RegExp,Error:Error,URIError:URIError,TypeError:TypeError,RangeError:RangeError,SyntaxError:SyntaxError,ReferenceError:ReferenceError,Math:Math,parseInt:parseInt,parseFloat:parseFloat,isNaN:isNaN,isFinite:isFinite,decodeURI:decodeURI,decodeURIComponent:decodeURIComponent,encodeURI:encodeURI,encodeURIComponent:encodeURIComponent,escape:escape,unescape:unescape};"undefined"!=typeof JSON&&(I.JSON=JSON),"undefined"!=typeof Promise&&(I.Promise=Promise),"undefined"!=typeof Set&&(I.Set=Set),"undefined"!=typeof Map&&(I.Map=Map),"undefined"!=typeof Symbol&&(I.Symbol=Symbol),"undefined"!=typeof Proxy&&(I.Proxy=Proxy),"undefined"!=typeof WeakMap&&(I.WeakMap=WeakMap),"undefined"!=typeof WeakSet&&(I.WeakSet=WeakSet),"undefined"!=typeof Reflect&&(I.Reflect=Reflect);var O=function(){function e(t,r){void 0===t&&(t=e.global),void 0===r&&(r={}),this.sourceList=[],this.collectDeclVars=Object.create(null),this.collectDeclFuncs=Object.create(null),this.isVarDeclMode=!1,this.lastExecNode=null,this.isRunning=!1,this.options={timeout:r.timeout||0,rootContext:r.rootContext,globalContextInFunction:void 0===r.globalContextInFunction?e.globalContextInFunction:r.globalContextInFunction,_initEnv:r._initEnv},this.context=t||Object.create(null),this.callStack=[],this.initEnvironment(this.context)}return e.prototype.initEnvironment=function(e){var t,r;if(e instanceof k)t=e;else{var n=null,a=this.createSuperScope(e);this.options.rootContext&&(n=new k((r=this.options.rootContext,Object.create(r)),a,v)),t=new k(e,n||a,E)}this.globalScope=t,this.currentScope=this.globalScope,this.globalContext=t.data,this.currentContext=t.data,this.collectDeclVars=Object.create(null),this.collectDeclFuncs=Object.create(null),this.execStartTime=Date.now(),this.execEndTime=this.execStartTime;var o=this.options._initEnv;o&&o.call(this)},e.prototype.getExecStartTime=function(){return this.execStartTime},e.prototype.getExecutionTime=function(){return this.execEndTime-this.execStartTime},e.prototype.setExecTimeout=function(e){void 0===e&&(e=0),this.options.timeout=e},e.prototype.getOptions=function(){return this.options},e.prototype.getCurrentScope=function(){return this.currentScope},e.prototype.getCurrentContext=function(){return this.currentContext},e.prototype.isInterruptThrow=function(e){return e instanceof c||e instanceof u||e instanceof s},e.prototype.createSuperScope=function(e){var t=r({},I);return Object.keys(t).forEach((function(r){r in e&&delete t[r]})),new k(t,null,S)},e.prototype.setCurrentContext=function(e){this.currentContext=e},e.prototype.setCurrentScope=function(e){this.currentScope=e},e.prototype.evaluateNode=function(e,t){var r=this;void 0===t&&(t=""),this.value=void 0,this.source=t,this.sourceList.push(t),this.isRunning=!0,this.execStartTime=Date.now(),this.execEndTime=this.execStartTime,this.collectDeclVars=Object.create(null),this.collectDeclFuncs=Object.create(null);var n=this.getCurrentScope(),a=this.getCurrentContext(),o=n.labelStack.concat([]),i=this.callStack.concat([]);try{var c=this.createClosure(e);this.addDeclarationsToScope(this.collectDeclVars,this.collectDeclFuncs,this.getCurrentScope()),c()}catch(e){throw e}finally{r.setCurrentScope(n),r.setCurrentContext(a),n.labelStack=o,r.callStack=i,this.execEndTime=Date.now()}return this.isRunning=!1,this.getValue()},e.prototype.createErrorMessage=function(e,t,r){var n=e[1].replace("%0",String(t));return null!==r&&(n+=this.getNodePosition(r||this.lastExecNode)),n},e.prototype.createError=function(e,t){return new t(e)},e.prototype.createThrowError=function(e,t){return this.createError(e,t)},e.prototype.createInternalThrowError=function(e,t,r){return this.createError(this.createErrorMessage(e,t,r),e[2])},e.prototype.checkTimeout=function(){if(!this.isRunning)return!1;var e=this.options.timeout||0;return Date.now()-this.execStartTime>e},e.prototype.getNodePosition=function(e){if(e){return e.loc?" [".concat(e.loc.start.line,":").concat(e.loc.start.column,"]").concat(""):""}return""},e.prototype.createClosure=function(e){var t,r=this;switch(e.type){case"BinaryExpression":t=this.binaryExpressionHandler(e);break;case"LogicalExpression":t=this.logicalExpressionHandler(e);break;case"UnaryExpression":t=this.unaryExpressionHandler(e);break;case"UpdateExpression":t=this.updateExpressionHandler(e);break;case"ObjectExpression":t=this.objectExpressionHandler(e);break;case"ArrayExpression":t=this.arrayExpressionHandler(e);break;case"CallExpression":t=this.callExpressionHandler(e);break;case"NewExpression":t=this.newExpressionHandler(e);break;case"MemberExpression":t=this.memberExpressionHandler(e);break;case"ThisExpression":t=this.thisExpressionHandler(e);break;case"SequenceExpression":t=this.sequenceExpressionHandler(e);break;case"Literal":t=this.literalHandler(e);break;case"Identifier":t=this.identifierHandler(e);break;case"AssignmentExpression":t=this.assignmentExpressionHandler(e);break;case"FunctionDeclaration":t=this.functionDeclarationHandler(e);break;case"VariableDeclaration":t=this.variableDeclarationHandler(e);break;case"BlockStatement":case"Program":t=this.programHandler(e);break;case"ExpressionStatement":t=this.expressionStatementHandler(e);break;case"EmptyStatement":t=this.emptyStatementHandler(e);break;case"ReturnStatement":t=this.returnStatementHandler(e);break;case"FunctionExpression":t=this.functionExpressionHandler(e);break;case"IfStatement":t=this.ifStatementHandler(e);break;case"ConditionalExpression":t=this.conditionalExpressionHandler(e);break;case"ForStatement":t=this.forStatementHandler(e);break;case"WhileStatement":t=this.whileStatementHandler(e);break;case"DoWhileStatement":t=this.doWhileStatementHandler(e);break;case"ForInStatement":t=this.forInStatementHandler(e);break;case"WithStatement":t=this.withStatementHandler(e);break;case"ThrowStatement":t=this.throwStatementHandler(e);break;case"TryStatement":t=this.tryStatementHandler(e);break;case"ContinueStatement":t=this.continueStatementHandler(e);break;case"BreakStatement":t=this.breakStatementHandler(e);break;case"SwitchStatement":t=this.switchStatementHandler(e);break;case"LabeledStatement":t=this.labeledStatementHandler(e);break;case"DebuggerStatement":t=this.debuggerStatementHandler(e);break;default:throw this.createInternalThrowError(l.NodeTypeSyntaxError,e.type,e)}return function(){for(var n=[],a=0;a<arguments.length;a++)n[a]=arguments[a];var o=r.options.timeout;if(o&&o>0&&r.checkTimeout())throw r.createInternalThrowError(l.ExecutionTimeOutError,o,null);return r.lastExecNode=e,t.apply(void 0,n)}},e.prototype.binaryExpressionHandler=function(e){var t=this,r=this.createClosure(e.left),n=this.createClosure(e.right);return function(){var a=r(),o=n();switch(e.operator){case"==":return a==o;case"!=":return a!=o;case"===":return a===o;case"!==":return a!==o;case"<":return a<o;case"<=":return a<=o;case">":return a>o;case">=":return a>=o;case"<<":return a<<o;case">>":return a>>o;case">>>":return a>>>o;case"+":return a+o;case"-":return a-o;case"*":return a*o;case"**":return Math.pow(a,o);case"/":return a/o;case"%":return a%o;case"|":return a|o;case"^":return a^o;case"&":return a&o;case"in":return a in o;case"instanceof":return a instanceof o;default:throw t.createInternalThrowError(l.BinaryOperatorSyntaxError,e.operator,e)}}},e.prototype.logicalExpressionHandler=function(e){var t=this,r=this.createClosure(e.left),n=this.createClosure(e.right);return function(){switch(e.operator){case"||":return r()||n();case"&&":return r()&&n();default:throw t.createInternalThrowError(l.LogicalOperatorSyntaxError,e.operator,e)}}},e.prototype.unaryExpressionHandler=function(e){var t,r=this;if("delete"===e.operator){var n=this.createObjectGetter(e.argument),a=this.createNameGetter(e.argument);return function(){return delete n()[a()]}}if("typeof"===e.operator&&"Identifier"===e.argument.type){var o=this.createObjectGetter(e.argument),i=this.createNameGetter(e.argument);t=function(){return o()[i()]}}else t=this.createClosure(e.argument);return function(){var n=t();switch(e.operator){case"-":return-n;case"+":return+n;case"!":return!n;case"~":return~n;case"void":return;case"typeof":return typeof n;default:throw r.createInternalThrowError(l.UnaryOperatorSyntaxError,e.operator,e)}}},e.prototype.updateExpressionHandler=function(e){var t=this,r=this.createObjectGetter(e.argument),n=this.createNameGetter(e.argument);return function(){var a=r(),o=n();switch(t.assertVariable(a,o,e),e.operator){case"++":return e.prefix?++a[o]:a[o]++;case"--":return e.prefix?--a[o]:a[o]--;default:throw t.createInternalThrowError(l.UpdateOperatorSyntaxError,e.operator,e)}}},e.prototype.objectExpressionHandler=function(e){var t=this,r=[];var n=Object.create(null);return e.properties.forEach((function(e){var a=e.kind,o=function(e){return"Identifier"===e.type?e.name:"Literal"===e.type?e.value:this.throwError(l.ObjectStructureSyntaxError,e.type,e)}(e.key);n[o]&&"init"!==a||(n[o]={}),n[o][a]=t.createClosure(e.value),r.push({key:o,property:e})})),function(){for(var e={},t=r.length,a=0;a<t;a++){var o=r[a],i=o.key,c=n[i],s=c.init?c.init():void 0,u=c.get?c.get():function(){},l=c.set?c.set():function(e){};if("set"in c||"get"in c){var f={configurable:!0,enumerable:!0,get:u,set:l};Object.defineProperty(e,i,f)}else{var h=o.property,d=h.kind;"Identifier"!==h.key.type||"FunctionExpression"!==h.value.type||"init"!==d||h.value.id||p(s,h.key.name),e[i]=s}}return e}},e.prototype.arrayExpressionHandler=function(e){var t=this,r=e.elements.map((function(e){return e?t.createClosure(e):e}));return function(){for(var e=r.length,t=Array(e),n=0;n<e;n++){var a=r[n];a&&(t[n]=a())}return t}},e.prototype.safeObjectGet=function(e,t,r){return e[t]},e.prototype.createCallFunctionGetter=function(e){var t=this;if("MemberExpression"===e.type){var r=this.createClosure(e.object),n=this.createMemberKeyGetter(e),a=this.source;return function(){var o=r(),i=n(),c=t.safeObjectGet(o,i,e);if(!c||!g(c)){var s=a.slice(e.start,e.end);throw t.createInternalThrowError(l.FunctionUndefinedReferenceError,s,e)}return c.bind(o)}}var o=this.createClosure(e);return function(){var r="";"Identifier"===e.type&&(r=e.name);var n=o();if(!n||!g(n))throw t.createInternalThrowError(l.FunctionUndefinedReferenceError,r,e);var a=t.options.globalContextInFunction;if("Identifier"===e.type){var i=t.getIdentifierScope(e);i.name===b&&(a=i.data)}return n.bind(a)}},e.prototype.callExpressionHandler=function(e){var t=this,r=this.createCallFunctionGetter(e.callee),n=e.arguments.map((function(e){return t.createClosure(e)}));return function(){return r().apply(void 0,n.map((function(e){return e()})))}},e.prototype.functionExpressionHandler=function(e){var t=this,r=this,n=this.source,a=this.collectDeclVars,o=this.collectDeclFuncs;this.collectDeclVars=Object.create(null),this.collectDeclFuncs=Object.create(null);var i=e.id?e.id.name:"",c=e.params.length,s=e.params.map((function(e){return t.createParamNameGetter(e)})),u=this.createClosure(e.body),l=this.collectDeclVars,f=this.collectDeclFuncs;return this.collectDeclVars=a,this.collectDeclFuncs=o,function(){var t=r.getCurrentScope(),a=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];r.callStack.push("".concat(i));var o=r.getCurrentScope(),c=function(e,t){return void 0===e&&(e=null),new k(Object.create(null),e,t)}(t,"FunctionScope(".concat(i,")"));r.setCurrentScope(c),r.addDeclarationsToScope(l,f,c),i&&(c.data[i]=a),c.data.arguments=arguments,s.forEach((function(t,r){c.data[t()]=e[r]}));var p=r.getCurrentContext();r.setCurrentContext(this);var h=u();if(r.setCurrentContext(p),r.setCurrentScope(o),r.callStack.pop(),h instanceof x)return h.value};return p(a,i),Object.defineProperty(a,"length",{value:c,writable:!1,enumerable:!1,configurable:!0}),Object.defineProperty(a,"toString",{value:function(){return n.slice(e.start,e.end)},writable:!0,configurable:!0,enumerable:!1}),Object.defineProperty(a,"valueOf",{value:function(){return n.slice(e.start,e.end)},writable:!0,configurable:!0,enumerable:!1}),a}},e.prototype.newExpressionHandler=function(e){var t=this,r=this.source,n=this.createClosure(e.callee),a=e.arguments.map((function(e){return t.createClosure(e)}));return function(){var o=n();if(!g(o)||o.__IS_EVAL_FUNC){var i=e.callee,c=r.slice(i.start,i.end);throw t.createInternalThrowError(l.IsNotConstructor,c,e)}return new(o.bind.apply(o,function(e,t,r){if(r||2===arguments.length)for(var n,a=0,o=t.length;a<o;a++)!n&&a in t||(n||(n=Array.prototype.slice.call(t,0,a)),n[a]=t[a]);return e.concat(n||Array.prototype.slice.call(t))}([void 0],a.map((function(e){return e()})),!1)))}},e.prototype.memberExpressionHandler=function(e){var t=this.createClosure(e.object),r=this.createMemberKeyGetter(e);return function(){return t()[r()]}},e.prototype.thisExpressionHandler=function(e){var t=this;return function(){return t.getCurrentContext()}},e.prototype.sequenceExpressionHandler=function(e){var t=this,r=e.expressions.map((function(e){return t.createClosure(e)}));return function(){for(var e,t=r.length,n=0;n<t;n++){e=(0,r[n])()}return e}},e.prototype.literalHandler=function(e){return function(){return e.regex?new RegExp(e.regex.pattern,e.regex.flags):e.value}},e.prototype.identifierHandler=function(e){var t=this;return function(){var r=t.getCurrentScope(),n=t.getScopeDataFromName(e.name,r);return t.assertVariable(n,e.name,e),n[e.name]}},e.prototype.getIdentifierScope=function(e){var t=this.getCurrentScope();return this.getScopeFromName(e.name,t)},e.prototype.assignmentExpressionHandler=function(e){var t=this;"Identifier"!==e.left.type||"FunctionExpression"!==e.right.type||e.right.id||(e.right.id={type:"Identifier",name:e.left.name});var r=this.createObjectGetter(e.left),n=this.createNameGetter(e.left),a=this.createClosure(e.right);return function(){var o=r(),i=n(),c=a();switch("="!==e.operator&&t.assertVariable(o,i,e),e.operator){case"=":return o[i]=c;case"+=":return o[i]+=c;case"-=":return o[i]-=c;case"*=":return o[i]*=c;case"**=":return o[i]=Math.pow(o[i],c);case"/=":return o[i]/=c;case"%=":return o[i]%=c;case"<<=":return o[i]<<=c;case">>=":return o[i]>>=c;case">>>=":return o[i]>>>=c;case"&=":return o[i]&=c;case"^=":return o[i]^=c;case"|=":return o[i]|=c;default:throw t.createInternalThrowError(l.AssignmentExpressionSyntaxError,e.type,e)}}},e.prototype.functionDeclarationHandler=function(e){if(e.id){var t=this.functionExpressionHandler(e);Object.defineProperty(t,"isFunctionDeclareClosure",{value:!0,writable:!1,configurable:!1,enumerable:!1}),this.funcDeclaration(e.id.name,t)}return function(){return m}},e.prototype.getVariableName=function(e){if("Identifier"===e.type)return e.name;throw this.createInternalThrowError(l.VariableTypeSyntaxError,e.type,e)},e.prototype.variableDeclarationHandler=function(e){for(var t,r=this,n=[],a=0;a<e.declarations.length;a++){var o=e.declarations[a];this.varDeclaration(this.getVariableName(o.id)),o.init&&n.push({type:"AssignmentExpression",operator:"=",left:o.id,right:o.init})}return n.length&&(t=this.createClosure({type:"BlockStatement",body:n})),function(){if(t){var e=r.isVarDeclMode;r.isVarDeclMode=!0,t(),r.isVarDeclMode=e}return m}},e.prototype.assertVariable=function(e,t,r){if(e===this.globalScope.data&&!(t in e))throw this.createInternalThrowError(l.VariableUndefinedReferenceError,t,r)},e.prototype.programHandler=function(e){var t=this,r=e.body.map((function(e){return t.createClosure(e)}));return function(){for(var e=m,n=0;n<r.length;n++){var a=r[n],o=t.setValue(a());if(o!==m&&((e=o)instanceof x||e instanceof C||e instanceof w||e===h||e===d))break}return e}},e.prototype.expressionStatementHandler=function(e){return this.createClosure(e.expression)},e.prototype.emptyStatementHandler=function(e){return function(){return m}},e.prototype.returnStatementHandler=function(e){var t=e.argument?this.createClosure(e.argument):H;return function(){return new x(t())}},e.prototype.ifStatementHandler=function(e){var t=this.createClosure(e.test),r=this.createClosure(e.consequent),n=e.alternate?this.createClosure(e.alternate):/*!important*/function(){return m};return function(){return t()?r():n()}},e.prototype.conditionalExpressionHandler=function(e){return this.ifStatementHandler(e)},e.prototype.forStatementHandler=function(e){var t=this,r=H,n=e.test?this.createClosure(e.test):function(){return!0},a=H,o=this.createClosure(e.body);return"ForStatement"===e.type&&(r=e.init?this.createClosure(e.init):r,a=e.update?this.createClosure(e.update):H),function(i){var c,s=m,u="DoWhileStatement"===e.type;for(i&&"LabeledStatement"===i.type&&(c=i.label.name),r();u||n();a()){u=!1;var l=t.setValue(o());if(l!==m&&l!==d){if(l===h)break;if((s=l)instanceof w&&s.value===c)s=m;else if(s instanceof x||s instanceof C||s instanceof w)break}}return s}},e.prototype.whileStatementHandler=function(e){return this.forStatementHandler(e)},e.prototype.doWhileStatementHandler=function(e){return this.forStatementHandler(e)},e.prototype.forInStatementHandler=function(e){var t=this,r=e.left,n=this.createClosure(e.right),a=this.createClosure(e.body);return"VariableDeclaration"===e.left.type&&(this.createClosure(e.left)(),r=e.left.declarations[0].id),function(e){var o,i,c=m;e&&"LabeledStatement"===e.type&&(o=e.label.name);var s=n();for(i in s){t.assignmentExpressionHandler({type:"AssignmentExpression",operator:"=",left:r,right:{type:"Literal",value:i}})();var u=t.setValue(a());if(u!==m&&u!==d){if(u===h)break;if((c=u)instanceof w&&c.value===o)c=m;else if(c instanceof x||c instanceof C||c instanceof w)break}}return c}},e.prototype.withStatementHandler=function(e){var t=this,r=this.createClosure(e.object),n=this.createClosure(e.body);return function(){var e=r(),a=t.getCurrentScope(),o=new k(e,a,b);t.setCurrentScope(o);var i=t.setValue(n());return t.setCurrentScope(a),i}},e.prototype.throwStatementHandler=function(e){var t=this,r=this.createClosure(e.argument);return function(){throw t.setValue(void 0),r()}},e.prototype.tryStatementHandler=function(e){var t=this,r=this.createClosure(e.block),n=e.handler?this.catchClauseHandler(e.handler):null,a=e.finalizer?this.createClosure(e.finalizer):null;return function(){var e,o,i=t.getCurrentScope(),c=t.getCurrentContext(),s=i.labelStack.concat([]),u=t.callStack.concat([]),l=m,p=function(){t.setCurrentScope(i),t.setCurrentContext(c),i.labelStack=s,t.callStack=u};try{(l=t.setValue(r()))instanceof x&&(e=l)}catch(r){if(p(),t.isInterruptThrow(r))throw r;if(n)try{(l=t.setValue(n(r)))instanceof x&&(e=l)}catch(e){if(p(),t.isInterruptThrow(e))throw e;o=e}}if(a)try{(l=a())instanceof x&&(e=l)}catch(e){if(p(),t.isInterruptThrow(e))throw e;o=e}if(o)throw o;return e||l}},e.prototype.catchClauseHandler=function(e){var t=this,r=this.createParamNameGetter(e.param),n=this.createClosure(e.body);return function(e){var a,o=t.getCurrentScope().data,i=r(),c=f.call(o,i),s=o[i];return o[i]=e,a=n(),c?o[i]=s:delete o[i],a}},e.prototype.continueStatementHandler=function(e){return function(){return e.label?new w(e.label.name):d}},e.prototype.breakStatementHandler=function(e){return function(){return e.label?new C(e.label.name):h}},e.prototype.switchStatementHandler=function(e){var t=this,r=this.createClosure(e.discriminant),n=e.cases.map((function(e){return t.switchCaseHandler(e)}));return function(){for(var e,a,o,i=r(),c=!1,s=0;s<n.length;s++){var u=n[s](),l=u.testClosure();if(l!==y){if(c||l===i){if(c=!0,(a=t.setValue(u.bodyClosure()))===m)continue;if(a===h)break;if((e=a)instanceof x||e instanceof C||e instanceof w||e===d)break}}else o=u}!c&&o&&((a=t.setValue(o.bodyClosure()))===m||a===h||(e=a));return e}},e.prototype.switchCaseHandler=function(e){var t=e.test?this.createClosure(e.test):function(){return y},r=this.createClosure({type:"BlockStatement",body:e.consequent});return function(){return{testClosure:t,bodyClosure:r}}},e.prototype.labeledStatementHandler=function(e){var t=this,r=e.label.name,n=this.createClosure(e.body);return function(){var a,o=t.getCurrentScope();return o.labelStack.push(r),(a=n(e))instanceof C&&a.value===r&&(a=m),o.labelStack.pop(),a}},e.prototype.debuggerStatementHandler=function(e){return function(){return m}},e.prototype.createParamNameGetter=function(e){if("Identifier"===e.type)return function(){return e.name};throw this.createInternalThrowError(l.ParamTypeSyntaxError,e.type,e)},e.prototype.createObjectKeyGetter=function(e){var t;return t="Identifier"===e.type?function(){return e.name}:this.createClosure(e),function(){return t()}},e.prototype.createMemberKeyGetter=function(e){return e.computed?this.createClosure(e.property):this.createObjectKeyGetter(e.property)},e.prototype.createObjectGetter=function(e){var t=this;switch(e.type){case"Identifier":return function(){return t.getScopeDataFromName(e.name,t.getCurrentScope())};case"MemberExpression":return this.createClosure(e.object);default:throw this.createInternalThrowError(l.AssignmentTypeSyntaxError,e.type,e)}},e.prototype.createNameGetter=function(e){switch(e.type){case"Identifier":return function(){return e.name};case"MemberExpression":return this.createMemberKeyGetter(e);default:throw this.createInternalThrowError(l.AssignmentTypeSyntaxError,e.type,e)}},e.prototype.varDeclaration=function(e){this.collectDeclVars[e]=void 0},e.prototype.funcDeclaration=function(e,t){this.collectDeclFuncs[e]=t},e.prototype.addDeclarationsToScope=function(e,t,r){var n=r.data;for(var a in t){var o=t[a];n[a]=o?o():o}for(var a in e)a in n||(n[a]=void 0)},e.prototype.getScopeValue=function(e,t){return this.getScopeFromName(e,t).data[e]},e.prototype.getScopeDataFromName=function(e,t){return this.getScopeFromName(e,t).data},e.prototype.getScopeFromName=function(e,t){var r=t;do{if(e in r.data)return r}while(r=r.parent);return this.globalScope},e.prototype.setValue=function(e){var t=this.callStack.length;return this.isVarDeclMode||t||e===m||e===h||e===d||e instanceof C||e instanceof w||(this.value=e instanceof x?e.value:e),e},e.prototype.getValue=function(){return this.value},e.version="0.0.2",e.ecmaVersion=5,e.globalContextInFunction=void 0,e.global=Object.create(null),e}();export{O as Interpreter};
//# sourceMappingURL=index.js.map