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

Side by Side Diff: src/d8.cc

Issue 15001041: Externalization API for ArrayBuffer (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: CR feedback Created 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 1030 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 chars[size] = '\0'; 1041 chars[size] = '\0';
1042 for (int i = 0; i < size;) { 1042 for (int i = 0; i < size;) {
1043 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); 1043 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
1044 i += read; 1044 i += read;
1045 } 1045 }
1046 fclose(file); 1046 fclose(file);
1047 *size_out = size; 1047 *size_out = size;
1048 return chars; 1048 return chars;
1049 } 1049 }
1050 1050
1051 static void ReadBufferWeakCallback(v8::Isolate* isolate,
1052 Persistent<Value>* object,
1053 uint8_t* data) {
1054 size_t byte_length = ArrayBuffer::Cast(**object)->ByteLength();
1055 isolate->AdjustAmountOfExternalAllocatedMemory(
1056 -static_cast<intptr_t>(byte_length));
1057
1058 delete[] data;
1059 object->Dispose(isolate);
1060 }
1051 1061
1052 Handle<Value> Shell::ReadBuffer(const Arguments& args) { 1062 Handle<Value> Shell::ReadBuffer(const Arguments& args) {
1053 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT 1063 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT
1054 String::Utf8Value filename(args[0]); 1064 String::Utf8Value filename(args[0]);
1055 int length; 1065 int length;
1056 if (*filename == NULL) { 1066 if (*filename == NULL) {
1057 return Throw("Error loading file"); 1067 return Throw("Error loading file");
1058 } 1068 }
1059 1069
1070 Isolate* isolate = args.GetIsolate();
1060 uint8_t* data = reinterpret_cast<uint8_t*>( 1071 uint8_t* data = reinterpret_cast<uint8_t*>(
1061 ReadChars(args.GetIsolate(), *filename, &length)); 1072 ReadChars(args.GetIsolate(), *filename, &length));
1062 if (data == NULL) { 1073 if (data == NULL) {
1063 return Throw("Error reading file"); 1074 return Throw("Error reading file");
1064 } 1075 }
1065 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(length); 1076 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length);
1066 memcpy(buffer->Data(), data, length); 1077 v8::Persistent<v8::Value> weak_handle =
1067 delete[] data; 1078 v8::Persistent<v8::Value>::New(isolate, buffer);
1079 weak_handle.MakeWeak(isolate, data, ReadBufferWeakCallback);
1080 weak_handle.MarkIndependent();
1081 isolate->AdjustAmountOfExternalAllocatedMemory(length);
1082
1068 return buffer; 1083 return buffer;
1069 } 1084 }
1070 1085
1071 1086
1072 #ifndef V8_SHARED 1087 #ifndef V8_SHARED
1073 static char* ReadToken(char* data, char token) { 1088 static char* ReadToken(char* data, char token) {
1074 char* next = i::OS::StrChr(data, token); 1089 char* next = i::OS::StrChr(data, token);
1075 if (next != NULL) { 1090 if (next != NULL) {
1076 *next = '\0'; 1091 *next = '\0';
1077 return (next + 1); 1092 return (next + 1);
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1602 } 1617 }
1603 1618
1604 } // namespace v8 1619 } // namespace v8
1605 1620
1606 1621
1607 #ifndef GOOGLE3 1622 #ifndef GOOGLE3
1608 int main(int argc, char* argv[]) { 1623 int main(int argc, char* argv[]) {
1609 return v8::Shell::Main(argc, argv); 1624 return v8::Shell::Main(argc, argv);
1610 } 1625 }
1611 #endif 1626 #endif
OLDNEW
« include/v8.h ('K') | « src/bootstrapper.cc ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698