2012-01-11 14:42:58 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-10-21 09:12:27 +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
|
|
|
|
2013-04-24 11:20:16 +00:00
|
|
|
#include <algorithm>
|
2016-04-27 11:10:41 +00:00
|
|
|
#include <fstream>
|
2016-10-11 19:22:03 +00:00
|
|
|
#include <unordered_map>
|
2016-09-20 23:38:58 +00:00
|
|
|
#include <utility>
|
2015-07-13 21:04:55 +00:00
|
|
|
#include <vector>
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2013-04-12 12:48:27 +00:00
|
|
|
#ifdef ENABLE_VTUNE_JIT_INTERFACE
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/third_party/vtune/v8-vtune.h"
|
2013-04-12 12:48:27 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/d8.h"
|
2016-02-16 13:28:12 +00:00
|
|
|
#include "src/ostreams.h"
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2014-07-03 07:37:27 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
2016-07-27 16:21:09 +00:00
|
|
|
#include "include/libplatform/v8-tracing.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/cpu.h"
|
2016-08-18 14:25:19 +00:00
|
|
|
#include "src/base/debug/stack_trace.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/logging.h"
|
|
|
|
#include "src/base/platform/platform.h"
|
2014-08-27 08:29:22 +00:00
|
|
|
#include "src/base/sys-info.h"
|
2014-09-29 07:29:14 +00:00
|
|
|
#include "src/basic-block-profiler.h"
|
2016-04-11 11:57:31 +00:00
|
|
|
#include "src/interpreter/interpreter.h"
|
2015-03-27 15:28:55 +00:00
|
|
|
#include "src/snapshot/natives.h"
|
2015-06-22 17:12:26 +00:00
|
|
|
#include "src/utils.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
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
|
2014-09-17 14:20:20 +00:00
|
|
|
#else
|
|
|
|
#include <windows.h> // NOLINT
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <crtdbg.h> // NOLINT
|
|
|
|
#endif // defined(_MSC_VER)
|
|
|
|
#endif // !defined(_WIN32) && !defined(_WIN64)
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
#ifndef DCHECK
|
|
|
|
#define DCHECK(condition) assert(condition)
|
2015-02-11 14:32:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CHECK
|
|
|
|
#define CHECK(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
|
|
|
|
2015-03-24 08:29:52 +00:00
|
|
|
namespace {
|
2015-04-29 09:54:34 +00:00
|
|
|
|
|
|
|
const int MB = 1024 * 1024;
|
2015-08-18 17:12:03 +00:00
|
|
|
const int kMaxWorkers = 50;
|
2015-04-29 09:54:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
|
|
public:
|
|
|
|
virtual void* Allocate(size_t length) {
|
|
|
|
void* data = AllocateUninitialized(length);
|
|
|
|
return data == NULL ? data : memset(data, 0, length);
|
|
|
|
}
|
|
|
|
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
|
|
|
|
virtual void Free(void* data, size_t) { free(data); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
|
|
|
|
public:
|
|
|
|
void* Allocate(size_t length) override {
|
|
|
|
size_t actual_length = length > 10 * MB ? 1 : length;
|
|
|
|
void* data = AllocateUninitialized(actual_length);
|
|
|
|
return data == NULL ? data : memset(data, 0, actual_length);
|
|
|
|
}
|
|
|
|
void* AllocateUninitialized(size_t length) override {
|
|
|
|
return length > 10 * MB ? malloc(1) : malloc(length);
|
|
|
|
}
|
|
|
|
void Free(void* p, size_t) override { free(p); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-12 13:42:49 +00:00
|
|
|
// Predictable v8::Platform implementation. All background and foreground
|
|
|
|
// tasks are run immediately, delayed tasks are not executed at all.
|
|
|
|
class PredictablePlatform : public Platform {
|
|
|
|
public:
|
|
|
|
PredictablePlatform() {}
|
|
|
|
|
|
|
|
void CallOnBackgroundThread(Task* task,
|
|
|
|
ExpectedRuntime expected_runtime) override {
|
|
|
|
task->Run();
|
|
|
|
delete task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override {
|
|
|
|
task->Run();
|
|
|
|
delete task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallDelayedOnForegroundThread(v8::Isolate* isolate, Task* task,
|
|
|
|
double delay_in_seconds) override {
|
|
|
|
delete task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallIdleOnForegroundThread(v8::Isolate* isolate,
|
|
|
|
IdleTask* task) override {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IdleTasksEnabled(v8::Isolate* isolate) override { return false; }
|
|
|
|
|
|
|
|
double MonotonicallyIncreasingTime() override {
|
|
|
|
return synthetic_time_in_sec_ += 0.00001;
|
|
|
|
}
|
|
|
|
|
[tracing] Avoid Gcc compilation fail by declaring AddTraceEvent function in Class derived from Platform Class.
The CL #39789 (https://codereview.chromium.org/2367603002 ) caused the Gcc compilation fail for v8 debug mode.
The error message was:
In file included from .././include/libplatform/v8-tracing.h:13:0,
from .././src/libplatform/default-platform.h:14,
from ../src/libplatform/default-platform.cc:5:
.././include/v8-platform.h:169:20: error: ‘virtual uint64_t v8::Platform::AddTraceEvent(char, const uint8_t*, const char*, const char*, uint64_t, uint64_t, int32_t, const char**, const uint8_t*, const uint64_t*, unsigned int)’ was hidden [-Werror=overloaded-virtual]
virtual uint64_t AddTraceEvent(
^
In file included from ../src/libplatform/default-platform.cc:5:0:
.././src/libplatform/default-platform.h:55:12: error: by ‘virtual uint64_t v8::platform::DefaultPlatform::AddTraceEvent(char, const uint8_t*, const char*, const char*, uint64_t, uint64_t, int32_t, const char**, const uint8_t*, const uint64_t*, std::unique_ptr<v8::ConvertableToTraceFormat>*, unsigned int)’ [-Werror=overloaded-virtual]
uint64_t AddTraceEvent(
^
This CL fixed this issue by adding "using Platform::AddTraceEvent;" before all declarations of AddTraceEvent functions in Classes derived from Platform Class.
BUG=
Review-Url: https://codereview.chromium.org/2380583002
Cr-Commit-Position: refs/heads/master@{#39810}
2016-09-28 08:47:16 +00:00
|
|
|
using Platform::AddTraceEvent;
|
2016-02-26 17:24:14 +00:00
|
|
|
uint64_t AddTraceEvent(char phase, const uint8_t* categoryEnabledFlag,
|
|
|
|
const char* name, const char* scope, uint64_t id,
|
|
|
|
uint64_t bind_id, int numArgs, const char** argNames,
|
|
|
|
const uint8_t* argTypes, const uint64_t* argValues,
|
|
|
|
unsigned int flags) override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:48:07 +00:00
|
|
|
void UpdateTraceEventDuration(const uint8_t* categoryEnabledFlag,
|
|
|
|
const char* name, uint64_t handle) override {}
|
|
|
|
|
|
|
|
const uint8_t* GetCategoryGroupEnabled(const char* name) override {
|
|
|
|
static uint8_t no = 0;
|
|
|
|
return &no;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetCategoryGroupName(
|
|
|
|
const uint8_t* categoryEnabledFlag) override {
|
|
|
|
static const char* dummy = "dummy";
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2015-11-12 13:42:49 +00:00
|
|
|
private:
|
|
|
|
double synthetic_time_in_sec_ = 0.0;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(PredictablePlatform);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-24 08:29:52 +00:00
|
|
|
v8::Platform* g_platform = NULL;
|
2015-04-29 09:54:34 +00:00
|
|
|
|
2015-06-22 17:12:26 +00:00
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
static Local<Value> Throw(Isolate* isolate, const char* message) {
|
|
|
|
return isolate->ThrowException(
|
|
|
|
String::NewFromUtf8(isolate, message, NewStringType::kNormal)
|
|
|
|
.ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
bool FindInObjectList(Local<Object> object, const Shell::ObjectList& list) {
|
2015-06-22 17:12:26 +00:00
|
|
|
for (int i = 0; i < list.length(); ++i) {
|
|
|
|
if (list[i]->StrictEquals(object)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
Worker* GetWorkerFromInternalField(Isolate* isolate, Local<Object> object) {
|
|
|
|
if (object->InternalFieldCount() != 1) {
|
|
|
|
Throw(isolate, "this is not a Worker");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-24 08:29:52 +00:00
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
Worker* worker =
|
|
|
|
static_cast<Worker*>(object->GetAlignedPointerFromInternalField(0));
|
|
|
|
if (worker == NULL) {
|
|
|
|
Throw(isolate, "Worker is defunct because main thread is terminating");
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-20 14:47:56 +00:00
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
return worker;
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-08-10 17:10:51 +00:00
|
|
|
namespace tracing {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// String options that can be used to initialize TraceOptions.
|
|
|
|
const char kRecordUntilFull[] = "record-until-full";
|
|
|
|
const char kRecordContinuously[] = "record-continuously";
|
|
|
|
const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible";
|
|
|
|
|
|
|
|
const char kRecordModeParam[] = "record_mode";
|
|
|
|
const char kEnableSystraceParam[] = "enable_systrace";
|
|
|
|
const char kEnableArgumentFilterParam[] = "enable_argument_filter";
|
|
|
|
const char kIncludedCategoriesParam[] = "included_categories";
|
|
|
|
const char kExcludedCategoriesParam[] = "excluded_categories";
|
|
|
|
|
|
|
|
class TraceConfigParser {
|
|
|
|
public:
|
|
|
|
static void FillTraceConfig(v8::Isolate* isolate,
|
|
|
|
platform::tracing::TraceConfig* trace_config,
|
|
|
|
const char* json_str) {
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
Local<Context> context = Context::New(isolate);
|
|
|
|
Context::Scope context_scope(context);
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
|
|
|
|
Local<String> source =
|
|
|
|
String::NewFromUtf8(isolate, json_str, NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<Value> result = JSON::Parse(context, source).ToLocalChecked();
|
|
|
|
Local<v8::Object> trace_config_object = Local<v8::Object>::Cast(result);
|
|
|
|
|
|
|
|
trace_config->SetTraceRecordMode(
|
|
|
|
GetTraceRecordMode(isolate, context, trace_config_object));
|
|
|
|
if (GetBoolean(isolate, context, trace_config_object,
|
|
|
|
kEnableSystraceParam)) {
|
|
|
|
trace_config->EnableSystrace();
|
|
|
|
}
|
|
|
|
if (GetBoolean(isolate, context, trace_config_object,
|
|
|
|
kEnableArgumentFilterParam)) {
|
|
|
|
trace_config->EnableArgumentFilter();
|
|
|
|
}
|
|
|
|
UpdateCategoriesList(isolate, context, trace_config_object,
|
|
|
|
kIncludedCategoriesParam, trace_config);
|
|
|
|
UpdateCategoriesList(isolate, context, trace_config_object,
|
|
|
|
kExcludedCategoriesParam, trace_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool GetBoolean(v8::Isolate* isolate, Local<Context> context,
|
|
|
|
Local<v8::Object> object, const char* property) {
|
|
|
|
Local<Value> value = GetValue(isolate, context, object, property);
|
|
|
|
if (value->IsNumber()) {
|
|
|
|
Local<Boolean> v8_boolean = value->ToBoolean(context).ToLocalChecked();
|
|
|
|
return v8_boolean->Value();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int UpdateCategoriesList(
|
|
|
|
v8::Isolate* isolate, Local<Context> context, Local<v8::Object> object,
|
|
|
|
const char* property, platform::tracing::TraceConfig* trace_config) {
|
|
|
|
Local<Value> value = GetValue(isolate, context, object, property);
|
|
|
|
if (value->IsArray()) {
|
|
|
|
Local<Array> v8_array = Local<Array>::Cast(value);
|
|
|
|
for (int i = 0, length = v8_array->Length(); i < length; ++i) {
|
|
|
|
Local<Value> v = v8_array->Get(context, i)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->ToString(context)
|
|
|
|
.ToLocalChecked();
|
|
|
|
String::Utf8Value str(v->ToString(context).ToLocalChecked());
|
|
|
|
if (kIncludedCategoriesParam == property) {
|
|
|
|
trace_config->AddIncludedCategory(*str);
|
|
|
|
} else {
|
|
|
|
trace_config->AddExcludedCategory(*str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v8_array->Length();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static platform::tracing::TraceRecordMode GetTraceRecordMode(
|
|
|
|
v8::Isolate* isolate, Local<Context> context, Local<v8::Object> object) {
|
|
|
|
Local<Value> value = GetValue(isolate, context, object, kRecordModeParam);
|
|
|
|
if (value->IsString()) {
|
|
|
|
Local<String> v8_string = value->ToString(context).ToLocalChecked();
|
|
|
|
String::Utf8Value str(v8_string);
|
|
|
|
if (strcmp(kRecordUntilFull, *str) == 0) {
|
|
|
|
return platform::tracing::TraceRecordMode::RECORD_UNTIL_FULL;
|
|
|
|
} else if (strcmp(kRecordContinuously, *str) == 0) {
|
|
|
|
return platform::tracing::TraceRecordMode::RECORD_CONTINUOUSLY;
|
|
|
|
} else if (strcmp(kRecordAsMuchAsPossible, *str) == 0) {
|
|
|
|
return platform::tracing::TraceRecordMode::RECORD_AS_MUCH_AS_POSSIBLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return platform::tracing::TraceRecordMode::RECORD_UNTIL_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Local<Value> GetValue(v8::Isolate* isolate, Local<Context> context,
|
|
|
|
Local<v8::Object> object, const char* property) {
|
|
|
|
Local<String> v8_str =
|
|
|
|
String::NewFromUtf8(isolate, property, NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
return object->Get(context, v8_str).ToLocalChecked();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static platform::tracing::TraceConfig* CreateTraceConfigFromJSON(
|
|
|
|
v8::Isolate* isolate, const char* json_str) {
|
|
|
|
platform::tracing::TraceConfig* trace_config =
|
|
|
|
new platform::tracing::TraceConfig();
|
|
|
|
TraceConfigParser::FillTraceConfig(isolate, trace_config, json_str);
|
|
|
|
return trace_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace tracing
|
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_;
|
2015-07-20 07:05:42 +00:00
|
|
|
Global<Context>* realms_;
|
|
|
|
Global<Value> realm_shared_;
|
2013-04-22 11:29:52 +00:00
|
|
|
|
2014-02-25 09:15:05 +00:00
|
|
|
int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args,
|
|
|
|
int arg_offset);
|
2015-07-20 07:05:42 +00:00
|
|
|
int RealmFind(Local<Context> context);
|
2012-11-20 14:47:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-24 13:33:54 +00:00
|
|
|
CounterMap* Shell::counter_map_;
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
|
2008-12-03 09:35:21 +00:00
|
|
|
CounterCollection Shell::local_counters_;
|
|
|
|
CounterCollection* Shell::counters_ = &local_counters_;
|
2015-07-16 16:40:37 +00:00
|
|
|
base::LazyMutex Shell::context_mutex_;
|
2014-06-30 13:25:46 +00:00
|
|
|
const base::TimeTicks Shell::kInitialTicks =
|
|
|
|
base::TimeTicks::HighResolutionNow();
|
2016-03-31 10:16:43 +00:00
|
|
|
Global<Function> Shell::stringify_function_;
|
2015-07-16 16:40:37 +00:00
|
|
|
base::LazyMutex Shell::workers_mutex_;
|
2015-06-25 18:01:11 +00:00
|
|
|
bool Shell::allow_new_workers_ = true;
|
2015-06-22 17:12:26 +00:00
|
|
|
i::List<Worker*> Shell::workers_;
|
|
|
|
i::List<SharedArrayBuffer::Contents> Shell::externalized_shared_contents_;
|
2011-07-14 15:43:40 +00:00
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Global<Context> Shell::evaluation_context_;
|
2015-06-22 17:12:26 +00:00
|
|
|
ArrayBuffer::Allocator* Shell::array_buffer_allocator;
|
2011-07-11 07:38:09 +00:00
|
|
|
ShellOptions Shell::options;
|
2015-07-14 23:04:18 +00:00
|
|
|
base::OnceType Shell::quit_once_ = V8_ONCE_INIT;
|
2012-01-24 15:48:16 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-21 06:40:15 +00:00
|
|
|
ScriptCompiler::CachedData* CompileForCachedData(
|
|
|
|
Local<String> source, Local<Value> name,
|
|
|
|
ScriptCompiler::CompileOptions compile_options) {
|
|
|
|
int source_length = source->Length();
|
|
|
|
uint16_t* source_buffer = new uint16_t[source_length];
|
|
|
|
source->Write(source_buffer, 0, source_length);
|
|
|
|
int name_length = 0;
|
|
|
|
uint16_t* name_buffer = NULL;
|
|
|
|
if (name->IsString()) {
|
2014-10-27 09:02:49 +00:00
|
|
|
Local<String> name_string = Local<String>::Cast(name);
|
2014-10-21 06:40:15 +00:00
|
|
|
name_length = name_string->Length();
|
|
|
|
name_buffer = new uint16_t[name_length];
|
|
|
|
name_string->Write(name_buffer, 0, name_length);
|
|
|
|
}
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate::CreateParams create_params;
|
2015-06-22 17:12:26 +00:00
|
|
|
create_params.array_buffer_allocator = Shell::array_buffer_allocator;
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate* temp_isolate = Isolate::New(create_params);
|
2014-10-21 06:40:15 +00:00
|
|
|
ScriptCompiler::CachedData* result = NULL;
|
|
|
|
{
|
|
|
|
Isolate::Scope isolate_scope(temp_isolate);
|
|
|
|
HandleScope handle_scope(temp_isolate);
|
|
|
|
Context::Scope context_scope(Context::New(temp_isolate));
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> source_copy =
|
|
|
|
v8::String::NewFromTwoByte(temp_isolate, source_buffer,
|
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
source_length).ToLocalChecked();
|
2014-10-21 06:40:15 +00:00
|
|
|
Local<Value> name_copy;
|
|
|
|
if (name_buffer) {
|
2015-07-20 07:05:42 +00:00
|
|
|
name_copy = v8::String::NewFromTwoByte(temp_isolate, name_buffer,
|
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
name_length).ToLocalChecked();
|
2014-10-21 06:40:15 +00:00
|
|
|
} else {
|
|
|
|
name_copy = v8::Undefined(temp_isolate);
|
|
|
|
}
|
|
|
|
ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy));
|
2015-07-20 07:05:42 +00:00
|
|
|
if (!ScriptCompiler::CompileUnboundScript(temp_isolate, &script_source,
|
|
|
|
compile_options).IsEmpty() &&
|
|
|
|
script_source.GetCachedData()) {
|
2014-10-21 06:40:15 +00:00
|
|
|
int length = script_source.GetCachedData()->length;
|
|
|
|
uint8_t* cache = new uint8_t[length];
|
|
|
|
memcpy(cache, script_source.GetCachedData()->data, length);
|
|
|
|
result = new ScriptCompiler::CachedData(
|
|
|
|
cache, length, ScriptCompiler::CachedData::BufferOwned);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
temp_isolate->Dispose();
|
|
|
|
delete[] source_buffer;
|
2014-10-21 10:59:41 +00:00
|
|
|
delete[] name_buffer;
|
2014-10-21 06:40:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
// Compile a string within the current v8 context.
|
2015-07-20 07:05:42 +00:00
|
|
|
MaybeLocal<Script> Shell::CompileString(
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
Isolate* isolate, Local<String> source, Local<Value> name,
|
2016-09-16 21:47:14 +00:00
|
|
|
ScriptCompiler::CompileOptions compile_options) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Context> context(isolate->GetCurrentContext());
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
ScriptOrigin origin(name);
|
2016-09-16 21:47:14 +00:00
|
|
|
if (compile_options == ScriptCompiler::kNoCompileOptions) {
|
2014-10-21 06:40:15 +00:00
|
|
|
ScriptCompiler::Source script_source(source, origin);
|
2016-09-16 21:47:14 +00:00
|
|
|
return ScriptCompiler::Compile(context, &script_source, compile_options);
|
2014-10-21 06:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptCompiler::CachedData* data =
|
|
|
|
CompileForCachedData(source, name, compile_options);
|
|
|
|
ScriptCompiler::Source cached_source(source, origin, data);
|
|
|
|
if (compile_options == ScriptCompiler::kProduceCodeCache) {
|
|
|
|
compile_options = ScriptCompiler::kConsumeCodeCache;
|
|
|
|
} else if (compile_options == ScriptCompiler::kProduceParserCache) {
|
|
|
|
compile_options = ScriptCompiler::kConsumeParserCache;
|
|
|
|
} else {
|
|
|
|
DCHECK(false); // A new compile option?
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
}
|
2014-10-21 06:40:15 +00:00
|
|
|
if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions;
|
2015-07-20 07:05:42 +00:00
|
|
|
MaybeLocal<Script> result =
|
2016-05-27 19:22:23 +00:00
|
|
|
ScriptCompiler::Compile(context, &cached_source, compile_options);
|
2015-02-11 14:10:02 +00:00
|
|
|
CHECK(data == NULL || !data->rejected);
|
|
|
|
return result;
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
// Executes a string within the current v8 context.
|
2015-07-20 07:05:42 +00:00
|
|
|
bool Shell::ExecuteString(Isolate* isolate, Local<String> source,
|
|
|
|
Local<Value> name, bool print_result,
|
2016-09-20 23:38:58 +00:00
|
|
|
bool report_exceptions) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch try_catch(isolate);
|
2016-02-12 09:12:20 +00:00
|
|
|
try_catch.SetVerbose(true);
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
MaybeLocal<Value> maybe_result;
|
2015-02-10 19:11:44 +00:00
|
|
|
{
|
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_]);
|
2015-02-10 19:11:44 +00:00
|
|
|
Context::Scope context_scope(realm);
|
2016-09-20 23:38:58 +00:00
|
|
|
Local<Script> script;
|
|
|
|
if (!Shell::CompileString(isolate, source, name, options.compile_options)
|
|
|
|
.ToLocal(&script)) {
|
|
|
|
// Print errors that happened during compilation.
|
|
|
|
if (report_exceptions) ReportException(isolate, &try_catch);
|
|
|
|
return false;
|
2015-02-10 19:11:44 +00:00
|
|
|
}
|
2016-09-20 23:38:58 +00:00
|
|
|
maybe_result = script->Run(realm);
|
2015-07-15 12:26:06 +00:00
|
|
|
EmptyMessageQueues(isolate);
|
2015-02-10 19:11:44 +00:00
|
|
|
data->realm_current_ = data->realm_switch_;
|
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> result;
|
|
|
|
if (!maybe_result.ToLocal(&result)) {
|
2015-02-10 19:11:44 +00:00
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
// Print errors that happened during execution.
|
2015-07-23 09:49:19 +00:00
|
|
|
if (report_exceptions) ReportException(isolate, &try_catch);
|
2015-02-10 19:11:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DCHECK(!try_catch.HasCaught());
|
|
|
|
if (print_result) {
|
|
|
|
if (options.test_shell) {
|
|
|
|
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:18:05 +00:00
|
|
|
}
|
2015-02-10 19:11:44 +00:00
|
|
|
} else {
|
2016-03-31 10:16:43 +00:00
|
|
|
v8::String::Utf8Value str(Stringify(isolate, result));
|
2015-02-10 19:11:44 +00:00
|
|
|
fwrite(*str, sizeof(**str), str.length(), stdout);
|
|
|
|
printf("\n");
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 19:11:44 +00:00
|
|
|
return true;
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:03:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-09-23 00:34:54 +00:00
|
|
|
std::string ToSTLString(Local<String> v8_str) {
|
|
|
|
String::Utf8Value utf8(v8_str);
|
|
|
|
// Should not be able to fail since the input is a String.
|
|
|
|
CHECK(*utf8);
|
|
|
|
return *utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAbsolutePath(const std::string& path) {
|
2016-09-22 21:03:19 +00:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
// TODO(adamk): This is an incorrect approximation, but should
|
|
|
|
// work for all our test-running cases.
|
2016-09-23 00:34:54 +00:00
|
|
|
return path.find(':') != std::string::npos;
|
2016-09-22 21:03:19 +00:00
|
|
|
#else
|
|
|
|
return path[0] == '/';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetWorkingDirectory() {
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
char system_buffer[MAX_PATH];
|
|
|
|
// TODO(adamk): Support Unicode paths.
|
|
|
|
DWORD len = GetCurrentDirectoryA(MAX_PATH, system_buffer);
|
|
|
|
CHECK(len > 0);
|
|
|
|
return system_buffer;
|
|
|
|
#else
|
|
|
|
char curdir[PATH_MAX];
|
|
|
|
CHECK_NOT_NULL(getcwd(curdir, PATH_MAX));
|
|
|
|
return curdir;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-23 00:34:54 +00:00
|
|
|
// Returns the directory part of path, without the trailing '/'.
|
2016-09-22 21:03:19 +00:00
|
|
|
std::string DirName(const std::string& path) {
|
2016-09-23 00:34:54 +00:00
|
|
|
DCHECK(IsAbsolutePath(path));
|
|
|
|
size_t last_slash = path.find_last_of('/');
|
2016-09-22 21:03:19 +00:00
|
|
|
DCHECK(last_slash != std::string::npos);
|
2016-09-23 00:34:54 +00:00
|
|
|
return path.substr(0, last_slash);
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:32:23 +00:00
|
|
|
// Resolves path to an absolute path if necessary, and does some
|
|
|
|
// normalization (eliding references to the current directory
|
|
|
|
// and replacing backslashes with slashes).
|
|
|
|
std::string NormalizePath(const std::string& path,
|
|
|
|
const std::string& dir_name) {
|
|
|
|
std::string result;
|
|
|
|
if (IsAbsolutePath(path)) {
|
|
|
|
result = path;
|
|
|
|
} else {
|
|
|
|
result = dir_name + '/' + path;
|
|
|
|
}
|
|
|
|
std::replace(result.begin(), result.end(), '\\', '/');
|
|
|
|
size_t i;
|
|
|
|
while ((i = result.find("/./")) != std::string::npos) {
|
|
|
|
result.erase(i, 2);
|
|
|
|
}
|
|
|
|
return result;
|
2016-09-22 21:03:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 19:22:03 +00:00
|
|
|
// Per-context Module data, allowing sharing of module maps
|
|
|
|
// across top-level module loads.
|
|
|
|
class ModuleEmbedderData {
|
|
|
|
private:
|
|
|
|
class ModuleGlobalHash {
|
|
|
|
public:
|
|
|
|
explicit ModuleGlobalHash(Isolate* isolate) : isolate_(isolate) {}
|
|
|
|
size_t operator()(const Global<Module>& module) const {
|
|
|
|
return module.Get(isolate_)->GetIdentityHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Isolate* isolate_;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ModuleEmbedderData(Isolate* isolate)
|
|
|
|
: module_to_directory_map(10, ModuleGlobalHash(isolate)) {}
|
|
|
|
|
|
|
|
// Map from normalized module specifier to Module.
|
|
|
|
std::unordered_map<std::string, Global<Module>> specifier_to_module_map;
|
|
|
|
// Map from Module to the directory that Module was loaded from.
|
|
|
|
std::unordered_map<Global<Module>, std::string, ModuleGlobalHash>
|
|
|
|
module_to_directory_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
// The debugger reserves the first slot in the Context embedder data.
|
|
|
|
kDebugIdIndex = Context::kDebugIdIndex,
|
|
|
|
kModuleEmbedderDataIndex
|
|
|
|
};
|
|
|
|
|
|
|
|
void InitializeModuleEmbedderData(Local<Context> context) {
|
|
|
|
context->SetAlignedPointerInEmbedderData(
|
|
|
|
kModuleEmbedderDataIndex, new ModuleEmbedderData(context->GetIsolate()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleEmbedderData* GetModuleDataFromContext(Local<Context> context) {
|
|
|
|
return static_cast<ModuleEmbedderData*>(
|
|
|
|
context->GetAlignedPointerFromEmbedderData(kModuleEmbedderDataIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisposeModuleEmbedderData(Local<Context> context) {
|
|
|
|
delete GetModuleDataFromContext(context);
|
|
|
|
context->SetAlignedPointerInEmbedderData(kModuleEmbedderDataIndex, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:03:19 +00:00
|
|
|
MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
|
|
|
|
Local<String> specifier,
|
2016-10-11 19:22:03 +00:00
|
|
|
Local<Module> referrer) {
|
2016-09-22 21:03:19 +00:00
|
|
|
Isolate* isolate = context->GetIsolate();
|
2016-10-11 19:22:03 +00:00
|
|
|
ModuleEmbedderData* d = GetModuleDataFromContext(context);
|
|
|
|
auto dir_name_it =
|
|
|
|
d->module_to_directory_map.find(Global<Module>(isolate, referrer));
|
|
|
|
CHECK(dir_name_it != d->module_to_directory_map.end());
|
2016-09-23 00:34:54 +00:00
|
|
|
std::string absolute_path =
|
2016-10-11 19:22:03 +00:00
|
|
|
NormalizePath(ToSTLString(specifier), dir_name_it->second);
|
|
|
|
auto module_it = d->specifier_to_module_map.find(absolute_path);
|
|
|
|
CHECK(module_it != d->specifier_to_module_map.end());
|
|
|
|
return module_it->second.Get(isolate);
|
2016-09-22 21:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-10-11 19:22:03 +00:00
|
|
|
MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
|
|
|
|
const std::string& file_name) {
|
2016-09-23 00:34:54 +00:00
|
|
|
DCHECK(IsAbsolutePath(file_name));
|
2016-10-11 19:22:03 +00:00
|
|
|
Isolate* isolate = context->GetIsolate();
|
2016-09-20 23:38:58 +00:00
|
|
|
TryCatch try_catch(isolate);
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
Local<String> source_text = ReadFile(isolate, file_name.c_str());
|
|
|
|
if (source_text.IsEmpty()) {
|
|
|
|
printf("Error reading '%s'\n", file_name.c_str());
|
|
|
|
Shell::Exit(1);
|
|
|
|
}
|
|
|
|
ScriptOrigin origin(
|
|
|
|
String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
|
|
|
|
.ToLocalChecked());
|
|
|
|
ScriptCompiler::Source source(source_text, origin);
|
|
|
|
Local<Module> module;
|
|
|
|
if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
|
|
|
|
ReportException(isolate, &try_catch);
|
|
|
|
return MaybeLocal<Module>();
|
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
|
|
|
|
ModuleEmbedderData* d = GetModuleDataFromContext(context);
|
|
|
|
CHECK(d->specifier_to_module_map
|
|
|
|
.insert(std::make_pair(file_name, Global<Module>(isolate, module)))
|
|
|
|
.second);
|
2016-09-22 21:03:19 +00:00
|
|
|
|
|
|
|
std::string dir_name = DirName(file_name);
|
2016-10-11 19:22:03 +00:00
|
|
|
CHECK(d->module_to_directory_map
|
|
|
|
.insert(std::make_pair(Global<Module>(isolate, module), dir_name))
|
|
|
|
.second);
|
2016-09-22 21:03:19 +00:00
|
|
|
|
2016-09-20 23:38:58 +00:00
|
|
|
for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
|
|
|
|
Local<String> name = module->GetModuleRequest(i);
|
2016-10-06 19:32:23 +00:00
|
|
|
std::string absolute_path = NormalizePath(ToSTLString(name), dir_name);
|
2016-10-11 19:22:03 +00:00
|
|
|
if (!d->specifier_to_module_map.count(absolute_path)) {
|
|
|
|
if (FetchModuleTree(context, absolute_path).IsEmpty()) {
|
2016-09-20 23:38:58 +00:00
|
|
|
return MaybeLocal<Module>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
|
2016-10-11 19:22:03 +00:00
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
|
|
|
Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
|
|
|
|
Context::Scope context_scope(realm);
|
|
|
|
|
2016-10-06 19:32:23 +00:00
|
|
|
std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
|
2016-09-22 21:03:19 +00:00
|
|
|
|
2016-09-20 23:38:58 +00:00
|
|
|
Local<Module> root_module;
|
2016-10-11 19:22:03 +00:00
|
|
|
if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
|
2016-09-20 23:38:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TryCatch try_catch(isolate);
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
|
|
|
|
MaybeLocal<Value> maybe_result;
|
2016-10-11 19:22:03 +00:00
|
|
|
if (root_module->Instantiate(realm, ResolveModuleCallback)) {
|
|
|
|
maybe_result = root_module->Evaluate(realm);
|
|
|
|
EmptyMessageQueues(isolate);
|
2016-09-20 23:38:58 +00:00
|
|
|
}
|
|
|
|
Local<Value> result;
|
|
|
|
if (!maybe_result.ToLocal(&result)) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
// Print errors that happened during execution.
|
|
|
|
ReportException(isolate, &try_catch);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DCHECK(!try_catch.HasCaught());
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
|
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;
|
2015-07-20 07:05:42 +00:00
|
|
|
data_->realms_ = new Global<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.
|
2016-10-11 19:22:03 +00:00
|
|
|
for (int i = 0; i < data_->realm_count_; ++i) {
|
|
|
|
Global<Context>& realm = data_->realms_[i];
|
|
|
|
if (realm.IsEmpty()) continue;
|
|
|
|
DisposeModuleEmbedderData(realm.Get(data_->isolate_));
|
|
|
|
// TODO(adamk): No need to reset manually, Globals reset when destructed.
|
|
|
|
realm.Reset();
|
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
delete[] data_->realms_;
|
2016-10-11 19:22:03 +00:00
|
|
|
// TODO(adamk): No need to reset manually, Globals reset when destructed.
|
2013-04-22 11:29:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
int PerIsolateData::RealmFind(Local<Context> context) {
|
2013-04-22 11:29:52 +00:00
|
|
|
for (int i = 0; i < realm_count_; ++i) {
|
|
|
|
if (realms_[i] == context) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-25 09:15:05 +00:00
|
|
|
int PerIsolateData::RealmIndexOrThrow(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args,
|
|
|
|
int arg_offset) {
|
|
|
|
if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) {
|
|
|
|
Throw(args.GetIsolate(), "Invalid argument");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
int index = args[arg_offset]
|
|
|
|
->Int32Value(args.GetIsolate()->GetCurrentContext())
|
|
|
|
.FromMaybe(-1);
|
|
|
|
if (index < 0 || index >= realm_count_ || realms_[index].IsEmpty()) {
|
2014-02-25 09:15:05 +00:00
|
|
|
Throw(args.GetIsolate(), "Invalid realm index");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-10 12:07:29 +00:00
|
|
|
// performance.now() returns a time stamp as double, measured in milliseconds.
|
2015-11-12 13:42:49 +00:00
|
|
|
// When FLAG_verify_predictable mode is enabled it returns result of
|
|
|
|
// v8::Platform::MonotonicallyIncreasingTime().
|
2014-01-10 12:07:29 +00:00
|
|
|
void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2014-06-20 07:35:48 +00:00
|
|
|
if (i::FLAG_verify_predictable) {
|
2015-11-12 13:42:49 +00:00
|
|
|
args.GetReturnValue().Set(g_platform->MonotonicallyIncreasingTime());
|
2014-06-20 07:35:48 +00:00
|
|
|
} else {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::TimeDelta delta =
|
|
|
|
base::TimeTicks::HighResolutionNow() - kInitialTicks;
|
2014-06-20 07:35:48 +00:00
|
|
|
args.GetReturnValue().Set(delta.InMillisecondsF());
|
|
|
|
}
|
2014-01-10 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
int index = data->RealmFind(args[0]
|
|
|
|
->ToObject(isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->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());
|
2014-02-25 09:15:05 +00:00
|
|
|
int index = data->RealmIndexOrThrow(args, 0);
|
|
|
|
if (index == -1) return;
|
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
|
|
|
}
|
|
|
|
|
2016-05-27 18:47:05 +00:00
|
|
|
MaybeLocal<Context> Shell::CreateRealm(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 11:29:52 +00:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2015-06-23 15:08:42 +00:00
|
|
|
TryCatch try_catch(isolate);
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData* data = PerIsolateData::Get(isolate);
|
2015-07-20 07:05:42 +00:00
|
|
|
Global<Context>* old_realms = data->realms_;
|
2013-04-22 11:29:52 +00:00
|
|
|
int index = data->realm_count_;
|
2015-07-20 07:05:42 +00:00
|
|
|
data->realms_ = new Global<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]);
|
2015-06-23 11:04:12 +00:00
|
|
|
old_realms[i].Reset();
|
2013-05-29 08:39:37 +00:00
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
delete[] old_realms;
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
|
2015-06-23 15:08:42 +00:00
|
|
|
Local<Context> context = Context::New(isolate, NULL, global_template);
|
|
|
|
if (context.IsEmpty()) {
|
|
|
|
DCHECK(try_catch.HasCaught());
|
|
|
|
try_catch.ReThrow();
|
2016-05-27 18:47:05 +00:00
|
|
|
return MaybeLocal<Context>();
|
2015-06-23 15:08:42 +00:00
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
InitializeModuleEmbedderData(context);
|
2015-06-23 15:08:42 +00:00
|
|
|
data->realms_[index].Reset(isolate, context);
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(index);
|
2016-05-27 18:47:05 +00:00
|
|
|
return context;
|
2013-04-22 11:29:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 18:47:05 +00:00
|
|
|
// Realm.create() creates a new realm with a distinct security token
|
|
|
|
// and returns its index.
|
|
|
|
void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
CreateRealm(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Realm.createAllowCrossRealmAccess() creates a new realm with the same
|
|
|
|
// security token as the current realm.
|
|
|
|
void Shell::RealmCreateAllowCrossRealmAccess(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Local<Context> context;
|
|
|
|
if (CreateRealm(args).ToLocal(&context)) {
|
|
|
|
context->SetSecurityToken(
|
|
|
|
args.GetIsolate()->GetEnteredContext()->GetSecurityToken());
|
|
|
|
}
|
|
|
|
}
|
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);
|
2014-02-25 09:15:05 +00:00
|
|
|
int index = data->RealmIndexOrThrow(args, 0);
|
|
|
|
if (index == -1) return;
|
|
|
|
if (index == 0 ||
|
2013-04-22 11:29:52 +00:00
|
|
|
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
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
DisposeModuleEmbedderData(data->realms_[index].Get(isolate));
|
2013-11-22 12:35:39 +00:00
|
|
|
data->realms_[index].Reset();
|
2015-05-05 14:25:20 +00:00
|
|
|
isolate->ContextDisposedNotification();
|
|
|
|
isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime());
|
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);
|
2014-02-25 09:15:05 +00:00
|
|
|
int index = data->RealmIndexOrThrow(args, 0);
|
|
|
|
if (index == -1) 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);
|
2014-02-25 09:15:05 +00:00
|
|
|
int index = data->RealmIndexOrThrow(args, 0);
|
|
|
|
if (index == -1) return;
|
|
|
|
if (args.Length() < 2 || !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
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
ScriptCompiler::Source script_source(
|
|
|
|
args[1]->ToString(isolate->GetCurrentContext()).ToLocalChecked());
|
|
|
|
Local<UnboundScript> script;
|
|
|
|
if (!ScriptCompiler::CompileUnboundScript(isolate, &script_source)
|
|
|
|
.ToLocal(&script)) {
|
|
|
|
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();
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> result;
|
|
|
|
if (!script->BindToCurrentContext()->Run(realm).ToLocal(&result)) {
|
|
|
|
realm->Exit();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-22 11:29:52 +00:00
|
|
|
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().
|
2015-05-28 12:49:31 +00:00
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
2015-12-29 09:57:03 +00:00
|
|
|
Local<Value> arg = args[i];
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> str_obj;
|
2015-12-29 09:57:03 +00:00
|
|
|
|
|
|
|
if (arg->IsSymbol()) {
|
|
|
|
arg = Local<Symbol>::Cast(arg)->Name();
|
|
|
|
}
|
|
|
|
if (!arg->ToString(args.GetIsolate()->GetCurrentContext())
|
2015-07-20 07:05:42 +00:00
|
|
|
.ToLocal(&str_obj)) {
|
2013-06-05 12:36:33 +00:00
|
|
|
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
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> Shell::ReadFromStdin(Isolate* isolate) {
|
2011-07-14 15:43:40 +00:00
|
|
|
static const int kBufferSize = 256;
|
|
|
|
char buffer[kBufferSize];
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> accumulator =
|
|
|
|
String::NewFromUtf8(isolate, "", NewStringType::kNormal).ToLocalChecked();
|
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;
|
2014-05-22 08:11:10 +00:00
|
|
|
input = fgets(buffer, kBufferSize, stdin);
|
2015-07-20 07:05:42 +00:00
|
|
|
if (input == NULL) return Local<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,
|
2015-07-20 07:05:42 +00:00
|
|
|
String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, length)
|
|
|
|
.ToLocalChecked());
|
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(
|
2015-07-20 07:05:42 +00:00
|
|
|
accumulator,
|
|
|
|
String::NewFromUtf8(isolate, buffer, NewStringType::kNormal,
|
|
|
|
length - 1).ToLocalChecked());
|
2011-08-01 14:15:02 +00:00
|
|
|
} else {
|
2013-11-22 12:35:39 +00:00
|
|
|
return String::Concat(
|
2015-07-20 07:05:42 +00:00
|
|
|
accumulator,
|
|
|
|
String::NewFromUtf8(isolate, buffer, NewStringType::kNormal,
|
|
|
|
length - 1).ToLocalChecked());
|
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
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<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
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
if (!ExecuteString(
|
|
|
|
args.GetIsolate(), source,
|
|
|
|
String::NewFromUtf8(args.GetIsolate(), *file,
|
|
|
|
NewStringType::kNormal).ToLocalChecked(),
|
|
|
|
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
|
|
|
|
2015-06-22 17:12:26 +00:00
|
|
|
void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
HandleScope handle_scope(isolate);
|
2015-07-01 16:41:48 +00:00
|
|
|
if (args.Length() < 1 || !args[0]->IsString()) {
|
|
|
|
Throw(args.GetIsolate(), "1st argument must be string");
|
2015-06-22 17:12:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:39:10 +00:00
|
|
|
if (!args.IsConstructCall()) {
|
|
|
|
Throw(args.GetIsolate(), "Worker must be constructed with new");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-25 18:01:11 +00:00
|
|
|
{
|
2015-07-16 16:40:37 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard(workers_mutex_.Pointer());
|
2015-08-18 17:12:03 +00:00
|
|
|
if (workers_.length() >= kMaxWorkers) {
|
|
|
|
Throw(args.GetIsolate(), "Too many workers, I won't let you create more");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
// Initialize the internal field to NULL; if we return early without
|
|
|
|
// creating a new Worker (because the main thread is terminating) we can
|
|
|
|
// early-out from the instance calls.
|
|
|
|
args.Holder()->SetAlignedPointerInInternalField(0, NULL);
|
|
|
|
|
2015-06-25 18:01:11 +00:00
|
|
|
if (!allow_new_workers_) return;
|
|
|
|
|
|
|
|
Worker* worker = new Worker;
|
2015-07-30 08:19:29 +00:00
|
|
|
args.Holder()->SetAlignedPointerInInternalField(0, worker);
|
2015-06-25 18:01:11 +00:00
|
|
|
workers_.Add(worker);
|
2015-06-22 17:12:26 +00:00
|
|
|
|
2015-07-01 16:41:48 +00:00
|
|
|
String::Utf8Value script(args[0]);
|
|
|
|
if (!*script) {
|
|
|
|
Throw(args.GetIsolate(), "Can't get worker script");
|
2015-06-29 15:53:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-30 08:19:29 +00:00
|
|
|
worker->StartExecuteInThread(*script);
|
2015-06-25 18:01:11 +00:00
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Local<Context> context = isolate->GetCurrentContext();
|
|
|
|
|
|
|
|
if (args.Length() < 1) {
|
|
|
|
Throw(isolate, "Invalid argument");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
Worker* worker = GetWorkerFromInternalField(isolate, args.Holder());
|
|
|
|
if (!worker) {
|
2015-06-22 17:12:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> message = args[0];
|
2015-06-22 17:12:26 +00:00
|
|
|
ObjectList to_transfer;
|
|
|
|
if (args.Length() >= 2) {
|
|
|
|
if (!args[1]->IsArray()) {
|
|
|
|
Throw(isolate, "Transfer list must be an Array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Array> transfer = Local<Array>::Cast(args[1]);
|
2015-06-22 17:12:26 +00:00
|
|
|
uint32_t length = transfer->Length();
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> element;
|
2015-06-22 17:12:26 +00:00
|
|
|
if (transfer->Get(context, i).ToLocal(&element)) {
|
|
|
|
if (!element->IsArrayBuffer() && !element->IsSharedArrayBuffer()) {
|
|
|
|
Throw(isolate,
|
|
|
|
"Transfer array elements must be an ArrayBuffer or "
|
|
|
|
"SharedArrayBuffer.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
to_transfer.Add(Local<Object>::Cast(element));
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectList seen_objects;
|
|
|
|
SerializationData* data = new SerializationData;
|
|
|
|
if (SerializeValue(isolate, message, to_transfer, &seen_objects, data)) {
|
|
|
|
worker->PostMessage(data);
|
|
|
|
} else {
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::WorkerGetMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
HandleScope handle_scope(isolate);
|
2015-07-30 08:19:29 +00:00
|
|
|
Worker* worker = GetWorkerFromInternalField(isolate, args.Holder());
|
|
|
|
if (!worker) {
|
2015-06-22 17:12:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializationData* data = worker->GetMessage();
|
|
|
|
if (data) {
|
|
|
|
int offset = 0;
|
|
|
|
Local<Value> data_value;
|
|
|
|
if (Shell::DeserializeValue(isolate, *data, &offset).ToLocal(&data_value)) {
|
|
|
|
args.GetReturnValue().Set(data_value);
|
|
|
|
}
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::WorkerTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
HandleScope handle_scope(isolate);
|
2015-07-30 08:19:29 +00:00
|
|
|
Worker* worker = GetWorkerFromInternalField(isolate, args.Holder());
|
|
|
|
if (!worker) {
|
2015-06-22 17:12:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
worker->Terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-14 23:04:18 +00:00
|
|
|
void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) {
|
2015-07-20 07:05:42 +00:00
|
|
|
int exit_code = (*args)[0]
|
|
|
|
->Int32Value(args->GetIsolate()->GetCurrentContext())
|
|
|
|
.FromMaybe(0);
|
2015-07-09 19:30:18 +00:00
|
|
|
CleanupWorkers();
|
2015-07-14 23:04:18 +00:00
|
|
|
OnExit(args->GetIsolate());
|
2015-08-18 16:47:19 +00:00
|
|
|
Exit(exit_code);
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-14 23:04:18 +00:00
|
|
|
void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
base::CallOnce(&quit_once_, &QuitOnce,
|
|
|
|
const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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(
|
2015-07-20 07:05:42 +00:00
|
|
|
String::NewFromUtf8(args.GetIsolate(), V8::GetVersion(),
|
|
|
|
NewStringType::kNormal).ToLocalChecked());
|
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);
|
2016-03-31 10:16:43 +00:00
|
|
|
Local<Context> 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) {
|
2016-03-31 10:16:43 +00:00
|
|
|
context = Local<Context>::New(isolate, evaluation_context_);
|
|
|
|
context->Enter();
|
2013-05-29 08:39:37 +00:00
|
|
|
}
|
2009-02-05 13:53:41 +00:00
|
|
|
v8::String::Utf8Value exception(try_catch->Exception());
|
|
|
|
const char* exception_string = ToCString(exception);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Message> message = try_catch->Message();
|
2008-10-21 09:12:27 +00:00
|
|
|
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).
|
2014-06-20 07:44:05 +00:00
|
|
|
v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
|
2009-02-05 13:53:41 +00:00
|
|
|
const char* filename_string = ToCString(filename);
|
2016-06-16 10:12:06 +00:00
|
|
|
Maybe<int> maybeline = message->GetLineNumber(isolate->GetCurrentContext());
|
|
|
|
int linenum = maybeline.IsJust() ? maybeline.FromJust() : -1;
|
2009-02-05 13:53:41 +00:00
|
|
|
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
|
2016-06-16 11:17:28 +00:00
|
|
|
Local<String> sourceline;
|
|
|
|
if (message->GetSourceLine(isolate->GetCurrentContext())
|
|
|
|
.ToLocal(&sourceline)) {
|
2016-06-16 10:12:06 +00:00
|
|
|
// Print line of source code.
|
2016-06-16 11:17:28 +00:00
|
|
|
v8::String::Utf8Value sourcelinevalue(sourceline);
|
|
|
|
const char* sourceline_string = ToCString(sourcelinevalue);
|
2016-06-16 10:12:06 +00:00
|
|
|
printf("%s\n", sourceline_string);
|
|
|
|
// Print wavy underline (GetUnderline is deprecated).
|
|
|
|
int start =
|
|
|
|
message->GetStartColumn(isolate->GetCurrentContext()).FromJust();
|
|
|
|
for (int i = 0; i < start; i++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
int end = message->GetEndColumn(isolate->GetCurrentContext()).FromJust();
|
|
|
|
for (int i = start; i < end; i++) {
|
|
|
|
printf("^");
|
|
|
|
}
|
|
|
|
printf("\n");
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> stack_trace_string;
|
|
|
|
if (try_catch->StackTrace(isolate->GetCurrentContext())
|
2015-07-20 14:49:21 +00:00
|
|
|
.ToLocal(&stack_trace_string) &&
|
|
|
|
stack_trace_string->IsString()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
v8::String::Utf8Value stack_trace(
|
|
|
|
Local<String>::Cast(stack_trace_string));
|
|
|
|
printf("%s\n", ToCString(stack_trace));
|
2011-05-23 10:41:02 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2011-09-09 11:10:36 +00:00
|
|
|
printf("\n");
|
2016-03-31 10:16:43 +00:00
|
|
|
if (enter_context) context->Exit();
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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_++];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-07 07:19:46 +00:00
|
|
|
void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
|
2014-06-30 13:25:46 +00:00
|
|
|
counters_file_ = base::OS::MemoryMappedFile::create(
|
2011-07-14 15:43:40 +00:00
|
|
|
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);
|
2014-07-07 07:19:46 +00:00
|
|
|
isolate->SetCounterFunction(LookupCounter);
|
|
|
|
isolate->SetCreateHistogramFunction(CreateHistogram);
|
|
|
|
isolate->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 {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(counter->is_histogram() == is_histogram);
|
2009-04-14 00:30:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-31 10:16:43 +00:00
|
|
|
// Turn a value into a human-readable string.
|
|
|
|
Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) {
|
|
|
|
v8::Local<v8::Context> context =
|
2013-05-29 08:39:37 +00:00
|
|
|
v8::Local<v8::Context>::New(isolate, evaluation_context_);
|
2016-03-31 10:16:43 +00:00
|
|
|
if (stringify_function_.IsEmpty()) {
|
|
|
|
int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
|
|
|
|
i::Vector<const char> source_string =
|
|
|
|
i::NativesCollection<i::D8>::GetScriptSource(source_index);
|
|
|
|
i::Vector<const char> source_name =
|
|
|
|
i::NativesCollection<i::D8>::GetScriptName(source_index);
|
|
|
|
Local<String> source =
|
|
|
|
String::NewFromUtf8(isolate, source_string.start(),
|
|
|
|
NewStringType::kNormal, source_string.length())
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<String> name =
|
|
|
|
String::NewFromUtf8(isolate, source_name.start(),
|
|
|
|
NewStringType::kNormal, source_name.length())
|
|
|
|
.ToLocalChecked();
|
|
|
|
ScriptOrigin origin(name);
|
|
|
|
Local<Script> script =
|
|
|
|
Script::Compile(context, source, &origin).ToLocalChecked();
|
|
|
|
stringify_function_.Reset(
|
|
|
|
isolate, script->Run(context).ToLocalChecked().As<Function>());
|
|
|
|
}
|
|
|
|
Local<Function> fun = Local<Function>::New(isolate, stringify_function_);
|
|
|
|
Local<Value> argv[1] = {value};
|
|
|
|
v8::TryCatch try_catch(isolate);
|
2016-07-06 14:00:43 +00:00
|
|
|
MaybeLocal<Value> result = fun->Call(context, Undefined(isolate), 1, argv);
|
2016-03-31 10:16:43 +00:00
|
|
|
if (result.IsEmpty()) return String::Empty(isolate);
|
|
|
|
return result.ToLocalChecked().As<String>();
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
|
|
|
|
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "print", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Print));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "write", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Write));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "read", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Read));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "readbuffer", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, ReadBuffer));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "readline", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, ReadLine));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "load", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Load));
|
2015-04-22 12:24:29 +00:00
|
|
|
// Some Emscripten-generated code tries to call 'quit', which in turn would
|
|
|
|
// call C's exit(). This would lead to memory leaks, because there is no way
|
|
|
|
// we can terminate cleanly then, so we need a way to hide 'quit'.
|
|
|
|
if (!options.omit_quit) {
|
2015-07-20 07:05:42 +00:00
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "quit", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Quit));
|
2015-04-22 12:24:29 +00:00
|
|
|
}
|
2015-07-20 07:05:42 +00:00
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "version", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, Version));
|
2016-06-23 13:37:35 +00:00
|
|
|
global_template->Set(
|
|
|
|
Symbol::GetToStringTag(isolate),
|
|
|
|
String::NewFromUtf8(isolate, "global", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked());
|
2009-03-31 06:51:25 +00:00
|
|
|
|
2013-04-22 11:29:52 +00:00
|
|
|
// Bind the Realm object.
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "current", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmCurrent));
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "owner", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmOwner));
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "global", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmGlobal));
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "create", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmCreate));
|
2016-05-27 18:47:05 +00:00
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "createAllowCrossRealmAccess",
|
|
|
|
NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess));
|
2015-07-20 07:05:42 +00:00
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmDispose));
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "switch", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmSwitch));
|
|
|
|
realm_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "eval", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, RealmEval));
|
|
|
|
realm_template->SetAccessor(
|
|
|
|
String::NewFromUtf8(isolate, "shared", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
RealmSharedGet, RealmSharedSet);
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "Realm", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
realm_template);
|
2013-04-22 11:29:52 +00:00
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
|
|
|
|
performance_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "now", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
FunctionTemplate::New(isolate, PerformanceNow));
|
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "performance", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
performance_template);
|
|
|
|
|
|
|
|
Local<FunctionTemplate> worker_fun_template =
|
2015-06-22 17:12:26 +00:00
|
|
|
FunctionTemplate::New(isolate, WorkerNew);
|
2015-07-30 08:19:29 +00:00
|
|
|
Local<Signature> worker_signature =
|
|
|
|
Signature::New(isolate, worker_fun_template);
|
|
|
|
worker_fun_template->SetClassName(
|
|
|
|
String::NewFromUtf8(isolate, "Worker", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked());
|
2015-08-11 00:17:01 +00:00
|
|
|
worker_fun_template->ReadOnlyPrototype();
|
2015-06-22 17:12:26 +00:00
|
|
|
worker_fun_template->PrototypeTemplate()->Set(
|
2015-07-20 07:05:42 +00:00
|
|
|
String::NewFromUtf8(isolate, "terminate", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
2015-07-30 08:19:29 +00:00
|
|
|
FunctionTemplate::New(isolate, WorkerTerminate, Local<Value>(),
|
|
|
|
worker_signature));
|
2015-06-22 17:12:26 +00:00
|
|
|
worker_fun_template->PrototypeTemplate()->Set(
|
2015-07-20 07:05:42 +00:00
|
|
|
String::NewFromUtf8(isolate, "postMessage", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
2015-07-30 08:19:29 +00:00
|
|
|
FunctionTemplate::New(isolate, WorkerPostMessage, Local<Value>(),
|
|
|
|
worker_signature));
|
2015-06-22 17:12:26 +00:00
|
|
|
worker_fun_template->PrototypeTemplate()->Set(
|
2015-07-20 07:05:42 +00:00
|
|
|
String::NewFromUtf8(isolate, "getMessage", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
2015-07-30 08:19:29 +00:00
|
|
|
FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(),
|
|
|
|
worker_signature));
|
2015-06-22 17:12:26 +00:00
|
|
|
worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1);
|
2015-07-20 07:05:42 +00:00
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "Worker", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
worker_fun_template);
|
2014-01-10 12:07:29 +00:00
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> os_templ = ObjectTemplate::New(isolate);
|
2013-11-22 12:35:39 +00:00
|
|
|
AddOSMethods(isolate, os_templ);
|
2015-07-20 07:05:42 +00:00
|
|
|
global_template->Set(
|
|
|
|
String::NewFromUtf8(isolate, "os", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
os_templ);
|
2008-10-21 09:12:27 +00:00
|
|
|
|
2011-06-09 14:28:49 +00:00
|
|
|
return global_template;
|
|
|
|
}
|
|
|
|
|
2016-02-12 09:12:20 +00:00
|
|
|
static void EmptyMessageCallback(Local<Message> message, Local<Value> error) {
|
|
|
|
// Nothing to be done here, exceptions thrown up to the shell will be reported
|
|
|
|
// separately by {Shell::ReportException} after they are caught.
|
|
|
|
}
|
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
|
|
|
// Set up counters
|
|
|
|
if (i::StrLength(i::FLAG_map_counters) != 0)
|
2014-07-07 07:19:46 +00:00
|
|
|
MapCounters(isolate, i::FLAG_map_counters);
|
2016-02-12 09:12:20 +00:00
|
|
|
// Disable default message reporting.
|
|
|
|
isolate->AddMessageListener(EmptyMessageCallback);
|
2012-11-22 08:35:21 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 20:04:32 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
|
2011-07-11 07:38:09 +00:00
|
|
|
// This needs to be a critical section since this is not thread-safe
|
2015-07-16 16:40:37 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard(context_mutex_.Pointer());
|
2011-06-09 14:28:49 +00:00
|
|
|
// Initialize the global objects
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
|
2013-12-04 10:09:08 +00:00
|
|
|
EscapableHandleScope handle_scope(isolate);
|
2013-05-08 07:45:16 +00:00
|
|
|
Local<Context> context = Context::New(isolate, NULL, global_template);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!context.IsEmpty());
|
2016-10-11 19:22:03 +00:00
|
|
|
InitializeModuleEmbedderData(context);
|
2013-05-08 07:45:16 +00:00
|
|
|
Context::Scope scope(context);
|
2008-11-25 12:58:48 +00:00
|
|
|
|
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 =
|
2014-04-17 13:27:02 +00:00
|
|
|
factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked();
|
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);
|
2015-07-20 07:05:42 +00:00
|
|
|
context->Global()
|
|
|
|
->Set(context,
|
|
|
|
String::NewFromUtf8(isolate, "arguments", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
Utils::ToLocal(arguments_jsarray))
|
|
|
|
.FromJust();
|
2013-12-04 10:09:08 +00:00
|
|
|
return handle_scope.Escape(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-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
|
|
|
}
|
2016-04-27 11:10:41 +00:00
|
|
|
|
|
|
|
void Shell::WriteIgnitionDispatchCountersFile(v8::Isolate* isolate) {
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Local<Context> context = Context::New(isolate);
|
|
|
|
Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
Local<Object> dispatch_counters = reinterpret_cast<i::Isolate*>(isolate)
|
|
|
|
->interpreter()
|
|
|
|
->GetDispatchCountersObject();
|
|
|
|
std::ofstream dispatch_counters_stream(
|
|
|
|
i::FLAG_trace_ignition_dispatches_output_file);
|
|
|
|
dispatch_counters_stream << *String::Utf8Value(
|
|
|
|
JSON::Stringify(context, dispatch_counters).ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:12:00 +00:00
|
|
|
|
2014-11-21 09:53:04 +00:00
|
|
|
void Shell::OnExit(v8::Isolate* isolate) {
|
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
|
|
|
}
|
2016-04-11 11:57:31 +00:00
|
|
|
|
2012-03-27 09:50:48 +00:00
|
|
|
delete counters_file_;
|
|
|
|
delete counter_map_;
|
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-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);
|
2015-04-28 06:54:08 +00:00
|
|
|
size_t size = ftell(file);
|
2008-10-21 09:12:27 +00:00
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
char* chars = new char[size + 1];
|
|
|
|
chars[size] = '\0';
|
2015-04-28 06:54:08 +00:00
|
|
|
for (size_t i = 0; i < size;) {
|
|
|
|
i += fread(&chars[i], 1, size - i, file);
|
|
|
|
if (ferror(file)) {
|
|
|
|
fclose(file);
|
|
|
|
delete[] chars;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
|
|
|
fclose(file);
|
2015-04-28 06:54:08 +00:00
|
|
|
*size_out = static_cast<int>(size);
|
2009-01-20 14:14:22 +00:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:40:13 +00:00
|
|
|
|
|
|
|
struct DataAndPersistent {
|
|
|
|
uint8_t* data;
|
2015-04-24 12:35:14 +00:00
|
|
|
int byte_length;
|
|
|
|
Global<ArrayBuffer> handle;
|
2013-12-03 10:40:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void ReadBufferWeakCallback(
|
2015-04-24 12:35:14 +00:00
|
|
|
const v8::WeakCallbackInfo<DataAndPersistent>& data) {
|
|
|
|
int byte_length = data.GetParameter()->byte_length;
|
2013-12-03 10:40:13 +00:00
|
|
|
data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(
|
2013-05-23 10:01:42 +00:00
|
|
|
-static_cast<intptr_t>(byte_length));
|
|
|
|
|
2013-12-03 10:40:13 +00:00
|
|
|
delete[] data.GetParameter()->data;
|
|
|
|
data.GetParameter()->handle.Reset();
|
|
|
|
delete data.GetParameter();
|
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) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(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();
|
2013-12-03 10:40:13 +00:00
|
|
|
DataAndPersistent* data = new DataAndPersistent;
|
|
|
|
data->data = reinterpret_cast<uint8_t*>(
|
2012-11-20 14:47:56 +00:00
|
|
|
ReadChars(args.GetIsolate(), *filename, &length));
|
2013-12-03 10:40:13 +00:00
|
|
|
if (data->data == NULL) {
|
|
|
|
delete data;
|
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
|
|
|
}
|
2015-04-24 12:35:14 +00:00
|
|
|
data->byte_length = length;
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data->data, length);
|
2013-12-03 10:40:13 +00:00
|
|
|
data->handle.Reset(isolate, buffer);
|
2015-04-24 12:35:14 +00:00
|
|
|
data->handle.SetWeak(data, ReadBufferWeakCallback,
|
|
|
|
v8::WeakCallbackType::kParameter);
|
2013-12-03 10:40:13 +00:00
|
|
|
data->handle.MarkIndependent();
|
2013-05-23 10:01:42 +00:00
|
|
|
isolate->AdjustAmountOfExternalAllocatedMemory(length);
|
|
|
|
|
2013-06-05 12:36:33 +00:00
|
|
|
args.GetReturnValue().Set(buffer);
|
2012-05-15 11:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 14:14:22 +00:00
|
|
|
// Reads a file into a v8 string.
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<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);
|
2015-07-20 07:05:42 +00:00
|
|
|
if (chars == NULL) return Local<String>();
|
|
|
|
Local<String> result =
|
|
|
|
String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size)
|
|
|
|
.ToLocalChecked();
|
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-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));
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> name =
|
|
|
|
String::NewFromUtf8(isolate, "(d8)", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
2015-07-23 13:20:14 +00:00
|
|
|
printf("V8 version %s\n", V8::GetVersion());
|
2008-10-21 09:12:27 +00:00
|
|
|
while (true) {
|
2013-03-15 12:06:53 +00:00
|
|
|
HandleScope inner_scope(isolate);
|
2015-07-23 19:26:50 +00:00
|
|
|
printf("d8> ");
|
2015-07-23 13:20:14 +00:00
|
|
|
Local<String> input = Shell::ReadFromStdin(isolate);
|
2012-01-17 13:37:09 +00:00
|
|
|
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-09-08 22:44:03 +00:00
|
|
|
SourceGroup::~SourceGroup() {
|
|
|
|
delete thread_;
|
|
|
|
thread_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> file_name =
|
|
|
|
String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<String> source =
|
|
|
|
String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
2015-08-04 14:31:41 +00:00
|
|
|
Shell::options.script_executed = true;
|
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;
|
2015-02-10 19:11:44 +00:00
|
|
|
continue;
|
|
|
|
} else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) {
|
|
|
|
// Treat the next file as a module.
|
|
|
|
arg = argv_[++i];
|
2016-09-20 23:38:58 +00:00
|
|
|
Shell::options.script_executed = true;
|
|
|
|
if (!Shell::ExecuteModule(isolate, arg)) {
|
|
|
|
exception_was_thrown = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
2011-07-11 07:38:09 +00:00
|
|
|
} else if (arg[0] == '-') {
|
|
|
|
// Ignore other options. They have been parsed already.
|
2015-02-10 19:11:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use all other arguments as names of files to load and run.
|
|
|
|
HandleScope handle_scope(isolate);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> file_name =
|
|
|
|
String::NewFromUtf8(isolate, arg, NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<String> source = ReadFile(isolate, arg);
|
2015-02-10 19:11:44 +00:00
|
|
|
if (source.IsEmpty()) {
|
|
|
|
printf("Error reading '%s'\n", arg);
|
|
|
|
Shell::Exit(1);
|
|
|
|
}
|
2015-08-04 14:31:41 +00:00
|
|
|
Shell::options.script_executed = true;
|
2016-09-20 23:38:58 +00:00
|
|
|
if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
|
2015-02-10 19:11:44 +00:00
|
|
|
exception_was_thrown = true;
|
|
|
|
break;
|
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
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<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);
|
2015-07-20 07:05:42 +00:00
|
|
|
if (chars == NULL) return Local<String>();
|
|
|
|
Local<String> result =
|
|
|
|
String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size)
|
|
|
|
.ToLocalChecked();
|
2011-07-11 07:38:09 +00:00
|
|
|
delete[] chars;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
base::Thread::Options SourceGroup::GetThreadOptions() {
|
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-24 15:48:16 +00:00
|
|
|
// OS-specific padding for thread startup code. 2Mbytes seems to be enough.
|
2014-06-30 13:25:46 +00:00
|
|
|
return base::Thread::Options("IsolateThread", 2 * MB);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SourceGroup::ExecuteInThread() {
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate::CreateParams create_params;
|
2015-06-22 17:12:26 +00:00
|
|
|
create_params.array_buffer_allocator = Shell::array_buffer_allocator;
|
2015-04-29 09:54:34 +00:00
|
|
|
Isolate* isolate = Isolate::New(create_params);
|
2015-07-30 08:20:35 +00:00
|
|
|
for (int i = 0; i < Shell::options.stress_runs; ++i) {
|
2013-09-02 12:26:06 +00:00
|
|
|
next_semaphore_.Wait();
|
2011-07-11 07:38:09 +00:00
|
|
|
{
|
|
|
|
Isolate::Scope iscope(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);
|
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
DisposeModuleEmbedderData(context);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2015-06-03 14:34:50 +00:00
|
|
|
Shell::CollectGarbage(isolate);
|
2009-03-16 10:08:59 +00:00
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
done_semaphore_.Signal();
|
2015-07-30 08:20:35 +00:00
|
|
|
}
|
2014-05-27 15:16:42 +00:00
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
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;
|
2015-07-30 08:20:35 +00:00
|
|
|
done_semaphore_.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SourceGroup::JoinThread() {
|
|
|
|
if (thread_ == NULL) return;
|
|
|
|
thread_->Join();
|
2008-10-21 09:12:27 +00:00
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
SerializationData::~SerializationData() {
|
2015-07-14 19:56:47 +00:00
|
|
|
// Any ArrayBuffer::Contents are owned by this SerializationData object if
|
|
|
|
// ownership hasn't been transferred out via ReadArrayBufferContents.
|
|
|
|
// SharedArrayBuffer::Contents may be used by multiple threads, so must be
|
2015-06-22 17:12:26 +00:00
|
|
|
// cleaned up by the main thread in Shell::CleanupWorkers().
|
2015-07-14 19:56:47 +00:00
|
|
|
for (int i = 0; i < array_buffer_contents_.length(); ++i) {
|
|
|
|
ArrayBuffer::Contents& contents = array_buffer_contents_[i];
|
|
|
|
if (contents.Data()) {
|
|
|
|
Shell::array_buffer_allocator->Free(contents.Data(),
|
|
|
|
contents.ByteLength());
|
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-14 19:56:47 +00:00
|
|
|
void SerializationData::WriteTag(SerializationTag tag) { data_.Add(tag); }
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::WriteMemory(const void* p, int length) {
|
2015-06-24 17:47:10 +00:00
|
|
|
if (length > 0) {
|
2015-07-14 19:56:47 +00:00
|
|
|
i::Vector<uint8_t> block = data_.AddBlock(0, length);
|
2015-06-24 17:47:10 +00:00
|
|
|
memcpy(&block[0], p, length);
|
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::WriteArrayBufferContents(
|
|
|
|
const ArrayBuffer::Contents& contents) {
|
2015-07-14 19:56:47 +00:00
|
|
|
array_buffer_contents_.Add(contents);
|
2015-06-22 17:12:26 +00:00
|
|
|
WriteTag(kSerializationTagTransferredArrayBuffer);
|
2015-07-14 19:56:47 +00:00
|
|
|
int index = array_buffer_contents_.length() - 1;
|
2015-06-22 17:12:26 +00:00
|
|
|
Write(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::WriteSharedArrayBufferContents(
|
|
|
|
const SharedArrayBuffer::Contents& contents) {
|
2015-07-14 19:56:47 +00:00
|
|
|
shared_array_buffer_contents_.Add(contents);
|
2015-06-22 17:12:26 +00:00
|
|
|
WriteTag(kSerializationTagTransferredSharedArrayBuffer);
|
2015-07-14 19:56:47 +00:00
|
|
|
int index = shared_array_buffer_contents_.length() - 1;
|
2015-06-22 17:12:26 +00:00
|
|
|
Write(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerializationTag SerializationData::ReadTag(int* offset) const {
|
|
|
|
return static_cast<SerializationTag>(Read<uint8_t>(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::ReadMemory(void* p, int length, int* offset) const {
|
2015-06-30 16:48:58 +00:00
|
|
|
if (length > 0) {
|
2015-07-14 19:56:47 +00:00
|
|
|
memcpy(p, &data_[*offset], length);
|
2015-06-30 16:48:58 +00:00
|
|
|
(*offset) += length;
|
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::ReadArrayBufferContents(ArrayBuffer::Contents* contents,
|
|
|
|
int* offset) const {
|
|
|
|
int index = Read<int>(offset);
|
2015-07-14 19:56:47 +00:00
|
|
|
DCHECK(index < array_buffer_contents_.length());
|
|
|
|
*contents = array_buffer_contents_[index];
|
|
|
|
// Ownership of this ArrayBuffer::Contents is passed to the caller. Neuter
|
|
|
|
// our copy so it won't be double-free'd when this SerializationData is
|
|
|
|
// destroyed.
|
|
|
|
array_buffer_contents_[index] = ArrayBuffer::Contents();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationData::ReadSharedArrayBufferContents(
|
|
|
|
SharedArrayBuffer::Contents* contents, int* offset) const {
|
|
|
|
int index = Read<int>(offset);
|
2015-07-14 19:56:47 +00:00
|
|
|
DCHECK(index < shared_array_buffer_contents_.length());
|
|
|
|
*contents = shared_array_buffer_contents_[index];
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationDataQueue::Enqueue(SerializationData* data) {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
data_.Add(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SerializationDataQueue::Dequeue(SerializationData** data) {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
2015-07-13 21:04:55 +00:00
|
|
|
*data = NULL;
|
2015-06-22 17:12:26 +00:00
|
|
|
if (data_.is_empty()) return false;
|
|
|
|
*data = data_.Remove(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SerializationDataQueue::IsEmpty() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
return data_.is_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerializationDataQueue::Clear() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
for (int i = 0; i < data_.length(); ++i) {
|
|
|
|
delete data_[i];
|
|
|
|
}
|
|
|
|
data_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Worker::Worker()
|
2015-06-25 18:01:11 +00:00
|
|
|
: in_semaphore_(0),
|
|
|
|
out_semaphore_(0),
|
|
|
|
thread_(NULL),
|
|
|
|
script_(NULL),
|
2015-07-30 08:19:29 +00:00
|
|
|
running_(false) {}
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
|
2015-07-14 19:56:47 +00:00
|
|
|
Worker::~Worker() {
|
|
|
|
delete thread_;
|
|
|
|
thread_ = NULL;
|
|
|
|
delete[] script_;
|
|
|
|
script_ = NULL;
|
|
|
|
in_queue_.Clear();
|
|
|
|
out_queue_.Clear();
|
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
|
2015-07-30 08:19:29 +00:00
|
|
|
void Worker::StartExecuteInThread(const char* script) {
|
|
|
|
running_ = true;
|
|
|
|
script_ = i::StrDup(script);
|
|
|
|
thread_ = new WorkerThread(this);
|
|
|
|
thread_->Start();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Worker::PostMessage(SerializationData* data) {
|
|
|
|
in_queue_.Enqueue(data);
|
|
|
|
in_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerializationData* Worker::GetMessage() {
|
2015-06-25 18:01:11 +00:00
|
|
|
SerializationData* data = NULL;
|
2015-06-22 17:12:26 +00:00
|
|
|
while (!out_queue_.Dequeue(&data)) {
|
2015-07-13 21:04:55 +00:00
|
|
|
// If the worker is no longer running, and there are no messages in the
|
|
|
|
// queue, don't expect any more messages from it.
|
2015-07-30 08:19:29 +00:00
|
|
|
if (!base::NoBarrier_Load(&running_)) break;
|
2015-06-22 17:12:26 +00:00
|
|
|
out_semaphore_.Wait();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Worker::Terminate() {
|
2015-07-30 08:19:29 +00:00
|
|
|
base::NoBarrier_Store(&running_, false);
|
|
|
|
// Post NULL to wake the Worker thread message loop, and tell it to stop
|
|
|
|
// running.
|
|
|
|
PostMessage(NULL);
|
2015-07-13 21:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Worker::WaitForThread() {
|
|
|
|
Terminate();
|
2015-07-30 08:19:29 +00:00
|
|
|
thread_->Join();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Worker::ExecuteInThread() {
|
|
|
|
Isolate::CreateParams create_params;
|
|
|
|
create_params.array_buffer_allocator = Shell::array_buffer_allocator;
|
|
|
|
Isolate* isolate = Isolate::New(create_params);
|
|
|
|
{
|
|
|
|
Isolate::Scope iscope(isolate);
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
PerIsolateData data(isolate);
|
|
|
|
Local<Context> context = Shell::CreateEvaluationContext(isolate);
|
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Object> global = context->Global();
|
|
|
|
Local<Value> this_value = External::New(isolate, this);
|
|
|
|
Local<FunctionTemplate> postmessage_fun_template =
|
2015-06-22 17:12:26 +00:00
|
|
|
FunctionTemplate::New(isolate, PostMessageOut, this_value);
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Function> postmessage_fun;
|
2015-06-22 17:12:26 +00:00
|
|
|
if (postmessage_fun_template->GetFunction(context)
|
|
|
|
.ToLocal(&postmessage_fun)) {
|
2015-07-20 07:05:42 +00:00
|
|
|
global->Set(context, String::NewFromUtf8(isolate, "postMessage",
|
|
|
|
NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
postmessage_fun).FromJust();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// First run the script
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<String> file_name =
|
|
|
|
String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
Local<String> source =
|
|
|
|
String::NewFromUtf8(isolate, script_, NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
2015-06-29 15:48:24 +00:00
|
|
|
if (Shell::ExecuteString(isolate, source, file_name, false, true)) {
|
2015-06-25 18:01:11 +00:00
|
|
|
// Get the message handler
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> onmessage =
|
|
|
|
global->Get(context, String::NewFromUtf8(isolate, "onmessage",
|
|
|
|
NewStringType::kNormal)
|
|
|
|
.ToLocalChecked()).ToLocalChecked();
|
2015-06-25 18:01:11 +00:00
|
|
|
if (onmessage->IsFunction()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Function> onmessage_fun = Local<Function>::Cast(onmessage);
|
2015-06-25 18:01:11 +00:00
|
|
|
// Now wait for messages
|
2015-07-13 21:04:55 +00:00
|
|
|
while (true) {
|
2015-06-25 18:01:11 +00:00
|
|
|
in_semaphore_.Wait();
|
|
|
|
SerializationData* data;
|
|
|
|
if (!in_queue_.Dequeue(&data)) continue;
|
|
|
|
if (data == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int offset = 0;
|
|
|
|
Local<Value> data_value;
|
|
|
|
if (Shell::DeserializeValue(isolate, *data, &offset)
|
|
|
|
.ToLocal(&data_value)) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> argv[] = {data_value};
|
2015-06-25 18:01:11 +00:00
|
|
|
(void)onmessage_fun->Call(context, global, 1, argv);
|
|
|
|
}
|
|
|
|
delete data;
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
DisposeModuleEmbedderData(context);
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
2015-06-24 17:09:48 +00:00
|
|
|
Shell::CollectGarbage(isolate);
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
isolate->Dispose();
|
2015-06-25 18:01:11 +00:00
|
|
|
|
2015-07-13 21:04:55 +00:00
|
|
|
// Post NULL to wake the thread waiting on GetMessage() if there is one.
|
|
|
|
out_queue_.Enqueue(NULL);
|
|
|
|
out_semaphore_.Signal();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
Isolate* isolate = args.GetIsolate();
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
if (args.Length() < 1) {
|
|
|
|
Throw(isolate, "Invalid argument");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> message = args[0];
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
// TODO(binji): Allow transferring from worker to main thread?
|
|
|
|
Shell::ObjectList to_transfer;
|
|
|
|
|
|
|
|
Shell::ObjectList seen_objects;
|
|
|
|
SerializationData* data = new SerializationData;
|
|
|
|
if (Shell::SerializeValue(isolate, message, to_transfer, &seen_objects,
|
|
|
|
data)) {
|
|
|
|
DCHECK(args.Data()->IsExternal());
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<External> this_value = Local<External>::Cast(args.Data());
|
2015-06-22 17:12:26 +00:00
|
|
|
Worker* worker = static_cast<Worker*>(this_value->Value());
|
|
|
|
worker->out_queue_.Enqueue(data);
|
|
|
|
worker->out_semaphore_.Signal();
|
|
|
|
} else {
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
2008-10-21 09:12:27 +00:00
|
|
|
|
|
|
|
|
2014-05-12 11:25:21 +00:00
|
|
|
void SetFlagsFromString(const char* flags) {
|
2014-05-12 11:42:53 +00:00
|
|
|
v8::V8::SetFlagsFromString(flags, static_cast<int>(strlen(flags)));
|
2014-05-12 11:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
bool Shell::SetOptions(int argc, char* argv[]) {
|
2014-05-12 11:25:21 +00:00
|
|
|
bool logfile_per_isolate = false;
|
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;
|
2014-05-12 11:25:21 +00:00
|
|
|
} else if (strcmp(argv[i], "--logfile-per-isolate") == 0) {
|
|
|
|
logfile_per_isolate = true;
|
|
|
|
argv[i] = NULL;
|
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;
|
2015-06-03 14:34:50 +00:00
|
|
|
} else if (strcmp(argv[i], "--notest") == 0 ||
|
|
|
|
strcmp(argv[i], "--no-test") == 0) {
|
|
|
|
options.test_shell = false;
|
|
|
|
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;
|
2014-05-27 15:16:42 +00:00
|
|
|
} else if (strcmp(argv[i], "--invoke-weak-callbacks") == 0) {
|
|
|
|
options.invoke_weak_callbacks = true;
|
|
|
|
// TODO(jochen) See issue 3351
|
|
|
|
options.send_idle_notification = true;
|
|
|
|
argv[i] = NULL;
|
2015-04-22 12:24:29 +00:00
|
|
|
} else if (strcmp(argv[i], "--omit-quit") == 0) {
|
|
|
|
options.omit_quit = 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) {
|
|
|
|
options.num_isolates++;
|
2013-08-02 09:02:02 +00:00
|
|
|
} else if (strcmp(argv[i], "--dump-heap-constants") == 0) {
|
|
|
|
options.dump_heap_constants = true;
|
|
|
|
argv[i] = NULL;
|
2013-10-04 16:21:23 +00:00
|
|
|
} else if (strcmp(argv[i], "--throws") == 0) {
|
|
|
|
options.expected_to_throw = true;
|
|
|
|
argv[i] = NULL;
|
2014-03-25 12:05:33 +00:00
|
|
|
} else if (strncmp(argv[i], "--icu-data-file=", 16) == 0) {
|
|
|
|
options.icu_data_file = argv[i] + 16;
|
|
|
|
argv[i] = NULL;
|
2014-06-23 13:52:17 +00:00
|
|
|
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
|
|
|
|
} else if (strncmp(argv[i], "--natives_blob=", 15) == 0) {
|
|
|
|
options.natives_blob = argv[i] + 15;
|
|
|
|
argv[i] = NULL;
|
|
|
|
} else if (strncmp(argv[i], "--snapshot_blob=", 16) == 0) {
|
|
|
|
options.snapshot_blob = argv[i] + 16;
|
|
|
|
argv[i] = NULL;
|
|
|
|
#endif // V8_USE_EXTERNAL_STARTUP_DATA
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
} else if (strcmp(argv[i], "--cache") == 0 ||
|
|
|
|
strncmp(argv[i], "--cache=", 8) == 0) {
|
|
|
|
const char* value = argv[i] + 7;
|
|
|
|
if (!*value || strncmp(value, "=code", 6) == 0) {
|
|
|
|
options.compile_options = v8::ScriptCompiler::kProduceCodeCache;
|
|
|
|
} else if (strncmp(value, "=parse", 7) == 0) {
|
|
|
|
options.compile_options = v8::ScriptCompiler::kProduceParserCache;
|
|
|
|
} else if (strncmp(value, "=none", 6) == 0) {
|
|
|
|
options.compile_options = v8::ScriptCompiler::kNoCompileOptions;
|
|
|
|
} else {
|
|
|
|
printf("Unknown option to --cache.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
argv[i] = NULL;
|
2016-07-27 16:21:09 +00:00
|
|
|
} else if (strcmp(argv[i], "--enable-tracing") == 0) {
|
|
|
|
options.trace_enabled = true;
|
|
|
|
argv[i] = NULL;
|
2016-08-10 17:10:51 +00:00
|
|
|
} else if (strncmp(argv[i], "--trace-config=", 15) == 0) {
|
|
|
|
options.trace_config = argv[i] + 15;
|
|
|
|
argv[i] = NULL;
|
2014-06-05 14:04:12 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-02-10 19:11:44 +00:00
|
|
|
} else if (strcmp(str, "--module") == 0) {
|
|
|
|
// Pass on to SourceGroup, which understands this option.
|
2011-07-11 07:38:09 +00:00
|
|
|
} else if (strncmp(argv[i], "--", 2) == 0) {
|
|
|
|
printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]);
|
2015-07-30 08:20:35 +00:00
|
|
|
} else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
|
|
|
|
options.script_executed = true;
|
|
|
|
} else if (strncmp(str, "-", 1) != 0) {
|
|
|
|
// Not a flag, so it must be a script to execute.
|
|
|
|
options.script_executed = true;
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
current->End(argc);
|
|
|
|
|
2014-05-12 11:25:21 +00:00
|
|
|
if (!logfile_per_isolate && options.num_isolates) {
|
|
|
|
SetFlagsFromString("--nologfile_per_isolate");
|
|
|
|
}
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-30 08:20:35 +00:00
|
|
|
int Shell::RunMain(Isolate* isolate, int argc, char* argv[], bool last_run) {
|
2011-07-14 15:43:40 +00:00
|
|
|
for (int i = 1; i < options.num_isolates; ++i) {
|
|
|
|
options.isolate_sources[i].StartExecuteInThread();
|
|
|
|
}
|
2014-05-22 08:11:10 +00:00
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Local<Context> context = CreateEvaluationContext(isolate);
|
2015-07-30 08:20:35 +00:00
|
|
|
if (last_run && options.use_interactive_shell()) {
|
2014-05-22 08:11:10 +00:00
|
|
|
// Keep using the same context in the interactive shell.
|
|
|
|
evaluation_context_.Reset(isolate, context);
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
2014-05-22 08:11:10 +00:00
|
|
|
{
|
|
|
|
Context::Scope cscope(context);
|
|
|
|
PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
|
|
|
|
options.isolate_sources[0].Execute(isolate);
|
|
|
|
}
|
2016-10-11 19:22:03 +00:00
|
|
|
DisposeModuleEmbedderData(context);
|
2014-05-22 08:11:10 +00:00
|
|
|
}
|
2015-06-03 14:34:50 +00:00
|
|
|
CollectGarbage(isolate);
|
|
|
|
for (int i = 1; i < options.num_isolates; ++i) {
|
2015-07-30 08:20:35 +00:00
|
|
|
if (last_run) {
|
|
|
|
options.isolate_sources[i].JoinThread();
|
|
|
|
} else {
|
|
|
|
options.isolate_sources[i].WaitForThread();
|
|
|
|
}
|
2015-06-03 14:34:50 +00:00
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
CleanupWorkers();
|
2015-06-03 14:34:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::CollectGarbage(Isolate* isolate) {
|
2014-05-27 14:21:46 +00:00
|
|
|
if (options.send_idle_notification) {
|
2015-03-24 08:29:52 +00:00
|
|
|
const double kLongIdlePauseInSeconds = 1.0;
|
2014-07-24 08:28:02 +00:00
|
|
|
isolate->ContextDisposedNotification();
|
2015-03-24 08:29:52 +00:00
|
|
|
isolate->IdleNotificationDeadline(
|
|
|
|
g_platform->MonotonicallyIncreasingTime() + kLongIdlePauseInSeconds);
|
2014-05-27 14:21:46 +00:00
|
|
|
}
|
2014-05-27 15:16:42 +00:00
|
|
|
if (options.invoke_weak_callbacks) {
|
|
|
|
// By sending a low memory notifications, we will try hard to collect all
|
|
|
|
// garbage and will therefore also invoke all weak callbacks of actually
|
|
|
|
// unreachable persistent handles.
|
2014-07-24 08:28:02 +00:00
|
|
|
isolate->LowMemoryNotification();
|
2014-05-27 15:16:42 +00:00
|
|
|
}
|
2011-07-11 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-15 12:26:06 +00:00
|
|
|
void Shell::EmptyMessageQueues(Isolate* isolate) {
|
2015-11-12 13:42:49 +00:00
|
|
|
if (!i::FLAG_verify_predictable) {
|
|
|
|
while (v8::platform::PumpMessageLoop(g_platform, isolate)) continue;
|
|
|
|
}
|
2015-07-15 12:26:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-20 07:05:42 +00:00
|
|
|
bool Shell::SerializeValue(Isolate* isolate, Local<Value> value,
|
2015-06-22 17:12:26 +00:00
|
|
|
const ObjectList& to_transfer,
|
|
|
|
ObjectList* seen_objects,
|
|
|
|
SerializationData* out_data) {
|
|
|
|
DCHECK(out_data);
|
|
|
|
Local<Context> context = isolate->GetCurrentContext();
|
|
|
|
|
|
|
|
if (value->IsUndefined()) {
|
|
|
|
out_data->WriteTag(kSerializationTagUndefined);
|
|
|
|
} else if (value->IsNull()) {
|
|
|
|
out_data->WriteTag(kSerializationTagNull);
|
|
|
|
} else if (value->IsTrue()) {
|
|
|
|
out_data->WriteTag(kSerializationTagTrue);
|
|
|
|
} else if (value->IsFalse()) {
|
|
|
|
out_data->WriteTag(kSerializationTagFalse);
|
|
|
|
} else if (value->IsNumber()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Number> num = Local<Number>::Cast(value);
|
2015-06-22 17:12:26 +00:00
|
|
|
double value = num->Value();
|
|
|
|
out_data->WriteTag(kSerializationTagNumber);
|
|
|
|
out_data->Write(value);
|
|
|
|
} else if (value->IsString()) {
|
|
|
|
v8::String::Utf8Value str(value);
|
|
|
|
out_data->WriteTag(kSerializationTagString);
|
|
|
|
out_data->Write(str.length());
|
|
|
|
out_data->WriteMemory(*str, str.length());
|
|
|
|
} else if (value->IsArray()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Array> array = Local<Array>::Cast(value);
|
2015-06-22 17:12:26 +00:00
|
|
|
if (FindInObjectList(array, *seen_objects)) {
|
|
|
|
Throw(isolate, "Duplicated arrays not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen_objects->Add(array);
|
|
|
|
out_data->WriteTag(kSerializationTagArray);
|
|
|
|
uint32_t length = array->Length();
|
|
|
|
out_data->Write(length);
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
Local<Value> element_value;
|
|
|
|
if (array->Get(context, i).ToLocal(&element_value)) {
|
|
|
|
if (!SerializeValue(isolate, element_value, to_transfer, seen_objects,
|
|
|
|
out_data))
|
|
|
|
return false;
|
2015-07-08 17:57:49 +00:00
|
|
|
} else {
|
|
|
|
Throw(isolate, "Failed to serialize array element.");
|
|
|
|
return false;
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (value->IsArrayBuffer()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ArrayBuffer> array_buffer = Local<ArrayBuffer>::Cast(value);
|
2015-06-22 17:12:26 +00:00
|
|
|
if (FindInObjectList(array_buffer, *seen_objects)) {
|
|
|
|
Throw(isolate, "Duplicated array buffers not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen_objects->Add(array_buffer);
|
|
|
|
if (FindInObjectList(array_buffer, to_transfer)) {
|
|
|
|
// Transfer ArrayBuffer
|
|
|
|
if (!array_buffer->IsNeuterable()) {
|
|
|
|
Throw(isolate, "Attempting to transfer an un-neuterable ArrayBuffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-14 19:56:47 +00:00
|
|
|
ArrayBuffer::Contents contents = array_buffer->IsExternal()
|
|
|
|
? array_buffer->GetContents()
|
|
|
|
: array_buffer->Externalize();
|
2015-06-22 17:12:26 +00:00
|
|
|
array_buffer->Neuter();
|
|
|
|
out_data->WriteArrayBufferContents(contents);
|
|
|
|
} else {
|
|
|
|
ArrayBuffer::Contents contents = array_buffer->GetContents();
|
|
|
|
// Clone ArrayBuffer
|
2015-08-03 17:07:48 +00:00
|
|
|
if (contents.ByteLength() > i::kMaxInt) {
|
2015-06-22 17:12:26 +00:00
|
|
|
Throw(isolate, "ArrayBuffer is too big to clone");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 17:07:48 +00:00
|
|
|
int32_t byte_length = static_cast<int32_t>(contents.ByteLength());
|
2015-06-22 17:12:26 +00:00
|
|
|
out_data->WriteTag(kSerializationTagArrayBuffer);
|
|
|
|
out_data->Write(byte_length);
|
2015-08-03 17:07:48 +00:00
|
|
|
out_data->WriteMemory(contents.Data(), byte_length);
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
} else if (value->IsSharedArrayBuffer()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<SharedArrayBuffer> sab = Local<SharedArrayBuffer>::Cast(value);
|
2015-06-22 17:12:26 +00:00
|
|
|
if (FindInObjectList(sab, *seen_objects)) {
|
|
|
|
Throw(isolate, "Duplicated shared array buffers not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen_objects->Add(sab);
|
|
|
|
if (!FindInObjectList(sab, to_transfer)) {
|
|
|
|
Throw(isolate, "SharedArrayBuffer must be transferred");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-14 19:56:47 +00:00
|
|
|
SharedArrayBuffer::Contents contents;
|
|
|
|
if (sab->IsExternal()) {
|
|
|
|
contents = sab->GetContents();
|
|
|
|
} else {
|
|
|
|
contents = sab->Externalize();
|
2015-07-30 08:20:35 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard(workers_mutex_.Pointer());
|
2015-07-14 19:56:47 +00:00
|
|
|
externalized_shared_contents_.Add(contents);
|
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
out_data->WriteSharedArrayBufferContents(contents);
|
|
|
|
} else if (value->IsObject()) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Object> object = Local<Object>::Cast(value);
|
2015-06-22 17:12:26 +00:00
|
|
|
if (FindInObjectList(object, *seen_objects)) {
|
|
|
|
Throw(isolate, "Duplicated objects not supported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
seen_objects->Add(object);
|
|
|
|
Local<Array> property_names;
|
|
|
|
if (!object->GetOwnPropertyNames(context).ToLocal(&property_names)) {
|
|
|
|
Throw(isolate, "Unable to get property names");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t length = property_names->Length();
|
|
|
|
out_data->WriteTag(kSerializationTagObject);
|
|
|
|
out_data->Write(length);
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Value> name;
|
|
|
|
Local<Value> property_value;
|
2015-06-22 17:12:26 +00:00
|
|
|
if (property_names->Get(context, i).ToLocal(&name) &&
|
|
|
|
object->Get(context, name).ToLocal(&property_value)) {
|
|
|
|
if (!SerializeValue(isolate, name, to_transfer, seen_objects, out_data))
|
|
|
|
return false;
|
|
|
|
if (!SerializeValue(isolate, property_value, to_transfer, seen_objects,
|
|
|
|
out_data))
|
|
|
|
return false;
|
2015-07-08 17:57:49 +00:00
|
|
|
} else {
|
|
|
|
Throw(isolate, "Failed to serialize property.");
|
|
|
|
return false;
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Throw(isolate, "Don't know how to serialize object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MaybeLocal<Value> Shell::DeserializeValue(Isolate* isolate,
|
|
|
|
const SerializationData& data,
|
|
|
|
int* offset) {
|
|
|
|
DCHECK(offset);
|
|
|
|
EscapableHandleScope scope(isolate);
|
|
|
|
Local<Value> result;
|
|
|
|
SerializationTag tag = data.ReadTag(offset);
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
case kSerializationTagUndefined:
|
|
|
|
result = Undefined(isolate);
|
|
|
|
break;
|
|
|
|
case kSerializationTagNull:
|
|
|
|
result = Null(isolate);
|
|
|
|
break;
|
|
|
|
case kSerializationTagTrue:
|
|
|
|
result = True(isolate);
|
|
|
|
break;
|
|
|
|
case kSerializationTagFalse:
|
|
|
|
result = False(isolate);
|
|
|
|
break;
|
|
|
|
case kSerializationTagNumber:
|
|
|
|
result = Number::New(isolate, data.Read<double>(offset));
|
|
|
|
break;
|
|
|
|
case kSerializationTagString: {
|
|
|
|
int length = data.Read<int>(offset);
|
2015-07-13 21:04:55 +00:00
|
|
|
CHECK(length >= 0);
|
|
|
|
std::vector<char> buffer(length + 1); // + 1 so it is never empty.
|
|
|
|
data.ReadMemory(&buffer[0], length, offset);
|
2015-07-20 07:05:42 +00:00
|
|
|
MaybeLocal<String> str =
|
|
|
|
String::NewFromUtf8(isolate, &buffer[0], NewStringType::kNormal,
|
|
|
|
length).ToLocalChecked();
|
2015-06-22 17:12:26 +00:00
|
|
|
if (!str.IsEmpty()) result = str.ToLocalChecked();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSerializationTagArray: {
|
|
|
|
uint32_t length = data.Read<uint32_t>(offset);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Array> array = Array::New(isolate, length);
|
2015-06-22 17:12:26 +00:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
Local<Value> element_value;
|
|
|
|
CHECK(DeserializeValue(isolate, data, offset).ToLocal(&element_value));
|
2015-07-20 07:05:42 +00:00
|
|
|
array->Set(isolate->GetCurrentContext(), i, element_value).FromJust();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
result = array;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSerializationTagObject: {
|
|
|
|
int length = data.Read<int>(offset);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<Object> object = Object::New(isolate);
|
2015-06-22 17:12:26 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
Local<Value> property_name;
|
|
|
|
CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_name));
|
|
|
|
Local<Value> property_value;
|
|
|
|
CHECK(DeserializeValue(isolate, data, offset).ToLocal(&property_value));
|
2015-07-20 07:05:42 +00:00
|
|
|
object->Set(isolate->GetCurrentContext(), property_name, property_value)
|
|
|
|
.FromJust();
|
2015-06-22 17:12:26 +00:00
|
|
|
}
|
|
|
|
result = object;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSerializationTagArrayBuffer: {
|
2015-08-03 17:07:48 +00:00
|
|
|
int32_t byte_length = data.Read<int32_t>(offset);
|
2015-07-20 07:05:42 +00:00
|
|
|
Local<ArrayBuffer> array_buffer = ArrayBuffer::New(isolate, byte_length);
|
2015-06-22 17:12:26 +00:00
|
|
|
ArrayBuffer::Contents contents = array_buffer->GetContents();
|
|
|
|
DCHECK(static_cast<size_t>(byte_length) == contents.ByteLength());
|
|
|
|
data.ReadMemory(contents.Data(), byte_length, offset);
|
|
|
|
result = array_buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSerializationTagTransferredArrayBuffer: {
|
|
|
|
ArrayBuffer::Contents contents;
|
|
|
|
data.ReadArrayBufferContents(&contents, offset);
|
2015-07-14 19:56:47 +00:00
|
|
|
result = ArrayBuffer::New(isolate, contents.Data(), contents.ByteLength(),
|
|
|
|
ArrayBufferCreationMode::kInternalized);
|
2015-06-22 17:12:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kSerializationTagTransferredSharedArrayBuffer: {
|
|
|
|
SharedArrayBuffer::Contents contents;
|
|
|
|
data.ReadSharedArrayBufferContents(&contents, offset);
|
|
|
|
result = SharedArrayBuffer::New(isolate, contents.Data(),
|
|
|
|
contents.ByteLength());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope.Escape(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Shell::CleanupWorkers() {
|
2015-06-25 18:01:11 +00:00
|
|
|
// Make a copy of workers_, because we don't want to call Worker::Terminate
|
|
|
|
// while holding the workers_mutex_ lock. Otherwise, if a worker is about to
|
|
|
|
// create a new Worker, it would deadlock.
|
|
|
|
i::List<Worker*> workers_copy;
|
|
|
|
{
|
2015-07-16 16:40:37 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard(workers_mutex_.Pointer());
|
2015-06-25 18:01:11 +00:00
|
|
|
allow_new_workers_ = false;
|
|
|
|
workers_copy.AddAll(workers_);
|
|
|
|
workers_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < workers_copy.length(); ++i) {
|
|
|
|
Worker* worker = workers_copy[i];
|
2015-07-13 21:04:55 +00:00
|
|
|
worker->WaitForThread();
|
2015-06-22 17:12:26 +00:00
|
|
|
delete worker;
|
|
|
|
}
|
2015-06-25 18:01:11 +00:00
|
|
|
|
|
|
|
// Now that all workers are terminated, we can re-enable Worker creation.
|
2015-07-30 08:20:35 +00:00
|
|
|
base::LockGuard<base::Mutex> lock_guard(workers_mutex_.Pointer());
|
|
|
|
allow_new_workers_ = true;
|
2015-06-22 17:12:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < externalized_shared_contents_.length(); ++i) {
|
|
|
|
const SharedArrayBuffer::Contents& contents =
|
|
|
|
externalized_shared_contents_[i];
|
|
|
|
Shell::array_buffer_allocator->Free(contents.Data(), contents.ByteLength());
|
|
|
|
}
|
|
|
|
externalized_shared_contents_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-02 09:02:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-11 07:38:09 +00:00
|
|
|
int Shell::Main(int argc, char* argv[]) {
|
2016-07-27 16:21:09 +00:00
|
|
|
std::ofstream trace_file;
|
2016-08-18 14:25:19 +00:00
|
|
|
v8::base::debug::EnableInProcessStackDumping();
|
2014-09-17 14:20:20 +00:00
|
|
|
#if (defined(_WIN32) || defined(_WIN64))
|
|
|
|
UINT new_flags =
|
|
|
|
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
|
|
|
|
UINT existing_flags = SetErrorMode(new_flags);
|
|
|
|
SetErrorMode(existing_flags | new_flags);
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
|
|
|
|
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
|
|
|
|
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
|
|
|
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
|
|
|
|
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
|
|
|
_set_error_mode(_OUT_TO_STDERR);
|
|
|
|
#endif // defined(_MSC_VER)
|
|
|
|
#endif // defined(_WIN32) || defined(_WIN64)
|
2011-07-11 07:38:09 +00:00
|
|
|
if (!SetOptions(argc, argv)) return 1;
|
2016-06-08 12:09:25 +00:00
|
|
|
v8::V8::InitializeICUDefaultLocation(argv[0], options.icu_data_file);
|
2015-11-12 13:42:49 +00:00
|
|
|
g_platform = i::FLAG_verify_predictable
|
|
|
|
? new PredictablePlatform()
|
|
|
|
: v8::platform::CreateDefaultPlatform();
|
|
|
|
|
2015-03-24 08:29:52 +00:00
|
|
|
v8::V8::InitializePlatform(g_platform);
|
2014-09-19 08:01:35 +00:00
|
|
|
v8::V8::Initialize();
|
2015-08-14 12:11:29 +00:00
|
|
|
if (options.natives_blob || options.snapshot_blob) {
|
|
|
|
v8::V8::InitializeExternalStartupData(options.natives_blob,
|
|
|
|
options.snapshot_blob);
|
|
|
|
} else {
|
|
|
|
v8::V8::InitializeExternalStartupData(argv[0]);
|
|
|
|
}
|
2014-05-12 11:25:21 +00:00
|
|
|
SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg");
|
2014-10-14 08:43:33 +00:00
|
|
|
SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");
|
2014-05-12 11:25:21 +00:00
|
|
|
SetFlagsFromString("--redirect-code-traces-to=code.asm");
|
2015-04-29 09:54:34 +00:00
|
|
|
int result = 0;
|
|
|
|
Isolate::CreateParams create_params;
|
2015-06-22 17:12:26 +00:00
|
|
|
ShellArrayBufferAllocator shell_array_buffer_allocator;
|
2013-11-18 14:50:45 +00:00
|
|
|
MockArrayBufferAllocator mock_arraybuffer_allocator;
|
|
|
|
if (options.mock_arraybuffer_allocator) {
|
2015-06-22 17:12:26 +00:00
|
|
|
Shell::array_buffer_allocator = &mock_arraybuffer_allocator;
|
2013-11-18 14:50:45 +00:00
|
|
|
} else {
|
2015-06-22 17:12:26 +00:00
|
|
|
Shell::array_buffer_allocator = &shell_array_buffer_allocator;
|
2013-11-18 14:50:45 +00:00
|
|
|
}
|
2015-06-22 17:12:26 +00:00
|
|
|
create_params.array_buffer_allocator = Shell::array_buffer_allocator;
|
2014-09-15 11:17:00 +00:00
|
|
|
#ifdef ENABLE_VTUNE_JIT_INTERFACE
|
2014-10-22 15:30:50 +00:00
|
|
|
create_params.code_event_handler = vTune::GetVtuneCodeEventHandler();
|
2014-09-15 11:17:00 +00:00
|
|
|
#endif
|
2014-09-16 09:15:02 +00:00
|
|
|
create_params.constraints.ConfigureDefaults(
|
|
|
|
base::SysInfo::AmountOfPhysicalMemory(),
|
2015-04-15 07:15:52 +00:00
|
|
|
base::SysInfo::AmountOfVirtualMemory());
|
2015-03-18 10:14:51 +00:00
|
|
|
|
|
|
|
Shell::counter_map_ = new CounterMap();
|
|
|
|
if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
|
|
|
|
create_params.counter_lookup_callback = LookupCounter;
|
|
|
|
create_params.create_histogram_callback = CreateHistogram;
|
|
|
|
create_params.add_histogram_sample_callback = AddHistogramSample;
|
|
|
|
}
|
2014-09-16 09:15:02 +00:00
|
|
|
Isolate* isolate = Isolate::New(create_params);
|
2012-11-20 14:47:56 +00:00
|
|
|
{
|
2014-05-09 08:40:18 +00:00
|
|
|
Isolate::Scope scope(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
Initialize(isolate);
|
2013-04-22 11:29:52 +00:00
|
|
|
PerIsolateData data(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
|
2016-07-27 16:21:09 +00:00
|
|
|
if (options.trace_enabled) {
|
|
|
|
trace_file.open("v8_trace.json");
|
|
|
|
platform::tracing::TracingController* tracing_controller =
|
|
|
|
new platform::tracing::TracingController();
|
|
|
|
platform::tracing::TraceBuffer* trace_buffer =
|
|
|
|
platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer(
|
|
|
|
platform::tracing::TraceBuffer::kRingBufferChunks,
|
|
|
|
platform::tracing::TraceWriter::CreateJSONTraceWriter(
|
|
|
|
trace_file));
|
|
|
|
platform::tracing::TraceConfig* trace_config;
|
2016-08-10 17:10:51 +00:00
|
|
|
if (options.trace_config) {
|
|
|
|
int size = 0;
|
|
|
|
char* trace_config_json_str =
|
|
|
|
ReadChars(nullptr, options.trace_config, &size);
|
|
|
|
trace_config =
|
|
|
|
tracing::CreateTraceConfigFromJSON(isolate, trace_config_json_str);
|
|
|
|
delete[] trace_config_json_str;
|
|
|
|
} else {
|
|
|
|
trace_config =
|
|
|
|
platform::tracing::TraceConfig::CreateDefaultTraceConfig();
|
|
|
|
}
|
2016-07-27 16:21:09 +00:00
|
|
|
tracing_controller->Initialize(trace_buffer);
|
|
|
|
tracing_controller->StartTracing(trace_config);
|
|
|
|
if (!i::FLAG_verify_predictable) {
|
|
|
|
platform::SetTracingController(g_platform, tracing_controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 09:02:02 +00:00
|
|
|
if (options.dump_heap_constants) {
|
|
|
|
DumpHeapConstants(reinterpret_cast<i::Isolate*>(isolate));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 14:47:56 +00:00
|
|
|
if (options.stress_opt || options.stress_deopt) {
|
|
|
|
Testing::SetStressRunType(options.stress_opt
|
|
|
|
? Testing::kStressTypeOpt
|
|
|
|
: Testing::kStressTypeDeopt);
|
2015-07-30 08:20:35 +00:00
|
|
|
options.stress_runs = Testing::GetStressRuns();
|
|
|
|
for (int i = 0; i < options.stress_runs && result == 0; i++) {
|
|
|
|
printf("============ Stress %d/%d ============\n", i + 1,
|
|
|
|
options.stress_runs);
|
2012-11-20 14:47:56 +00:00
|
|
|
Testing::PrepareStressRun(i);
|
2015-07-30 08:20:35 +00:00
|
|
|
bool last_run = i == options.stress_runs - 1;
|
|
|
|
result = RunMain(isolate, argc, argv, last_run);
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
printf("======== Full Deoptimization =======\n");
|
2015-11-30 08:16:35 +00:00
|
|
|
Testing::DeoptimizeAll(isolate);
|
2012-11-20 14:47:56 +00:00
|
|
|
} else if (i::FLAG_stress_runs > 0) {
|
2015-07-30 08:20:35 +00:00
|
|
|
options.stress_runs = i::FLAG_stress_runs;
|
|
|
|
for (int i = 0; i < options.stress_runs && result == 0; i++) {
|
|
|
|
printf("============ Run %d/%d ============\n", i + 1,
|
|
|
|
options.stress_runs);
|
|
|
|
bool last_run = i == options.stress_runs - 1;
|
|
|
|
result = RunMain(isolate, argc, argv, last_run);
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-30 08:20:35 +00:00
|
|
|
bool last_run = true;
|
|
|
|
result = RunMain(isolate, argc, argv, last_run);
|
2012-11-20 14:47:56 +00:00
|
|
|
}
|
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
|
2014-05-27 07:19:32 +00:00
|
|
|
if (options.use_interactive_shell()) {
|
2012-11-20 14:47:56 +00:00
|
|
|
RunShell(isolate);
|
|
|
|
}
|
2015-06-03 14:34:50 +00:00
|
|
|
|
2016-10-20 14:08:44 +00:00
|
|
|
if (i::FLAG_ignition && i::FLAG_trace_ignition_dispatches &&
|
2016-05-04 11:21:16 +00:00
|
|
|
i::FLAG_trace_ignition_dispatches_output_file != nullptr) {
|
2016-04-27 11:10:41 +00:00
|
|
|
WriteIgnitionDispatchCountersFile(isolate);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:34:50 +00:00
|
|
|
// Shut down contexts and collect garbage.
|
|
|
|
evaluation_context_.Reset();
|
2016-04-05 16:22:08 +00:00
|
|
|
stringify_function_.Reset();
|
2015-06-03 14:34:50 +00:00
|
|
|
CollectGarbage(isolate);
|
2011-06-09 14:28:49 +00:00
|
|
|
}
|
2014-11-21 09:53:04 +00:00
|
|
|
OnExit(isolate);
|
2014-09-29 07:29:14 +00:00
|
|
|
// Dump basic block profiling data.
|
|
|
|
if (i::BasicBlockProfiler* profiler =
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate)->basic_block_profiler()) {
|
|
|
|
i::OFStream os(stdout);
|
|
|
|
os << *profiler;
|
|
|
|
}
|
2014-05-09 11:29:29 +00:00
|
|
|
isolate->Dispose();
|
2011-07-11 07:38:09 +00:00
|
|
|
V8::Dispose();
|
2014-07-03 07:37:27 +00:00
|
|
|
V8::ShutdownPlatform();
|
2015-03-24 08:29:52 +00:00
|
|
|
delete g_platform;
|
2011-08-24 10:48:48 +00:00
|
|
|
|
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
|