Index: src/d8.cc |
diff --git a/src/d8.cc b/src/d8.cc |
index edd201bbdf1f2d40474a999160c4f5d568443ff2..adbe5501368e20b412a028b910854bf84d8b87b1 100644 |
--- a/src/d8.cc |
+++ b/src/d8.cc |
@@ -835,6 +835,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() { |
global_template->Set(String::New("read"), FunctionTemplate::New(Read)); |
global_template->Set(String::New("readbinary"), |
FunctionTemplate::New(ReadBinary)); |
+ global_template->Set(String::New("readbuffer"), |
+ FunctionTemplate::New(ReadBuffer)); |
global_template->Set(String::New("readline"), |
FunctionTemplate::New(ReadLine)); |
global_template->Set(String::New("load"), FunctionTemplate::New(Load)); |
@@ -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 |
static char* ReadToken(char* data, char token) { |
char* next = i::OS::StrChr(data, token); |