2002/03/21 01:19:33
[org.ibex.core.git] / src / org / mozilla / javascript / NativeCall.java
diff --git a/src/org/mozilla/javascript/NativeCall.java b/src/org/mozilla/javascript/NativeCall.java
new file mode 100644 (file)
index 0000000..fdab7b6
--- /dev/null
@@ -0,0 +1,171 @@
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
+ *\r
+ * The contents of this file are subject to the Netscape Public\r
+ * License Version 1.1 (the "License"); you may not use this file\r
+ * except in compliance with the License. You may obtain a copy of\r
+ * the License at http://www.mozilla.org/NPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS\r
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
+ * implied. See the License for the specific language governing\r
+ * rights and limitations under the License.\r
+ *\r
+ * The Original Code is Rhino code, released\r
+ * May 6, 1999.\r
+ *\r
+ * The Initial Developer of the Original Code is Netscape\r
+ * Communications Corporation.  Portions created by Netscape are\r
+ * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
+ * Rights Reserved.\r
+ *\r
+ * Contributor(s): \r
+ * Norris Boyd\r
+ *\r
+ * Alternatively, the contents of this file may be used under the\r
+ * terms of the GNU Public License (the "GPL"), in which case the\r
+ * provisions of the GPL are applicable instead of those above.\r
+ * If you wish to allow use of your version of this file only\r
+ * under the terms of the GPL and not to allow others to use your\r
+ * version of this file under the NPL, indicate your decision by\r
+ * deleting the provisions above and replace them with the notice\r
+ * and other provisions required by the GPL.  If you do not delete\r
+ * the provisions above, a recipient may use your version of this\r
+ * file under either the NPL or the GPL.\r
+ */\r
+\r
+package org.mozilla.javascript;\r
+\r
+/**\r
+ * This class implements the activation object.\r
+ *\r
+ * See ECMA 10.1.6\r
+ *\r
+ * @see org.mozilla.javascript.Arguments\r
+ * @author Norris Boyd\r
+ */\r
+public final class NativeCall extends IdScriptable {\r
+\r
+    static void init(Context cx, Scriptable scope, boolean sealed) {\r
+        NativeCall obj = new NativeCall();\r
+        obj.prototypeFlag = true;\r
+        obj.addAsPrototype(MAX_PROTOTYPE_ID, cx, scope, sealed);\r
+    }\r
+\r
+    NativeCall(Context cx, Scriptable scope, NativeFunction funObj, \r
+               Scriptable thisObj, Object[] args)\r
+    {\r
+        this.funObj = funObj;\r
+        this.thisObj = thisObj;\r
+        \r
+        setParentScope(scope);\r
+        // leave prototype null\r
+        \r
+        // save current activation\r
+        this.caller = cx.currentActivation;\r
+        cx.currentActivation = this;\r
+\r
+        this.originalArgs = (args == null) ? ScriptRuntime.emptyArgs : args;\r
+        \r
+        // initialize values of arguments\r
+        String[] argNames = funObj.argNames;\r
+        if (argNames != null) {\r
+            for (int i=0; i < funObj.argCount; i++) {\r
+                Object val = i < args.length ? args[i] \r
+                                             : Undefined.instance;\r
+                super.put(argNames[i], this, val);\r
+            }\r
+        }\r
+        \r
+        // initialize "arguments" property\r
+        super.put("arguments", this, new Arguments(this));\r
+    }\r
+    \r
+    private NativeCall() {\r
+    }\r
+\r
+    public String getClassName() {\r
+        return "Call";\r
+    }\r
+    \r
+    private static Object jsConstructor(Context cx, Object[] args, \r
+                                        Function ctorObj, boolean inNewExpr)\r
+    {\r
+        if (!inNewExpr) {\r
+            throw Context.reportRuntimeError1("msg.only.from.new", "Call");\r
+        }\r
+        ScriptRuntime.checkDeprecated(cx, "Call");\r
+        NativeCall result = new NativeCall();\r
+        result.setPrototype(getObjectPrototype(ctorObj));\r
+        return result;\r
+    }\r
+    \r
+    NativeCall getActivation(Function f) {\r
+        NativeCall x = this;\r
+        do {\r
+            if (x.funObj == f)\r
+                return x;\r
+            x = x.caller;\r
+        } while (x != null);\r
+        return null;\r
+    }\r
+        \r
+    public Function getFunctionObject() {\r
+        return funObj;\r
+    }\r
+\r
+    public Object[] getOriginalArguments() {\r
+        return originalArgs;\r
+    }\r
+    \r
+    public NativeCall getCaller() {\r
+        return caller;\r
+    }\r
+        \r
+    public Scriptable getThisObj() {\r
+        return thisObj;\r
+    }\r
+    \r
+    public int methodArity(int methodId) {\r
+        if (prototypeFlag) {\r
+            if (methodId == Id_constructor) return 1;\r
+        }\r
+        return super.methodArity(methodId);\r
+    }\r
+\r
+    public Object execMethod\r
+        (int methodId, IdFunction f,\r
+         Context cx, Scriptable scope, Scriptable thisObj, Object[] args)\r
+        throws JavaScriptException\r
+    {\r
+        if (prototypeFlag) {\r
+            if (methodId == Id_constructor) {\r
+                return jsConstructor(cx, args, f, thisObj == null);\r
+            }\r
+        }\r
+        return super.execMethod(methodId, f, cx, scope, thisObj, args);\r
+    }\r
+\r
+    protected String getIdName(int id) {\r
+        if (prototypeFlag) {\r
+            if (id == Id_constructor) return "constructor";\r
+        }\r
+        return null;        \r
+    }\r
+    \r
+    protected int mapNameToId(String s) {\r
+        if (!prototypeFlag) { return 0; }\r
+        return s.equals("constructor") ? Id_constructor : 0;\r
+    }\r
+\r
+    private static final int\r
+        Id_constructor   = 1,\r
+        MAX_PROTOTYPE_ID = 1;\r
+\r
+    NativeCall caller;\r
+    NativeFunction funObj;\r
+    Scriptable thisObj;\r
+    Object[] originalArgs;\r
+    public int debugPC;\r
+\r
+    private boolean prototypeFlag;\r
+}\r