~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/InterpretedFunction.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 
 *
3
 
 * The contents of this file are subject to the Netscape Public
4
 
 * License Version 1.1 (the "License"); you may not use this file
5
 
 * except in compliance with the License. You may obtain a copy of
6
 
 * the License at http://www.mozilla.org/NPL/
7
 
 *
8
 
 * Software distributed under the License is distributed on an "AS
9
 
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10
 
 * implied. See the License for the specific language governing
11
 
 * rights and limitations under the License.
12
 
 *
13
 
 * The Original Code is Rhino code, released
14
 
 * May 6, 1999.
15
 
 *
16
 
 * The Initial Developer of the Original Code is Netscape
17
 
 * Communications Corporation.  Portions created by Netscape are
18
 
 * Copyright (C) 1997-2000 Netscape Communications Corporation. All
19
 
 * Rights Reserved.
20
 
 *
21
 
 * Contributor(s): 
22
 
 * Roger Lawrence
23
 
 *
24
 
 * Alternatively, the contents of this file may be used under the
25
 
 * terms of the GNU Public License (the "GPL"), in which case the
26
 
 * provisions of the GPL are applicable instead of those above.
27
 
 * If you wish to allow use of your version of this file only
28
 
 * under the terms of the GPL and not to allow others to use your
29
 
 * version of this file under the NPL, indicate your decision by
30
 
 * deleting the provisions above and replace them with the notice
31
 
 * and other provisions required by the GPL.  If you do not delete
32
 
 * the provisions above, a recipient may use your version of this
33
 
 * file under either the NPL or the GPL.
34
 
 */
35
 
 
36
 
package org.mozilla.javascript;
37
 
 
38
 
import java.util.*;
39
 
import org.mozilla.javascript.debug.DebuggableScript;
40
 
 
41
 
class InterpretedFunction extends NativeFunction implements DebuggableScript {
42
 
    
43
 
    InterpretedFunction(Context cx,
44
 
                        InterpreterData theData, 
45
 
                        String[] argNames, short argCount)
46
 
    {
47
 
        itsData = theData;
48
 
        this.argNames = argNames;
49
 
        this.argCount = argCount;
50
 
        init(cx);
51
 
    }
52
 
    
53
 
    void init(Context cx)
54
 
    {
55
 
        functionName = itsData.itsName;
56
 
        source = itsData.itsSource;
57
 
        nestedFunctions = itsData.itsNestedFunctions;
58
 
        if (cx != null)
59
 
            version = (short)cx.getLanguageVersion();
60
 
    }
61
 
    
62
 
    InterpretedFunction(InterpretedFunction theOther,
63
 
                        Scriptable theScope, Context cx)
64
 
    {
65
 
        itsData = theOther.itsData;
66
 
        this.argNames = theOther.argNames;
67
 
        this.argCount = theOther.argCount;
68
 
        itsClosure = theScope;
69
 
        init(cx);
70
 
    }
71
 
    
72
 
    public Object call(Context cx, Scriptable scope, Scriptable thisObj,
73
 
                       Object[] args)
74
 
        throws JavaScriptException
75
 
    {            
76
 
        if (itsClosure != null)
77
 
            scope = itsClosure;
78
 
        else if (!itsData.itsUseDynamicScope)
79
 
            scope = getParentScope();
80
 
 
81
 
        if (itsData.itsCheckThis) 
82
 
            thisObj = ScriptRuntime.getThis(thisObj);
83
 
        
84
 
        if (itsData.itsNeedsActivation) {
85
 
            scope = ScriptRuntime.initVarObj(cx, scope, this, thisObj, args);
86
 
        }
87
 
        try {
88
 
            return Interpreter.interpret(cx, scope, thisObj, args, this,
89
 
                                         itsData);
90
 
        }
91
 
        finally {
92
 
            if (itsData.itsNeedsActivation) {
93
 
                ScriptRuntime.popActivation(cx);
94
 
            }
95
 
        }
96
 
    }
97
 
    
98
 
    public boolean isFunction() {
99
 
        return true;
100
 
    }
101
 
    
102
 
    public Scriptable getScriptable() {
103
 
        return this;
104
 
    }
105
 
    
106
 
    public String getSourceName() {
107
 
        return itsData.itsSourceFile;
108
 
    }
109
 
    
110
 
    public int[] getLineNumbers() { 
111
 
        return itsData.itsLineNumberTable.getKeys();
112
 
    }
113
 
    
114
 
    public boolean placeBreakpoint(int line) { // XXX throw exn?
115
 
        return itsData.placeBreakpoint(line);
116
 
    }
117
 
    
118
 
    public boolean removeBreakpoint(int line) {
119
 
        return itsData.removeBreakpoint(line);
120
 
    }
121
 
    
122
 
    InterpreterData itsData;
123
 
    Scriptable itsClosure;
124
 
}
125
 
    
 
1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is Rhino code, released
 
14
 * May 6, 1999.
 
15
 *
 
16
 * The Initial Developer of the Original Code is Netscape
 
17
 * Communications Corporation.  Portions created by Netscape are
 
18
 * Copyright (C) 1997-2000 Netscape Communications Corporation. All
 
19
 * Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Igor Bukanov
 
23
 * Roger Lawrence
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the
 
26
 * terms of the GNU Public License (the "GPL"), in which case the
 
27
 * provisions of the GPL are applicable instead of those above.
 
28
 * If you wish to allow use of your version of this file only
 
29
 * under the terms of the GPL and not to allow others to use your
 
30
 * version of this file under the NPL, indicate your decision by
 
31
 * deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL.  If you do not delete
 
33
 * the provisions above, a recipient may use your version of this
 
34
 * file under either the NPL or the GPL.
 
35
 */
 
36
 
 
37
package org.mozilla.javascript;
 
38
 
 
39
import org.mozilla.javascript.debug.DebuggableScript;
 
40
 
 
41
final class InterpretedFunction extends NativeFunction implements Script
 
42
{
 
43
    InterpreterData idata;
 
44
    SecurityController securityController;
 
45
    Object securityDomain;
 
46
    Scriptable[] functionRegExps;
 
47
 
 
48
    private InterpretedFunction(InterpreterData idata,
 
49
                                Object staticSecurityDomain)
 
50
    {
 
51
        this.idata = idata;
 
52
 
 
53
        // Always get Context from the current thread to
 
54
        // avoid security breaches via passing mangled Context instances
 
55
        // with bogus SecurityController
 
56
        Context cx = Context.getContext();
 
57
        SecurityController sc = cx.getSecurityController();
 
58
        Object dynamicDomain;
 
59
        if (sc != null) {
 
60
            dynamicDomain = sc.getDynamicSecurityDomain(staticSecurityDomain);
 
61
        } else {
 
62
            if (staticSecurityDomain != null) {
 
63
                throw new IllegalArgumentException();
 
64
            }
 
65
            dynamicDomain = null;
 
66
        }
 
67
 
 
68
        this.securityController = sc;
 
69
        this.securityDomain = dynamicDomain;
 
70
    }
 
71
 
 
72
    private InterpretedFunction(InterpretedFunction parent, int index)
 
73
    {
 
74
        this.idata = parent.idata.itsNestedFunctions[index];
 
75
        this.securityController = parent.securityController;
 
76
        this.securityDomain = parent.securityDomain;
 
77
    }
 
78
 
 
79
    /**
 
80
     * Create script from compiled bytecode.
 
81
     */
 
82
    static InterpretedFunction createScript(InterpreterData idata,
 
83
                                            Object staticSecurityDomain)
 
84
    {
 
85
        InterpretedFunction f;
 
86
        f = new InterpretedFunction(idata, staticSecurityDomain);
 
87
        f.initScriptObject();
 
88
        return f;
 
89
    }
 
90
 
 
91
    /**
 
92
     * Create function compiled from Function(...) constructor.
 
93
     */
 
94
    static InterpretedFunction createFunction(Context cx,Scriptable scope,
 
95
                                              InterpreterData idata,
 
96
                                              Object staticSecurityDomain)
 
97
    {
 
98
        InterpretedFunction f;
 
99
        f = new InterpretedFunction(idata, staticSecurityDomain);
 
100
        f.initInterpretedFunction(cx, scope);
 
101
        return f;
 
102
    }
 
103
 
 
104
    /**
 
105
     * Create function embedded in script or another function.
 
106
     */
 
107
    static InterpretedFunction createFunction(Context cx, Scriptable scope,
 
108
                                              InterpretedFunction  parent,
 
109
                                              int index)
 
110
    {
 
111
        InterpretedFunction f = new InterpretedFunction(parent, index);
 
112
        f.initInterpretedFunction(cx, scope);
 
113
        return f;
 
114
    }
 
115
 
 
116
    Scriptable[] createRegExpWraps(Context cx, Scriptable scope)
 
117
    {
 
118
        if (idata.itsRegExpLiterals == null) Kit.codeBug();
 
119
 
 
120
        RegExpProxy rep = ScriptRuntime.checkRegExpProxy(cx);
 
121
        int N = idata.itsRegExpLiterals.length;
 
122
        Scriptable[] array = new Scriptable[N];
 
123
        for (int i = 0; i != N; ++i) {
 
124
            array[i] = rep.wrapRegExp(cx, scope, idata.itsRegExpLiterals[i]);
 
125
        }
 
126
        return array;
 
127
    }
 
128
 
 
129
    private void initInterpretedFunction(Context cx, Scriptable scope)
 
130
    {
 
131
        initScriptFunction(cx, scope, idata.itsName);
 
132
        if (idata.itsRegExpLiterals != null) {
 
133
            functionRegExps = createRegExpWraps(cx, scope);
 
134
        }
 
135
    }
 
136
 
 
137
    public Object call(Context cx, Scriptable scope, Scriptable thisObj,
 
138
                       Object[] args)
 
139
    {
 
140
        return Interpreter.interpret(this, cx, scope, thisObj, args);
 
141
    }
 
142
 
 
143
    public Object exec(Context cx, Scriptable scope)
 
144
    {
 
145
        if (idata.itsFunctionType != 0) {
 
146
            // Can only be applied to scripts
 
147
            throw new IllegalStateException();
 
148
        }
 
149
        return call(cx, scope, scope, ScriptRuntime.emptyArgs);
 
150
    }
 
151
 
 
152
    public String getEncodedSource()
 
153
    {
 
154
        return Interpreter.getEncodedSource(idata);
 
155
    }
 
156
 
 
157
    public DebuggableScript getDebuggableView()
 
158
    {
 
159
        return idata;
 
160
    }
 
161
 
 
162
    protected int getLanguageVersion()
 
163
    {
 
164
        return idata.languageVersion;
 
165
    }
 
166
 
 
167
    protected int getParamCount()
 
168
    {
 
169
        return idata.argCount;
 
170
    }
 
171
 
 
172
    protected int getParamAndVarCount()
 
173
    {
 
174
        return idata.argNames.length;
 
175
    }
 
176
 
 
177
    protected String getParamOrVarName(int index)
 
178
    {
 
179
        return idata.argNames[index];
 
180
    }
 
181
 
 
182
}
 
183