Add a "read" extension to the shell programs. This global function
reads the contents of a file into a string and returns it. Review URL: http://codereview.chromium.org/67262 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1741 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
6290e19716
commit
8775634598
@ -38,6 +38,7 @@ bool ExecuteString(v8::Handle<v8::String> source,
|
||||
bool print_result,
|
||||
bool report_exceptions);
|
||||
v8::Handle<v8::Value> Print(const v8::Arguments& args);
|
||||
v8::Handle<v8::Value> Read(const v8::Arguments& args);
|
||||
v8::Handle<v8::Value> Load(const v8::Arguments& args);
|
||||
v8::Handle<v8::Value> Quit(const v8::Arguments& args);
|
||||
v8::Handle<v8::Value> Version(const v8::Arguments& args);
|
||||
@ -52,6 +53,8 @@ int RunMain(int argc, char* argv[]) {
|
||||
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
|
||||
// Bind the global 'print' function to the C++ Print callback.
|
||||
global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
|
||||
// Bind the global 'read' function to the C++ Read callback.
|
||||
global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
|
||||
// Bind the global 'load' function to the C++ Load callback.
|
||||
global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load));
|
||||
// Bind the 'quit' function
|
||||
@ -135,6 +138,25 @@ v8::Handle<v8::Value> Print(const v8::Arguments& args) {
|
||||
}
|
||||
|
||||
|
||||
// The callback that is invoked by v8 whenever the JavaScript 'read'
|
||||
// function is called. This function loads the content of the file named in
|
||||
// the argument into a JavaScript string.
|
||||
v8::Handle<v8::Value> Read(const v8::Arguments& args) {
|
||||
if (args.Length() != 1) {
|
||||
return v8::ThrowException(v8::String::New("Bad parameters"));
|
||||
}
|
||||
v8::String::Utf8Value file(args[0]);
|
||||
if (*file == NULL) {
|
||||
return v8::ThrowException(v8::String::New("Error loading file"));
|
||||
}
|
||||
v8::Handle<v8::String> source = ReadFile(*file);
|
||||
if (source.IsEmpty()) {
|
||||
return v8::ThrowException(v8::String::New("Error loading file"));
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
// The callback that is invoked by v8 whenever the JavaScript 'load'
|
||||
// function is called. Loads, compiles and executes its argument
|
||||
// JavaScript file.
|
||||
|
19
src/d8.cc
19
src/d8.cc
@ -163,6 +163,22 @@ Handle<Value> Shell::Print(const Arguments& args) {
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Shell::Read(const Arguments& args) {
|
||||
if (args.Length() != 1) {
|
||||
return ThrowException(String::New("Bad parameters"));
|
||||
}
|
||||
String::Utf8Value file(args[0]);
|
||||
if (*file == NULL) {
|
||||
return ThrowException(String::New("Error loading file"));
|
||||
}
|
||||
Handle<String> source = ReadFile(*file);
|
||||
if (source.IsEmpty()) {
|
||||
return ThrowException(String::New("Error loading file"));
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
Handle<Value> Shell::Load(const Arguments& args) {
|
||||
for (int i = 0; i < args.Length(); i++) {
|
||||
HandleScope handle_scope;
|
||||
@ -381,6 +397,7 @@ void Shell::Initialize() {
|
||||
HandleScope scope;
|
||||
Handle<ObjectTemplate> global_template = ObjectTemplate::New();
|
||||
global_template->Set(String::New("print"), FunctionTemplate::New(Print));
|
||||
global_template->Set(String::New("read"), FunctionTemplate::New(Read));
|
||||
global_template->Set(String::New("load"), FunctionTemplate::New(Load));
|
||||
global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
|
||||
global_template->Set(String::New("version"), FunctionTemplate::New(Version));
|
||||
@ -555,6 +572,8 @@ void ShellThread::Run() {
|
||||
Handle<ObjectTemplate> global_template = ObjectTemplate::New();
|
||||
global_template->Set(String::New("print"),
|
||||
FunctionTemplate::New(Shell::Print));
|
||||
global_template->Set(String::New("read"),
|
||||
FunctionTemplate::New(Shell::Read));
|
||||
global_template->Set(String::New("load"),
|
||||
FunctionTemplate::New(Shell::Load));
|
||||
global_template->Set(String::New("yield"),
|
||||
|
1
src/d8.h
1
src/d8.h
@ -139,6 +139,7 @@ class Shell: public i::AllStatic {
|
||||
static Handle<Value> Yield(const Arguments& args);
|
||||
static Handle<Value> Quit(const Arguments& args);
|
||||
static Handle<Value> Version(const Arguments& args);
|
||||
static Handle<Value> Read(const Arguments& args);
|
||||
static Handle<Value> Load(const Arguments& args);
|
||||
// The OS object on the global object contains methods for performing
|
||||
// operating system calls:
|
||||
|
Loading…
Reference in New Issue
Block a user