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

Side by Side Diff: samples/shell.cc

Issue 2101413002: Provide a convenience array buffer allocator (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 5 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
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); 56 void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
57 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); 57 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
58 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); 58 void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
59 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); 59 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
60 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); 60 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
61 61
62 62
63 static bool run_shell; 63 static bool run_shell;
64 64
65 65
66 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
67 public:
68 virtual void* Allocate(size_t length) {
69 void* data = AllocateUninitialized(length);
70 return data == NULL ? data : memset(data, 0, length);
71 }
72 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
73 virtual void Free(void* data, size_t) { free(data); }
74 };
75
76
77 int main(int argc, char* argv[]) { 66 int main(int argc, char* argv[]) {
78 v8::V8::InitializeICUDefaultLocation(argv[0]); 67 v8::V8::InitializeICUDefaultLocation(argv[0]);
79 v8::V8::InitializeExternalStartupData(argv[0]); 68 v8::V8::InitializeExternalStartupData(argv[0]);
80 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); 69 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
81 v8::V8::InitializePlatform(platform); 70 v8::V8::InitializePlatform(platform);
82 v8::V8::Initialize(); 71 v8::V8::Initialize();
83 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 72 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
84 ShellArrayBufferAllocator array_buffer_allocator;
85 v8::Isolate::CreateParams create_params; 73 v8::Isolate::CreateParams create_params;
86 create_params.array_buffer_allocator = &array_buffer_allocator; 74 create_params.array_buffer_allocator =
75 v8::ArrayBuffer::Allocator::NewSimpleAllocator();
87 v8::Isolate* isolate = v8::Isolate::New(create_params); 76 v8::Isolate* isolate = v8::Isolate::New(create_params);
88 run_shell = (argc == 1); 77 run_shell = (argc == 1);
89 int result; 78 int result;
90 { 79 {
91 v8::Isolate::Scope isolate_scope(isolate); 80 v8::Isolate::Scope isolate_scope(isolate);
92 v8::HandleScope handle_scope(isolate); 81 v8::HandleScope handle_scope(isolate);
93 v8::Local<v8::Context> context = CreateShellContext(isolate); 82 v8::Local<v8::Context> context = CreateShellContext(isolate);
94 if (context.IsEmpty()) { 83 if (context.IsEmpty()) {
95 fprintf(stderr, "Error creating context\n"); 84 fprintf(stderr, "Error creating context\n");
96 return 1; 85 return 1;
97 } 86 }
98 v8::Context::Scope context_scope(context); 87 v8::Context::Scope context_scope(context);
99 result = RunMain(isolate, platform, argc, argv); 88 result = RunMain(isolate, platform, argc, argv);
100 if (run_shell) RunShell(context, platform); 89 if (run_shell) RunShell(context, platform);
101 } 90 }
102 isolate->Dispose(); 91 isolate->Dispose();
103 v8::V8::Dispose(); 92 v8::V8::Dispose();
104 v8::V8::ShutdownPlatform(); 93 v8::V8::ShutdownPlatform();
105 delete platform; 94 delete platform;
95 delete create_params.array_buffer_allocator;
106 return result; 96 return result;
107 } 97 }
108 98
109 99
110 // Extracts a C string from a V8 Utf8Value. 100 // Extracts a C string from a V8 Utf8Value.
111 const char* ToCString(const v8::String::Utf8Value& value) { 101 const char* ToCString(const v8::String::Utf8Value& value) {
112 return *value ? *value : "<string conversion failed>"; 102 return *value ? *value : "<string conversion failed>";
113 } 103 }
114 104
115 105
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 v8::Local<v8::Value> stack_trace_string; 405 v8::Local<v8::Value> stack_trace_string;
416 if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) && 406 if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
417 stack_trace_string->IsString() && 407 stack_trace_string->IsString() &&
418 v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) { 408 v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) {
419 v8::String::Utf8Value stack_trace(stack_trace_string); 409 v8::String::Utf8Value stack_trace(stack_trace_string);
420 const char* stack_trace_string = ToCString(stack_trace); 410 const char* stack_trace_string = ToCString(stack_trace);
421 fprintf(stderr, "%s\n", stack_trace_string); 411 fprintf(stderr, "%s\n", stack_trace_string);
422 } 412 }
423 } 413 }
424 } 414 }
OLDNEW
« include/v8.h ('K') | « samples/process.cc ('k') | src/api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698