| OLD | NEW |
| 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 17 matching lines...) Expand all Loading... |
| 28 #include <include/v8.h> | 28 #include <include/v8.h> |
| 29 | 29 |
| 30 #include <include/libplatform/libplatform.h> | 30 #include <include/libplatform/libplatform.h> |
| 31 | 31 |
| 32 #include <assert.h> | 32 #include <assert.h> |
| 33 #include <fcntl.h> | 33 #include <fcntl.h> |
| 34 #include <stdio.h> | 34 #include <stdio.h> |
| 35 #include <stdlib.h> | 35 #include <stdlib.h> |
| 36 #include <string.h> | 36 #include <string.h> |
| 37 | 37 |
| 38 #if V8_OS_WIN |
| 39 #include <malloc.h> |
| 40 #endif |
| 41 |
| 42 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 43 #error Using compressed startup data is not supported for this sample |
| 44 #endif |
| 45 |
| 38 /** | 46 /** |
| 39 * This sample program shows how to implement a simple javascript shell | 47 * This sample program shows how to implement a simple javascript shell |
| 40 * based on V8. This includes initializing V8 with command line options, | 48 * based on V8. This includes initializing V8 with command line options, |
| 41 * creating global functions, compiling and executing strings. | 49 * creating global functions, compiling and executing strings. |
| 42 * | 50 * |
| 43 * For a more sophisticated shell, consider using the debug shell D8. | 51 * For a more sophisticated shell, consider using the debug shell D8. |
| 44 */ | 52 */ |
| 45 | 53 |
| 46 | 54 |
| 47 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); | 55 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 63 | 71 |
| 64 static bool run_shell; | 72 static bool run_shell; |
| 65 | 73 |
| 66 | 74 |
| 67 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 75 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 68 public: | 76 public: |
| 69 virtual void* Allocate(size_t length) { | 77 virtual void* Allocate(size_t length) { |
| 70 void* data = AllocateUninitialized(length); | 78 void* data = AllocateUninitialized(length); |
| 71 return data == NULL ? data : memset(data, 0, length); | 79 return data == NULL ? data : memset(data, 0, length); |
| 72 } | 80 } |
| 73 virtual void* AllocateUninitialized(size_t length) { return malloc(length); } | 81 virtual void* AllocateUninitialized(size_t length) { |
| 74 virtual void Free(void* data, size_t) { free(data); } | 82 return Allocate(length, 8); |
| 83 } |
| 84 virtual void* Allocate(size_t length, size_t alignment) { |
| 85 #if V8_OS_ANDROID |
| 86 return memalign(alignment, length); |
| 87 #elif V8_OS_WIN |
| 88 return _aligned_malloc(length, alignment); |
| 89 #else |
| 90 void* ptr = NULL; |
| 91 posix_memalign(&ptr, alignment, length); |
| 92 return ptr; |
| 93 #endif |
| 94 } |
| 95 virtual void Free(void* data, size_t) { Free(data); } |
| 96 virtual void Free(void* data) { |
| 97 #if V8_OS_WIN |
| 98 _aligned_free(data); |
| 99 #else |
| 100 free(data); |
| 101 #endif |
| 102 } |
| 75 }; | 103 }; |
| 76 | 104 |
| 77 | 105 |
| 78 int main(int argc, char* argv[]) { | 106 int main(int argc, char* argv[]) { |
| 79 v8::V8::InitializeICU(); | 107 v8::V8::InitializeICU(); |
| 80 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | 108 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 81 v8::V8::InitializePlatform(platform); | 109 v8::V8::InitializePlatform(platform); |
| 82 v8::V8::Initialize(); | 110 v8::V8::Initialize(); |
| 83 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 111 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 84 ShellArrayBufferAllocator array_buffer_allocator; | 112 ShellArrayBufferAllocator array_buffer_allocator; |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 fprintf(stderr, "^"); | 408 fprintf(stderr, "^"); |
| 381 } | 409 } |
| 382 fprintf(stderr, "\n"); | 410 fprintf(stderr, "\n"); |
| 383 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 411 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 384 if (stack_trace.length() > 0) { | 412 if (stack_trace.length() > 0) { |
| 385 const char* stack_trace_string = ToCString(stack_trace); | 413 const char* stack_trace_string = ToCString(stack_trace); |
| 386 fprintf(stderr, "%s\n", stack_trace_string); | 414 fprintf(stderr, "%s\n", stack_trace_string); |
| 387 } | 415 } |
| 388 } | 416 } |
| 389 } | 417 } |
| OLD | NEW |