01 
02 /*
03  *  Descripter 1.0 - Java Script Engines
04  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
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.descripter.js.api;
21 
22 /**
23  * <p>Emulates JavaScript with statements.</p>
24  * 
25  * @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>
26  * @since Descripter 1.0
27  */
28 public abstract class With<W extends Script<?>> extends Script<W>
29 {
30     private final Objective<?> base;
31 
32     /**
33      * <p>Constructs a script context of this type.</p>
34      * @param with The containing script context.
35      * @param o The base object
36      * @since Descripter 1.0
37      */
38     public With(W with, Object o) {
39         super(with);
40         base = with.object(o);
41     }
42 
43     /**
44      * <p>Returns the value associated with the specified index.</p>
45      * @param i An index to lookup
46      * @return The value associated with the specified index or <tt>null</tt> for none.
47      * @since Descripter 1.0
48      */
49     @Override
50     public final Object get(Integer i) {
51         Object o = base.get(i);
52         return o != null ? o : super.get(i);
53     }
54 
55     /**
56      * <p>Sets the value associated with the specified index.</p>
57      * @param i An index to set the value
58      * @param v The value to set
59      * @throws RuntimeException if the base context is read-only.
60      * @since Descripter 1.0
61      */
62     @Override
63     public final void put(Integer i, Object v) {
64         base.set(i, evaluate(v));
65     }
66 }