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.
|
|
|
|
|
|
|
|
|
2012-05-16 15:48:06 +00:00
|
|
|
// Defined when linking against shared lib on Windows.
|
|
|
|
#if defined(USING_V8_SHARED) && !defined(V8_SHARED)
|
2011-08-08 09:34:18 +00:00
|
|
|
#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>
|
2013-04-24 11:20:16 +00:00
|
|
|
#endif // V8_SHARED
|
|
|
|
|
|
|
|
#ifndef V8_SHARED
|
|
|
|
#include <algorithm>
|
|
|
|
#endif // !V8_SHARED
|
|
|
|
|
|
|
|
#ifdef V8_SHARED
|
2011-07-14 15:43:40 +00:00
|
|
|
#include "../include/v8-testing.h"
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2013-04-12 12:48:27 +00:00
|
|
|
#ifdef ENABLE_VTUNE_JIT_INTERFACE
|
|
|
|
#include "third_party/vtune/v8-vtune.h"
|
|
|
|
#endif
|
|
|
|
|
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"
|
2013-11-19 13:08:37 +00:00
|
|
|
#include "cpu.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
|
|
|
|
2013-07-29 10:23:07 +00:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2011-07-11 07:38:09 +00:00
|
|
|
#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
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
|
2013-11-22 12:35:39 +00:00
|
|
|
static Handle<Value> Throw(Isolate* isolate, const char* message) {
|
|
|
|
return isolate->ThrowException(String::NewFromUtf8(isolate, message));
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
class PerIsolateData {
|
2012-11-20 14:47:56 +00:00
|
|
|
public:
|
2013-04-22 11:29:52 +00:00
|
|
|
explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(isolate);
|
2013-11-20 15:16:18 +00:00
|
|
|
isolate->SetData(0, this);
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
~PerIsolateData() {
|
2013-11-20 15:16:18 +00:00
|
|
|
isolate_->SetData(0, NULL); // Not really needed, just to be sure...
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
inline static PerIsolateData* Get(Isolate* isolate) {
|
2013-11-20 15:16:18 +00:00
|
|
|
return reinterpret_cast<PerIsolateData*>(isolate->GetData(0));
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
|
|
|
|
class RealmScope {
|
|
|
|
public:
|
|
|
|
explicit RealmScope(PerIsolateData* data);
|
|
|
|
~RealmScope();
|
|
|
|
private:
|
|
|
|
PerIsolateData* data_;
|
|
|
|
};
|
2012-11-20 14:47:56 +00:00
|
|
|
|
|
|
|
private:
|
2013-04-22 11:29:52 +00:00
|
|
|
friend class Shell;
|
|
|
|
friend class RealmScope;
|
2012-11-20 14:47:56 +00:00
|
|
|
Isolate* isolate_;
|
2013-04-22 11:29:52 +00:00
|
|
|
int realm_count_;
|
|
|
|
int realm_current_;
|
|
|
|
int realm_switch_;
|
|
|
|
Persistent<Context>* realms_;
|
|
|
|
Persistent<Value> realm_shared_;
|
|
|
|
|
|
|
|
int RealmFind(Handle<Context> context);
|
2012-11-20 14:47:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-28 17:40:24 +00:00
|
|
|
LineEditor *LineEditor::current_ = NULL;
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
LineEditor::LineEditor(Type type, const char* name)
|
2013-02-28 17:40:24 +00:00
|
|
|
: type_(type), name_(name) {
|
|
|
|
if (current_ == NULL || current_->type_ < type) current_ = this;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DumbLineEditor: public LineEditor {
|
|
|
|
public:
|
2012-11-20 14:47:56 +00:00
|
|
|
explicit DumbLineEditor(Isolate* isolate)
|
|
|
|
: LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { }
|
2012-01-17 13:37:09 +00:00
|
|
|
virtual Handle<String> Prompt(const char* prompt);
|
2012-11-20 14:47:56 +00:00
|
|
|
private:
|
|
|
|
Isolate* isolate_;
|
2008-10-21 09:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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);
|
2013-04-09 15:11:45 +00:00
|
|
|
#if defined(__native_client__)
|
|
|
|
// Native Client libc is used to being embedded in Chrome and
|
|
|
|
// has trouble recognizing when to flush.
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2012-11-20 14:47:56 +00:00
|
|
|
return Shell::ReadFromStdin(isolate_);
|
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_;
|
2013-08-29 09:58:30 +00:00
|
|
|
i::Mutex Shell::context_mutex_;
|
2013-10-24 12:25:40 +00:00
|
|
|
const i::TimeTicks Shell::kInitialTicks = i::TimeTicks::HighResolutionNow();
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2012-01-24 15:48:16 +00:00
|
|
|
const int MB = 1024 * 1024;
|
|
|
|
|
|
|
|
|
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.
|
2013-03-15 12:06:53 +00:00
|
|
|
bool Shell::ExecuteString(Isolate* isolate,
|
|
|
|
Handle<String> source,
|
2008-10-21 09:12:27 +00:00
|
|
|
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
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2008-10-21 09:12:27 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
Handle<Script> script = Script::New(source, name);
|
2008-10-21 09:12:27 +00:00
|
|
|
if (script.IsEmpty()) {
|
|
|
|
// Print errors that happened during compilation.
|
2011-07-14 15:43:40 +00:00
|
|
|
if (report_exceptions && !FLAG_debugger)
|
2013-03-15 12:06:53 +00:00
|
|
|
ReportException(isolate, &try_catch);
|
2008-10-21 09:12:27 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
Local<Context> realm =
|
2013-05-02 20:18:42 +00:00
|
|
|
Local<Context>::New(isolate, data->realms_[data->realm_current_]);
|
2013-04-22 11:29:52 +00:00
|
|
|
realm->Enter();
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<Value> result = script->Run();
|
2013-04-22 11:29:52 +00:00
|
|
|
realm->Exit();
|
|
|
|
data->realm_current_ = data->realm_switch_;
|
2008-10-21 09:12:27 +00:00
|
|
|
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)
|
2013-03-15 12:06:53 +00:00
|
|
|
ReportException(isolate, &try_catch);
|
2008-10-21 09:12:27 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2011-02-16 11:40:48 +00:00
|
|
|
ASSERT(!try_catch.HasCaught());
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
if (print_result) {
|
2013-03-18 16:04:27 +00:00
|
|
|
#if !defined(V8_SHARED)
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
if (options.test_shell) {
|
2013-03-18 16:04:27 +00:00
|
|
|
#endif
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
if (!result->IsUndefined()) {
|
|
|
|
// If all went well and the result wasn't undefined then print
|
|
|
|
// the returned value.
|
|
|
|
v8::String::Utf8Value str(result);
|
|
|
|
fwrite(*str, sizeof(**str), str.length(), stdout);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-03-18 16:04:27 +00:00
|
|
|
#if !defined(V8_SHARED)
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
} else {
|
2013-03-22 17:27:44 +00:00
|
|
|
v8::TryCatch try_catch;
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, utility_context_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2013-05-29 08:39:37 +00:00
|
|
|
Handle<Object> global = context->Global();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<Value> fun =
|
|
|
|
global->Get(String::NewFromUtf8(isolate, "Stringify"));
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
Handle<Value> argv[1] = { result };
|
|
|
|
Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv);
|
2013-03-22 17:27:44 +00:00
|
|
|
if (try_catch.HasCaught()) return true;
|
More useful result pretty printing for d8
For example:
d8> ["hi", 5, [2,3],Object.prototype.toString, {get a() { return a}, b: 9, set a(x) {this.b=x}}, null, undefined]
["hi", 5, [2, 3], function toString() { [native code] }, {get a() { return a}, set a(x) {this.b=x}, b: 9}, null, undefined]
Deactivated in test mode.
R=yangguo@chromium.org
BUG=
Review URL: https://codereview.chromium.org/12653003
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13966 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-03-18 14:03:15 +00:00
|
|
|
v8::String::Utf8Value str(s);
|
|
|
|
fwrite(*str, sizeof(**str), str.length(), stdout);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-03-18 16:04:27 +00:00
|
|
|
#endif
|
2013-03-18 16:18:05 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
|
|
|
|
data_->realm_count_ = 1;
|
|
|
|
data_->realm_current_ = 0;
|
|
|
|
data_->realm_switch_ = 0;
|
|
|
|
data_->realms_ = new Persistent<Context>[1];
|
2013-09-23 11:25:52 +00:00
|
|
|
data_->realms_[0].Reset(data_->isolate_,
|
|
|
|
data_->isolate_->GetEnteredContext());
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PerIsolateData::RealmScope::~RealmScope() {
|
|
|
|
// Drop realms to avoid keeping them alive.
|
|
|
|
for (int i = 0; i < data_->realm_count_; ++i)
|
2013-11-22 12:35:39 +00:00
|
|
|
data_->realms_[i].Reset();
|
2013-04-22 11:29:52 +00:00
|
|
|
delete[] data_->realms_;
|
|
|
|
if (!data_->realm_shared_.IsEmpty())
|
2013-11-22 12:35:39 +00:00
|
|
|
data_->realm_shared_.Reset();
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PerIsolateData::RealmFind(Handle<Context> context) {
|
|
|
|
for (int i = 0; i < realm_count_; ++i) {
|
|
|
|
if (realms_[i] == context) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-24 12:25:40 +00:00
|
|
|
#ifndef V8_SHARED
|
|
|
|
// performance.now() returns a time stamp as double, measured in milliseconds.
|
|
|
|
void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
i::TimeDelta delta = i::TimeTicks::HighResolutionNow() - kInitialTicks;
|
|
|
|
args.GetReturnValue().Set(delta.InMillisecondsF());
|
|
|
|
}
|
|
|
|
#endif // V8_SHARED
|
|
|
|
|
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
// Realm.current() returns the index of the currently active realm.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
2013-09-23 11:25:52 +00:00
|
|
|
int index = data->RealmFind(isolate->GetEnteredContext());
|
2013-06-05 12:36:33 +00:00
|
|
|
if (index == -1) return;
|
|
|
|
args.GetReturnValue().Set(index);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.owner(o) returns the index of the realm that created o.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
if (args.Length() < 1 || !args[0]->IsObject()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
int index = data->RealmFind(args[0]->ToObject()->CreationContext());
|
2013-06-05 12:36:33 +00:00
|
|
|
if (index == -1) return;
|
|
|
|
args.GetReturnValue().Set(index);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.global(i) returns the global object of realm i.
|
|
|
|
// (Note that properties of global objects cannot be read/written cross-realm.)
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
|
|
|
|
if (args.Length() < 1 || !args[0]->IsNumber()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
int index = args[0]->Uint32Value();
|
|
|
|
if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid realm index");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(
|
|
|
|
Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global());
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.create() creates a new realm and returns its index.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
Persistent<Context>* old_realms = data->realms_;
|
|
|
|
int index = data->realm_count_;
|
|
|
|
data->realms_ = new Persistent<Context>[++data->realm_count_];
|
2013-05-29 08:39:37 +00:00
|
|
|
for (int i = 0; i < index; ++i) {
|
|
|
|
data->realms_[i].Reset(isolate, old_realms[i]);
|
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
delete[] old_realms;
|
|
|
|
Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
|
2013-05-29 08:39:37 +00:00
|
|
|
data->realms_[index].Reset(
|
2013-04-22 11:29:52 +00:00
|
|
|
isolate, Context::New(isolate, NULL, global_template));
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(index);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.dispose(i) disposes the reference to the realm i.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
if (args.Length() < 1 || !args[0]->IsNumber()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
int index = args[0]->Uint32Value();
|
|
|
|
if (index >= data->realm_count_ || data->realms_[index].IsEmpty() ||
|
|
|
|
index == 0 ||
|
|
|
|
index == data->realm_current_ || index == data->realm_switch_) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid realm index");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
2013-11-22 12:35:39 +00:00
|
|
|
data->realms_[index].Reset();
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.switch(i) switches to the realm i for consecutive interactive inputs.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
if (args.Length() < 1 || !args[0]->IsNumber()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
int index = args[0]->Uint32Value();
|
|
|
|
if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid realm index");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
data->realm_switch_ = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.eval(i, s) evaluates s in realm i and returns the result.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
int index = args[0]->Uint32Value();
|
|
|
|
if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid realm index");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
Handle<Script> script = Script::New(args[1]->ToString());
|
2013-06-05 12:36:33 +00:00
|
|
|
if (script.IsEmpty()) return;
|
2013-05-02 20:18:42 +00:00
|
|
|
Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]);
|
2013-04-22 11:29:52 +00:00
|
|
|
realm->Enter();
|
|
|
|
Handle<Value> result = script->Run();
|
|
|
|
realm->Exit();
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(result);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Realm.shared is an accessor for a single shared value across realms.
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::RealmSharedGet(Local<String> property,
|
|
|
|
const PropertyCallbackInfo<Value>& info) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = info.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
2013-06-05 12:36:33 +00:00
|
|
|
if (data->realm_shared_.IsEmpty()) return;
|
|
|
|
info.GetReturnValue().Set(data->realm_shared_);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shell::RealmSharedSet(Local<String> property,
|
|
|
|
Local<Value> value,
|
2013-06-05 12:36:33 +00:00
|
|
|
const PropertyCallbackInfo<void>& info) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = info.GetIsolate();
|
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
2013-05-29 08:39:37 +00:00
|
|
|
data->realm_shared_.Reset(isolate, value);
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Write(args);
|
2009-08-12 11:52:22 +00:00
|
|
|
printf("\n");
|
2011-07-20 13:14:46 +00:00
|
|
|
fflush(stdout);
|
2009-08-12 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2008-10-21 09:12:27 +00:00
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(args.GetIsolate());
|
2009-08-12 11:52:22 +00:00
|
|
|
if (i != 0) {
|
2008-10-21 09:12:27 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2012-09-14 11:43:46 +00:00
|
|
|
|
|
|
|
// Explicitly catch potential exceptions in toString().
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
Handle<String> str_obj = args[i]->ToString();
|
2013-06-05 12:36:33 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
try_catch.ReThrow();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-14 11:43:46 +00:00
|
|
|
|
|
|
|
v8::String::Utf8Value str(str_obj);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-04-17 21:04:34 +00:00
|
|
|
String::Utf8Value file(args[0]);
|
|
|
|
if (*file == NULL) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error loading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2009-04-17 21:04:34 +00:00
|
|
|
}
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> source = ReadFile(args.GetIsolate(), *file);
|
2009-04-17 21:04:34 +00:00
|
|
|
if (source.IsEmpty()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error loading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2009-04-17 21:04:34 +00:00
|
|
|
}
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(source);
|
2009-04-17 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> Shell::ReadFromStdin(Isolate* isolate) {
|
2011-07-14 15:43:40 +00:00
|
|
|
static const int kBufferSize = 256;
|
|
|
|
char buffer[kBufferSize];
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> accumulator = String::NewFromUtf8(isolate, "");
|
2011-07-14 15:43:40 +00:00
|
|
|
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-20 11:59:00 +00:00
|
|
|
char* input = NULL;
|
|
|
|
{ // Release lock for blocking input.
|
2012-11-20 14:47:56 +00:00
|
|
|
Unlocker unlock(isolate);
|
2012-01-20 11:59:00 +00:00
|
|
|
input = fgets(buffer, kBufferSize, stdin);
|
|
|
|
}
|
|
|
|
if (input == 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') {
|
2013-11-22 12:35:39 +00:00
|
|
|
accumulator = String::Concat(
|
|
|
|
accumulator,
|
|
|
|
String::NewFromUtf8(isolate, buffer, String::kNormalString, length));
|
2011-08-01 14:15:02 +00:00
|
|
|
} else if (length > 1 && buffer[length-2] == '\\') {
|
|
|
|
buffer[length-2] = '\n';
|
2013-11-22 12:35:39 +00:00
|
|
|
accumulator = String::Concat(
|
|
|
|
accumulator, String::NewFromUtf8(isolate, buffer,
|
|
|
|
String::kNormalString, length - 1));
|
2011-08-01 14:15:02 +00:00
|
|
|
} else {
|
2013-11-22 12:35:39 +00:00
|
|
|
return String::Concat(
|
|
|
|
accumulator, String::NewFromUtf8(isolate, buffer,
|
|
|
|
String::kNormalString, length - 1));
|
2011-08-01 14:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-13 07:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2008-10-21 09:12:27 +00:00
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(args.GetIsolate());
|
2008-10-21 09:12:27 +00:00
|
|
|
String::Utf8Value file(args[i]);
|
2009-02-05 13:53:41 +00:00
|
|
|
if (*file == NULL) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error loading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2009-02-05 13:53:41 +00:00
|
|
|
}
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> source = ReadFile(args.GetIsolate(), *file);
|
2008-10-21 09:12:27 +00:00
|
|
|
if (source.IsEmpty()) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error loading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2013-03-15 12:06:53 +00:00
|
|
|
if (!ExecuteString(args.GetIsolate(),
|
|
|
|
source,
|
2013-11-22 12:35:39 +00:00
|
|
|
String::NewFromUtf8(args.GetIsolate(), *file),
|
2013-03-15 12:06:53 +00:00
|
|
|
false,
|
|
|
|
true)) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error executing file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 09:42:17 +00:00
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2008-10-21 09:12:27 +00:00
|
|
|
int exit_code = args[0]->Int32Value();
|
|
|
|
OnExit();
|
|
|
|
exit(exit_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-11-22 12:35:39 +00:00
|
|
|
args.GetReturnValue().Set(
|
|
|
|
String::NewFromUtf8(args.GetIsolate(), V8::GetVersion()));
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
|
|
|
|
HandleScope handle_scope(isolate);
|
2012-01-19 17:04:34 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2013-05-29 08:39:37 +00:00
|
|
|
Handle<Context> utility_context;
|
2013-11-22 12:35:39 +00:00
|
|
|
bool enter_context = !isolate->InContext();
|
2013-05-29 08:39:37 +00:00
|
|
|
if (enter_context) {
|
|
|
|
utility_context = Local<Context>::New(isolate, utility_context_);
|
|
|
|
utility_context->Enter();
|
|
|
|
}
|
2012-01-19 17:04:34 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
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");
|
2012-01-19 17:04:34 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2013-05-29 08:39:37 +00:00
|
|
|
if (enter_context) utility_context->Exit();
|
2012-01-19 17:04:34 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2013-03-15 12:06:53 +00:00
|
|
|
Handle<Array> Shell::GetCompletions(Isolate* isolate,
|
|
|
|
Handle<String> text,
|
|
|
|
Handle<String> full) {
|
|
|
|
HandleScope handle_scope(isolate);
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context> utility_context =
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context>::New(isolate, utility_context_);
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Context::Scope context_scope(utility_context);
|
|
|
|
Handle<Object> global = utility_context->Global();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<Value> fun =
|
|
|
|
global->Get(String::NewFromUtf8(isolate, "GetCompletions"));
|
2008-10-21 09:12:27 +00:00
|
|
|
static const int kArgc = 3;
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context> evaluation_context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, evaluation_context_);
|
|
|
|
Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full };
|
2008-10-21 09:12:27 +00:00
|
|
|
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
|
2013-05-02 20:18:42 +00:00
|
|
|
Handle<Object> Shell::DebugMessageDetails(Isolate* isolate,
|
|
|
|
Handle<String> message) {
|
|
|
|
HandleScope handle_scope(isolate);
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, utility_context_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2013-05-29 08:39:37 +00:00
|
|
|
Handle<Object> global = context->Global();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<Value> fun =
|
|
|
|
global->Get(String::NewFromUtf8(isolate, "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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-02 20:18:42 +00:00
|
|
|
Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate,
|
|
|
|
Handle<String> command) {
|
|
|
|
HandleScope handle_scope(isolate);
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, utility_context_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2013-05-29 08:39:37 +00:00
|
|
|
Handle<Object> global = context->Global();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<Value> fun =
|
|
|
|
global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest"));
|
2008-12-18 10:06:49 +00:00
|
|
|
static const int kArgc = 1;
|
|
|
|
Handle<Value> argv[kArgc] = { command };
|
|
|
|
Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
|
|
|
|
return val;
|
|
|
|
}
|
2012-01-20 11:59:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Shell::DispatchDebugMessages() {
|
2013-05-02 20:18:42 +00:00
|
|
|
Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
HandleScope handle_scope(isolate);
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, Shell::evaluation_context_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2012-01-20 11:59:00 +00:00
|
|
|
v8::Debug::ProcessDebugMessages();
|
|
|
|
}
|
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
|
|
|
|
2013-01-18 07:20:17 +00:00
|
|
|
void Shell::InstallUtilityScript(Isolate* isolate) {
|
|
|
|
Locker lock(isolate);
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(isolate);
|
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.
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context> utility_context =
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context>::New(isolate, utility_context_);
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context> evaluation_context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, evaluation_context_);
|
|
|
|
utility_context->SetSecurityToken(Undefined(isolate));
|
|
|
|
evaluation_context->SetSecurityToken(Undefined(isolate));
|
|
|
|
v8::Context::Scope context_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
|
2013-09-03 06:59:01 +00:00
|
|
|
i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug();
|
2011-06-24 20:04:32 +00:00
|
|
|
debug->Load();
|
|
|
|
i::Handle<i::JSObject> js_debug
|
2012-08-17 12:59:00 +00:00
|
|
|
= i::Handle<i::JSObject>(debug->debug_context()->global_object());
|
2013-11-22 12:35:39 +00:00
|
|
|
utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"),
|
|
|
|
Utils::ToLocal(js_debug));
|
2013-09-10 14:30:36 +00:00
|
|
|
debug->debug_context()->set_security_token(
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate)->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);
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> source =
|
|
|
|
String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString,
|
|
|
|
shell_source.length());
|
|
|
|
Handle<String> name =
|
|
|
|
String::NewFromUtf8(isolate, shell_source_name.start(),
|
|
|
|
String::kNormalString, shell_source_name.length());
|
2011-06-09 14:28:49 +00:00
|
|
|
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) {
|
2013-07-19 09:38:18 +00:00
|
|
|
v8::Debug::SetDebugEventListener2(HandleDebugEvent);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
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
|
|
|
|
|
2012-06-28 16:29:53 +00:00
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
|
2008-10-21 09:12:27 +00:00
|
|
|
Handle<ObjectTemplate> global_template = ObjectTemplate::New();
|
2013-11-22 12:35:39 +00:00
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "print"),
|
|
|
|
FunctionTemplate::New(Print));
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "write"),
|
|
|
|
FunctionTemplate::New(Write));
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "read"),
|
|
|
|
FunctionTemplate::New(Read));
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "readbuffer"),
|
2012-05-15 11:00:30 +00:00
|
|
|
FunctionTemplate::New(ReadBuffer));
|
2013-11-22 12:35:39 +00:00
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "readline"),
|
2009-08-13 07:07:41 +00:00
|
|
|
FunctionTemplate::New(ReadLine));
|
2013-11-22 12:35:39 +00:00
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "load"),
|
|
|
|
FunctionTemplate::New(Load));
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "quit"),
|
|
|
|
FunctionTemplate::New(Quit));
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "version"),
|
|
|
|
FunctionTemplate::New(Version));
|
2009-03-31 06:51:25 +00:00
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
// Bind the Realm object.
|
|
|
|
Handle<ObjectTemplate> realm_template = ObjectTemplate::New();
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "current"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmCurrent));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "owner"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmOwner));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "global"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmGlobal));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "create"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmCreate));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "dispose"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmDispose));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "switch"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmSwitch));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->Set(String::NewFromUtf8(isolate, "eval"),
|
2013-04-22 11:29:52 +00:00
|
|
|
FunctionTemplate::New(RealmEval));
|
2013-11-22 12:35:39 +00:00
|
|
|
realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"),
|
2013-04-22 11:29:52 +00:00
|
|
|
RealmSharedGet, RealmSharedSet);
|
2013-11-22 12:35:39 +00:00
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template);
|
2013-04-22 11:29:52 +00:00
|
|
|
|
2013-10-24 12:25:40 +00:00
|
|
|
#ifndef V8_SHARED
|
|
|
|
Handle<ObjectTemplate> performance_template = ObjectTemplate::New();
|
2013-11-22 12:35:39 +00:00
|
|
|
performance_template->Set(String::NewFromUtf8(isolate, "now"),
|
2013-10-24 12:25:40 +00:00
|
|
|
FunctionTemplate::New(PerformanceNow));
|
2013-11-22 12:35:39 +00:00
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "performance"),
|
|
|
|
performance_template);
|
2013-10-24 12:25:40 +00:00
|
|
|
#endif // V8_SHARED
|
|
|
|
|
2013-07-29 11:38:34 +00:00
|
|
|
#if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64)
|
2009-03-31 06:51:25 +00:00
|
|
|
Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
|
2013-11-22 12:35:39 +00:00
|
|
|
AddOSMethods(isolate, os_templ);
|
|
|
|
global_template->Set(String::NewFromUtf8(isolate, "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
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
void Shell::Initialize(Isolate* isolate) {
|
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);
|
2012-07-18 12:27:29 +00:00
|
|
|
if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
|
2011-06-09 14:28:49 +00:00
|
|
|
V8::SetCounterFunction(LookupCounter);
|
|
|
|
V8::SetCreateHistogramFunction(CreateHistogram);
|
|
|
|
V8::SetAddHistogramSampleFunction(AddHistogramSample);
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2012-11-22 08:35:21 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2012-11-22 08:35:21 +00:00
|
|
|
void Shell::InitializeDebugger(Isolate* isolate) {
|
|
|
|
if (options.test_shell) return;
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2013-01-18 07:20:17 +00:00
|
|
|
Locker lock(isolate);
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope scope(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
|
2013-05-08 07:45:16 +00:00
|
|
|
utility_context_.Reset(isolate,
|
|
|
|
Context::New(isolate, NULL, global_template));
|
2008-10-21 09:12:27 +00:00
|
|
|
|
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);
|
2012-01-20 11:59:00 +00:00
|
|
|
v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
|
2011-06-24 20:04:32 +00:00
|
|
|
}
|
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
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
|
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
|
2013-08-29 09:58:30 +00:00
|
|
|
i::LockGuard<i::Mutex> lock_guard(&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
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
|
2013-05-07 16:28:51 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2013-05-08 07:45:16 +00:00
|
|
|
Local<Context> context = Context::New(isolate, NULL, global_template);
|
|
|
|
ASSERT(!context.IsEmpty());
|
|
|
|
Context::Scope scope(context);
|
2008-11-25 12:58:48 +00:00
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
#ifndef V8_SHARED
|
2013-09-03 06:59:01 +00:00
|
|
|
i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory();
|
2011-06-16 08:35:26 +00:00
|
|
|
i::JSArguments js_args = i::FLAG_js_arguments;
|
|
|
|
i::Handle<i::FixedArray> arguments_array =
|
2013-09-17 13:48:17 +00:00
|
|
|
factory->NewFixedArray(js_args.argc);
|
|
|
|
for (int j = 0; j < js_args.argc; j++) {
|
2011-06-16 08:35:26 +00:00
|
|
|
i::Handle<i::String> arg =
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewStringFromUtf8(i::CStrVector(js_args[j]));
|
2011-06-16 08:35:26 +00:00
|
|
|
arguments_array->set(j, *arg);
|
|
|
|
}
|
|
|
|
i::Handle<i::JSArray> arguments_jsarray =
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewJSArrayWithElements(arguments_array);
|
2013-11-22 12:35:39 +00:00
|
|
|
context->Global()->Set(String::NewFromUtf8(isolate, "arguments"),
|
2011-07-14 15:43:40 +00:00
|
|
|
Utils::ToLocal(arguments_jsarray));
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2013-05-08 07:45:16 +00:00
|
|
|
return handle_scope.Close(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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-24 11:20:16 +00:00
|
|
|
inline bool operator<(const CounterAndKey& lhs, const CounterAndKey& rhs) {
|
|
|
|
return strcmp(lhs.key, rhs.key) < 0;
|
2011-11-28 12:12:00 +00:00
|
|
|
}
|
2013-01-08 15:24:17 +00:00
|
|
|
#endif // V8_SHARED
|
2011-11-28 12:12:00 +00:00
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
void Shell::OnExit() {
|
2013-01-08 15:24:17 +00:00
|
|
|
LineEditor* line_editor = LineEditor::Get();
|
|
|
|
if (line_editor) line_editor->Close();
|
|
|
|
#ifndef V8_SHARED
|
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();
|
|
|
|
}
|
2013-04-24 11:20:16 +00:00
|
|
|
std::sort(counters, counters + number_of_counters);
|
2012-07-13 12:22:09 +00:00
|
|
|
printf("+----------------------------------------------------------------+"
|
|
|
|
"-------------+\n");
|
|
|
|
printf("| Name |"
|
|
|
|
" Value |\n");
|
|
|
|
printf("+----------------------------------------------------------------+"
|
|
|
|
"-------------+\n");
|
2011-11-28 12:12:00 +00:00
|
|
|
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()) {
|
2012-07-13 12:22:09 +00:00
|
|
|
printf("| c:%-60s | %11i |\n", key, counter->count());
|
|
|
|
printf("| t:%-60s | %11i |\n", key, counter->sample_total());
|
2009-04-14 00:30:44 +00:00
|
|
|
} else {
|
2012-07-13 12:22:09 +00:00
|
|
|
printf("| %-62s | %11i |\n", key, counter->count());
|
2009-04-14 00:30:44 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2012-07-13 12:22:09 +00:00
|
|
|
printf("+----------------------------------------------------------------+"
|
|
|
|
"-------------+\n");
|
2011-11-28 12:12:00 +00:00
|
|
|
delete [] counters;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2012-03-27 09:50:48 +00:00
|
|
|
delete counters_file_;
|
|
|
|
delete counter_map_;
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2013-01-08 15:24:17 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 09:34:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
static FILE* FOpen(const char* path, const char* mode) {
|
2013-07-29 11:38:34 +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
|
|
|
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
static char* ReadChars(Isolate* isolate, const char* name, int* size_out) {
|
2011-07-11 07:38:09 +00:00
|
|
|
// Release the V8 lock while reading files.
|
2012-11-20 14:47:56 +00:00
|
|
|
v8::Unlocker unlocker(isolate);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-23 10:01:42 +00:00
|
|
|
static void ReadBufferWeakCallback(v8::Isolate* isolate,
|
2013-06-13 09:27:09 +00:00
|
|
|
Persistent<ArrayBuffer>* array_buffer,
|
2013-05-23 10:01:42 +00:00
|
|
|
uint8_t* data) {
|
2013-06-13 09:27:09 +00:00
|
|
|
size_t byte_length =
|
|
|
|
Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength();
|
2013-05-23 10:01:42 +00:00
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
-static_cast<intptr_t>(byte_length));
|
|
|
|
|
|
|
|
delete[] data;
|
2013-11-22 12:35:39 +00:00
|
|
|
array_buffer->Reset();
|
2013-05-23 10:01:42 +00:00
|
|
|
}
|
2009-01-20 14:14:22 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2012-06-01 12:28:15 +00:00
|
|
|
ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT
|
2012-05-15 11:00:30 +00:00
|
|
|
String::Utf8Value filename(args[0]);
|
|
|
|
int length;
|
|
|
|
if (*filename == NULL) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error loading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2012-05-15 11:00:30 +00:00
|
|
|
}
|
2012-06-01 11:46:05 +00:00
|
|
|
|
2013-05-23 10:01:42 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2012-11-20 14:47:56 +00:00
|
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(
|
|
|
|
ReadChars(args.GetIsolate(), *filename, &length));
|
2012-05-15 11:00:30 +00:00
|
|
|
if (data == NULL) {
|
2013-11-22 12:35:39 +00:00
|
|
|
Throw(args.GetIsolate(), "Error reading file");
|
2013-06-05 12:36:33 +00:00
|
|
|
return;
|
2012-05-15 11:00:30 +00:00
|
|
|
}
|
2013-11-28 08:21:26 +00:00
|
|
|
Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data, length);
|
2013-06-13 09:27:09 +00:00
|
|
|
v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer);
|
2013-06-24 13:15:07 +00:00
|
|
|
weak_handle.MakeWeak(data, ReadBufferWeakCallback);
|
2013-05-23 10:01:42 +00:00
|
|
|
weak_handle.MarkIndependent();
|
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(length);
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(buffer);
|
2012-05-15 11:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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, ' ');
|
|
|
|
}
|
2013-05-02 20:18:42 +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.
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) {
|
2009-01-20 14:14:22 +00:00
|
|
|
int size = 0;
|
2012-11-20 14:47:56 +00:00
|
|
|
char* chars = ReadChars(isolate, name, &size);
|
2009-01-20 14:14:22 +00:00
|
|
|
if (chars == NULL) return Handle<String>();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> result =
|
|
|
|
String::NewFromUtf8(isolate, chars, String::kNormalString, size);
|
2008-10-21 09:12:27 +00:00
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
void Shell::RunShell(Isolate* isolate) {
|
2013-01-18 07:20:17 +00:00
|
|
|
Locker locker(isolate);
|
2013-05-03 10:36:13 +00:00
|
|
|
HandleScope outer_scope(isolate);
|
2013-05-28 10:36:21 +00:00
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Local<v8::Context>::New(isolate, evaluation_context_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> name = String::NewFromUtf8(isolate, "(d8)");
|
2012-11-20 14:47:56 +00:00
|
|
|
LineEditor* console = LineEditor::Get();
|
2011-09-14 14:39:56 +00:00
|
|
|
printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
|
2013-03-12 08:00:20 +00:00
|
|
|
console->Open(isolate);
|
2008-10-21 09:12:27 +00:00
|
|
|
while (true) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope inner_scope(isolate);
|
2012-01-17 13:37:09 +00:00
|
|
|
Handle<String> input = console->Prompt(Shell::kPrompt);
|
|
|
|
if (input.IsEmpty()) break;
|
2013-03-15 12:06:53 +00:00
|
|
|
ExecuteString(isolate, 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|.
|
2012-11-29 15:13:49 +00:00
|
|
|
ShellThread(Isolate* isolate, char* files)
|
2011-07-14 15:43:40 +00:00
|
|
|
: Thread("d8:ShellThread"),
|
2012-11-29 15:13:49 +00:00
|
|
|
isolate_(isolate), 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:
|
2012-11-20 14:47:56 +00:00
|
|
|
Isolate* isolate_;
|
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.
|
2013-01-18 07:20:17 +00:00
|
|
|
Locker locker(isolate_);
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope outer_scope(isolate_);
|
2013-05-08 07:45:16 +00:00
|
|
|
Local<Context> thread_context =
|
2012-11-20 14:47:56 +00:00
|
|
|
Shell::CreateEvaluationContext(isolate_);
|
2013-05-08 07:45:16 +00:00
|
|
|
Context::Scope context_scope(thread_context);
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate_));
|
2009-02-19 04:26:04 +00:00
|
|
|
|
|
|
|
while ((ptr != NULL) && (*ptr != '\0')) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope inner_scope(isolate_);
|
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
|
|
|
}
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> str = Shell::ReadFile(isolate_, filename);
|
2009-02-19 04:26:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-22 12:35:39 +00:00
|
|
|
Shell::ExecuteString(
|
|
|
|
isolate_, str, String::NewFromUtf8(isolate_, filename), false, false);
|
2009-02-19 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 thread_;
|
|
|
|
thread_ = NULL;
|
2011-09-08 23:43:36 +00:00
|
|
|
#endif // V8_SHARED
|
2011-09-08 22:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
void SourceGroup::Execute(Isolate* isolate) {
|
2013-10-04 16:21:23 +00:00
|
|
|
bool exception_was_thrown = false;
|
2011-07-11 07:38:09 +00:00
|
|
|
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.
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed");
|
|
|
|
Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]);
|
2013-03-15 12:06:53 +00:00
|
|
|
if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
|
2013-10-04 16:21:23 +00:00
|
|
|
exception_was_thrown = true;
|
|
|
|
break;
|
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.
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> file_name = String::NewFromUtf8(isolate, arg);
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> source = ReadFile(isolate, arg);
|
2011-07-11 07:38:09 +00:00
|
|
|
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
|
|
|
}
|
2013-03-15 12:06:53 +00:00
|
|
|
if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
|
2013-10-04 16:21:23 +00:00
|
|
|
exception_was_thrown = true;
|
|
|
|
break;
|
2008-11-25 12:58:48 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2013-10-04 16:21:23 +00:00
|
|
|
if (exception_was_thrown != Shell::options.expected_to_throw) {
|
|
|
|
Shell::Exit(1);
|
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2009-03-03 12:23:45 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) {
|
2011-08-08 09:34:18 +00:00
|
|
|
int size;
|
2012-11-20 14:47:56 +00:00
|
|
|
char* chars = ReadChars(isolate, name, &size);
|
2011-08-08 09:34:18 +00:00
|
|
|
if (chars == NULL) return Handle<String>();
|
2013-11-22 12:35:39 +00:00
|
|
|
Handle<String> result =
|
|
|
|
String::NewFromUtf8(isolate, chars, String::kNormalString, size);
|
2011-07-11 07:38:09 +00:00
|
|
|
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() {
|
|
|
|
// 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-24 15:48:16 +00:00
|
|
|
// OS-specific padding for thread startup code. 2Mbytes seems to be enough.
|
|
|
|
return i::Thread::Options("IsolateThread", 2 * MB);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SourceGroup::ExecuteInThread() {
|
|
|
|
Isolate* isolate = Isolate::New();
|
|
|
|
do {
|
2013-09-02 12:26:06 +00:00
|
|
|
next_semaphore_.Wait();
|
2011-07-11 07:38:09 +00:00
|
|
|
{
|
|
|
|
Isolate::Scope iscope(isolate);
|
|
|
|
Locker lock(isolate);
|
|
|
|
{
|
2013-05-08 07:45:16 +00:00
|
|
|
HandleScope scope(isolate);
|
|
|
|
PerIsolateData data(isolate);
|
|
|
|
Local<Context> context = Shell::CreateEvaluationContext(isolate);
|
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
|
|
|
|
Execute(isolate);
|
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2012-08-21 07:06:21 +00:00
|
|
|
if (Shell::options.send_idle_notification) {
|
|
|
|
const int kLongIdlePauseInMs = 1000;
|
|
|
|
V8::ContextDisposedNotification();
|
|
|
|
V8::IdleNotification(kLongIdlePauseInMs);
|
|
|
|
}
|
2009-03-16 10:08:59 +00:00
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
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
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
next_semaphore_.Signal();
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2013-09-02 12:26:06 +00:00
|
|
|
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;
|
2013-11-15 16:37:15 +00:00
|
|
|
} else if (strcmp(argv[i], "--nostress-opt") == 0) {
|
|
|
|
options.stress_opt = false;
|
|
|
|
argv[i] = NULL;
|
2011-06-09 14:28:49 +00:00
|
|
|
} 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;
|
2013-11-18 14:50:45 +00:00
|
|
|
} else if (strcmp(argv[i], "--mock-arraybuffer-allocator") == 0) {
|
|
|
|
options.mock_arraybuffer_allocator = true;
|
|
|
|
argv[i] = NULL;
|
2011-06-09 14:28:49 +00:00
|
|
|
} 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;
|
2012-08-20 13:19:52 +00:00
|
|
|
} else if (strcmp(argv[i], "--send-idle-notification") == 0) {
|
|
|
|
options.send_idle_notification = true;
|
|
|
|
argv[i] = NULL;
|
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
|
2013-08-02 09:02:02 +00:00
|
|
|
} else if (strcmp(argv[i], "--dump-heap-constants") == 0) {
|
|
|
|
#ifdef V8_SHARED
|
|
|
|
printf("D8 with shared library does not support constant dumping\n");
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
options.dump_heap_constants = true;
|
|
|
|
argv[i] = NULL;
|
|
|
|
#endif
|
2013-10-04 16:21:23 +00:00
|
|
|
} else if (strcmp(argv[i], "--throws") == 0) {
|
|
|
|
options.expected_to_throw = true;
|
|
|
|
argv[i] = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
int Shell::RunMain(Isolate* isolate, 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;
|
2012-11-20 14:47:56 +00:00
|
|
|
{ Locker lock(isolate);
|
2011-09-13 13:16:13 +00:00
|
|
|
int size = 0;
|
2012-11-20 14:47:56 +00:00
|
|
|
files = ReadChars(isolate, options.parallel_files[i], &size);
|
2011-09-13 13:16:13 +00:00
|
|
|
}
|
|
|
|
if (files == NULL) {
|
|
|
|
printf("File list '%s' not found\n", options.parallel_files[i]);
|
|
|
|
Exit(1);
|
|
|
|
}
|
2012-11-29 15:13:49 +00:00
|
|
|
ShellThread* thread = new ShellThread(isolate, files);
|
2011-07-14 15:43:40 +00:00
|
|
|
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
|
2013-01-18 07:20:17 +00:00
|
|
|
Locker lock(isolate);
|
2013-05-08 07:45:16 +00:00
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Local<Context> context = CreateEvaluationContext(isolate);
|
|
|
|
if (options.last_run) {
|
|
|
|
// Keep using the same context in the interactive shell.
|
|
|
|
evaluation_context_.Reset(isolate, context);
|
2011-09-29 08:55:36 +00:00
|
|
|
#if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
|
2013-05-08 07:45:16 +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(isolate);
|
|
|
|
}
|
2013-05-07 16:28:51 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2013-05-08 07:45:16 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
|
|
|
|
options.isolate_sources[0].Execute(isolate);
|
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2011-09-21 13:42:25 +00:00
|
|
|
if (!options.last_run) {
|
2012-08-20 13:19:52 +00:00
|
|
|
if (options.send_idle_notification) {
|
2012-03-23 13:33:11 +00:00
|
|
|
const int kLongIdlePauseInMs = 1000;
|
|
|
|
V8::ContextDisposedNotification();
|
|
|
|
V8::IdleNotification(kLongIdlePauseInMs);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // V8_SHARED
|
2011-07-11 07:38:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-13 16:32:32 +00:00
|
|
|
#ifdef V8_SHARED
|
2013-07-25 14:11:53 +00:00
|
|
|
static void SetStandaloneFlagsViaCommandLine() {
|
2013-08-20 13:55:52 +00:00
|
|
|
int fake_argc = 2;
|
|
|
|
char **fake_argv = new char*[2];
|
2013-05-13 16:32:32 +00:00
|
|
|
fake_argv[0] = NULL;
|
2013-08-20 13:55:52 +00:00
|
|
|
fake_argv[1] = strdup("--trace-hydrogen-file=hydrogen.cfg");
|
2013-11-07 16:35:27 +00:00
|
|
|
fake_argv[2] = strdup("--redirect-code-traces-to=code.asm");
|
2013-05-13 16:32:32 +00:00
|
|
|
v8::V8::SetFlagsFromCommandLine(&fake_argc, fake_argv, false);
|
|
|
|
free(fake_argv[1]);
|
|
|
|
delete[] fake_argv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-08-02 09:02:02 +00:00
|
|
|
#ifndef V8_SHARED
|
|
|
|
static void DumpHeapConstants(i::Isolate* isolate) {
|
|
|
|
i::Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
// Dump the INSTANCE_TYPES table to the console.
|
|
|
|
printf("# List of known V8 instance types.\n");
|
|
|
|
#define DUMP_TYPE(T) printf(" %d: \"%s\",\n", i::T, #T);
|
|
|
|
printf("INSTANCE_TYPES = {\n");
|
|
|
|
INSTANCE_TYPE_LIST(DUMP_TYPE)
|
|
|
|
printf("}\n");
|
|
|
|
#undef DUMP_TYPE
|
|
|
|
|
|
|
|
// Dump the KNOWN_MAP table to the console.
|
|
|
|
printf("\n# List of known V8 maps.\n");
|
|
|
|
#define ROOT_LIST_CASE(type, name, camel_name) \
|
2013-08-05 16:47:45 +00:00
|
|
|
if (n == NULL && o == heap->name()) n = #camel_name;
|
2013-08-02 09:02:02 +00:00
|
|
|
#define STRUCT_LIST_CASE(upper_name, camel_name, name) \
|
2013-08-05 16:47:45 +00:00
|
|
|
if (n == NULL && o == heap->name##_map()) n = #camel_name "Map";
|
2013-08-02 09:02:02 +00:00
|
|
|
i::HeapObjectIterator it(heap->map_space());
|
|
|
|
printf("KNOWN_MAPS = {\n");
|
|
|
|
for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
|
|
|
|
i::Map* m = i::Map::cast(o);
|
|
|
|
const char* n = NULL;
|
|
|
|
intptr_t p = reinterpret_cast<intptr_t>(m) & 0xfffff;
|
|
|
|
int t = m->instance_type();
|
|
|
|
ROOT_LIST(ROOT_LIST_CASE)
|
|
|
|
STRUCT_LIST(STRUCT_LIST_CASE)
|
|
|
|
if (n == NULL) continue;
|
2013-08-02 09:35:44 +00:00
|
|
|
printf(" 0x%05" V8PRIxPTR ": (%d, \"%s\"),\n", p, t, n);
|
2013-08-02 09:02:02 +00:00
|
|
|
}
|
|
|
|
printf("}\n");
|
|
|
|
#undef STRUCT_LIST_CASE
|
|
|
|
#undef ROOT_LIST_CASE
|
|
|
|
|
|
|
|
// Dump the KNOWN_OBJECTS table to the console.
|
|
|
|
printf("\n# List of known V8 objects.\n");
|
|
|
|
#define ROOT_LIST_CASE(type, name, camel_name) \
|
2013-08-05 16:47:45 +00:00
|
|
|
if (n == NULL && o == heap->name()) n = #camel_name;
|
2013-08-02 09:02:02 +00:00
|
|
|
i::OldSpaces spit(heap);
|
|
|
|
printf("KNOWN_OBJECTS = {\n");
|
|
|
|
for (i::PagedSpace* s = spit.next(); s != NULL; s = spit.next()) {
|
|
|
|
i::HeapObjectIterator it(s);
|
|
|
|
const char* sname = AllocationSpaceName(s->identity());
|
|
|
|
for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
|
|
|
|
const char* n = NULL;
|
|
|
|
intptr_t p = reinterpret_cast<intptr_t>(o) & 0xfffff;
|
|
|
|
ROOT_LIST(ROOT_LIST_CASE)
|
|
|
|
if (n == NULL) continue;
|
2013-08-02 09:35:44 +00:00
|
|
|
printf(" (\"%s\", 0x%05" V8PRIxPTR "): \"%s\",\n", sname, p, n);
|
2013-08-02 09:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("}\n");
|
|
|
|
#undef ROOT_LIST_CASE
|
|
|
|
}
|
|
|
|
#endif // V8_SHARED
|
|
|
|
|
|
|
|
|
2013-06-11 10:41:14 +00:00
|
|
|
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
|
|
public:
|
2013-08-01 08:52:21 +00:00
|
|
|
virtual void* Allocate(size_t length) {
|
|
|
|
void* result = malloc(length);
|
|
|
|
memset(result, 0, length);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
virtual void* AllocateUninitialized(size_t length) {
|
|
|
|
return malloc(length);
|
|
|
|
}
|
2013-08-02 13:03:06 +00:00
|
|
|
virtual void Free(void* data, size_t) { free(data); }
|
|
|
|
// TODO(dslomov): Remove when v8:2823 is fixed.
|
|
|
|
virtual void Free(void* data) {
|
|
|
|
#ifndef V8_SHARED
|
|
|
|
UNREACHABLE();
|
|
|
|
#endif
|
|
|
|
}
|
2013-06-11 10:41:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-11-18 14:50:45 +00:00
|
|
|
class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
|
|
public:
|
|
|
|
virtual void* Allocate(size_t) V8_OVERRIDE {
|
|
|
|
return malloc(0);
|
|
|
|
}
|
|
|
|
virtual void* AllocateUninitialized(size_t length) V8_OVERRIDE {
|
|
|
|
return malloc(0);
|
|
|
|
}
|
|
|
|
virtual void Free(void*, size_t) V8_OVERRIDE {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
int Shell::Main(int argc, char* argv[]) {
|
|
|
|
if (!SetOptions(argc, argv)) return 1;
|
2013-07-11 09:58:54 +00:00
|
|
|
v8::V8::InitializeICU();
|
2013-05-13 14:18:43 +00:00
|
|
|
#ifndef V8_SHARED
|
2013-07-25 14:11:53 +00:00
|
|
|
i::FLAG_trace_hydrogen_file = "hydrogen.cfg";
|
2013-11-07 16:35:27 +00:00
|
|
|
i::FLAG_redirect_code_traces_to = "code.asm";
|
2013-05-13 16:32:32 +00:00
|
|
|
#else
|
2013-07-25 14:11:53 +00:00
|
|
|
SetStandaloneFlagsViaCommandLine();
|
2013-05-13 14:18:43 +00:00
|
|
|
#endif
|
2013-06-11 10:41:14 +00:00
|
|
|
ShellArrayBufferAllocator array_buffer_allocator;
|
2013-11-18 14:50:45 +00:00
|
|
|
MockArrayBufferAllocator mock_arraybuffer_allocator;
|
|
|
|
if (options.mock_arraybuffer_allocator) {
|
|
|
|
v8::V8::SetArrayBufferAllocator(&mock_arraybuffer_allocator);
|
|
|
|
} else {
|
|
|
|
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
|
|
|
|
}
|
2011-06-09 14:28:49 +00:00
|
|
|
int result = 0;
|
2012-11-20 14:47:56 +00:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-11-13 15:32:02 +00:00
|
|
|
#ifndef V8_SHARED
|
2013-11-13 14:05:06 +00:00
|
|
|
v8::ResourceConstraints constraints;
|
2013-11-19 13:08:37 +00:00
|
|
|
constraints.ConfigureDefaults(i::OS::TotalPhysicalMemory(),
|
|
|
|
i::CPU::NumberOfProcessorsOnline());
|
2013-11-13 14:05:06 +00:00
|
|
|
v8::SetResourceConstraints(isolate, &constraints);
|
2013-11-13 15:32:02 +00:00
|
|
|
#endif
|
2013-02-12 08:59:53 +00:00
|
|
|
DumbLineEditor dumb_line_editor(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
{
|
|
|
|
Initialize(isolate);
|
2013-04-12 12:48:27 +00:00
|
|
|
#ifdef ENABLE_VTUNE_JIT_INTERFACE
|
2013-05-08 16:17:23 +00:00
|
|
|
vTune::InitializeVtuneForV8();
|
2013-04-12 12:48:27 +00:00
|
|
|
#endif
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData data(isolate);
|
2012-11-22 08:35:21 +00:00
|
|
|
InitializeDebugger(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
|
2013-08-02 09:02:02 +00:00
|
|
|
#ifndef V8_SHARED
|
|
|
|
if (options.dump_heap_constants) {
|
|
|
|
DumpHeapConstants(reinterpret_cast<i::Isolate*>(isolate));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
if (options.stress_opt || options.stress_deopt) {
|
|
|
|
Testing::SetStressRunType(options.stress_opt
|
|
|
|
? Testing::kStressTypeOpt
|
|
|
|
: Testing::kStressTypeDeopt);
|
|
|
|
int stress_runs = Testing::GetStressRuns();
|
|
|
|
for (int i = 0; i < stress_runs && result == 0; i++) {
|
|
|
|
printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
|
|
|
|
Testing::PrepareStressRun(i);
|
|
|
|
options.last_run = (i == stress_runs - 1);
|
|
|
|
result = RunMain(isolate, argc, argv);
|
|
|
|
}
|
|
|
|
printf("======== Full Deoptimization =======\n");
|
|
|
|
Testing::DeoptimizeAll();
|
2012-02-01 17:28:04 +00:00
|
|
|
#if !defined(V8_SHARED)
|
2012-11-20 14:47:56 +00:00
|
|
|
} else if (i::FLAG_stress_runs > 0) {
|
|
|
|
int stress_runs = i::FLAG_stress_runs;
|
|
|
|
for (int i = 0; i < stress_runs && result == 0; i++) {
|
|
|
|
printf("============ Run %d/%d ============\n", i + 1, stress_runs);
|
|
|
|
options.last_run = (i == stress_runs - 1);
|
|
|
|
result = RunMain(isolate, argc, argv);
|
|
|
|
}
|
2012-02-01 17:28:04 +00:00
|
|
|
#endif
|
2012-11-20 14:47:56 +00:00
|
|
|
} else {
|
|
|
|
result = RunMain(isolate, 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)
|
2012-11-20 14:47:56 +00:00
|
|
|
// Run remote debugger if requested, but never on --test
|
|
|
|
if (i::FLAG_remote_debugger && !options.test_shell) {
|
2013-01-18 07:20:17 +00:00
|
|
|
InstallUtilityScript(isolate);
|
2013-03-15 12:06:53 +00:00
|
|
|
RunRemoteDebugger(isolate, i::FLAG_debugger_port);
|
2012-11-20 14:47:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-08-08 09:34:18 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2011-06-09 14:28:49 +00:00
|
|
|
|
2012-11-20 14:47:56 +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
|
|
|
|
2012-11-20 14:47:56 +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)
|
2012-11-20 14:47:56 +00:00
|
|
|
if (!i::FLAG_debugger) {
|
2013-01-18 07:20:17 +00:00
|
|
|
InstallUtilityScript(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
2011-09-29 08:55:36 +00:00
|
|
|
#endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
|
2012-11-20 14:47:56 +00:00
|
|
|
RunShell(isolate);
|
|
|
|
}
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
V8::Dispose();
|
2011-08-24 10:48:48 +00:00
|
|
|
|
2011-08-24 09:47:10 +00:00
|
|
|
OnExit();
|
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
|