Bug 779038 - Add an Evaluate variant which can read files. r=jimb

This commit is contained in:
Benjamin Peterson 2012-07-31 15:25:41 -07:00
parent dec43a91e0
commit c9ff152177
3 changed files with 54 additions and 20 deletions

View File

@ -5058,6 +5058,28 @@ ReadCompleteFile(JSContext *cx, FILE *fp, FileContents &buffer)
return true;
}
/*
* Open a source file for reading. Supports "-" and NULL to mean stdin. The
* return value must be fclosed unless it is stdin.
*/
static FILE *
OpenFile(JSContext *cx, const char *filename)
{
FILE *fp;
if (!filename || strcmp(filename, "-") == 0) {
fp = stdin;
} else {
fp = fopen(filename, "r");
if (!fp) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_OPEN,
filename, "No such file or directory");
return NULL;
}
}
return fp;
}
JS::CompileOptions::CompileOptions(JSContext *cx)
: principals(NULL),
originPrincipals(NULL),
@ -5123,18 +5145,10 @@ JS::Compile(JSContext *cx, HandleObject obj, CompileOptions options, FILE *fp)
JSScript *
JS::Compile(JSContext *cx, HandleObject obj, CompileOptions options, const char *filename)
{
FILE *fp;
if (!filename || strcmp(filename, "-") == 0) {
fp = stdin;
} else {
fp = fopen(filename, "r");
if (!fp) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_OPEN,
filename, "No such file or directory");
FILE *fp = OpenFile(cx, filename);
if (!fp)
return NULL;
}
}
options = options.setFileAndLine(filename, 1);
JSScript *script = Compile(cx, obj, options, fp);
if (fp != stdin)
fclose(fp);
@ -5625,6 +5639,24 @@ JS::Evaluate(JSContext *cx, HandleObject obj, CompileOptions options,
return ok;
}
extern JS_PUBLIC_API(bool)
JS::Evaluate(JSContext *cx, HandleObject obj, CompileOptions options,
const char *filename, jsval *rval)
{
FILE *fp = OpenFile(cx, filename);
if (!fp)
return NULL;
FileContents buffer(cx);
bool ok = ReadCompleteFile(cx, fp, buffer);
if (fp != stdin)
fclose(fp);
if (!ok)
return NULL;
options = options.setFileAndLine(filename, 1);
return Evaluate(cx, obj, options, buffer.begin(), buffer.length(), rval);
}
JS_PUBLIC_API(JSBool)
JS_EvaluateUCScriptForPrincipals(JSContext *cx, JSObject *obj_,
JSPrincipals *principals,

View File

@ -5138,6 +5138,10 @@ extern JS_PUBLIC_API(bool)
Evaluate(JSContext *cx, JSHandleObject obj, CompileOptions options,
const char *bytes, size_t length, jsval *rval);
extern JS_PUBLIC_API(bool)
Evaluate(JSContext *cx, JSHandleObject obj, CompileOptions options,
const char *filename, jsval *rval);
} /* namespace JS */
JS_BEGIN_EXTERN_C

View File

@ -713,16 +713,14 @@ Load(JSContext *cx, unsigned argc, jsval *vp)
if (!filename)
return false;
errno = 0;
uint32_t oldopts = JS_GetOptions(cx);
JS_SetOptions(cx, oldopts | JSOPTION_COMPILE_N_GO | JSOPTION_NO_SCRIPT_RVAL);
JSScript *script = JS_CompileUTF8File(cx, thisobj, filename.ptr());
JS_SetOptions(cx, oldopts);
if (!script)
return false;
if (!compileOnly && !JS_ExecuteScript(cx, thisobj, script, NULL))
CompileOptions opts(cx);
opts.setCompileAndGo(true).setNoScriptRval(true);
if ((compileOnly && !Compile(cx, thisobj, opts, filename.ptr())) ||
!Evaluate(cx, thisobj, opts, filename.ptr(), NULL))
{
return false;
}
}
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return true;