1
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Rhino code, released
19
* The Initial Developer of the Original Code is
20
* Netscape Communications Corporation.
21
* Portions created by the Initial Developer are Copyright (C) 1997-1999
22
* the Initial Developer. All Rights Reserved.
29
* Alternatively, the contents of this file may be used under the terms of
30
* the GNU General Public License Version 2 or later (the "GPL"), in which
31
* case the provisions of the GPL are applicable instead of those above. If
32
* you wish to allow use of your version of this file only under the terms of
33
* the GPL and not to allow others to use your version of this file under the
34
* MPL, indicate your decision by deleting the provisions above and replacing
35
* them with the notice and other provisions required by the GPL. If you do
36
* not delete the provisions above, a recipient may use your version of this
37
* file under either the MPL or the GPL.
39
* ***** END LICENSE BLOCK ***** */
41
package org.mozilla.javascript;
43
import java.util.ArrayList;
45
public class ScriptOrFnNode extends Node.Scope {
47
public ScriptOrFnNode(int nodeType) {
49
symbols = new ArrayList(4);
53
public final String getSourceName() { return sourceName; }
55
public final void setSourceName(String sourceName) {
56
this.sourceName = sourceName;
59
public final int getEncodedSourceStart() { return encodedSourceStart; }
61
public final int getEncodedSourceEnd() { return encodedSourceEnd; }
63
public final void setEncodedSourceBounds(int start, int end) {
64
this.encodedSourceStart = start;
65
this.encodedSourceEnd = end;
68
public final int getBaseLineno() { return this.lineno; }
70
public final void setBaseLineno(int lineno) {
72
if (lineno < 0 || this.lineno >= 0) Kit.codeBug();
76
public final int getEndLineno() { return endLineno; }
78
public final void setEndLineno(int lineno) {
80
if (lineno < 0 || endLineno >= 0) Kit.codeBug();
84
public final int getFunctionCount() {
85
if (functions == null) { return 0; }
86
return functions.size();
89
public final FunctionNode getFunctionNode(int i) {
90
return (FunctionNode)functions.get(i);
93
public final int addFunction(FunctionNode fnNode) {
94
if (fnNode == null) Kit.codeBug();
95
if (functions == null) { functions = new ObjArray(); }
96
functions.add(fnNode);
97
return functions.size() - 1;
100
public final int getRegexpCount() {
101
if (regexps == null) { return 0; }
102
return regexps.size() / 2;
105
public final String getRegexpString(int index) {
106
return (String)regexps.get(index * 2);
109
public final String getRegexpFlags(int index) {
110
return (String)regexps.get(index * 2 + 1);
113
/*APPJET*/public final int getRegexpLineno(int index) {
114
return (Integer)regexpLinenos.get(index);
117
public final int addRegexp(String string, String flags, /*APPJET*/
119
if (string == null) Kit.codeBug();
120
if (regexps == null) {
122
regexps = new ObjArray();
123
regexpLinenos = new ObjArray();
127
/*APPJET*/regexpLinenos.add(lineno);
128
return regexps.size() / 2 - 1;
131
public int getIndexForNameNode(Node nameNode) {
132
if (variableNames == null) throw Kit.codeBug();
133
Node.Scope node = nameNode.getScope();
134
Symbol symbol = node == null ? null
135
: node.getSymbol(nameNode.getString());
141
public final String getParamOrVarName(int index) {
142
if (variableNames == null) throw Kit.codeBug();
143
return variableNames[index];
146
public final int getParamCount() {
150
public final int getParamAndVarCount() {
151
if (variableNames == null) throw Kit.codeBug();
152
return symbols.size();
155
public final String[] getParamAndVarNames() {
156
if (variableNames == null) throw Kit.codeBug();
157
return variableNames;
160
public final boolean[] getParamAndVarConst() {
161
if (variableNames == null) throw Kit.codeBug();
165
void addSymbol(Symbol symbol) {
166
if (variableNames != null) throw Kit.codeBug();
167
if (symbol.declType == Token.LP) {
174
* Assign every symbol a unique integer index. Generate arrays of variable
175
* names and constness that can be indexed by those indices.
177
* @param flattenAllTables if true, flatten all symbol tables, included
178
* nested block scope symbol tables. If false, just flatten the script's
179
* or function's symbol table.
181
void flattenSymbolTable(boolean flattenAllTables) {
182
if (!flattenAllTables) {
183
ArrayList newSymbols = new ArrayList();
184
if (this.symbolTable != null) {
185
// Just replace "symbols" with the symbols in this object's
186
// symbol table. Can't just work from symbolTable map since
187
// we need to retain duplicate parameters.
188
for (int i=0; i < symbols.size(); i++) {
189
Symbol symbol = (Symbol) symbols.get(i);
190
if (symbol.containingTable == this) {
191
newSymbols.add(symbol);
195
symbols = newSymbols;
197
variableNames = new String[symbols.size()];
198
isConsts = new boolean[symbols.size()];
199
for (int i=0; i < symbols.size(); i++) {
200
Symbol symbol = (Symbol) symbols.get(i);
201
variableNames[i] = symbol.name;
202
isConsts[i] = symbol.declType == Token.CONST;
207
public final Object getCompilerData()
212
public final void setCompilerData(Object data)
214
if (data == null) throw new IllegalArgumentException();
215
// Can only call once
216
if (compilerData != null) throw new IllegalStateException();
220
public String getNextTempName()
222
return "$" + tempNumber++;
225
private int encodedSourceStart;
226
private int encodedSourceEnd;
227
private String sourceName;
228
private int endLineno = -1;
230
private ObjArray functions;
231
private ObjArray regexps;
232
/*APPJET*/ private ObjArray regexpLinenos;
234
private ArrayList symbols;
235
private int paramCount = 0;
236
private String[] variableNames;
237
private boolean[] isConsts;
239
private Object compilerData;
240
private int tempNumber = 0;