Commit 1016c4a2 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Add a readbuffer function to d8, that reads a file into an ArrayBuffer.

BUG=
TEST=

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@11563 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 37ef0375
...@@ -835,6 +835,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() { ...@@ -835,6 +835,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
global_template->Set(String::New("read"), FunctionTemplate::New(Read)); global_template->Set(String::New("read"), FunctionTemplate::New(Read));
global_template->Set(String::New("readbinary"), global_template->Set(String::New("readbinary"),
FunctionTemplate::New(ReadBinary)); FunctionTemplate::New(ReadBinary));
global_template->Set(String::New("readbuffer"),
FunctionTemplate::New(ReadBuffer));
global_template->Set(String::New("readline"), global_template->Set(String::New("readline"),
FunctionTemplate::New(ReadLine)); FunctionTemplate::New(ReadLine));
global_template->Set(String::New("load"), FunctionTemplate::New(Load)); global_template->Set(String::New("load"), FunctionTemplate::New(Load));
...@@ -1070,6 +1072,32 @@ Handle<Value> Shell::ReadBinary(const Arguments& args) { ...@@ -1070,6 +1072,32 @@ Handle<Value> Shell::ReadBinary(const Arguments& args) {
} }
Handle<Value> Shell::ReadBuffer(const Arguments& args) {
String::Utf8Value filename(args[0]);
int length;
if (*filename == NULL) {
return ThrowException(String::New("Error loading file"));
}
char* data = ReadChars(*filename, &length);
if (data == NULL) {
return ThrowException(String::New("Error reading file"));
}
Handle<Object> buffer = Object::New();
buffer->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly);
Persistent<Object> persistent_buffer = Persistent<Object>::New(buffer);
persistent_buffer.MakeWeak(data, ExternalArrayWeakCallback);
persistent_buffer.MarkIndependent();
buffer->SetIndexedPropertiesToExternalArrayData(
reinterpret_cast<uint8_t*>(data), kExternalUnsignedByteArray, length);
buffer->Set(String::New("byteLength"),
Int32::New(static_cast<int32_t>(length)), ReadOnly);
return buffer;
}
#ifndef V8_SHARED #ifndef V8_SHARED
static char* ReadToken(char* data, char token) { static char* ReadToken(char* data, char token) {
char* next = i::OS::StrChr(data, token); char* next = i::OS::StrChr(data, token);
......
...@@ -308,6 +308,7 @@ class Shell : public i::AllStatic { ...@@ -308,6 +308,7 @@ class Shell : public i::AllStatic {
static Handle<Value> DisableProfiler(const Arguments& args); static Handle<Value> DisableProfiler(const Arguments& args);
static Handle<Value> Read(const Arguments& args); static Handle<Value> Read(const Arguments& args);
static Handle<Value> ReadBinary(const Arguments& args); static Handle<Value> ReadBinary(const Arguments& args);
static Handle<Value> ReadBuffer(const Arguments& args);
static Handle<String> ReadFromStdin(); static Handle<String> ReadFromStdin();
static Handle<Value> ReadLine(const Arguments& args) { static Handle<Value> ReadLine(const Arguments& args) {
return ReadFromStdin(); 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