2012-01-11 14:42:58 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-10-21 09:12:27 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef USING_V8_SHARED // Defined when linking against shared lib on Windows.
|
|
|
|
#define V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
#endif
|
|
|
|
|
2011-06-08 09:00:25 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2009-03-16 10:08:59 +00:00
|
|
|
#include <errno.h>
|
2011-06-08 09:00:25 +00:00
|
|
|
#include <stdlib.h>
|
2011-07-14 15:43:40 +00:00
|
|
|
#include <string.h>
|
2011-08-08 09:34:18 +00:00
|
|
|
#include <sys/stat.h>
|
2008-11-20 23:53:18 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include "../include/v8-testing.h"
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
#include "d8.h"
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
#include "api.h"
|
|
|
|
#include "checks.h"
|
2008-12-18 10:06:49 +00:00
|
|
|
#include "d8-debug.h"
|
2008-10-21 09:12:27 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "natives.h"
|
2009-01-20 14:14:22 +00:00
|
|
|
#include "platform.h"
|
2011-07-14 15:43:40 +00:00
|
|
|
#include "v8.h"
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef ASSERT
|
2011-07-14 15:43:40 +00:00
|
|
|
#define ASSERT(condition) assert(condition)
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-07-14 15:43:40 +00:00
|
|
|
namespace v8 {
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
LineEditor *LineEditor::first_ = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
LineEditor::LineEditor(Type type, const char* name)
|
|
|
|
: type_(type),
|
|
|
|
name_(name),
|
|
|
|
next_(first_) {
|
|
|
|
first_ = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LineEditor* LineEditor::Get() {
|
|
|
|
LineEditor* current = first_;
|
|
|
|
LineEditor* best = current;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (current->type_ > best->type_)
|
|
|
|
best = current;
|
|
|
|
current = current->next_;
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DumbLineEditor: public LineEditor {
|
|
|
|
public:
|
|
|
|
DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { }
|
2012-01-17 13:37:09 +00:00
|
|
|
virtual Handle<String> Prompt(const char* prompt);
|
2008-10-21 09:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static DumbLineEditor dumb_line_editor;
|
|
|
|
|
|
|
|
|
2012-01-17 13:37:09 +00:00
|
|
|
Handle<String> DumbLineEditor::Prompt(const char* prompt) {
|
2008-10-21 09:12:27 +00:00
|
|
|
printf("%s", prompt);
|
2012-01-17 13:37:09 +00:00
|
|
|
return Shell::ReadFromStdin();
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 13:37:09 +00:00
|
|
|
#ifndef V8_SHARED
|
2009-03-24 13:33:54 +00:00
|
|
|
CounterMap* Shell::counter_map_;
|
2008-12-03 09:35:21 +00:00
|
|
|
i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
|
|
|
|
CounterCollection Shell::local_counters_;
|
|
|
|
CounterCollection* Shell::counters_ = &local_counters_;
|
2011-07-14 15:43:40 +00:00
|
|
|
i::Mutex* Shell::context_mutex_(i::OS::CreateMutex());
|
2008-10-21 09:12:27 +00:00
|
|
|
Persistent<Context> Shell::utility_context_;
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2012-01-17 13:37:09 +00:00
|
|
|
LineEditor* Shell::console = NULL;
|
2008-10-21 09:12:27 +00:00
|
|
|
Persistent<Context> Shell::evaluation_context_;
|
2011-07-11 07:38:09 +00:00
|
|
|
ShellOptions Shell::options;
|
2011-07-14 15:43:40 +00:00
|
|
|
const char* Shell::kPrompt = "d8> ";
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2009-03-24 13:33:54 +00:00
|
|
|
bool CounterMap::Match(void* key1, void* key2) {
|
|
|
|
const char* name1 = reinterpret_cast<const char*>(key1);
|
|
|
|
const char* name2 = reinterpret_cast<const char*>(key2);
|
2009-03-24 14:29:49 +00:00
|
|
|
return strcmp(name1, name2) == 0;
|
2009-03-24 13:33:54 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2009-03-24 13:33:54 +00:00
|
|
|
|
|
|
|
|
2009-02-05 13:53:41 +00:00
|
|
|
// Converts a V8 value to a C string.
|
2010-04-16 12:19:47 +00:00
|
|
|
const char* Shell::ToCString(const v8::String::Utf8Value& value) {
|
2009-02-05 13:53:41 +00:00
|
|
|
return *value ? *value : "<string conversion failed>";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
// Executes a string within the current v8 context.
|
|
|
|
bool Shell::ExecuteString(Handle<String> source,
|
|
|
|
Handle<Value> name,
|
|
|
|
bool print_result,
|
|
|
|
bool report_exceptions) {
|
2011-09-29 08:55:36 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2011-07-14 15:43:40 +00:00
|
|
|
bool FLAG_debugger = i::FLAG_debugger;
|
|
|
|
#else
|
|
|
|
bool FLAG_debugger = false;
|
2011-09-29 08:55:36 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2008-10-21 09:12:27 +00:00
|
|
|
HandleScope handle_scope;
|
|
|
|
TryCatch try_catch;
|
2011-07-11 07:38:09 +00:00
|
|
|
options.script_executed = true;
|
2011-07-14 15:43:40 +00:00
|
|
|
if (FLAG_debugger) {
|
2008-12-18 10:06:49 +00:00
|
|
|
// When debugging make exceptions appear to be uncaught.
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Script> script = Script::Compile(source, name);
|
|
|
|
if (script.IsEmpty()) {
|
|
|
|
// Print errors that happened during compilation.
|
2011-07-14 15:43:40 +00:00
|
|
|
if (report_exceptions && !FLAG_debugger)
|
2008-10-21 09:12:27 +00:00
|
|
|
ReportException(&try_catch);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
Handle<Value> result = script->Run();
|
|
|
|
if (result.IsEmpty()) {
|
2011-02-16 11:40:48 +00:00
|
|
|
ASSERT(try_catch.HasCaught());
|
2008-10-21 09:12:27 +00:00
|
|
|
// Print errors that happened during execution.
|
2011-07-14 15:43:40 +00:00
|
|
|
if (report_exceptions && !FLAG_debugger)
|
2008-10-21 09:12:27 +00:00
|
|
|
ReportException(&try_catch);
|
|
|
|
return false;
|
|
|
|
} else {
|
2011-02-16 11:40:48 +00:00
|
|
|
ASSERT(!try_catch.HasCaught());
|
2008-10-21 09:12:27 +00:00
|
|
|
if (print_result && !result->IsUndefined()) {
|
|
|
|
// If all went well and the result wasn't undefined then print
|
|
|
|
// the returned value.
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value str(result);
|
2011-10-18 12:10:13 +00:00
|
|
|
size_t count = fwrite(*str, sizeof(**str), str.length(), stdout);
|
|
|
|
(void) count; // Silence GCC-4.5.x "unused result" warning.
|
2011-09-01 14:02:35 +00:00
|
|
|
printf("\n");
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Print(const Arguments& args) {
|
2009-08-12 11:52:22 +00:00
|
|
|
Handle<Value> val = Write(args);
|
|
|
|
printf("\n");
|
2011-07-20 13:14:46 +00:00
|
|
|
fflush(stdout);
|
2009-08-12 11:52:22 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Write(const Arguments& args) {
|
2008-10-21 09:12:27 +00:00
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
|
|
|
HandleScope handle_scope;
|
2009-08-12 11:52:22 +00:00
|
|
|
if (i != 0) {
|
2008-10-21 09:12:27 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value str(args[i]);
|
2011-07-26 08:15:49 +00:00
|
|
|
int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout));
|
2009-12-03 08:48:32 +00:00
|
|
|
if (n != str.length()) {
|
|
|
|
printf("Error in fwrite\n");
|
2011-09-13 13:16:13 +00:00
|
|
|
Exit(1);
|
2009-12-03 08:48:32 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-08 08:54:28 +00:00
|
|
|
Handle<Value> Shell::EnableProfiler(const Arguments& args) {
|
|
|
|
V8::ResumeProfiler();
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::DisableProfiler(const Arguments& args) {
|
|
|
|
V8::PauseProfiler();
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-17 21:04:34 +00:00
|
|
|
Handle<Value> Shell::Read(const Arguments& args) {
|
|
|
|
String::Utf8Value file(args[0]);
|
|
|
|
if (*file == NULL) {
|
|
|
|
return ThrowException(String::New("Error loading file"));
|
|
|
|
}
|
|
|
|
Handle<String> source = ReadFile(*file);
|
|
|
|
if (source.IsEmpty()) {
|
|
|
|
return ThrowException(String::New("Error loading file"));
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 13:37:09 +00:00
|
|
|
Handle<String> Shell::ReadFromStdin() {
|
2011-07-14 15:43:40 +00:00
|
|
|
static const int kBufferSize = 256;
|
|
|
|
char buffer[kBufferSize];
|
|
|
|
Handle<String> accumulator = String::New("");
|
|
|
|
int length;
|
2011-08-01 14:15:02 +00:00
|
|
|
while (true) {
|
|
|
|
// Continue reading if the line ends with an escape '\\' or the line has
|
|
|
|
// not been fully read into the buffer yet (does not end with '\n').
|
|
|
|
// If fgets gets an error, just give up.
|
2012-01-17 13:37:09 +00:00
|
|
|
if (fgets(buffer, kBufferSize, stdin) == NULL) return Handle<String>();
|
2011-07-26 08:15:49 +00:00
|
|
|
length = static_cast<int>(strlen(buffer));
|
2011-08-01 14:15:02 +00:00
|
|
|
if (length == 0) {
|
|
|
|
return accumulator;
|
|
|
|
} else if (buffer[length-1] != '\n') {
|
|
|
|
accumulator = String::Concat(accumulator, String::New(buffer, length));
|
|
|
|
} else if (length > 1 && buffer[length-2] == '\\') {
|
|
|
|
buffer[length-2] = '\n';
|
|
|
|
accumulator = String::Concat(accumulator, String::New(buffer, length-1));
|
|
|
|
} else {
|
|
|
|
return String::Concat(accumulator, String::New(buffer, length-1));
|
|
|
|
}
|
|
|
|
}
|
2009-08-13 07:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Value> Shell::Load(const Arguments& args) {
|
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
|
|
|
HandleScope handle_scope;
|
|
|
|
String::Utf8Value file(args[i]);
|
2009-02-05 13:53:41 +00:00
|
|
|
if (*file == NULL) {
|
|
|
|
return ThrowException(String::New("Error loading file"));
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<String> source = ReadFile(*file);
|
|
|
|
if (source.IsEmpty()) {
|
|
|
|
return ThrowException(String::New("Error loading file"));
|
|
|
|
}
|
2011-09-09 11:10:36 +00:00
|
|
|
if (!ExecuteString(source, String::New(*file), false, true)) {
|
2009-12-03 08:48:32 +00:00
|
|
|
return ThrowException(String::New("Error executing file"));
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
2012-01-11 14:42:58 +00:00
|
|
|
static size_t convertToUint(Local<Value> value_in, TryCatch* try_catch) {
|
|
|
|
if (value_in->IsUint32()) {
|
|
|
|
return value_in->Uint32Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Local<Value> number = value_in->ToNumber();
|
|
|
|
if (try_catch->HasCaught()) return 0;
|
|
|
|
|
|
|
|
ASSERT(number->IsNumber());
|
|
|
|
Local<Int32> int32 = number->ToInt32();
|
|
|
|
if (try_catch->HasCaught() || int32.IsEmpty()) return 0;
|
|
|
|
|
|
|
|
int32_t raw_value = int32->Int32Value();
|
|
|
|
if (try_catch->HasCaught()) return 0;
|
|
|
|
|
|
|
|
if (raw_value < 0) {
|
|
|
|
ThrowException(String::New("Array length must not be negative."));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int kMaxLength = 0x3fffffff;
|
|
|
|
#ifndef V8_SHARED
|
|
|
|
ASSERT(kMaxLength == i::ExternalArray::kMaxLength);
|
|
|
|
#endif // V8_SHARED
|
|
|
|
if (raw_value > static_cast<int32_t>(kMaxLength)) {
|
|
|
|
ThrowException(
|
|
|
|
String::New("Array length exceeds maximum length."));
|
|
|
|
}
|
|
|
|
return static_cast<size_t>(raw_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char kArrayBufferReferencePropName[] = "_is_array_buffer_";
|
|
|
|
const char kArrayBufferMarkerPropName[] = "_array_buffer_ref_";
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
Handle<Value> Shell::CreateExternalArray(const Arguments& args,
|
|
|
|
ExternalArrayType type,
|
2011-06-29 10:27:14 +00:00
|
|
|
size_t element_size) {
|
2012-01-11 14:42:58 +00:00
|
|
|
TryCatch try_catch;
|
|
|
|
bool is_array_buffer_construct = element_size == 0;
|
|
|
|
if (is_array_buffer_construct) {
|
|
|
|
type = v8::kExternalByteArray;
|
|
|
|
element_size = 1;
|
|
|
|
}
|
2011-06-29 10:27:14 +00:00
|
|
|
ASSERT(element_size == 1 || element_size == 2 || element_size == 4 ||
|
|
|
|
element_size == 8);
|
2012-01-11 14:42:58 +00:00
|
|
|
if (args.Length() == 0) {
|
2011-05-31 09:42:17 +00:00
|
|
|
return ThrowException(
|
2012-01-11 14:42:58 +00:00
|
|
|
String::New("Array constructor must have at least one "
|
|
|
|
"parameter."));
|
2011-05-31 09:42:17 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
bool first_arg_is_array_buffer =
|
|
|
|
args[0]->IsObject() &&
|
|
|
|
args[0]->ToObject()->Get(
|
|
|
|
String::New(kArrayBufferMarkerPropName))->IsTrue();
|
|
|
|
// Currently, only the following constructors are supported:
|
|
|
|
// TypedArray(unsigned long length)
|
|
|
|
// TypedArray(ArrayBuffer buffer,
|
|
|
|
// optional unsigned long byteOffset,
|
|
|
|
// optional unsigned long length)
|
|
|
|
if (args.Length() > 3) {
|
|
|
|
return ThrowException(
|
|
|
|
String::New("Array constructor from ArrayBuffer must "
|
|
|
|
"have 1-3 parameters."));
|
|
|
|
}
|
|
|
|
|
|
|
|
Local<Value> length_value = (args.Length() < 3)
|
|
|
|
? (first_arg_is_array_buffer
|
|
|
|
? args[0]->ToObject()->Get(String::New("length"))
|
|
|
|
: args[0])
|
|
|
|
: args[2];
|
|
|
|
size_t length = convertToUint(length_value, &try_catch);
|
|
|
|
if (try_catch.HasCaught()) return try_catch.Exception();
|
|
|
|
|
|
|
|
void* data = NULL;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
Handle<Object> array = Object::New();
|
|
|
|
if (first_arg_is_array_buffer) {
|
|
|
|
Handle<Object> derived_from = args[0]->ToObject();
|
|
|
|
data = derived_from->GetIndexedPropertiesExternalArrayData();
|
|
|
|
|
|
|
|
size_t array_buffer_length = convertToUint(
|
|
|
|
derived_from->Get(String::New("length")),
|
|
|
|
&try_catch);
|
|
|
|
if (try_catch.HasCaught()) return try_catch.Exception();
|
|
|
|
|
|
|
|
if (data == NULL && array_buffer_length != 0) {
|
|
|
|
return ThrowException(
|
|
|
|
String::New("ArrayBuffer doesn't have data"));
|
2011-12-13 13:51:58 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
if (args.Length() > 1) {
|
|
|
|
offset = convertToUint(args[1], &try_catch);
|
|
|
|
if (try_catch.HasCaught()) return try_catch.Exception();
|
|
|
|
|
|
|
|
// The given byteOffset must be a multiple of the element size of the
|
|
|
|
// specific type, otherwise an exception is raised.
|
|
|
|
if (offset % element_size != 0) {
|
|
|
|
return ThrowException(
|
|
|
|
String::New("offset must be multiple of element_size"));
|
2011-12-13 13:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
if (offset > array_buffer_length) {
|
2012-01-11 15:10:56 +00:00
|
|
|
return ThrowException(
|
|
|
|
String::New("byteOffset must be less than ArrayBuffer length."));
|
2011-09-12 12:42:05 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
if (args.Length() == 2) {
|
|
|
|
// If length is not explicitly specified, the length of the ArrayBuffer
|
|
|
|
// minus the byteOffset must be a multiple of the element size of the
|
|
|
|
// specific type, or an exception is raised.
|
|
|
|
length = array_buffer_length - offset;
|
2011-06-29 10:27:14 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
if (args.Length() != 3) {
|
|
|
|
if (length % element_size != 0) {
|
|
|
|
return ThrowException(
|
|
|
|
String::New("ArrayBuffer length minus the byteOffset must be a "
|
|
|
|
"multiple of the element size"));
|
|
|
|
}
|
|
|
|
length /= element_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a given byteOffset and length references an area beyond the end of
|
|
|
|
// the ArrayBuffer an exception is raised.
|
|
|
|
if (offset + (length * element_size) > array_buffer_length) {
|
2011-06-29 10:27:14 +00:00
|
|
|
return ThrowException(
|
2012-01-11 14:42:58 +00:00
|
|
|
String::New("length references an area beyond the end of the "
|
|
|
|
"ArrayBuffer"));
|
2011-06-29 10:27:14 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
// Hold a reference to the ArrayBuffer so its buffer doesn't get collected.
|
|
|
|
array->Set(String::New(kArrayBufferReferencePropName), args[0], ReadOnly);
|
2011-06-29 10:27:14 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
|
|
|
if (is_array_buffer_construct) {
|
|
|
|
array->Set(String::New(kArrayBufferMarkerPropName), True(), ReadOnly);
|
2011-06-29 10:27:14 +00:00
|
|
|
}
|
2012-01-11 14:42:58 +00:00
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
Persistent<Object> persistent_array = Persistent<Object>::New(array);
|
|
|
|
persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
|
|
|
|
persistent_array.MarkIndependent();
|
2012-01-11 14:42:58 +00:00
|
|
|
if (data == NULL && length != 0) {
|
|
|
|
data = calloc(length, element_size);
|
|
|
|
if (data == NULL) {
|
|
|
|
return ThrowException(String::New("Memory allocation failed."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array->SetIndexedPropertiesToExternalArrayData(
|
|
|
|
reinterpret_cast<uint8_t*>(data) + offset, type,
|
|
|
|
static_cast<int>(length));
|
2011-07-26 08:15:49 +00:00
|
|
|
array->Set(String::New("length"),
|
|
|
|
Int32::New(static_cast<int32_t>(length)), ReadOnly);
|
|
|
|
array->Set(String::New("BYTES_PER_ELEMENT"),
|
|
|
|
Int32::New(static_cast<int32_t>(element_size)));
|
2011-05-31 09:42:17 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) {
|
2012-01-11 14:42:58 +00:00
|
|
|
HandleScope scope;
|
|
|
|
Handle<String> prop_name = String::New(kArrayBufferReferencePropName);
|
|
|
|
Handle<Object> converted_object = object->ToObject();
|
|
|
|
Local<Value> prop_value = converted_object->Get(prop_name);
|
|
|
|
if (data != NULL && !prop_value->IsObject()) {
|
|
|
|
free(data);
|
|
|
|
}
|
2011-05-31 09:42:17 +00:00
|
|
|
object.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-11 14:42:58 +00:00
|
|
|
Handle<Value> Shell::ArrayBuffer(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, v8::kExternalByteArray, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
Handle<Value> Shell::Int8Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Uint8Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Int16Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalShortArray, sizeof(int16_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Uint16Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalUnsignedShortArray,
|
|
|
|
sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
Handle<Value> Shell::Int32Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalIntArray, sizeof(int32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Uint32Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalUnsignedIntArray, sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Float32Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalFloatArray,
|
|
|
|
sizeof(float)); // NOLINT
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Float64Array(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalDoubleArray,
|
|
|
|
sizeof(double)); // NOLINT
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::PixelArray(const Arguments& args) {
|
|
|
|
return CreateExternalArray(args, kExternalPixelArray, sizeof(uint8_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 10:08:59 +00:00
|
|
|
Handle<Value> Shell::Yield(const Arguments& args) {
|
|
|
|
v8::Unlocker unlocker;
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Value> Shell::Quit(const Arguments& args) {
|
|
|
|
int exit_code = args[0]->Int32Value();
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
OnExit();
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
exit(exit_code);
|
|
|
|
return Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::Version(const Arguments& args) {
|
|
|
|
return String::New(V8::GetVersion());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::ReportException(v8::TryCatch* try_catch) {
|
|
|
|
HandleScope handle_scope;
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value exception(try_catch->Exception());
|
|
|
|
const char* exception_string = ToCString(exception);
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Message> message = try_catch->Message();
|
|
|
|
if (message.IsEmpty()) {
|
|
|
|
// V8 didn't provide any extra information about this error; just
|
|
|
|
// print the exception.
|
2009-02-05 13:53:41 +00:00
|
|
|
printf("%s\n", exception_string);
|
2008-10-21 09:12:27 +00:00
|
|
|
} else {
|
|
|
|
// Print (filename):(line number): (message).
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value filename(message->GetScriptResourceName());
|
|
|
|
const char* filename_string = ToCString(filename);
|
2008-10-21 09:12:27 +00:00
|
|
|
int linenum = message->GetLineNumber();
|
2009-02-05 13:53:41 +00:00
|
|
|
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
|
2008-10-21 09:12:27 +00:00
|
|
|
// Print line of source code.
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value sourceline(message->GetSourceLine());
|
|
|
|
const char* sourceline_string = ToCString(sourceline);
|
|
|
|
printf("%s\n", sourceline_string);
|
2008-10-21 09:12:27 +00:00
|
|
|
// Print wavy underline (GetUnderline is deprecated).
|
|
|
|
int start = message->GetStartColumn();
|
|
|
|
for (int i = 0; i < start; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
int end = message->GetEndColumn();
|
|
|
|
for (int i = start; i < end; i++) {
|
|
|
|
printf("^");
|
|
|
|
}
|
|
|
|
printf("\n");
|
2011-05-23 10:41:02 +00:00
|
|
|
v8::String::Utf8Value stack_trace(try_catch->StackTrace());
|
|
|
|
if (stack_trace.length() > 0) {
|
|
|
|
const char* stack_trace_string = ToCString(stack_trace);
|
|
|
|
printf("%s\n", stack_trace_string);
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-09-09 11:10:36 +00:00
|
|
|
printf("\n");
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) {
|
|
|
|
HandleScope handle_scope;
|
|
|
|
Context::Scope context_scope(utility_context_);
|
|
|
|
Handle<Object> global = utility_context_->Global();
|
|
|
|
Handle<Value> fun = global->Get(String::New("GetCompletions"));
|
|
|
|
static const int kArgc = 3;
|
|
|
|
Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full };
|
|
|
|
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
|
|
|
|
return handle_scope.Close(Handle<Array>::Cast(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2009-03-02 19:02:27 +00:00
|
|
|
Handle<Object> Shell::DebugMessageDetails(Handle<String> message) {
|
2008-12-18 10:06:49 +00:00
|
|
|
Context::Scope context_scope(utility_context_);
|
|
|
|
Handle<Object> global = utility_context_->Global();
|
2009-03-02 19:02:27 +00:00
|
|
|
Handle<Value> fun = global->Get(String::New("DebugMessageDetails"));
|
2008-12-18 10:06:49 +00:00
|
|
|
static const int kArgc = 1;
|
2009-03-02 19:02:27 +00:00
|
|
|
Handle<Value> argv[kArgc] = { message };
|
2008-12-18 10:06:49 +00:00
|
|
|
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
|
2009-03-02 19:02:27 +00:00
|
|
|
return Handle<Object>::Cast(val);
|
2008-12-18 10:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) {
|
|
|
|
Context::Scope context_scope(utility_context_);
|
|
|
|
Handle<Object> global = utility_context_->Global();
|
|
|
|
Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest"));
|
|
|
|
static const int kArgc = 1;
|
|
|
|
Handle<Value> argv[kArgc] = { command };
|
|
|
|
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
|
|
|
|
return val;
|
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-12-18 10:06:49 +00:00
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2009-04-14 00:30:44 +00:00
|
|
|
int32_t* Counter::Bind(const char* name, bool is_histogram) {
|
2008-12-03 09:35:21 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
|
|
|
|
name_[i] = static_cast<char>(name[i]);
|
|
|
|
name_[i] = '\0';
|
2009-04-14 00:30:44 +00:00
|
|
|
is_histogram_ = is_histogram;
|
|
|
|
return ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::AddSample(int32_t sample) {
|
|
|
|
count_++;
|
|
|
|
sample_total_ += sample;
|
2008-12-03 09:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CounterCollection::CounterCollection() {
|
|
|
|
magic_number_ = 0xDEADFACE;
|
|
|
|
max_counters_ = kMaxCounters;
|
|
|
|
max_name_size_ = Counter::kMaxNameSize;
|
|
|
|
counters_in_use_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Counter* CounterCollection::GetNextCounter() {
|
|
|
|
if (counters_in_use_ == kMaxCounters) return NULL;
|
|
|
|
return &counters_[counters_in_use_++];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::MapCounters(const char* name) {
|
2011-07-14 15:43:40 +00:00
|
|
|
counters_file_ = i::OS::MemoryMappedFile::create(
|
|
|
|
name, sizeof(CounterCollection), &local_counters_);
|
2008-12-03 09:35:21 +00:00
|
|
|
void* memory = (counters_file_ == NULL) ?
|
|
|
|
NULL : counters_file_->memory();
|
|
|
|
if (memory == NULL) {
|
|
|
|
printf("Could not map counters file %s\n", name);
|
2011-09-13 13:16:13 +00:00
|
|
|
Exit(1);
|
2008-12-03 09:35:21 +00:00
|
|
|
}
|
|
|
|
counters_ = static_cast<CounterCollection*>(memory);
|
|
|
|
V8::SetCounterFunction(LookupCounter);
|
2009-04-14 00:30:44 +00:00
|
|
|
V8::SetCreateHistogramFunction(CreateHistogram);
|
|
|
|
V8::SetAddHistogramSampleFunction(AddHistogramSample);
|
2008-12-03 09:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-24 13:33:54 +00:00
|
|
|
int CounterMap::Hash(const char* name) {
|
|
|
|
int h = 0;
|
|
|
|
int c;
|
|
|
|
while ((c = *name++) != 0) {
|
|
|
|
h += h << 5;
|
|
|
|
h += c;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 00:30:44 +00:00
|
|
|
Counter* Shell::GetCounter(const char* name, bool is_histogram) {
|
2009-03-24 13:33:54 +00:00
|
|
|
Counter* counter = counter_map_->Lookup(name);
|
2009-04-14 00:30:44 +00:00
|
|
|
|
|
|
|
if (counter == NULL) {
|
|
|
|
counter = counters_->GetNextCounter();
|
|
|
|
if (counter != NULL) {
|
|
|
|
counter_map_->Set(name, counter);
|
|
|
|
counter->Bind(name, is_histogram);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ASSERT(counter->is_histogram() == is_histogram);
|
|
|
|
}
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int* Shell::LookupCounter(const char* name) {
|
|
|
|
Counter* counter = GetCounter(name, false);
|
|
|
|
|
2009-03-24 13:33:54 +00:00
|
|
|
if (counter != NULL) {
|
|
|
|
return counter->ptr();
|
2009-04-14 00:30:44 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2009-04-14 00:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* Shell::CreateHistogram(const char* name,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
size_t buckets) {
|
|
|
|
return GetCounter(name, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::AddHistogramSample(void* histogram, int sample) {
|
|
|
|
Counter* counter = reinterpret_cast<Counter*>(histogram);
|
|
|
|
counter->AddSample(sample);
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
void Shell::InstallUtilityScript() {
|
|
|
|
Locker lock;
|
|
|
|
HandleScope scope;
|
2011-06-24 20:04:32 +00:00
|
|
|
// If we use the utility context, we have to set the security tokens so that
|
|
|
|
// utility, evaluation and debug context can all access each other.
|
|
|
|
utility_context_->SetSecurityToken(Undefined());
|
|
|
|
evaluation_context_->SetSecurityToken(Undefined());
|
2011-06-09 14:28:49 +00:00
|
|
|
Context::Scope utility_scope(utility_context_);
|
2011-06-24 20:04:32 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2011-09-29 08:55:36 +00:00
|
|
|
if (i::FLAG_debugger) printf("JavaScript debugger enabled\n");
|
2011-06-24 20:04:32 +00:00
|
|
|
// Install the debugger object in the utility scope
|
|
|
|
i::Debug* debug = i::Isolate::Current()->debug();
|
|
|
|
debug->Load();
|
|
|
|
i::Handle<i::JSObject> js_debug
|
|
|
|
= i::Handle<i::JSObject>(debug->debug_context()->global());
|
|
|
|
utility_context_->Global()->Set(String::New("$debug"),
|
|
|
|
Utils::ToLocal(js_debug));
|
|
|
|
debug->debug_context()->set_security_token(HEAP->undefined_value());
|
2011-07-14 15:43:40 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
// Run the d8 shell utility script in the utility context
|
|
|
|
int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
|
|
|
|
i::Vector<const char> shell_source =
|
|
|
|
i::NativesCollection<i::D8>::GetRawScriptSource(source_index);
|
|
|
|
i::Vector<const char> shell_source_name =
|
|
|
|
i::NativesCollection<i::D8>::GetScriptName(source_index);
|
|
|
|
Handle<String> source = String::New(shell_source.start(),
|
|
|
|
shell_source.length());
|
|
|
|
Handle<String> name = String::New(shell_source_name.start(),
|
|
|
|
shell_source_name.length());
|
|
|
|
Handle<Script> script = Script::Compile(source, name);
|
|
|
|
script->Run();
|
|
|
|
// Mark the d8 shell script as native to avoid it showing up as normal source
|
|
|
|
// in the debugger.
|
|
|
|
i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script);
|
|
|
|
i::Handle<i::Script> script_object = compiled_script->IsJSFunction()
|
2011-07-14 15:43:40 +00:00
|
|
|
? i::Handle<i::Script>(i::Script::cast(
|
|
|
|
i::JSFunction::cast(*compiled_script)->shared()->script()))
|
|
|
|
: i::Handle<i::Script>(i::Script::cast(
|
|
|
|
i::SharedFunctionInfo::cast(*compiled_script)->script()));
|
2011-06-09 14:28:49 +00:00
|
|
|
script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
|
2011-07-11 07:38:09 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
// Start the in-process debugger if requested.
|
|
|
|
if (i::FLAG_debugger && !i::FLAG_debugger_agent) {
|
|
|
|
v8::Debug::SetDebugEventListener(HandleDebugEvent);
|
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2011-06-08 09:00:25 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
class BZip2Decompressor : public v8::StartupDataDecompressor {
|
|
|
|
public:
|
|
|
|
virtual ~BZip2Decompressor() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual int DecompressData(char* raw_data,
|
|
|
|
int* raw_data_size,
|
|
|
|
const char* compressed_data,
|
|
|
|
int compressed_data_size) {
|
|
|
|
ASSERT_EQ(v8::StartupData::kBZip2,
|
|
|
|
v8::V8::GetCompressedStartupDataAlgorithm());
|
|
|
|
unsigned int decompressed_size = *raw_data_size;
|
|
|
|
int result =
|
|
|
|
BZ2_bzBuffToBuffDecompress(raw_data,
|
|
|
|
&decompressed_size,
|
|
|
|
const_cast<char*>(compressed_data),
|
|
|
|
compressed_data_size,
|
|
|
|
0, 1);
|
|
|
|
if (result == BZ_OK) {
|
|
|
|
*raw_data_size = decompressed_size;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<ObjectTemplate> global_template = ObjectTemplate::New();
|
|
|
|
global_template->Set(String::New("print"), FunctionTemplate::New(Print));
|
2009-08-12 11:52:22 +00:00
|
|
|
global_template->Set(String::New("write"), FunctionTemplate::New(Write));
|
2009-04-17 21:04:34 +00:00
|
|
|
global_template->Set(String::New("read"), FunctionTemplate::New(Read));
|
2009-08-13 07:07:41 +00:00
|
|
|
global_template->Set(String::New("readline"),
|
|
|
|
FunctionTemplate::New(ReadLine));
|
2008-10-21 09:12:27 +00:00
|
|
|
global_template->Set(String::New("load"), FunctionTemplate::New(Load));
|
|
|
|
global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
|
|
|
|
global_template->Set(String::New("version"), FunctionTemplate::New(Version));
|
2011-09-08 09:49:52 +00:00
|
|
|
global_template->Set(String::New("enableProfiler"),
|
|
|
|
FunctionTemplate::New(EnableProfiler));
|
|
|
|
global_template->Set(String::New("disableProfiler"),
|
|
|
|
FunctionTemplate::New(DisableProfiler));
|
2009-03-31 06:51:25 +00:00
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
// Bind the handlers for external arrays.
|
2012-01-11 14:42:58 +00:00
|
|
|
global_template->Set(String::New("ArrayBuffer"),
|
|
|
|
FunctionTemplate::New(ArrayBuffer));
|
2011-05-31 09:42:17 +00:00
|
|
|
global_template->Set(String::New("Int8Array"),
|
|
|
|
FunctionTemplate::New(Int8Array));
|
|
|
|
global_template->Set(String::New("Uint8Array"),
|
|
|
|
FunctionTemplate::New(Uint8Array));
|
|
|
|
global_template->Set(String::New("Int16Array"),
|
|
|
|
FunctionTemplate::New(Int16Array));
|
|
|
|
global_template->Set(String::New("Uint16Array"),
|
|
|
|
FunctionTemplate::New(Uint16Array));
|
|
|
|
global_template->Set(String::New("Int32Array"),
|
|
|
|
FunctionTemplate::New(Int32Array));
|
|
|
|
global_template->Set(String::New("Uint32Array"),
|
|
|
|
FunctionTemplate::New(Uint32Array));
|
|
|
|
global_template->Set(String::New("Float32Array"),
|
|
|
|
FunctionTemplate::New(Float32Array));
|
|
|
|
global_template->Set(String::New("Float64Array"),
|
|
|
|
FunctionTemplate::New(Float64Array));
|
|
|
|
global_template->Set(String::New("PixelArray"),
|
|
|
|
FunctionTemplate::New(PixelArray));
|
|
|
|
|
2011-03-02 08:10:38 +00:00
|
|
|
#ifdef LIVE_OBJECT_LIST
|
2011-08-30 18:45:48 +00:00
|
|
|
global_template->Set(String::New("lol_is_enabled"), True());
|
2011-03-02 08:10:38 +00:00
|
|
|
#else
|
2011-08-30 18:45:48 +00:00
|
|
|
global_template->Set(String::New("lol_is_enabled"), False());
|
2011-03-02 08:10:38 +00:00
|
|
|
#endif
|
|
|
|
|
2011-09-06 14:48:08 +00:00
|
|
|
#if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64)
|
2009-03-31 06:51:25 +00:00
|
|
|
Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
|
2009-03-31 12:45:33 +00:00
|
|
|
AddOSMethods(os_templ);
|
2009-03-31 06:51:25 +00:00
|
|
|
global_template->Set(String::New("os"), os_templ);
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
return global_template;
|
|
|
|
}
|
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
void Shell::Initialize() {
|
2011-06-09 14:28:49 +00:00
|
|
|
#ifdef COMPRESS_STARTUP_DATA_BZ2
|
|
|
|
BZip2Decompressor startup_data_decompressor;
|
|
|
|
int bz2_result = startup_data_decompressor.Decompress();
|
|
|
|
if (bz2_result != BZ_OK) {
|
|
|
|
fprintf(stderr, "bzip error code: %d\n", bz2_result);
|
2011-09-13 13:16:13 +00:00
|
|
|
Exit(1);
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-06-09 14:28:49 +00:00
|
|
|
Shell::counter_map_ = new CounterMap();
|
|
|
|
// Set up counters
|
|
|
|
if (i::StrLength(i::FLAG_map_counters) != 0)
|
|
|
|
MapCounters(i::FLAG_map_counters);
|
|
|
|
if (i::FLAG_dump_counters) {
|
|
|
|
V8::SetCounterFunction(LookupCounter);
|
|
|
|
V8::SetCreateHistogramFunction(CreateHistogram);
|
|
|
|
V8::SetAddHistogramSampleFunction(AddHistogramSample);
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
if (options.test_shell) return;
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-06-24 20:04:32 +00:00
|
|
|
Locker lock;
|
2011-06-09 14:28:49 +00:00
|
|
|
HandleScope scope;
|
|
|
|
Handle<ObjectTemplate> global_template = CreateGlobalTemplate();
|
2008-10-21 09:12:27 +00:00
|
|
|
utility_context_ = Context::New(NULL, global_template);
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2011-06-24 20:04:32 +00:00
|
|
|
// Start the debugger agent if requested.
|
|
|
|
if (i::FLAG_debugger_agent) {
|
|
|
|
v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true);
|
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2009-03-09 08:44:59 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
Persistent<Context> Shell::CreateEvaluationContext() {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
// This needs to be a critical section since this is not thread-safe
|
|
|
|
i::ScopedLock lock(context_mutex_);
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-06-09 14:28:49 +00:00
|
|
|
// Initialize the global objects
|
|
|
|
Handle<ObjectTemplate> global_template = CreateGlobalTemplate();
|
2011-07-11 07:38:09 +00:00
|
|
|
Persistent<Context> context = Context::New(NULL, global_template);
|
2011-08-11 16:29:28 +00:00
|
|
|
ASSERT(!context.IsEmpty());
|
2011-07-11 07:38:09 +00:00
|
|
|
Context::Scope scope(context);
|
2008-11-25 12:58:48 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-06-16 08:35:26 +00:00
|
|
|
i::JSArguments js_args = i::FLAG_js_arguments;
|
|
|
|
i::Handle<i::FixedArray> arguments_array =
|
|
|
|
FACTORY->NewFixedArray(js_args.argc());
|
|
|
|
for (int j = 0; j < js_args.argc(); j++) {
|
|
|
|
i::Handle<i::String> arg =
|
|
|
|
FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j]));
|
|
|
|
arguments_array->set(j, *arg);
|
|
|
|
}
|
|
|
|
i::Handle<i::JSArray> arguments_jsarray =
|
|
|
|
FACTORY->NewJSArrayWithElements(arguments_array);
|
2011-07-11 07:38:09 +00:00
|
|
|
context->Global()->Set(String::New("arguments"),
|
2011-07-14 15:43:40 +00:00
|
|
|
Utils::ToLocal(arguments_jsarray));
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
return context;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 13:16:13 +00:00
|
|
|
void Shell::Exit(int exit_code) {
|
|
|
|
// Use _exit instead of exit to avoid races between isolate
|
|
|
|
// threads and static destructors.
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
_exit(exit_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-11-28 12:12:00 +00:00
|
|
|
struct CounterAndKey {
|
|
|
|
Counter* counter;
|
|
|
|
const char* key;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int CompareKeys(const void* a, const void* b) {
|
|
|
|
return strcmp(static_cast<const CounterAndKey*>(a)->key,
|
|
|
|
static_cast<const CounterAndKey*>(b)->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
void Shell::OnExit() {
|
2011-09-14 14:39:56 +00:00
|
|
|
if (console != NULL) console->Close();
|
2008-10-21 09:12:27 +00:00
|
|
|
if (i::FLAG_dump_counters) {
|
2011-11-28 12:12:00 +00:00
|
|
|
int number_of_counters = 0;
|
2009-03-24 13:33:54 +00:00
|
|
|
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
|
2011-11-28 12:12:00 +00:00
|
|
|
number_of_counters++;
|
|
|
|
}
|
|
|
|
CounterAndKey* counters = new CounterAndKey[number_of_counters];
|
|
|
|
int j = 0;
|
|
|
|
for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) {
|
|
|
|
counters[j].counter = i.CurrentValue();
|
|
|
|
counters[j].key = i.CurrentKey();
|
|
|
|
}
|
|
|
|
qsort(counters, number_of_counters, sizeof(counters[0]), CompareKeys);
|
|
|
|
printf("+--------------------------------------------+-------------+\n");
|
|
|
|
printf("| Name | Value |\n");
|
|
|
|
printf("+--------------------------------------------+-------------+\n");
|
|
|
|
for (j = 0; j < number_of_counters; j++) {
|
|
|
|
Counter* counter = counters[j].counter;
|
|
|
|
const char* key = counters[j].key;
|
2009-04-14 00:30:44 +00:00
|
|
|
if (counter->is_histogram()) {
|
2011-11-28 12:12:00 +00:00
|
|
|
printf("| c:%-40s | %11i |\n", key, counter->count());
|
|
|
|
printf("| t:%-40s | %11i |\n", key, counter->sample_total());
|
2009-04-14 00:30:44 +00:00
|
|
|
} else {
|
2011-11-28 12:12:00 +00:00
|
|
|
printf("| %-42s | %11i |\n", key, counter->count());
|
2009-04-14 00:30:44 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-11-28 12:12:00 +00:00
|
|
|
printf("+--------------------------------------------+-------------+\n");
|
|
|
|
delete [] counters;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2008-12-03 09:35:21 +00:00
|
|
|
if (counters_file_ != NULL)
|
|
|
|
delete counters_file_;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
|
|
|
|
|
|
|
|
|
|
|
static FILE* FOpen(const char* path, const char* mode) {
|
2011-09-30 07:46:13 +00:00
|
|
|
#if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64))
|
2011-08-08 09:34:18 +00:00
|
|
|
FILE* result;
|
|
|
|
if (fopen_s(&result, path, mode) == 0) {
|
2011-08-08 09:53:28 +00:00
|
|
|
return result;
|
2011-08-08 09:34:18 +00:00
|
|
|
} else {
|
2011-08-08 09:53:28 +00:00
|
|
|
return NULL;
|
2011-08-08 09:34:18 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
FILE* file = fopen(path, mode);
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
struct stat file_stat;
|
|
|
|
if (fstat(fileno(file), &file_stat) != 0) return NULL;
|
|
|
|
bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
|
|
|
|
if (is_regular_file) return file;
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
2009-05-13 12:34:35 +00:00
|
|
|
static char* ReadChars(const char* name, int* size_out) {
|
2011-07-11 07:38:09 +00:00
|
|
|
// Release the V8 lock while reading files.
|
|
|
|
v8::Unlocker unlocker(Isolate::GetCurrent());
|
2011-08-08 09:34:18 +00:00
|
|
|
FILE* file = FOpen(name, "rb");
|
2009-01-20 14:14:22 +00:00
|
|
|
if (file == NULL) return NULL;
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
|
|
|
for (int i = 0; i < size;) {
|
2011-07-26 08:15:49 +00:00
|
|
|
int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
|
2008-10-21 09:12:27 +00:00
|
|
|
i += read;
|
|
|
|
}
|
|
|
|
fclose(file);
|
2009-01-20 14:14:22 +00:00
|
|
|
*size_out = size;
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2009-02-20 11:31:44 +00:00
|
|
|
static char* ReadToken(char* data, char token) {
|
|
|
|
char* next = i::OS::StrChr(data, token);
|
2009-02-19 04:26:04 +00:00
|
|
|
if (next != NULL) {
|
|
|
|
*next = '\0';
|
|
|
|
return (next + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-20 11:31:44 +00:00
|
|
|
static char* ReadLine(char* data) {
|
2009-02-19 04:26:04 +00:00
|
|
|
return ReadToken(data, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-20 11:31:44 +00:00
|
|
|
static char* ReadWord(char* data) {
|
2009-02-19 04:26:04 +00:00
|
|
|
return ReadToken(data, ' ');
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2009-02-19 04:26:04 +00:00
|
|
|
|
|
|
|
|
2009-01-20 14:14:22 +00:00
|
|
|
// Reads a file into a v8 string.
|
|
|
|
Handle<String> Shell::ReadFile(const char* name) {
|
|
|
|
int size = 0;
|
|
|
|
char* chars = ReadChars(name, &size);
|
|
|
|
if (chars == NULL) return Handle<String>();
|
|
|
|
Handle<String> result = String::New(chars);
|
2008-10-21 09:12:27 +00:00
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::RunShell() {
|
2011-07-14 15:43:40 +00:00
|
|
|
Locker locker;
|
|
|
|
Context::Scope context_scope(evaluation_context_);
|
2011-09-06 09:07:39 +00:00
|
|
|
HandleScope outer_scope;
|
2011-07-14 15:43:40 +00:00
|
|
|
Handle<String> name = String::New("(d8)");
|
2011-09-14 14:39:56 +00:00
|
|
|
console = LineEditor::Get();
|
|
|
|
printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
|
|
|
|
console->Open();
|
2008-10-21 09:12:27 +00:00
|
|
|
while (true) {
|
2011-09-06 09:07:39 +00:00
|
|
|
HandleScope inner_scope;
|
2012-01-17 13:37:09 +00:00
|
|
|
Handle<String> input = console->Prompt(Shell::kPrompt);
|
|
|
|
if (input.IsEmpty()) break;
|
|
|
|
ExecuteString(input, name, true, true);
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2009-01-20 14:14:22 +00:00
|
|
|
class ShellThread : public i::Thread {
|
|
|
|
public:
|
2011-09-13 13:16:13 +00:00
|
|
|
// Takes ownership of the underlying char array of |files|.
|
|
|
|
ShellThread(int no, char* files)
|
2011-07-14 15:43:40 +00:00
|
|
|
: Thread("d8:ShellThread"),
|
|
|
|
no_(no), files_(files) { }
|
2011-09-13 13:16:13 +00:00
|
|
|
|
|
|
|
~ShellThread() {
|
|
|
|
delete[] files_;
|
|
|
|
}
|
|
|
|
|
2009-01-20 14:14:22 +00:00
|
|
|
virtual void Run();
|
|
|
|
private:
|
|
|
|
int no_;
|
2011-09-13 13:16:13 +00:00
|
|
|
char* files_;
|
2009-01-20 14:14:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void ShellThread::Run() {
|
2011-09-13 13:16:13 +00:00
|
|
|
char* ptr = files_;
|
2009-01-26 18:09:46 +00:00
|
|
|
while ((ptr != NULL) && (*ptr != '\0')) {
|
2009-01-20 14:14:22 +00:00
|
|
|
// For each newline-separated line.
|
2009-02-19 04:26:04 +00:00
|
|
|
char* next_line = ReadLine(ptr);
|
|
|
|
|
|
|
|
if (*ptr == '#') {
|
|
|
|
// Skip comment lines.
|
|
|
|
ptr = next_line;
|
|
|
|
continue;
|
2009-01-20 14:14:22 +00:00
|
|
|
}
|
2009-01-26 18:23:01 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
// Prepare the context for this thread.
|
|
|
|
Locker locker;
|
2011-09-13 13:16:13 +00:00
|
|
|
HandleScope outer_scope;
|
2011-07-11 07:38:09 +00:00
|
|
|
Persistent<Context> thread_context = Shell::CreateEvaluationContext();
|
2009-02-19 04:26:04 +00:00
|
|
|
Context::Scope context_scope(thread_context);
|
|
|
|
|
|
|
|
while ((ptr != NULL) && (*ptr != '\0')) {
|
2011-09-13 13:16:13 +00:00
|
|
|
HandleScope inner_scope;
|
2009-02-19 04:26:04 +00:00
|
|
|
char* filename = ptr;
|
|
|
|
ptr = ReadWord(ptr);
|
|
|
|
|
|
|
|
// Skip empty strings.
|
|
|
|
if (strlen(filename) == 0) {
|
2011-09-13 13:16:13 +00:00
|
|
|
continue;
|
2009-02-19 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> str = Shell::ReadFile(filename);
|
|
|
|
if (str.IsEmpty()) {
|
2011-09-13 13:16:13 +00:00
|
|
|
printf("File '%s' not found\n", filename);
|
|
|
|
Shell::Exit(1);
|
2009-02-19 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Shell::ExecuteString(str, String::New(filename), false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_context.Dispose();
|
|
|
|
ptr = next_line;
|
|
|
|
}
|
2009-01-20 14:14:22 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2009-01-20 14:14:22 +00:00
|
|
|
|
2009-03-16 10:08:59 +00:00
|
|
|
|
2011-09-08 22:44:03 +00:00
|
|
|
SourceGroup::~SourceGroup() {
|
2011-09-08 23:43:36 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-09-08 22:44:03 +00:00
|
|
|
delete next_semaphore_;
|
|
|
|
next_semaphore_ = NULL;
|
|
|
|
delete done_semaphore_;
|
|
|
|
done_semaphore_ = NULL;
|
|
|
|
delete thread_;
|
|
|
|
thread_ = NULL;
|
2011-09-08 23:43:36 +00:00
|
|
|
#endif // V8_SHARED
|
2011-09-08 22:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
void SourceGroup::Execute() {
|
|
|
|
for (int i = begin_offset_; i < end_offset_; ++i) {
|
|
|
|
const char* arg = argv_[i];
|
|
|
|
if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
|
|
|
|
// Execute argument given to -e option directly.
|
|
|
|
HandleScope handle_scope;
|
|
|
|
Handle<String> file_name = String::New("unnamed");
|
|
|
|
Handle<String> source = String::New(argv_[i + 1]);
|
|
|
|
if (!Shell::ExecuteString(source, file_name, false, true)) {
|
2011-09-13 13:16:13 +00:00
|
|
|
Shell::Exit(1);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else if (arg[0] == '-') {
|
|
|
|
// Ignore other options. They have been parsed already.
|
|
|
|
} else {
|
|
|
|
// Use all other arguments as names of files to load and run.
|
|
|
|
HandleScope handle_scope;
|
|
|
|
Handle<String> file_name = String::New(arg);
|
|
|
|
Handle<String> source = ReadFile(arg);
|
|
|
|
if (source.IsEmpty()) {
|
|
|
|
printf("Error reading '%s'\n", arg);
|
2011-09-13 13:16:13 +00:00
|
|
|
Shell::Exit(1);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
if (!Shell::ExecuteString(source, file_name, false, true)) {
|
2011-09-13 13:16:13 +00:00
|
|
|
Shell::Exit(1);
|
2008-11-25 12:58:48 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
|
|
|
|
Handle<String> SourceGroup::ReadFile(const char* name) {
|
2011-08-08 09:34:18 +00:00
|
|
|
int size;
|
|
|
|
const char* chars = ReadChars(name, &size);
|
|
|
|
if (chars == NULL) return Handle<String>();
|
2011-07-11 07:38:09 +00:00
|
|
|
Handle<String> result = String::New(chars, size);
|
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
i::Thread::Options SourceGroup::GetThreadOptions() {
|
2012-01-17 13:13:55 +00:00
|
|
|
i::Thread::Options options;
|
|
|
|
options.name = "IsolateThread";
|
2011-07-11 07:38:09 +00:00
|
|
|
// On some systems (OSX 10.6) the stack size default is 0.5Mb or less
|
|
|
|
// which is not enough to parse the big literal expressions used in tests.
|
|
|
|
// The stack size should be at least StackGuard::kLimitSize + some
|
2012-01-17 13:13:55 +00:00
|
|
|
// OS-specific padding for thread startup code.
|
|
|
|
options.stack_size = 2 << 20; // 2 Mb seems to be enough
|
|
|
|
return options;
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SourceGroup::ExecuteInThread() {
|
|
|
|
Isolate* isolate = Isolate::New();
|
|
|
|
do {
|
2011-09-08 22:44:03 +00:00
|
|
|
if (next_semaphore_ != NULL) next_semaphore_->Wait();
|
2011-07-11 07:38:09 +00:00
|
|
|
{
|
|
|
|
Isolate::Scope iscope(isolate);
|
|
|
|
Locker lock(isolate);
|
|
|
|
HandleScope scope;
|
|
|
|
Persistent<Context> context = Shell::CreateEvaluationContext();
|
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
Execute();
|
|
|
|
}
|
|
|
|
context.Dispose();
|
2009-03-16 10:08:59 +00:00
|
|
|
}
|
2011-09-08 22:44:03 +00:00
|
|
|
if (done_semaphore_ != NULL) done_semaphore_->Signal();
|
2011-07-11 07:38:09 +00:00
|
|
|
} while (!Shell::options.last_run);
|
|
|
|
isolate->Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SourceGroup::StartExecuteInThread() {
|
2011-09-08 22:44:03 +00:00
|
|
|
if (thread_ == NULL) {
|
|
|
|
thread_ = new IsolateThread(this);
|
2011-07-11 07:38:09 +00:00
|
|
|
thread_->Start();
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
next_semaphore_->Signal();
|
|
|
|
}
|
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
void SourceGroup::WaitForThread() {
|
2011-09-08 22:44:03 +00:00
|
|
|
if (thread_ == NULL) return;
|
2011-07-11 07:38:09 +00:00
|
|
|
if (Shell::options.last_run) {
|
|
|
|
thread_->Join();
|
|
|
|
} else {
|
|
|
|
done_semaphore_->Wait();
|
2009-01-20 14:14:22 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
bool Shell::SetOptions(int argc, char* argv[]) {
|
2011-06-09 14:28:49 +00:00
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "--stress-opt") == 0) {
|
2011-07-11 07:38:09 +00:00
|
|
|
options.stress_opt = true;
|
2011-06-09 14:28:49 +00:00
|
|
|
argv[i] = NULL;
|
|
|
|
} else if (strcmp(argv[i], "--stress-deopt") == 0) {
|
2011-07-11 07:38:09 +00:00
|
|
|
options.stress_deopt = true;
|
2011-06-09 14:28:49 +00:00
|
|
|
argv[i] = NULL;
|
|
|
|
} else if (strcmp(argv[i], "--noalways-opt") == 0) {
|
|
|
|
// No support for stressing if we can't use --always-opt.
|
2011-07-11 07:38:09 +00:00
|
|
|
options.stress_opt = false;
|
|
|
|
options.stress_deopt = false;
|
2011-06-09 14:28:49 +00:00
|
|
|
} else if (strcmp(argv[i], "--shell") == 0) {
|
2011-07-11 07:38:09 +00:00
|
|
|
options.interactive_shell = true;
|
2011-06-24 20:04:32 +00:00
|
|
|
argv[i] = NULL;
|
|
|
|
} else if (strcmp(argv[i], "--test") == 0) {
|
2011-07-11 07:38:09 +00:00
|
|
|
options.test_shell = true;
|
|
|
|
argv[i] = NULL;
|
|
|
|
} else if (strcmp(argv[i], "--preemption") == 0) {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
printf("D8 with shared library does not support multi-threading\n");
|
|
|
|
return false;
|
|
|
|
#else
|
2011-07-11 07:38:09 +00:00
|
|
|
options.use_preemption = true;
|
2011-06-09 14:28:49 +00:00
|
|
|
argv[i] = NULL;
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
} else if (strcmp(argv[i], "--no-preemption") == 0) {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
printf("D8 with shared library does not support multi-threading\n");
|
|
|
|
return false;
|
|
|
|
#else
|
2011-07-11 07:38:09 +00:00
|
|
|
options.use_preemption = false;
|
|
|
|
argv[i] = NULL;
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
} else if (strcmp(argv[i], "--preemption-interval") == 0) {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
printf("D8 with shared library does not support multi-threading\n");
|
|
|
|
return false;
|
|
|
|
#else
|
2011-07-11 07:38:09 +00:00
|
|
|
if (++i < argc) {
|
|
|
|
argv[i-1] = NULL;
|
|
|
|
char* end = NULL;
|
|
|
|
options.preemption_interval = strtol(argv[i], &end, 10); // NOLINT
|
|
|
|
if (options.preemption_interval <= 0
|
|
|
|
|| *end != '\0'
|
|
|
|
|| errno == ERANGE) {
|
|
|
|
printf("Invalid value for --preemption-interval '%s'\n", argv[i]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
|
|
|
} else {
|
|
|
|
printf("Missing value for --preemption-interval\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
} else if (strcmp(argv[i], "-f") == 0) {
|
|
|
|
// Ignore any -f flags for compatibility with other stand-alone
|
|
|
|
// JavaScript engines.
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(argv[i], "--isolate") == 0) {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
printf("D8 with shared library does not support multi-threading\n");
|
|
|
|
return false;
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
options.num_isolates++;
|
2011-09-13 13:16:13 +00:00
|
|
|
} else if (strcmp(argv[i], "-p") == 0) {
|
|
|
|
#ifdef V8_SHARED
|
|
|
|
printf("D8 with shared library does not support multi-threading\n");
|
|
|
|
return false;
|
2011-09-13 14:31:54 +00:00
|
|
|
#else
|
|
|
|
options.num_parallel_files++;
|
2011-09-13 13:16:13 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
else if (strcmp(argv[i], "--dump-counters") == 0) {
|
|
|
|
printf("D8 with shared library does not include counters\n");
|
|
|
|
return false;
|
|
|
|
} else if (strcmp(argv[i], "--debugger") == 0) {
|
|
|
|
printf("Javascript debugger not included\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
// Run parallel threads if we are not using --isolate
|
2011-09-13 13:16:13 +00:00
|
|
|
options.parallel_files = new char*[options.num_parallel_files];
|
|
|
|
int parallel_files_set = 0;
|
2011-07-11 07:38:09 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (argv[i] == NULL) continue;
|
|
|
|
if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
|
|
|
if (options.num_isolates > 1) {
|
|
|
|
printf("-p is not compatible with --isolate\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
2011-09-13 13:16:13 +00:00
|
|
|
i++;
|
|
|
|
options.parallel_files[parallel_files_set] = argv[i];
|
|
|
|
parallel_files_set++;
|
2011-07-11 07:38:09 +00:00
|
|
|
argv[i] = NULL;
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-13 13:16:13 +00:00
|
|
|
if (parallel_files_set != options.num_parallel_files) {
|
|
|
|
printf("-p requires a file containing a list of files as parameter\n");
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-06-09 14:28:49 +00:00
|
|
|
|
|
|
|
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
|
|
|
|
|
2011-09-13 09:31:41 +00:00
|
|
|
// Set up isolated source groups.
|
2011-07-11 07:38:09 +00:00
|
|
|
options.isolate_sources = new SourceGroup[options.num_isolates];
|
|
|
|
SourceGroup* current = options.isolate_sources;
|
|
|
|
current->Begin(argv, 1);
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
const char* str = argv[i];
|
|
|
|
if (strcmp(str, "--isolate") == 0) {
|
|
|
|
current->End(i);
|
|
|
|
current++;
|
|
|
|
current->Begin(argv, i + 1);
|
|
|
|
} else if (strncmp(argv[i], "--", 2) == 0) {
|
|
|
|
printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current->End(argc);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Shell::RunMain(int argc, char* argv[]) {
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
i::List<i::Thread*> threads(1);
|
2011-09-13 13:16:13 +00:00
|
|
|
if (options.parallel_files != NULL) {
|
|
|
|
for (int i = 0; i < options.num_parallel_files; i++) {
|
|
|
|
char* files = NULL;
|
|
|
|
{ Locker lock(Isolate::GetCurrent());
|
|
|
|
int size = 0;
|
|
|
|
files = ReadChars(options.parallel_files[i], &size);
|
|
|
|
}
|
|
|
|
if (files == NULL) {
|
|
|
|
printf("File list '%s' not found\n", options.parallel_files[i]);
|
|
|
|
Exit(1);
|
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
ShellThread* thread = new ShellThread(threads.length(), files);
|
|
|
|
thread->Start();
|
|
|
|
threads.Add(thread);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2011-09-13 13:16:13 +00:00
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
for (int i = 1; i < options.num_isolates; ++i) {
|
|
|
|
options.isolate_sources[i].StartExecuteInThread();
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
{ // NOLINT
|
2011-07-11 07:38:09 +00:00
|
|
|
Locker lock;
|
|
|
|
HandleScope scope;
|
|
|
|
Persistent<Context> context = CreateEvaluationContext();
|
2011-09-21 13:42:25 +00:00
|
|
|
if (options.last_run) {
|
|
|
|
// Keep using the same context in the interactive shell.
|
|
|
|
evaluation_context_ = context;
|
2011-09-29 08:55:36 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2011-09-21 13:42:25 +00:00
|
|
|
// If the interactive debugger is enabled make sure to activate
|
|
|
|
// it before running the files passed on the command line.
|
|
|
|
if (i::FLAG_debugger) {
|
|
|
|
InstallUtilityScript();
|
|
|
|
}
|
2011-09-29 08:55:36 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2011-09-21 13:42:25 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
options.isolate_sources[0].Execute();
|
|
|
|
}
|
2011-09-21 13:42:25 +00:00
|
|
|
if (!options.last_run) {
|
2011-07-11 07:38:09 +00:00
|
|
|
context.Dispose();
|
|
|
|
}
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
// Start preemption if threads have been created and preemption is enabled.
|
2011-09-13 13:16:13 +00:00
|
|
|
if (threads.length() > 0
|
2011-07-11 07:38:09 +00:00
|
|
|
&& options.use_preemption) {
|
|
|
|
Locker::StartPreemption(options.preemption_interval);
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
for (int i = 1; i < options.num_isolates; ++i) {
|
|
|
|
options.isolate_sources[i].WaitForThread();
|
|
|
|
}
|
|
|
|
|
2011-09-13 13:16:13 +00:00
|
|
|
for (int i = 0; i < threads.length(); i++) {
|
|
|
|
i::Thread* thread = threads[i];
|
|
|
|
thread->Join();
|
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threads.length() > 0 && options.use_preemption) {
|
|
|
|
Locker lock;
|
|
|
|
Locker::StopPreemption();
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Shell::Main(int argc, char* argv[]) {
|
|
|
|
if (!SetOptions(argc, argv)) return 1;
|
|
|
|
Initialize();
|
2011-06-09 14:28:49 +00:00
|
|
|
|
|
|
|
int result = 0;
|
2011-07-11 07:38:09 +00:00
|
|
|
if (options.stress_opt || options.stress_deopt) {
|
|
|
|
Testing::SetStressRunType(
|
|
|
|
options.stress_opt ? Testing::kStressTypeOpt
|
|
|
|
: Testing::kStressTypeDeopt);
|
|
|
|
int stress_runs = Testing::GetStressRuns();
|
2011-06-09 14:28:49 +00:00
|
|
|
for (int i = 0; i < stress_runs && result == 0; i++) {
|
|
|
|
printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
|
2011-07-11 07:38:09 +00:00
|
|
|
Testing::PrepareStressRun(i);
|
|
|
|
options.last_run = (i == stress_runs - 1);
|
|
|
|
result = RunMain(argc, argv);
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
|
|
|
printf("======== Full Deoptimization =======\n");
|
2011-07-11 07:38:09 +00:00
|
|
|
Testing::DeoptimizeAll();
|
2011-06-09 14:28:49 +00:00
|
|
|
} else {
|
2011-07-11 07:38:09 +00:00
|
|
|
result = RunMain(argc, argv);
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2011-06-24 20:04:32 +00:00
|
|
|
// Run remote debugger if requested, but never on --test
|
2011-07-11 07:38:09 +00:00
|
|
|
if (i::FLAG_remote_debugger && !options.test_shell) {
|
2011-06-24 20:04:32 +00:00
|
|
|
InstallUtilityScript();
|
2011-06-09 14:28:49 +00:00
|
|
|
RunRemoteDebugger(i::FLAG_debugger_port);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2011-06-09 14:28:49 +00:00
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
// Run interactive shell if explicitly requested or if no script has been
|
|
|
|
// executed, but never on --test
|
2011-07-11 07:38:09 +00:00
|
|
|
|
|
|
|
if (( options.interactive_shell
|
|
|
|
|| !options.script_executed )
|
|
|
|
&& !options.test_shell ) {
|
2011-09-29 08:55:36 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2011-09-21 13:42:25 +00:00
|
|
|
if (!i::FLAG_debugger) {
|
|
|
|
InstallUtilityScript();
|
|
|
|
}
|
2011-09-29 08:55:36 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2011-06-09 14:28:49 +00:00
|
|
|
RunShell();
|
|
|
|
}
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
V8::Dispose();
|
2011-08-24 10:48:48 +00:00
|
|
|
|
|
|
|
#ifndef V8_SHARED
|
2011-08-24 09:47:10 +00:00
|
|
|
OnExit();
|
2011-08-24 10:48:48 +00:00
|
|
|
#endif // V8_SHARED
|
2011-06-09 14:28:49 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-06-09 15:07:45 +00:00
|
|
|
} // namespace v8
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
2011-05-23 10:41:02 +00:00
|
|
|
#ifndef GOOGLE3
|
2008-10-21 09:12:27 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
return v8::Shell::Main(argc, argv);
|
|
|
|
}
|
2011-05-23 10:41:02 +00:00
|
|
|
#endif
|