Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 9548773

Browse files
committed
strip hashbang statements before compiling JS source
1 parent 819a02a commit 9548773

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/org/mozilla/javascript/Context.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,30 @@ static Context getContext()
23052305
return cx;
23062306
}
23072307

2308+
/**
2309+
* Replaces the leading hashbang statement, if present, with a blank line.
2310+
*
2311+
* @param sourceString The JavaScript source to process.
2312+
* @return The source without a leading hashbang.
2313+
*/
2314+
public static String removeHashbang(String sourceString)
2315+
{
2316+
String modifiedSourceString = new String(sourceString);
2317+
// Support the executable script #! syntax: If the first line begins
2318+
// with a '#', treat the whole line as a comment.
2319+
if (modifiedSourceString.length() > 0 && modifiedSourceString.charAt(0) == '#') {
2320+
for (int i = 1; i != modifiedSourceString.length(); ++i) {
2321+
int c = modifiedSourceString.charAt(i);
2322+
if (c == '\n' || c == '\r') {
2323+
modifiedSourceString = modifiedSourceString.substring(i);
2324+
break;
2325+
}
2326+
}
2327+
}
2328+
2329+
return modifiedSourceString;
2330+
}
2331+
23082332
private Object compileImpl(Scriptable scope,
23092333
Reader sourceReader, String sourceString,
23102334
String sourceName, int lineno,
@@ -2332,7 +2356,9 @@ private Object compileImpl(Scriptable scope,
23322356
compilationErrorReporter = compilerEnv.getErrorReporter();
23332357
}
23342358

2335-
if (debugger != null) {
2359+
// always load the source here so we can strip the hashbang
2360+
//if (debugger != null) {
2361+
if (true) {
23362362
if (sourceReader != null) {
23372363
sourceString = Kit.readReader(sourceReader);
23382364
sourceReader = null;
@@ -2345,6 +2371,7 @@ private Object compileImpl(Scriptable scope,
23452371
}
23462372
AstRoot ast;
23472373
if (sourceString != null) {
2374+
sourceString = removeHashbang(sourceString);
23482375
ast = p.parse(sourceString, sourceName, lineno);
23492376
} else {
23502377
ast = p.parse(sourceReader, sourceName, lineno);

0 commit comments

Comments
 (0)