1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\r
3 * The contents of this file are subject to the Netscape Public
\r
4 * License Version 1.1 (the "License"); you may not use this file
\r
5 * except in compliance with the License. You may obtain a copy of
\r
6 * the License at http://www.mozilla.org/NPL/
\r
8 * Software distributed under the License is distributed on an "AS
\r
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
\r
10 * implied. See the License for the specific language governing
\r
11 * rights and limitations under the License.
\r
13 * The Original Code is Rhino code, released
\r
16 * The Initial Developer of the Original Code is Netscape
\r
17 * Communications Corporation. Portions created by Netscape are
\r
18 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
\r
24 * Alternatively, the contents of this file may be used under the
\r
25 * terms of the GNU Public License (the "GPL"), in which case the
\r
26 * provisions of the GPL are applicable instead of those above.
\r
27 * If you wish to allow use of your version of this file only
\r
28 * under the terms of the GPL and not to allow others to use your
\r
29 * version of this file under the NPL, indicate your decision by
\r
30 * deleting the provisions above and replace them with the notice
\r
31 * and other provisions required by the GPL. If you do not delete
\r
32 * the provisions above, a recipient may use your version of this
\r
33 * file under either the NPL or the GPL.
\r
36 package org.mozilla.javascript;
\r
38 import java.lang.reflect.Method;
\r
41 * This class implements the object lookup required for the
\r
42 * <code>with</code> statement.
\r
43 * It simply delegates every action to its prototype except
\r
44 * for operations on its parent.
\r
46 public class NativeWith implements Scriptable, IdFunctionMaster {
\r
48 public static void init(Context cx, Scriptable scope, boolean sealed) {
\r
49 NativeWith obj = new NativeWith();
\r
50 obj.prototypeFlag = true;
\r
52 IdFunction ctor = new IdFunction(obj, "constructor", Id_constructor);
\r
53 ctor.initAsConstructor(scope, obj);
\r
54 if (sealed) { ctor.sealObject(); }
\r
56 obj.setParentScope(ctor);
\r
57 obj.setPrototype(ScriptableObject.getObjectPrototype(scope));
\r
59 ScriptableObject.defineProperty(scope, "With", ctor,
\r
60 ScriptableObject.DONTENUM);
\r
63 public NativeWith() {
\r
66 public NativeWith(Scriptable parent, Scriptable prototype) {
\r
67 this.parent = parent;
\r
68 this.prototype = prototype;
\r
71 public String getClassName() {
\r
75 public boolean has(String id, Scriptable start) {
\r
78 return prototype.has(id, start);
\r
81 public boolean has(int index, Scriptable start) {
\r
84 return prototype.has(index, start);
\r
87 public Object get(String id, Scriptable start) {
\r
90 return prototype.get(id, start);
\r
93 public Object get(int index, Scriptable start) {
\r
96 return prototype.get(index, start);
\r
99 public void put(String id, Scriptable start, Object value) {
\r
102 prototype.put(id, start, value);
\r
105 public void put(int index, Scriptable start, Object value) {
\r
108 prototype.put(index, start, value);
\r
111 public void delete(String id) {
\r
112 prototype.delete(id);
\r
115 public void delete(int index) {
\r
116 prototype.delete(index);
\r
119 public Scriptable getPrototype() {
\r
123 public void setPrototype(Scriptable prototype) {
\r
124 this.prototype = prototype;
\r
127 public Scriptable getParentScope() {
\r
131 public void setParentScope(Scriptable parent) {
\r
132 this.parent = parent;
\r
135 public Object[] getIds() {
\r
136 return prototype.getIds();
\r
139 public Object getDefaultValue(Class typeHint) {
\r
140 return prototype.getDefaultValue(typeHint);
\r
143 public boolean hasInstance(Scriptable value) {
\r
144 return prototype.hasInstance(value);
\r
147 public Object execMethod(int methodId, IdFunction function, Context cx,
\r
148 Scriptable scope, Scriptable thisObj,
\r
150 throws JavaScriptException
\r
152 if (prototypeFlag) {
\r
153 if (methodId == Id_constructor) {
\r
154 throw Context.reportRuntimeError1
\r
155 ("msg.cant.call.indirect", "With");
\r
158 throw IdFunction.onBadMethodId(this, methodId);
\r
161 public int methodArity(int methodId) {
\r
162 if (prototypeFlag) {
\r
163 if (methodId == Id_constructor) { return 0; }
\r
168 public static Object newWithSpecial(Context cx, Object[] args,
\r
169 Function ctorObj, boolean inNewExpr)
\r
172 throw Context.reportRuntimeError1("msg.only.from.new", "With");
\r
175 ScriptRuntime.checkDeprecated(cx, "With");
\r
177 Scriptable scope = ScriptableObject.getTopLevelScope(ctorObj);
\r
178 NativeWith thisObj = new NativeWith();
\r
179 thisObj.setPrototype(args.length == 0
\r
180 ? ScriptableObject.getClassPrototype(scope,
\r
182 : ScriptRuntime.toObject(scope, args[0]));
\r
183 thisObj.setParentScope(scope);
\r
187 private static final int
\r
188 Id_constructor = 1;
\r
190 private Scriptable prototype;
\r
191 private Scriptable parent;
\r
192 private Scriptable constructor;
\r
194 private boolean prototypeFlag;
\r