001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.descripter;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Global;
024 
025 /**
026  * <p>Tests Interpreting JavaScript.</p>
027  * 
028  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
029  * @since Descripter 1.0
030  */
031 public class Interpreters extends Global<Core>
032 {
033     /**
034      * <p>Constructs a {@link Global} script context of this type.</p>
035      * @param with The containing {@link Core} context.
036      * @since Descripter 1.0
037      */
038     public Interpreters(Core with) {
039         super(with);
040     }
041 
042     /**
043      * <p>Executes the script context of this type.</p>
044      * @since Descripter 1.0
045      */
046     @Override
047     public void run() {
048         Object o = eval(";;{a:111,b:222};");
049         print(o);
050         print(var(o, "a"));
051         print(var(o, "b"));
052         o = eval("/*test*/[12, 3.02e-9, 0xff, 'asgs', /gas/ig];");
053         print(o);
054         o = eval("Array;");
055         print(o);
056         o = eval("Array.prototype;");
057         print(o);
058         o = eval("Array['constructor'];");
059         print(o);
060         o = eval("new String('my string');");
061         print(o);
062         o = eval("function(){return 'hello';}();");
063         print(o);
064         o = eval("function(s){print(s);return s;}(22222);");
065         print(o);
066         o = eval("function(s){print(s);return s;}('hello2')");
067         print(o);
068         o = eval("function test(s){print(s);return s;}; test(333333);");
069         print(o);
070         o = eval("function test(s){print(s);return s;}; test('hello3');");
071         print(o);
072         o = eval("100+10+3*3-7+7-17%10+7;");
073         print(o);
074         o = eval("false||true&&true;");
075         print(o);
076         o = eval("-10001;");
077         print(o);
078         o = eval("0 ? '666' : 555;");
079         print(o);
080 //      eval("if(0) print('000\n0H'); else pr\u0048nt('elseee');");
081 ///*
082         eval("try {print('try');do{print('doWhile');}while(false);throw 'thrown';} catch(e){print(e);} finally{print('finally');};");
083         eval("with(String){print(prototype);};");
084         eval("aaa='AAA';print(aaa);");
085         eval("while(true){if(true)break;}print('abrupted');");
086         eval("label:while(true){if(true)break label;}print('abrupted');");
087         eval("var a='A',b='BB';print(a+b);");
088         eval("var v=10;while(v){print(v);v=v-1;if(v) {print(v);print(!!v);break;}};print(v);");
089         eval("var v=10;while(v){print(v);v=v-1;}");
090         eval("switch('aa11'){case 'bb':{print('bb');}case 'aa11':{print('aa11');break;}default:{print('default');}};");
091         eval("for(var v in Object){print(v+'='+Object[v]);}print(v);");
092         eval("for(v in Object){print(v+'='+Object[v]);}print(v);");
093         eval("var v=10;for(;v;v=v-1){print(v);}");
094         eval("for(var v=10;v;v=v-1){print(v);}print(v);");
095         eval("print(7&2);print(7|2);print(7<<2);print(-7>>2);print(-7>>>2);");
096         eval("print(Math);print(Math.PI);print(Math.sin(0));print(Math.sin(Math.PI/4));");
097         eval("var a = new Array(5); debugger;a[0]=1000; a[1]= 2000; print(a.length); print(a[1]);");
098         eval("var s = 'Hello World!'; var v ='print(s);eval(\"print(s)\")'; print(v);eval(v);");
099 //      eval("eval(\"print('Hello World')\");#System.out.println(\"Hello Hash!\");");
100         eval("print(Math.sin(Math.PI/4));");
101     }
102 
103     /**
104      * <p>Creates and runs {@link Global} script contexts of the containing type.</p>
105      * @param args Ignored
106      * @since Descripter 1.0
107      */
108     public static void main(String[] args) {
109         Core c = new Core();
110         new Interpreters(c).run();
111     }
112 }