001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Norris Boyd
026: * Bob Jervis
027: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript;
041:
042: /**
043: * This class implements the activation object.
044: *
045: * See ECMA 10.1.6
046: *
047: * @see org.mozilla.javascript.Arguments
048: * @author Norris Boyd
049: */
050: public final class NativeCall extends IdScriptableObject {
051: static final long serialVersionUID = -7471457301304454454L;
052:
053: private static final Object CALL_TAG = new Object();
054:
055: static void init(Scriptable scope, boolean sealed) {
056: NativeCall obj = new NativeCall();
057: obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
058: }
059:
060: NativeCall() {
061: }
062:
063: NativeCall(NativeFunction function, Scriptable scope, Object[] args) {
064: this .function = function;
065:
066: setParentScope(scope);
067: // leave prototype null
068:
069: this .originalArgs = (args == null) ? ScriptRuntime.emptyArgs
070: : args;
071:
072: // initialize values of arguments
073: int paramAndVarCount = function.getParamAndVarCount();
074: int paramCount = function.getParamCount();
075: if (paramAndVarCount != 0) {
076: for (int i = 0; i < paramCount; ++i) {
077: String name = function.getParamOrVarName(i);
078: Object val = i < args.length ? args[i]
079: : Undefined.instance;
080: defineProperty(name, val, PERMANENT);
081: }
082: }
083:
084: // initialize "arguments" property but only if it was not overridden by
085: // the parameter with the same name
086: if (!super .has("arguments", this )) {
087: defineProperty("arguments", new Arguments(this ), PERMANENT);
088: }
089:
090: if (paramAndVarCount != 0) {
091: for (int i = paramCount; i < paramAndVarCount; ++i) {
092: String name = function.getParamOrVarName(i);
093: if (!super .has(name, this )) {
094: if (function.getParamOrVarConst(i))
095: defineProperty(name, Undefined.instance, CONST);
096: else
097: defineProperty(name, Undefined.instance,
098: PERMANENT);
099: }
100: }
101: }
102: }
103:
104: public String getClassName() {
105: return "Call";
106: }
107:
108: protected int findPrototypeId(String s) {
109: return s.equals("constructor") ? Id_constructor : 0;
110: }
111:
112: protected void initPrototypeId(int id) {
113: String s;
114: int arity;
115: if (id == Id_constructor) {
116: arity = 1;
117: s = "constructor";
118: } else {
119: throw new IllegalArgumentException(String.valueOf(id));
120: }
121: initPrototypeMethod(CALL_TAG, id, s, arity);
122: }
123:
124: public Object execIdCall(IdFunctionObject f, Context cx,
125: Scriptable scope, Scriptable this Obj, Object[] args) {
126: if (!f.hasTag(CALL_TAG)) {
127: return super .execIdCall(f, cx, scope, this Obj, args);
128: }
129: int id = f.methodId();
130: if (id == Id_constructor) {
131: if (this Obj != null) {
132: throw Context.reportRuntimeError1("msg.only.from.new",
133: "Call");
134: }
135: ScriptRuntime.checkDeprecated(cx, "Call");
136: NativeCall result = new NativeCall();
137: result.setPrototype(getObjectPrototype(scope));
138: return result;
139: }
140: throw new IllegalArgumentException(String.valueOf(id));
141: }
142:
143: private static final int Id_constructor = 1, MAX_PROTOTYPE_ID = 1;
144:
145: NativeFunction function;
146: Object[] originalArgs;
147:
148: transient NativeCall parentActivationCall;
149: }
|