Skip to content
Snippets Groups Projects
Commit fe130de5 authored by rswindell's avatar rswindell
Browse files

Bugfix in new read/compile block (was always reading script from stdin).

parent 4a1c0ca1
No related branches found
No related tags found
No related merge requests found
...@@ -498,6 +498,7 @@ long js_exec(const char *fname, char** args) ...@@ -498,6 +498,7 @@ long js_exec(const char *fname, char** args)
JSScript* js_script=NULL; JSScript* js_script=NULL;
JSString* arg; JSString* arg;
JSObject* argv; JSObject* argv;
FILE* fp=stdin;
jsval val; jsval val;
jsval rval=JSVAL_VOID; jsval rval=JSVAL_VOID;
int32 result=0; int32 result=0;
...@@ -514,6 +515,11 @@ long js_exec(const char *fname, char** args) ...@@ -514,6 +515,11 @@ long js_exec(const char *fname, char** args)
fprintf(errfp,"!Module file (%s) doesn't exist\n",path); fprintf(errfp,"!Module file (%s) doesn't exist\n",path);
return(-1); return(-1);
} }
if((fp=fopen(path,"r"))==NULL) {
fprintf(errfp,"!Error %d (%s) opening %s\n",errno,STRERROR(errno),path);
return(-1);
}
} }
JS_ClearPendingException(js_cx); JS_ClearPendingException(js_cx);
...@@ -541,13 +547,14 @@ long js_exec(const char *fname, char** args) ...@@ -541,13 +547,14 @@ long js_exec(const char *fname, char** args)
JS_SetBranchCallback(js_cx, js_BranchCallback); JS_SetBranchCallback(js_cx, js_BranchCallback);
if(fname==NULL) /* Use stdin for script source */ if(fp==stdin) /* Using stdin for script source */
SAFECOPY(path,"stdin"); SAFECOPY(path,"stdin");
fprintf(statfp,"Reading script from %s\n",path); fprintf(statfp,"Reading script from %s\n",path);
line_no=0; line_no=0;
js_buflen=0; js_buflen=0;
while(!feof(stdin)) { while(!feof(fp)) {
if(!fgets(line,sizeof(line),stdin)) if(!fgets(line,sizeof(line),fp))
break; break;
line_no++; line_no++;
#ifdef __unix__ /* Support Unix Shell Scripts that start with #!/path/to/jsexec */ #ifdef __unix__ /* Support Unix Shell Scripts that start with #!/path/to/jsexec */
...@@ -563,7 +570,7 @@ long js_exec(const char *fname, char** args) ...@@ -563,7 +570,7 @@ long js_exec(const char *fname, char** args)
js_buflen+=strlen(line); js_buflen+=strlen(line);
} }
if((js_script=JS_CompileScript(js_cx, js_scope, js_buf, js_buflen, fname==NULL ? NULL : path, 1))==NULL) { if((js_script=JS_CompileScript(js_cx, js_scope, js_buf, js_buflen, fname==NULL ? NULL : path, 1))==NULL) {
fprintf(errfp,"!Error compiling script from stdin\n"); fprintf(errfp,"!Error compiling script from %s\n",path);
return(-1); return(-1);
} }
JS_ExecuteScript(js_cx, js_scope, js_script, &rval); JS_ExecuteScript(js_cx, js_scope, js_script, &rval);
...@@ -581,6 +588,12 @@ long js_exec(const char *fname, char** args) ...@@ -581,6 +588,12 @@ long js_exec(const char *fname, char** args)
JS_GC(js_cx); JS_GC(js_cx);
if(fp!=NULL && fp!=stdin)
fclose(fp);
if(js_buf!=NULL)
free(js_buf);
return(result); return(result);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment