Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Unified Diff: src/d8.cc

Issue 10388140: Add a readbuffer function to d8, that reads a file into an ArrayBuffer. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/d8.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« no previous file with comments | « src/d8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698