OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void RunShell(v8::Handle<v8::Context> context); | 58 void RunShell(v8::Handle<v8::Context> context); |
59 bool ExecuteString(v8::Handle<v8::String> source, | 59 bool ExecuteString(v8::Handle<v8::String> source, |
60 v8::Handle<v8::Value> name, | 60 v8::Handle<v8::Value> name, |
61 bool print_result, | 61 bool print_result, |
62 bool report_exceptions); | 62 bool report_exceptions); |
63 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 63 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
64 v8::Handle<v8::Value> Read(const v8::Arguments& args); | 64 v8::Handle<v8::Value> Read(const v8::Arguments& args); |
65 v8::Handle<v8::Value> Load(const v8::Arguments& args); | 65 v8::Handle<v8::Value> Load(const v8::Arguments& args); |
66 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 66 v8::Handle<v8::Value> Quit(const v8::Arguments& args); |
67 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 67 v8::Handle<v8::Value> Version(const v8::Arguments& args); |
| 68 v8::Handle<v8::Value> Int8Array(const v8::Arguments& args); |
| 69 v8::Handle<v8::Value> Uint8Array(const v8::Arguments& args); |
| 70 v8::Handle<v8::Value> Int16Array(const v8::Arguments& args); |
| 71 v8::Handle<v8::Value> Uint16Array(const v8::Arguments& args); |
| 72 v8::Handle<v8::Value> Int32Array(const v8::Arguments& args); |
| 73 v8::Handle<v8::Value> Uint32Array(const v8::Arguments& args); |
| 74 v8::Handle<v8::Value> Float32Array(const v8::Arguments& args); |
| 75 v8::Handle<v8::Value> PixelArray(const v8::Arguments& args); |
68 v8::Handle<v8::String> ReadFile(const char* name); | 76 v8::Handle<v8::String> ReadFile(const char* name); |
69 void ReportException(v8::TryCatch* handler); | 77 void ReportException(v8::TryCatch* handler); |
70 | 78 |
71 | 79 |
72 static bool last_run = true; | 80 static bool last_run = true; |
73 | 81 |
74 class SourceGroup { | 82 class SourceGroup { |
75 public: | 83 public: |
76 SourceGroup() : | 84 SourceGroup() : |
77 #ifndef USING_V8_SHARED | 85 #ifndef USING_V8_SHARED |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // Bind the global 'print' function to the C++ Print callback. | 336 // Bind the global 'print' function to the C++ Print callback. |
329 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); | 337 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); |
330 // Bind the global 'read' function to the C++ Read callback. | 338 // Bind the global 'read' function to the C++ Read callback. |
331 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); | 339 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); |
332 // Bind the global 'load' function to the C++ Load callback. | 340 // Bind the global 'load' function to the C++ Load callback. |
333 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); | 341 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); |
334 // Bind the 'quit' function | 342 // Bind the 'quit' function |
335 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); | 343 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); |
336 // Bind the 'version' function | 344 // Bind the 'version' function |
337 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 345 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); |
| 346 |
| 347 // Bind the handlers for external arrays. |
| 348 global->Set(v8::String::New("Int8Array"), |
| 349 v8::FunctionTemplate::New(Int8Array)); |
| 350 global->Set(v8::String::New("Uint8Array"), |
| 351 v8::FunctionTemplate::New(Uint8Array)); |
| 352 global->Set(v8::String::New("Int16Array"), |
| 353 v8::FunctionTemplate::New(Int16Array)); |
| 354 global->Set(v8::String::New("Uint16Array"), |
| 355 v8::FunctionTemplate::New(Uint16Array)); |
| 356 global->Set(v8::String::New("Int32Array"), |
| 357 v8::FunctionTemplate::New(Int32Array)); |
| 358 global->Set(v8::String::New("Uint32Array"), |
| 359 v8::FunctionTemplate::New(Uint32Array)); |
| 360 global->Set(v8::String::New("Float32Array"), |
| 361 v8::FunctionTemplate::New(Float32Array)); |
| 362 global->Set(v8::String::New("PixelArray"), |
| 363 v8::FunctionTemplate::New(PixelArray)); |
| 364 |
338 return v8::Context::New(NULL, global); | 365 return v8::Context::New(NULL, global); |
339 } | 366 } |
340 | 367 |
341 | 368 |
342 // The callback that is invoked by v8 whenever the JavaScript 'print' | 369 // The callback that is invoked by v8 whenever the JavaScript 'print' |
343 // function is called. Prints its arguments on stdout separated by | 370 // function is called. Prints its arguments on stdout separated by |
344 // spaces and ending with a newline. | 371 // spaces and ending with a newline. |
345 v8::Handle<v8::Value> Print(const v8::Arguments& args) { | 372 v8::Handle<v8::Value> Print(const v8::Arguments& args) { |
346 bool first = true; | 373 bool first = true; |
347 for (int i = 0; i < args.Length(); i++) { | 374 for (int i = 0; i < args.Length(); i++) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 ExitShell(exit_code); | 438 ExitShell(exit_code); |
412 return v8::Undefined(); | 439 return v8::Undefined(); |
413 } | 440 } |
414 | 441 |
415 | 442 |
416 v8::Handle<v8::Value> Version(const v8::Arguments& args) { | 443 v8::Handle<v8::Value> Version(const v8::Arguments& args) { |
417 return v8::String::New(v8::V8::GetVersion()); | 444 return v8::String::New(v8::V8::GetVersion()); |
418 } | 445 } |
419 | 446 |
420 | 447 |
| 448 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { |
| 449 free(data); |
| 450 object.Dispose(); |
| 451 } |
| 452 |
| 453 |
| 454 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, |
| 455 v8::ExternalArrayType type, |
| 456 int element_size) { |
| 457 if (args.Length() != 1) { |
| 458 return v8::ThrowException( |
| 459 v8::String::New("Array constructor needs one parameter.")); |
| 460 } |
| 461 int length = args[0]->Int32Value(); |
| 462 void* data = malloc(length * element_size); |
| 463 memset(data, 0, length * element_size); |
| 464 v8::Handle<v8::Object> array = v8::Object::New(); |
| 465 v8::Persistent<v8::Object> persistent_array = |
| 466 v8::Persistent<v8::Object>::New(array); |
| 467 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 468 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
| 469 return array; |
| 470 } |
| 471 |
| 472 |
| 473 v8::Handle<v8::Value> Int8Array(const v8::Arguments& args) { |
| 474 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); |
| 475 } |
| 476 |
| 477 |
| 478 v8::Handle<v8::Value> Uint8Array(const v8::Arguments& args) { |
| 479 return CreateExternalArray(args, v8::kExternalUnsignedByteArray, |
| 480 sizeof(uint8_t)); |
| 481 } |
| 482 |
| 483 |
| 484 v8::Handle<v8::Value> Int16Array(const v8::Arguments& args) { |
| 485 return CreateExternalArray(args, v8::kExternalShortArray, sizeof(int16_t)); |
| 486 } |
| 487 |
| 488 |
| 489 v8::Handle<v8::Value> Uint16Array(const v8::Arguments& args) { |
| 490 return CreateExternalArray(args, v8::kExternalUnsignedShortArray, |
| 491 sizeof(uint16_t)); |
| 492 } |
| 493 |
| 494 v8::Handle<v8::Value> Int32Array(const v8::Arguments& args) { |
| 495 return CreateExternalArray(args, v8::kExternalIntArray, sizeof(int32_t)); |
| 496 } |
| 497 |
| 498 |
| 499 v8::Handle<v8::Value> Uint32Array(const v8::Arguments& args) { |
| 500 return CreateExternalArray(args, v8::kExternalUnsignedIntArray, |
| 501 sizeof(uint32_t)); |
| 502 } |
| 503 |
| 504 |
| 505 v8::Handle<v8::Value> Float32Array(const v8::Arguments& args) { |
| 506 return CreateExternalArray(args, v8::kExternalFloatArray, |
| 507 sizeof(float)); // NOLINT |
| 508 } |
| 509 |
| 510 |
| 511 v8::Handle<v8::Value> PixelArray(const v8::Arguments& args) { |
| 512 return CreateExternalArray(args, v8::kExternalPixelArray, sizeof(uint8_t)); |
| 513 } |
| 514 |
| 515 |
421 // Reads a file into a v8 string. | 516 // Reads a file into a v8 string. |
422 v8::Handle<v8::String> ReadFile(const char* name) { | 517 v8::Handle<v8::String> ReadFile(const char* name) { |
423 FILE* file = fopen(name, "rb"); | 518 FILE* file = fopen(name, "rb"); |
424 if (file == NULL) return v8::Handle<v8::String>(); | 519 if (file == NULL) return v8::Handle<v8::String>(); |
425 | 520 |
426 fseek(file, 0, SEEK_END); | 521 fseek(file, 0, SEEK_END); |
427 int size = ftell(file); | 522 int size = ftell(file); |
428 rewind(file); | 523 rewind(file); |
429 | 524 |
430 char* chars = new char[size + 1]; | 525 char* chars = new char[size + 1]; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 printf("^"); | 621 printf("^"); |
527 } | 622 } |
528 printf("\n"); | 623 printf("\n"); |
529 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 624 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
530 if (stack_trace.length() > 0) { | 625 if (stack_trace.length() > 0) { |
531 const char* stack_trace_string = ToCString(stack_trace); | 626 const char* stack_trace_string = ToCString(stack_trace); |
532 printf("%s\n", stack_trace_string); | 627 printf("%s\n", stack_trace_string); |
533 } | 628 } |
534 } | 629 } |
535 } | 630 } |
OLD | NEW |