Issue 117: Skip first line if it starts with a shebang when loading files.

A file that starts with #! is going to be a syntax error anyway, so we
won't be changing the behavior of any other valid source files with this
fix.
This commit is contained in:
Tor Andersson
2020-01-02 14:30:43 +01:00
parent 457f87b17b
commit c695b53a75

View File

@@ -130,7 +130,7 @@ void js_loadstring(js_State *J, const char *filename, const char *source)
void js_loadfile(js_State *J, const char *filename)
{
FILE *f;
char *s;
char *s, *p;
int n, t;
f = fopen(filename, "rb");
@@ -176,7 +176,15 @@ void js_loadfile(js_State *J, const char *filename)
js_throw(J);
}
js_loadstring(J, filename, s);
/* skip first line if it starts with "#!" */
p = s;
if (p[0] == '#' && p[1] == '!') {
p += 2;
while (*p && *p != '\n')
++p;
}
js_loadstring(J, filename, p);
js_free(J, s);
fclose(f);