16147
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.nashorn.internal.runtime;
|
|
27 |
|
|
28 |
import java.io.ByteArrayOutputStream;
|
|
29 |
import java.io.File;
|
|
30 |
import java.io.IOError;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.Reader;
|
|
34 |
import java.net.MalformedURLException;
|
|
35 |
import java.net.URISyntaxException;
|
|
36 |
import java.net.URL;
|
|
37 |
import java.nio.charset.Charset;
|
|
38 |
import java.nio.charset.StandardCharsets;
|
|
39 |
import java.nio.file.Files;
|
|
40 |
import java.nio.file.Path;
|
|
41 |
import java.nio.file.Paths;
|
|
42 |
import java.util.Arrays;
|
|
43 |
import java.util.Objects;
|
|
44 |
import jdk.nashorn.internal.parser.Token;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Source objects track the origin of JavaScript entities.
|
|
48 |
*
|
|
49 |
*/
|
|
50 |
public final class Source {
|
|
51 |
/**
|
|
52 |
* Descriptive name of the source as supplied by the user. Used for error
|
|
53 |
* reporting to the user. For example, SyntaxError will use this to print message.
|
|
54 |
* Used to implement __FILE__. Also used for SourceFile in .class for debugger usage.
|
|
55 |
*/
|
|
56 |
private final String name;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Base directory the File or base part of the URL. Used to implement __DIR__.
|
|
60 |
* Used to load scripts relative to the 'directory' or 'base' URL of current script.
|
|
61 |
* This will be null when it can't be computed.
|
|
62 |
*/
|
|
63 |
private final String base;
|
|
64 |
|
|
65 |
/** Cached source content. */
|
|
66 |
private final char[] content;
|
|
67 |
|
|
68 |
/** Length of source content. */
|
|
69 |
private final int length;
|
|
70 |
|
|
71 |
/** Cached hash code */
|
|
72 |
private int hash;
|
|
73 |
|
|
74 |
private static final int BUFSIZE = 8 * 1024;
|
|
75 |
|
|
76 |
private Source(final String name, final String base, final char[] content) {
|
|
77 |
this.name = name;
|
|
78 |
this.base = base;
|
|
79 |
this.content = content;
|
|
80 |
this.length = content.length;
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Constructor
|
|
85 |
*
|
|
86 |
* @param name source name
|
|
87 |
* @param content contents as char array
|
|
88 |
*/
|
|
89 |
public Source(final String name, final char[] content) {
|
|
90 |
this(name, baseName(name, null), content);
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Constructor
|
|
95 |
*
|
|
96 |
* @param name source name
|
|
97 |
* @param content contents as string
|
|
98 |
*/
|
|
99 |
public Source(final String name, final String content) {
|
|
100 |
this(name, content.toCharArray());
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Constructor
|
|
105 |
*
|
|
106 |
* @param name source name
|
|
107 |
* @param url url from which source can be loaded
|
|
108 |
*
|
|
109 |
* @throws IOException if source cannot be loaded
|
|
110 |
*/
|
|
111 |
public Source(final String name, final URL url) throws IOException {
|
|
112 |
this(name, baseURL(url, null), readFully(url.openStream()));
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Constructor
|
|
117 |
*
|
|
118 |
* @param name source name
|
|
119 |
* @param file file from which source can be loaded
|
|
120 |
*
|
|
121 |
* @throws IOException if source cannot be loaded
|
|
122 |
*/
|
|
123 |
public Source(final String name, final File file) throws IOException {
|
|
124 |
this(name, dirName(file, null), readFully(file));
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public boolean equals(final Object obj) {
|
|
129 |
if (this == obj) {
|
|
130 |
return true;
|
|
131 |
}
|
|
132 |
|
|
133 |
if (!(obj instanceof Source)) {
|
|
134 |
return false;
|
|
135 |
}
|
|
136 |
|
|
137 |
final Source src = (Source)obj;
|
|
138 |
// Only compare content as a last resort measure
|
|
139 |
return length == src.length && Objects.equals(name, src.name) && Arrays.equals(content, src.content);
|
|
140 |
}
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public int hashCode() {
|
|
144 |
int h = hash;
|
|
145 |
if (h == 0) {
|
|
146 |
h = hash = Arrays.hashCode(content) ^ Objects.hashCode(name);
|
|
147 |
}
|
|
148 |
return h;
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Fetch source content.
|
|
153 |
* @return Source content.
|
|
154 |
*/
|
|
155 |
public String getString() {
|
|
156 |
return new String(content, 0, length);
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Get the user supplied name of this script.
|
|
161 |
* @return User supplied source name.
|
|
162 |
*/
|
|
163 |
public String getName() {
|
|
164 |
return name;
|
|
165 |
}
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Get the "directory" part of the file or "base" of the URL.
|
|
169 |
* @return base of file or URL.
|
|
170 |
*/
|
|
171 |
public String getBase() {
|
|
172 |
return base;
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Fetch a portion of source content.
|
|
177 |
* @param start start index in source
|
|
178 |
* @param len length of portion
|
|
179 |
* @return Source content portion.
|
|
180 |
*/
|
|
181 |
public String getString(final int start, final int len) {
|
|
182 |
return new String(content, start, len);
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Fetch a portion of source content associated with a token.
|
|
187 |
* @param token Token descriptor.
|
|
188 |
* @return Source content portion.
|
|
189 |
*/
|
|
190 |
public String getString(final long token) {
|
|
191 |
final int start = Token.descPosition(token);
|
|
192 |
final int len = Token.descLength(token);
|
|
193 |
return new String(content, start, len);
|
|
194 |
}
|
|
195 |
|
|
196 |
/**
|
|
197 |
* Fetch a portion of source content associated with a range of tokens.
|
|
198 |
* @param startTokenDesc Starting token descriptor.
|
|
199 |
* @param endTokenDesc Ending token descriptor.
|
|
200 |
* @return Source content portion.
|
|
201 |
*/
|
|
202 |
public String getString(final long startTokenDesc, final long endTokenDesc) {
|
|
203 |
final int start = Token.descPosition(startTokenDesc);
|
|
204 |
final int end = Token.descPosition(endTokenDesc) + Token.descLength(endTokenDesc);
|
|
205 |
return new String(content, start, end - start);
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Find the beginning of the line containing position.
|
|
210 |
* @param position Index to offending token.
|
|
211 |
* @return Index of first character of line.
|
|
212 |
*/
|
|
213 |
private int findBOLN(final int position) {
|
|
214 |
for (int i = position - 1; i > 0; i--) {
|
|
215 |
final char ch = content[i];
|
|
216 |
|
|
217 |
if (ch == '\n' || ch == '\r') {
|
|
218 |
return i + 1;
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
return 0;
|
|
223 |
}
|
|
224 |
|
|
225 |
/**
|
|
226 |
* Find the end of the line containing position.
|
|
227 |
* @param position Index to offending token.
|
|
228 |
* @return Index of last character of line.
|
|
229 |
*/
|
|
230 |
private int findEOLN(final int position) {
|
|
231 |
for (int i = position; i < length; i++) {
|
|
232 |
final char ch = content[i];
|
|
233 |
|
|
234 |
if (ch == '\n' || ch == '\r') {
|
|
235 |
return i - 1;
|
|
236 |
}
|
|
237 |
}
|
|
238 |
|
|
239 |
return length - 1;
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Return line number of character position.
|
|
244 |
* @param position Position of character in source content.
|
|
245 |
* @return Line number.
|
|
246 |
*/
|
|
247 |
public int getLine(final int position) {
|
|
248 |
// Line count starts at 1.
|
|
249 |
int line = 1;
|
|
250 |
|
|
251 |
for (int i = 0; i < position; i++) {
|
|
252 |
final char ch = content[i];
|
|
253 |
// Works for both \n and \r\n.
|
|
254 |
if (ch == '\n') {
|
|
255 |
line++;
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
return line;
|
|
260 |
}
|
|
261 |
|
|
262 |
/**
|
|
263 |
* Return column number of character position.
|
|
264 |
* @param position Position of character in source content.
|
|
265 |
* @return Column number.
|
|
266 |
*/
|
|
267 |
public int getColumn(final int position) {
|
|
268 |
// TODO - column needs to account for tabs.
|
|
269 |
return position - findBOLN(position);
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Return line text including character position.
|
|
274 |
* @param position Position of character in source content.
|
|
275 |
* @return Line text.
|
|
276 |
*/
|
|
277 |
public String getSourceLine(final int position) {
|
|
278 |
// Find end of previous line.
|
|
279 |
final int first = findBOLN(position);
|
|
280 |
// Find end of this line.
|
|
281 |
final int last = findEOLN(position);
|
|
282 |
|
|
283 |
return new String(content, first, last - first + 1);
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
|
287 |
* Get the content of this source as a char array
|
|
288 |
* @return content
|
|
289 |
*/
|
|
290 |
public char[] getContent() {
|
|
291 |
return content;
|
|
292 |
}
|
|
293 |
|
|
294 |
/**
|
|
295 |
* Get the length in chars for this source
|
|
296 |
* @return length
|
|
297 |
*/
|
|
298 |
public int getLength() {
|
|
299 |
return length;
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* Read all of the source until end of file. Return it as char array
|
|
304 |
*
|
|
305 |
* @param reader reader opened to source stream
|
|
306 |
* @return source as content
|
|
307 |
*
|
|
308 |
* @throws IOException if source could not be read
|
|
309 |
*/
|
|
310 |
public static char[] readFully(final Reader reader) throws IOException {
|
|
311 |
final char[] arr = new char[BUFSIZE];
|
|
312 |
final StringBuilder sb = new StringBuilder();
|
|
313 |
|
|
314 |
try {
|
|
315 |
int numChars;
|
|
316 |
while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
|
|
317 |
sb.append(arr, 0, numChars);
|
|
318 |
}
|
|
319 |
} finally {
|
|
320 |
reader.close();
|
|
321 |
}
|
|
322 |
|
|
323 |
return sb.toString().toCharArray();
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Read all of the source until end of file. Return it as char array
|
|
328 |
*
|
|
329 |
* @param file source file
|
|
330 |
* @return source as content
|
|
331 |
*
|
|
332 |
* @throws IOException if source could not be read
|
|
333 |
*/
|
|
334 |
public static char[] readFully(final File file) throws IOException {
|
|
335 |
if (!file.isFile()) {
|
|
336 |
throw new IOException(file + " is not a file"); //TODO localize?
|
|
337 |
}
|
|
338 |
return byteToCharArray(Files.readAllBytes(file.toPath()));
|
|
339 |
}
|
|
340 |
|
|
341 |
/**
|
|
342 |
* Get the base url. This is currently used for testing only
|
|
343 |
* @param url a URL
|
|
344 |
* @return base URL for url
|
|
345 |
*/
|
|
346 |
public static String baseURL(final URL url) {
|
|
347 |
return baseURL(url, null);
|
|
348 |
}
|
|
349 |
|
|
350 |
private static String baseURL(final URL url, final String defaultValue) {
|
|
351 |
if (url.getProtocol().equals("file")) {
|
|
352 |
try {
|
|
353 |
final Path path = Paths.get(url.toURI());
|
|
354 |
final Path parent = path.getParent();
|
|
355 |
return (parent != null) ? (parent + File.separator) : defaultValue;
|
|
356 |
} catch (final SecurityException | URISyntaxException | IOError e) {
|
|
357 |
return defaultValue;
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
// FIXME: is there a better way to find 'base' URL of a given URL?
|
|
362 |
String path = url.getPath();
|
|
363 |
if (path.isEmpty()) {
|
|
364 |
return defaultValue;
|
|
365 |
}
|
|
366 |
path = path.substring(0, path.lastIndexOf('/') + 1);
|
|
367 |
final int port = url.getPort();
|
|
368 |
try {
|
|
369 |
return new URL(url.getProtocol(), url.getHost(), port, path).toString();
|
|
370 |
} catch (final MalformedURLException e) {
|
|
371 |
return defaultValue;
|
|
372 |
}
|
|
373 |
}
|
|
374 |
|
|
375 |
private static String dirName(final File file, final String defaultValue) {
|
|
376 |
final String res = file.getParent();
|
|
377 |
return (res != null)? (res + File.separator) : defaultValue;
|
|
378 |
}
|
|
379 |
|
|
380 |
// fake directory like name
|
|
381 |
private static String baseName(final String name, final String defaultValue) {
|
|
382 |
int idx = name.lastIndexOf('/');
|
|
383 |
if (idx == -1) {
|
|
384 |
idx = name.lastIndexOf('\\');
|
|
385 |
}
|
|
386 |
return (idx != -1)? name.substring(0, idx + 1) : defaultValue;
|
|
387 |
}
|
|
388 |
|
|
389 |
private static char[] readFully(final InputStream is) throws IOException {
|
|
390 |
return byteToCharArray(readBytes(is));
|
|
391 |
}
|
|
392 |
|
|
393 |
private static char[] byteToCharArray(final byte[] bytes) {
|
|
394 |
Charset cs = StandardCharsets.UTF_8;
|
|
395 |
int start = 0;
|
|
396 |
// BOM detection.
|
|
397 |
if (bytes.length > 1 && bytes[0] == (byte)0xFE && bytes[1] == (byte)0xFF) {
|
|
398 |
start = 2;
|
|
399 |
cs = StandardCharsets.UTF_16BE;
|
|
400 |
} else if (bytes.length > 1 && bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE) {
|
|
401 |
start = 2;
|
|
402 |
cs = StandardCharsets.UTF_16LE;
|
|
403 |
} else if (bytes.length > 2 && bytes[0] == (byte)0xEF && bytes[1] == (byte)0xBB && bytes[2] == (byte)0xBF) {
|
|
404 |
start = 3;
|
|
405 |
cs = StandardCharsets.UTF_8;
|
|
406 |
} else if (bytes.length > 3 && bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE && bytes[2] == 0 && bytes[3] == 0) {
|
|
407 |
start = 4;
|
|
408 |
cs = Charset.forName("UTF-32LE");
|
|
409 |
} else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte)0xFE && bytes[3] == (byte)0xFF) {
|
|
410 |
start = 4;
|
|
411 |
cs = Charset.forName("UTF-32BE");
|
|
412 |
}
|
|
413 |
|
|
414 |
return new String(bytes, start, bytes.length - start, cs).toCharArray();
|
|
415 |
}
|
|
416 |
|
|
417 |
static byte[] readBytes(final InputStream is) throws IOException {
|
|
418 |
final byte[] arr = new byte[BUFSIZE];
|
|
419 |
try {
|
|
420 |
try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
|
|
421 |
int numBytes;
|
|
422 |
while ((numBytes = is.read(arr, 0, arr.length)) > 0) {
|
|
423 |
buf.write(arr, 0, numBytes);
|
|
424 |
}
|
|
425 |
return buf.toByteArray();
|
|
426 |
}
|
|
427 |
} finally {
|
|
428 |
is.close();
|
|
429 |
}
|
|
430 |
}
|
|
431 |
|
|
432 |
@Override
|
|
433 |
public String toString() {
|
|
434 |
return getName();
|
|
435 |
}
|
|
436 |
}
|