Commit 4aa558f2 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Introduce readbinary function in d8 to read binary files.

Review URL: https://chromiumcodereview.appspot.com/9283015

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10486 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent d16c6a98
......@@ -803,6 +803,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
global_template->Set(String::New("print"), FunctionTemplate::New(Print));
global_template->Set(String::New("write"), FunctionTemplate::New(Write));
global_template->Set(String::New("read"), FunctionTemplate::New(Read));
global_template->Set(String::New("readbinary"),
FunctionTemplate::New(ReadBinary));
global_template->Set(String::New("readline"),
FunctionTemplate::New(ReadLine));
global_template->Set(String::New("load"), FunctionTemplate::New(Load));
......@@ -1021,6 +1023,25 @@ static char* ReadChars(const char* name, int* size_out) {
}
Handle<Value> Shell::ReadBinary(const Arguments& args) {
String::Utf8Value filename(args[0]);
int size;
if (*filename == NULL) {
return ThrowException(String::New("Error loading file"));
}
char* chars = ReadChars(*filename, &size);
if (chars == NULL) {
return ThrowException(String::New("Error reading file"));
}
// We skip checking the string for UTF8 characters and use it raw as
// backing store for the external string with 8-bit characters.
BinaryResource* resource = new BinaryResource(chars, size);
i::Handle<i::String> string(
FACTORY->NewExternalStringFromAscii(resource));
return Utils::ToLocal(string);
}
#ifndef V8_SHARED
static char* ReadToken(char* data, char token) {
char* next = i::OS::StrChr(data, token);
......
......@@ -195,6 +195,27 @@ class SourceGroup {
};
class BinaryResource : public v8::String::ExternalAsciiStringResource {
public:
BinaryResource(const char* string, int length)
: data_(string),
length_(length) { }
~BinaryResource() {
delete[] data_;
data_ = NULL;
length_ = 0;
}
virtual const char* data() const { return data_; }
virtual size_t length() const { return length_; }
private:
const char* data_;
size_t length_;
};
class ShellOptions {
public:
ShellOptions() :
......@@ -286,6 +307,7 @@ class Shell : public i::AllStatic {
static Handle<Value> EnableProfiler(const Arguments& args);
static Handle<Value> DisableProfiler(const Arguments& args);
static Handle<Value> Read(const Arguments& args);
static Handle<Value> ReadBinary(const Arguments& args);
static Handle<String> ReadFromStdin();
static Handle<Value> ReadLine(const Arguments& args) {
return ReadFromStdin();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment