01 
02 /*
03  *  JScripter Simulation 1.0 - For Java To Script
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package org.jscripter.sim.core;
21 
22 import js.Js;
23 import js.Var;
24 import js.core.JsFunction;
25 import js.core.JsGlobal;
26 import js.core.JsObject;
27 
28 final class JscGlobal extends JsGlobal
29 {
30     JscGlobal() {
31         super(new JscObject());
32     }
33 
34     void init() {
35         var(Object  , CreatorObject  .get());
36         var(Function, CreatorFunction.get());
37         JsGlobal.Object go = JsGlobal.Object.with();
38         JsObject gop = new JscObject();
39         JsFunction.prototype.with(go, gop);
40         JsFunction<?> gf = JsGlobal.Function.with();
41         JsObject.constructor.with(go, gf);
42         JsObject.constructor.with(gf, go);
43         JsObject gfp = new CoreFunction<Void>(JsFunction.prototype.toString()) {
44             @Override
45             protected Void function(java.lang.Object jsthis, Call<Void> callee) {
46                 return null;
47             }
48         }.var();
49 //      JsFunction.prototype.with(gop, gfp);
50         JsFunction.prototype.with(gf, gfp);
51         JsFunction.prototype.with(Js.core(), go.create());
52         gop.var(
53                 JsObject.toString,
54                 new CoreFunction<java.lang.String>(JsObject.toString.toString()) {
55                     @Override
56                     protected java.lang.String function(java.lang.Object jsthis, Call<java.lang.String> callee) {
57                         return Js.toString(CoreFunction.like(jsthis));
58                     }
59                 }
60         );
61         gop.var(
62                 JsObject.valueOf,
63                 new CoreFunction<java.lang.Object>(JsObject.valueOf.toString()) {
64                     @Override
65                     protected java.lang.Object function(java.lang.Object jsthis, Call<java.lang.Object> callee) {
66                         return jsthis instanceof JsObject ? jsthis : ((Var<?>)jsthis).var();
67                     }
68                 }
69         );
70         gfp.var(
71                 JsFunction.toString,
72                 new CoreFunction<java.lang.String>(JsFunction.toString.toString()) {
73                     @Override
74                     protected java.lang.String function(java.lang.Object jsthis, Call<java.lang.String> callee) {
75                         return Js.toString((js.Function<?>)CoreFunction.like(jsthis));
76                     }
77                 }
78         );
79         gfp.var(
80                 JsFunction.valueOf,
81                 new CoreFunction<JsFunction<?>>(JsFunction.valueOf.toString()) {
82                     @Override
83                     protected JsFunction<?> function(java.lang.Object jsthis, Call<JsFunction<?>> callee) {
84                         return ((js.Function<?>)CoreFunction.like(jsthis)).valueOf();
85                     }
86                 }
87         );
88         var(Array   , CreatorArray   .get());
89         var(Boolean , CreatorBoolean .get());
90         var(Date    , CreatorDate    .get());
91         var(Math    , CoreMath       .get());
92         var(Number  , CreatorNumber  .get());
93         var(RegExp  , CreatorRegExp  .get());
94         var(String  , CreatorString  .get());
95     }
96 }