diff --git a/.gitignore b/.gitignore index aebe00aa..464c9db8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.komodoproject +node_modules/ diff --git a/README.md b/README.md index 27a6075a..8c1b07ef 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,81 @@ JS-Interpreter ============== -A sandboxed JavaScript interpreter in JavaScript. Execute arbitrary JavaScript -code line by line in isolation and safety. +A sandboxed JavaScript interpreter written in TypeScript, packed with `Webpack` or `tsc` for use with Node.js or within a browser (either loaded [statically](https://junkato.jp/JS-Interpreter/) or [dynamically](https://junkato.jp/JS-Interpreter/?required)). Execute arbitrary JavaScript code line by line in isolation and safety. + +This interpreter is a TypeScript port of [the original JavaScript version](https://github.com/NeilFraser/JS-Interpreter). + +There are several library files that serve different use cases. See below for more concrete instructions. + +- `dist/interpreter.js` ... packed with `tsc` (`npm run build`) and can be loaded from Node.js or a browser with RequireJS. `acorn` needs to be loaded separately. +- `dist/interpreter.d.ts` ... TypeScript type definition +- `dist/acorn_interpreter.js` ... packed with `Webpack` (`npm run build-with-acorn`) as a library and can be loaded with a ` + +``` + +## Dynamic loading with RequireJS + +Load `dist/acorn.js` and `dist/interpreter.js` dynamically with [RequireJS](//requirejs.org) and use the `Interpreter` class. +No global scope pollution! + +```html + + +``` + +-------------- +(c) 2013-2017 Google Inc. and [Jun Kato](https://junkato.jp) diff --git a/acorn_interpreter.js b/acorn_interpreter.js deleted file mode 100644 index d9c24a21..00000000 --- a/acorn_interpreter.js +++ /dev/null @@ -1,134 +0,0 @@ -// Acorn: Copyright 2012 Marijn Haverbeke, MIT License -var mod$$inline_58=function(a){function b(a){n=a||{};for(var b in Ua)Object.prototype.hasOwnProperty.call(n,b)||(n[b]=Ua[b]);wa=n.sourceFile||null}function c(a,b){var c=Ab(k,a);b+=" ("+c.line+":"+c.column+")";var d=new SyntaxError(b);d.pos=a;d.loc=c;d.raisedAt=f;throw d;}function d(a){function b(a){if(1==a.length)return c+="return str === "+JSON.stringify(a[0])+";";c+="switch(str){";for(var va=0;vaa)++f;else if(47===a)if(a=k.charCodeAt(f+1),42===a){var a=n.onComment&&n.locations&&new e,b=f,d=k.indexOf("*/",f+=2);-1===d&&c(f-2,"Unterminated comment"); -f=d+2;if(n.locations){Y.lastIndex=b;for(var g=void 0;(g=Y.exec(k))&&g.index=a?a=P(!0):(++f,a=g(xa)),a;case 40:return++f,g(I);case 41:return++f,g(E);case 59:return++f,g(J);case 44:return++f,g(L);case 91:return++f,g(ja); -case 93:return++f,g(ka);case 123:return++f,g(Z);case 125:return++f,g(T);case 58:return++f,g(aa);case 63:return++f,g(ya);case 48:if(a=k.charCodeAt(f+1),120===a||88===a)return f+=2,a=B(16),null==a&&c(x+2,"Expected hexadecimal number"),la(k.charCodeAt(f))&&c(f,"Identifier directly after number"),a=g(ba,a);case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return P(!1);case 34:case 39:a:{f++;for(var b="";;){f>=S&&c(x,"Unterminated string constant");var d=k.charCodeAt(f);if(d===a){++f; -a=g(da,b);break a}if(92===d){var d=k.charCodeAt(++f),e=/^[0-7]+/.exec(k.slice(f,f+3));for(e&&(e=e[0]);e&&255=S)return g(pa);var b=k.charCodeAt(f);if(la(b)||92===b)return Ya();a=m(b);if(!1===a){b=String.fromCharCode(b);if("\\"===b||Za.test(b))return Ya();c(f,"Unexpected character '"+b+"'")}return a}function t(a,b){var c=k.slice(f,f+b);f+=b;g(a,c)}function K(){for(var a,b,d=f;;){f>=S&&c(d, -"Unterminated regular expression");var e=k.charAt(f);na.test(e)&&c(d,"Unterminated regular expression");if(a)a=!1;else{if("["===e)b=!0;else if("]"===e&&b)b=!1;else if("/"===e&&!b)break;a="\\"===e}++f}a=k.slice(d,f);++f;(b=$a())&&!/^[gmsiy]*$/.test(b)&&c(d,"Invalid regexp flag");return g(Ba,new RegExp(a,b))}function B(a,b){for(var c=f,d=0,e=0,g=null==b?Infinity:b;e=h?h-48:Infinity;if(h>=a)break;++f;d=d*a+h}return f===c||null!=b&& -f-c!==b?null:d}function P(a){var b=f,d=!1,e=48===k.charCodeAt(f);a||null!==B(10)||c(b,"Invalid number");46===k.charCodeAt(f)&&(++f,B(10),d=!0);a=k.charCodeAt(f);if(69===a||101===a)a=k.charCodeAt(++f),43!==a&&45!==a||++f,null===B(10)&&c(b,"Invalid number"),d=!0;la(k.charCodeAt(f))&&c(f,"Identifier directly after number");a=k.slice(b,f);var h;d?h=parseFloat(a):e&&1!==a.length?/[89]/.test(a)||C?c(b,"Invalid number"):h=parseInt(a,8):h=parseInt(a,10);return g(ba,h)}function ma(a){a=B(16,a);null===a&&c(x, -"Bad character escape sequence");return a}function $a(){ca=!1;for(var a,b=!0,d=f;;){var e=k.charCodeAt(f);if(ab(e))ca&&(a+=k.charAt(f)),++f;else if(92===e){ca||(a=k.slice(d,f));ca=!0;117!=k.charCodeAt(++f)&&c(f,"Expecting Unicode escape sequence \\uXXXX");++f;var e=ma(4),g=String.fromCharCode(e);g||c(f-1,"Invalid Unicode escape");(b?la(e):ab(e))||c(f-4,"Invalid Unicode escape");a+=g}else break;b=!1}return ca?a:k.slice(d,f)}function Ya(){var a=$a(),b=V;ca||(Lb(a)?b=Ca[a]:(n.forbidReserved&&(3===n.ecmaVersion? -Mb:Nb)(a)||C&&bb(a))&&c(x,"The keyword '"+a+"' is reserved"));return g(b,a)}function r(){Da=x;M=X;Ea=ia;z()}function Fa(a){C=a;f=M;if(n.locations)for(;fb){var e=Q(a);e.left=a;e.operator=H;a=p;r();e.right=Ra(Sa(),d,c);d=q(e,a===Va||a===Wa?"LogicalExpression":"BinaryExpression");return Ra(d,b,c)}return a}function Sa(){if(p.prefix){var a=y(),b=p.isUpdate;a.operator=H;R=a.prefix=!0;r();a.argument= -Sa();b?ra(a.argument):C&&"delete"===a.operator&&"Identifier"===a.argument.type&&c(a.start,"Deleting local variable in strict mode");return q(a,b?"UpdateExpression":"UnaryExpression")}for(b=ha(ua());p.postfix&&!qa();)a=Q(b),a.operator=H,a.prefix=!1,a.argument=b,ra(b),r(),b=q(a,"UpdateExpression");return b}function ha(a,b){if(u(xa)){var c=Q(a);c.object=a;c.property=O(!0);c.computed=!1;return ha(q(c,"MemberExpression"),b)}return u(ja)?(c=Q(a),c.object=a,c.property=A(),c.computed=!0,v(ka),ha(q(c,"MemberExpression"), -b)):!b&&u(I)?(c=Q(a),c.callee=a,c.arguments=Ta(E,!1),ha(q(c,"CallExpression"),b)):a}function ua(){switch(p){case ub:var a=y();r();return q(a,"ThisExpression");case V:return O();case ba:case da:case Ba:return a=y(),a.value=H,a.raw=k.slice(x,X),r(),q(a,"Literal");case vb:case wb:case xb:return a=y(),a.value=p.atomValue,a.raw=p.keyword,r(),q(a,"Literal");case I:var a=oa,b=x;r();var d=A();d.start=b;d.end=X;n.locations&&(d.loc.start=a,d.loc.end=ia);n.ranges&&(d.range=[b,X]);v(E);return d;case ja:return a= -y(),r(),a.elements=Ta(ka,!0,!0),q(a,"ArrayExpression");case Z:a=y();b=!0;d=!1;a.properties=[];for(r();!u(T);){if(b)b=!1;else if(v(L),n.allowTrailingCommas&&u(T))break;var e={key:p===ba||p===da?ua():O(!0)},g=!1,h;u(aa)?(e.value=A(!0),h=e.kind="init"):5<=n.ecmaVersion&&"Identifier"===e.key.type&&("get"===e.key.name||"set"===e.key.name)?(g=d=!0,h=e.kind=e.key.name,e.key=p===ba||p===da?ua():O(!0),p!==I&&N(),e.value=Na(y(),!1)):N();if("Identifier"===e.key.type&&(C||d))for(var f=0;fd?a.id:a.params[d],(bb(e.name)||sa(e.name))&&c(e.start,"Defining '"+e.name+"' in strict mode"),0<=d)for(var g=0;ga?36===a:91>a?!0:97>a?95===a:123>a?!0:170<=a&&Za.test(String.fromCharCode(a))},ab=a.isIdentifierChar=function(a){return 48>a?36===a:58>a?!0:65>a?!1:91>a?!0:97>a?95===a:123>a?!0:170<=a&&Pb.test(String.fromCharCode(a))},ca,Ia={kind:"loop"},Ob={kind:"switch"}}; -"object"==typeof exports&&"object"==typeof module?mod$$inline_58(exports):"function"==typeof define&&define.amd?define(["exports"],mod$$inline_58):mod$$inline_58(this.acorn||(this.acorn={})); -// JS-Interpreter: Copyright 2013 Google Inc, Apache 2.0 -var g; -function h(a,b){"string"===typeof a&&(a=acorn.parse(a,k));this.Y=a;this.La=b;this.pa=!1;this.ca=[];this.ta=0;this.Ja=Object.create(null);var c=/^step([A-Z]\w*)$/,d,e;for(e in this)if(d=e.match(c))this.Ja[d[1]]=this[e].bind(this);this.global=l(this,this.Y,null);this.Y=acorn.parse(this.ca.join("\n"),k);this.ca=void 0;n(this,this.Y,void 0,void 0);c=new p(this.Y,this.global);c.done=!1;this.g=[c];this.Na();this.value=void 0;this.Y=a;c=new p(this.Y,this.global);c.done=!1;this.g.length=0;this.g[0]=c;this.Aa= -c.node.constructor;this.stateStack=this.g;this.OBJECT=this.l;this.OBJECT_PROTO=this.F;this.FUNCTION=this.v;this.FUNCTION_PROTO=this.A;this.ARRAY=this.j;this.ARRAY_PROTO=this.K;this.REGEXP=this.G;this.REGEXP_PROTO=this.ra;this.UNDEFINED=void 0;this.NULL=null;this.NAN=NaN;this.TRUE=!0;this.FALSE=!1;this.STRING_EMPTY="";this.NUMBER_ZERO=0;this.NUMBER_ONE=1} -var k={$a:5},q={configurable:!0,enumerable:!0,writable:!1},t={configurable:!0,enumerable:!1,writable:!0},u={configurable:!0,enumerable:!1,writable:!1},v={configurable:!1,enumerable:!0,writable:!0},x={},z={},A=[]; -h.prototype.Qa=function(a){var b=this.g[0];if(!b||"Program"!==b.node.type)throw Error("Expecting original AST to start with a Program node.");"string"===typeof a&&(a=acorn.parse(a,k));if(!a||"Program"!==a.type)throw Error("Expecting new AST to start with a Program node.");B(this,a,b.scope);for(var c=0,d;d=a.body[c];c++)b.node.body.push(d);b.done=!1}; -h.prototype.step=function(){var a=this.g,b=a[a.length-1];if(!b)return!1;var c=b.node,d=c.type;if("Program"===d&&b.done)return!1;if(this.pa)return!0;try{var e=this.Ja[d](a,b,c)}catch(f){if(f!==x)throw f;}e&&a.push(e);return c.end?!0:this.step()};h.prototype.Na=function(){for(;!this.pa&&this.step(););return this.pa}; -function C(a,b){a.setProperty(b,"Infinity",Infinity,q);a.setProperty(b,"NaN",NaN,q);a.setProperty(b,"undefined",void 0,q);a.setProperty(b,"window",b,q);a.setProperty(b,"this",b,q);a.setProperty(b,"self",b);a.F=new D(null);a.A=new D(a.F);E(a,b);aa(a,b);b.ea=a.F;a.setProperty(b,"constructor",a.l);ba(a,b);ca(a,b);da(a,b);ea(a,b);fa(a,b);ga(a,b);ha(a,b);ia(a,b);ja(a,b);a.setProperty(b,"isNaN",a.b(isNaN,!1));a.setProperty(b,"isFinite",a.b(isFinite,!1));a.setProperty(b,"parseFloat",a.b(parseFloat,!1)); -a.setProperty(b,"parseInt",a.b(parseInt,!1));var c=a.c(a.A);c.eval=!0;c.Ka=!0;a.setProperty(c,"length",1,q);a.setProperty(b,"eval",c);for(var c=[[escape,"escape"],[unescape,"unescape"],[decodeURI,"decodeURI"],[decodeURIComponent,"decodeURIComponent"],[encodeURI,"encodeURI"],[encodeURIComponent,"encodeURIComponent"]],d=0;db?Math.max(this.length+b,0):Math.min(b,this.length);d=c(d,Infinity);d=Math.min(d,this.length-b);for(var e=a.c(a.K),f=b;f=b;f--)this.a[f+arguments.length-2]=this.a[f];this.length+=arguments.length-2;for(f=2;ff&&(f=this.length+f);var f=Math.max(0,Math.min(f,this.length)),r=c(d,this.length);0>r&&(r=this.length+r);for(var r=Math.max(0,Math.min(r,this.length)),w=0;fe&&(e=this.length+ -e);for(e=Math.max(0,e);ee&&(e=this.length+e);for(e=Math.min(e,this.length-1);0<=e;e--)if(a.m(this,e)===b)return e;return-1};L(a,a.j,"lastIndexOf",d);a.ca.push("Object.defineProperty(Array.prototype, 'every',","{configurable: true, writable: true, value:","function(callbackfn, thisArg) {","if (!this || typeof callbackfn !== 'function') throw TypeError();","var T, k;","var O = Object(this);", -"var len = O.length >>> 0;","if (arguments.length > 1) T = thisArg;","k = 0;","while (k < len) {","if (k in O && !callbackfn.call(T, O[k], k, O)) return false;","k++;","}","return true;","}","});","Object.defineProperty(Array.prototype, 'filter',","{configurable: true, writable: true, value:","function(fun/*, thisArg*/) {","if (this === void 0 || this === null || typeof fun !== 'function') throw TypeError();","var t = Object(this);","var len = t.length >>> 0;","var res = [];","var thisArg = arguments.length >= 2 ? arguments[1] : void 0;", -"for (var i = 0; i < len; i++) {","if (i in t) {","var val = t[i];","if (fun.call(thisArg, val, i, t)) res.push(val);","}","}","return res;","}","});","Object.defineProperty(Array.prototype, 'forEach',","{configurable: true, writable: true, value:","function(callback, thisArg) {","if (!this || typeof callback !== 'function') throw TypeError();","var T, k;","var O = Object(this);","var len = O.length >>> 0;","if (arguments.length > 1) T = thisArg;","k = 0;","while (k < len) {","if (k in O) callback.call(T, O[k], k, O);", -"k++;","}","}","});","Object.defineProperty(Array.prototype, 'map',","{configurable: true, writable: true, value:","function(callback, thisArg) {","if (!this || typeof callback !== 'function') new TypeError;","var T, A, k;","var O = Object(this);","var len = O.length >>> 0;","if (arguments.length > 1) T = thisArg;","A = new Array(len);","k = 0;","while (k < len) {","if (k in O) A[k] = callback.call(T, O[k], k, O);","k++;","}","return A;","}","});","Object.defineProperty(Array.prototype, 'reduce',", -"{configurable: true, writable: true, value:","function(callback /*, initialValue*/) {","if (!this || typeof callback !== 'function') throw TypeError();","var t = Object(this), len = t.length >>> 0, k = 0, value;","if (arguments.length === 2) {","value = arguments[1];","} else {","while (k < len && !(k in t)) k++;","if (k >= len) {","throw TypeError('Reduce of empty array with no initial value');","}","value = t[k++];","}","for (; k < len; k++) {","if (k in t) value = callback(value, t[k], k, t);", -"}","return value;","}","});","Object.defineProperty(Array.prototype, 'reduceRight',","{configurable: true, writable: true, value:","function(callback /*, initialValue*/) {","if (null === this || 'undefined' === typeof this || 'function' !== typeof callback) throw TypeError();","var t = Object(this), len = t.length >>> 0, k = len - 1, value;","if (arguments.length >= 2) {","value = arguments[1];","} else {","while (k >= 0 && !(k in t)) k--;","if (k < 0) {","throw TypeError('Reduce of empty array with no initial value');", -"}","value = t[k--];","}","for (; k >= 0; k--) {","if (k in t) value = callback(value, t[k], k, t);","}","return value;","}","});","Object.defineProperty(Array.prototype, 'some',","{configurable: true, writable: true, value:","function(fun/*, thisArg*/) {","if (!this || typeof fun !== 'function') throw TypeError();","var t = Object(this);","var len = t.length >>> 0;","var thisArg = arguments.length >= 2 ? arguments[1] : void 0;","for (var i = 0; i < len; i++) {","if (i in t && fun.call(thisArg, t[i], i, t)) {", -"return true;","}","}","return false;","}","});","Object.defineProperty(Array.prototype, 'sort',","{configurable: true, writable: true, value:","function(opt_comp) {","for (var i = 0; i < this.length; i++) {","var changes = 0;","for (var j = 0; j < this.length - i - 1; j++) {","if (opt_comp ?opt_comp(this[j], this[j + 1]) > 0 : this[j] > this[j + 1]) {","var swap = this[j];","this[j] = this[j + 1];","this[j + 1] = swap;","changes++;","}","}","if (changes <= 1) break;","}","return this;","}","});", -"Object.defineProperty(Array.prototype, 'toLocaleString',","{configurable: true, writable: true, value:","function() {","var out = [];","for (var i = 0; i < this.length; i++) {","out[i] = (this[i] === null || this[i] === undefined) ? '' : this[i].toLocaleString();","}","return out.join(',');","}","});","")} -function ca(a,b){var c=function(b){b=Number(b);return I(a)?(this.data=b,this):b};a.M=a.b(c,!0);a.setProperty(b,"Number",a.M);c=["MAX_VALUE","MIN_VALUE","NaN","NEGATIVE_INFINITY","POSITIVE_INFINITY"];for(var d=0;d>>0;return b===Number(a)?b:NaN}function O(a){var b=a>>>0;return String(b)===String(a)&&4294967295!==b?b:NaN}function D(a){this.ba=Object.create(null);this.fa=Object.create(null);this.a=Object.create(null);this.ea=a}g=D.prototype;g.ea=null;g.o=!0;g.B="Object"; -g.data=null; -g.toString=function(){if("Array"===this.B){var a=A;a.push(this);try{var b=[];for(var c=0;cb.charCodeAt(0)&&J(this,a,this.s)){var c=O(b);if(!isNaN(c)&&c>=":d>>=e;break;case ">>>=":d>>>=e;break;case "&=":d&=e;break;case "^=":d^=e;break;case "|=":d|=e;break;default:throw SyntaxError("Unknown assignment expression: "+c.operator);}if(c=W(this,b.la,d))return b.N=d,Z(this,c,b.la,d);a.pop();a[a.length-1].value=d}}; -h.prototype.stepBinaryExpression=function(a,b,c){if(!b.P)return b.P=!0,new p(c.left,b.scope);if(!b.ja)return b.ja=!0,b.S=b.value,new p(c.right,b.scope);a.pop();var d=b.S;b=b.value;switch(c.operator){case "==":c=d==b;break;case "!=":c=d!=b;break;case "===":c=d===b;break;case "!==":c=d!==b;break;case ">":c=d>b;break;case ">=":c=d>=b;break;case "<":c=d>":c=d>>b;break;case ">>>":c=d>>>b;break;case "in":b&&b.o||F(this,this.f,"'in' expects an object, not '"+b+"'");c=Q(this,b,d);break;case "instanceof":J(this,b,this.v)||F(this,this.f,"Right-hand side of instanceof is not an object");c=d.o?J(this,d,b):!1;break;default:throw SyntaxError("Unknown binary operator: "+c.operator);}a[a.length-1].value=c}; -h.prototype.stepBlockStatement=function(a,b,c){var d=b.i||0;if(c=c.body[d])return b.i=d+1,new p(c,b.scope);a.pop()};h.prototype.stepBreakStatement=function(a,b,c){a.pop();var d=null;for(c.label&&(d=c.label.name);b&&"CallExpression"!==b.node.type&&"NewExpression"!==b.node.type;){if(d){if(b.labels&&-1!==b.labels.indexOf(d))return}else if(b.ka||b.Ya)return;b=a.pop()}throw SyntaxError("Illegal break statement");}; -h.prototype.stepCallExpression=function(a,b,c){var d;if(!b.aa){b.aa=1;var e=new p(c.callee,b.scope);e.Z=!0;return e}if(1===b.aa){b.aa=2;e=b.value;if(Array.isArray(e)){if(b.R=V(this,e),e[0]!==z&&(b.H=e[0]),(e=b.R)&&"object"===typeof e&&e.J)return e.J=!1,b.aa=1,Y(this,e,b.value)}else b.R=e;b.u=[];b.i=0}e=b.R;if(!b.xa){0!==b.i&&b.u.push(b.value);if(c.arguments[b.i])return new p(c.arguments[b.i++],b.scope);"NewExpression"===c.type?(e.Ka&&F(this,this.f,e+" is not a constructor"),b.H=this.$(e),b.isConstructor= -!0):void 0===b.H&&(b.H=b.scope.C?void 0:this.global);b.xa=!0}if(b.ya)a.pop(),a[a.length-1].value=b.isConstructor&&"object"!==typeof b.value?b.H:b.value;else{b.ya=!0;e&&e.o||F(this,this.f,e+" is not a function");if(a=e.node){c=l(this,a.body,e.U);for(d=0;dd?b.u[d]:void 0);var f=this.c(this.K);for(d=0;dd)throw SyntaxError("Illegal return statement");b=a[d]}b.value=c};h.prototype.stepSequenceExpression=function(a,b,c){var d=b.i||0;if(c=c.expressions[d])return b.i=d+1,new p(c,b.scope);a.pop();a[a.length-1].value=b.value}; -h.prototype.stepSwitchStatement=function(a,b,c){if(!b.O)return b.O=1,new p(c.discriminant,b.scope);1===b.O&&(b.O=2,b.Za=b.value);for(;;){var d=b.za||0,e=c.cases[d];if(b.na||!e||e.test)if(e||b.na||!b.Ga)if(e){if(!b.na&&!a.Oa&&e.test)return a.Oa=!0,new p(e.test,b.scope);if(b.na||b.value===b.Za){b.na=!0;var f=b.i||0;if(e.consequent[f])return b.Ya=!0,b.i=f+1,new p(e.consequent[f],b.scope)}a.Oa=!1;b.i=0;b.za=d+1}else{a.pop();break}else b.na=!0,b.za=b.Ga;else b.Ga=d,b.za=d+1}}; -h.prototype.stepThisExpression=function(a){a.pop();a[a.length-1].value=T(this,"this")};h.prototype.stepThrowStatement=function(a,b,c){if(b.L)F(this,b.value);else return b.L=!0,new p(c.argument,b.scope)};h.prototype.stepTryStatement=function(a,b,c){if(!b.Sa)return b.Sa=!0,new p(c.block,b.scope);if(b.V&&!b.Va&&c.handler)return b.Va=!0,a=new p(c.handler,b.scope),a.V=b.V,b.V=null,a;if(!b.Ua&&c.finalizer)return b.Ua=!0,new p(c.finalizer,b.scope);b.V?X(this,b.V):a.pop()}; -h.prototype.stepUnaryExpression=function(a,b,c){if(!b.L)return b.L=!0,a=new p(c.argument,b.scope),a.Z="delete"===c.operator,a;a.pop();var d=b.value;if("-"===c.operator)d=-d;else if("+"===c.operator)d=+d;else if("!"===c.operator)d=!d;else if("~"===c.operator)d=~d;else if("delete"===c.operator){c=!0;if(Array.isArray(d)){var e=d[0];e===z&&(e=b.scope);d=String(d[1]);try{delete e.a[d]}catch(f){b.scope.C?F(this,this.f,"Cannot delete property '"+d+"' of '"+e+"'"):c=!1}}d=c}else if("typeof"===c.operator)d= -d&&"Function"===d.B?"function":typeof d;else if("void"===c.operator)d=void 0;else throw SyntaxError("Unknown unary operator: "+c.operator);a[a.length-1].value=d}; -h.prototype.stepUpdateExpression=function(a,b,c){if(!b.P)return b.P=!0,a=new p(c.argument,b.scope),a.Z=!0,a;b.ma||(b.ma=b.value);b.ha&&(b.S=b.value);if(!b.ha){var d=V(this,b.ma);if((b.S=d)&&"object"===typeof d&&d.J)return d.J=!1,b.ha=!0,Y(this,d,b.ma)}if(b.N)a.pop(),a[a.length-1].value=b.N;else{d=Number(b.S);if("++"===c.operator)var e=d+1;else if("--"===c.operator)e=d-1;else throw SyntaxError("Unknown update expression: "+c.operator);c=c.prefix?e:d;if(d=W(this,b.ma,e))return b.N=c,Z(this,d,b.ma,e); -a.pop();a[a.length-1].value=c}};h.prototype.stepVariableDeclaration=function(a,b,c){c=c.declarations;var d=b.i||0,e=c[d];b.Ma&&e&&(U(this,e.id.name,b.value),b.Ma=!1,e=c[++d]);for(;e;){if(e.init)return b.i=d,b.Ma=!0,new p(e.init,b.scope);e=c[++d]}a.pop()};h.prototype.stepWithStatement=function(a,b,c){if(b.ia)if(b.Ta)a.pop();else return b.Ta=!0,a=R(this,b.scope,b.value),new p(c.body,a);else return b.ia=!0,new p(c.object,b.scope)};h.prototype.stepWhileStatement=h.prototype.stepDoWhileStatement; -this.Interpreter=h;h.prototype.step=h.prototype.step;h.prototype.run=h.prototype.Na;h.prototype.appendCode=h.prototype.Qa;h.prototype.createObject=h.prototype.$;h.prototype.createObjectProto=h.prototype.c;h.prototype.createAsyncFunction=h.prototype.Ra;h.prototype.createNativeFunction=h.prototype.b;h.prototype.getProperty=h.prototype.m;h.prototype.setProperty=h.prototype.setProperty;h.prototype.nativeToPseudo=h.prototype.I;h.prototype.pseudoToNative=h.prototype.w;h.prototype.createPrimitive=function(a){return a}; diff --git a/demos/async.html b/demos/async.html index e70fc8a4..202fe857 100644 --- a/demos/async.html +++ b/demos/async.html @@ -4,8 +4,8 @@ JS-Interpreter Async Demo - - + + + + + \ No newline at end of file diff --git a/demos/browser/index.js b/demos/browser/index.js new file mode 100644 index 00000000..87c2e9ad --- /dev/null +++ b/demos/browser/index.js @@ -0,0 +1,9 @@ +define(["require", "exports", "acorn", "../../dist/interpreter"], function (require, exports, acorn, Interpreter) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + Interpreter.acorn = acorn; + // Test the interpreter. + var interpreter = new Interpreter("var a = 1, b = 2; a + b;"); + interpreter.run(); + alert('1 + 2 = ' + interpreter.value); // shows an alert '1 + 2 = 3' +}); diff --git a/demos/browser/index.ts b/demos/browser/index.ts new file mode 100644 index 00000000..4688e08e --- /dev/null +++ b/demos/browser/index.ts @@ -0,0 +1,9 @@ + +import acorn = require('acorn'); +import Interpreter = require('../../dist/interpreter'); +Interpreter.acorn = acorn; + +// Test the interpreter. +var interpreter = new Interpreter("var a = 1, b = 2; a + b;"); +interpreter.run(); +alert('1 + 2 = ' + interpreter.value); // shows an alert '1 + 2 = 3' diff --git a/demos/json.html b/demos/json.html index c513e18a..679bec5a 100644 --- a/demos/json.html +++ b/demos/json.html @@ -4,8 +4,8 @@ JS-Interpreter JSON Demo - - + + - + + - + +