blob: caa5a0adba472ef5e8132ae9a7fdb5138dba34ef [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_D8_H_
6#define V8_D8_H_
7
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/allocation.h"
10#include "src/hashmap.h"
11#include "src/smart-pointers.h"
12#include "src/v8.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000013#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "include/v8.h"
15#include "src/base/compiler-specific.h"
16#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000017
18namespace v8 {
19
Steve Blocka7e24c12009-10-30 11:49:00 +000020
Ben Murdoch69a99ed2011-11-30 16:03:39 +000021#ifndef V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000022// A single counter in a counter collection.
23class Counter {
24 public:
25 static const int kMaxNameSize = 64;
26 int32_t* Bind(const char* name, bool histogram);
27 int32_t* ptr() { return &count_; }
28 int32_t count() { return count_; }
29 int32_t sample_total() { return sample_total_; }
30 bool is_histogram() { return is_histogram_; }
31 void AddSample(int32_t sample);
32 private:
33 int32_t count_;
34 int32_t sample_total_;
35 bool is_histogram_;
36 uint8_t name_[kMaxNameSize];
37};
38
39
40// A set of counters and associated information. An instance of this
41// class is stored directly in the memory-mapped counters file if
42// the --map-counters options is used
43class CounterCollection {
44 public:
45 CounterCollection();
46 Counter* GetNextCounter();
47 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 static const unsigned kMaxCounters = 512;
Steve Blocka7e24c12009-10-30 11:49:00 +000049 uint32_t magic_number_;
50 uint32_t max_counters_;
51 uint32_t max_name_size_;
52 uint32_t counters_in_use_;
53 Counter counters_[kMaxCounters];
54};
55
56
57class CounterMap {
58 public:
59 CounterMap(): hash_map_(Match) { }
60 Counter* Lookup(const char* name) {
61 i::HashMap::Entry* answer = hash_map_.Lookup(
62 const_cast<char*>(name),
63 Hash(name),
64 false);
65 if (!answer) return NULL;
66 return reinterpret_cast<Counter*>(answer->value);
67 }
68 void Set(const char* name, Counter* value) {
69 i::HashMap::Entry* answer = hash_map_.Lookup(
70 const_cast<char*>(name),
71 Hash(name),
72 true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 DCHECK(answer != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +000074 answer->value = value;
75 }
76 class Iterator {
77 public:
78 explicit Iterator(CounterMap* map)
79 : map_(&map->hash_map_), entry_(map_->Start()) { }
80 void Next() { entry_ = map_->Next(entry_); }
81 bool More() { return entry_ != NULL; }
82 const char* CurrentKey() { return static_cast<const char*>(entry_->key); }
83 Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); }
84 private:
85 i::HashMap* map_;
86 i::HashMap::Entry* entry_;
87 };
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000088
Steve Blocka7e24c12009-10-30 11:49:00 +000089 private:
90 static int Hash(const char* name);
91 static bool Match(void* key1, void* key2);
92 i::HashMap hash_map_;
93};
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96
Ben Murdoch589d6972011-11-30 16:04:58 +000097class LineEditor {
98 public:
99 enum Type { DUMB = 0, READLINE = 1 };
100 LineEditor(Type type, const char* name);
101 virtual ~LineEditor() { }
102
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100103 virtual Handle<String> Prompt(const char* prompt) = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 virtual bool Open(Isolate* isolate) { return true; }
Ben Murdoch589d6972011-11-30 16:04:58 +0000105 virtual bool Close() { return true; }
106 virtual void AddHistory(const char* str) { }
107
108 const char* name() { return name_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 static LineEditor* Get() { return current_; }
Ben Murdoch589d6972011-11-30 16:04:58 +0000110 private:
111 Type type_;
112 const char* name_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 static LineEditor* current_;
Ben Murdoch589d6972011-11-30 16:04:58 +0000114};
Ben Murdoch589d6972011-11-30 16:04:58 +0000115
116
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000117class SourceGroup {
118 public:
119 SourceGroup() :
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000120#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 next_semaphore_(0),
122 done_semaphore_(0),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 thread_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 argv_(NULL),
126 begin_offset_(0),
Ben Murdoch589d6972011-11-30 16:04:58 +0000127 end_offset_(0) {}
128
129 ~SourceGroup();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000130
131 void Begin(char** argv, int offset) {
132 argv_ = const_cast<const char**>(argv);
133 begin_offset_ = offset;
134 }
135
136 void End(int offset) { end_offset_ = offset; }
137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 void Execute(Isolate* isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000139
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000140#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000141 void StartExecuteInThread();
142 void WaitForThread();
143
144 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 class IsolateThread : public base::Thread {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000146 public:
147 explicit IsolateThread(SourceGroup* group)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 : base::Thread(GetThreadOptions()), group_(group) {}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000149
150 virtual void Run() {
151 group_->ExecuteInThread();
152 }
153
154 private:
155 SourceGroup* group_;
156 };
157
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 static base::Thread::Options GetThreadOptions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000159 void ExecuteInThread();
160
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 base::Semaphore next_semaphore_;
162 base::Semaphore done_semaphore_;
163 base::Thread* thread_;
164#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000165
166 void ExitShell(int exit_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 Handle<String> ReadFile(Isolate* isolate, const char* name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000168
169 const char** argv_;
170 int begin_offset_;
171 int end_offset_;
172};
173
174
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175class BinaryResource : public v8::String::ExternalOneByteStringResource {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100176 public:
177 BinaryResource(const char* string, int length)
178 : data_(string),
179 length_(length) { }
180
181 ~BinaryResource() {
182 delete[] data_;
183 data_ = NULL;
184 length_ = 0;
185 }
186
187 virtual const char* data() const { return data_; }
188 virtual size_t length() const { return length_; }
189
190 private:
191 const char* data_;
192 size_t length_;
193};
194
195
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000196class ShellOptions {
197 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 ShellOptions()
199 : script_executed(false),
200 last_run(true),
201 send_idle_notification(false),
202 invoke_weak_callbacks(false),
203 stress_opt(false),
204 stress_deopt(false),
205 interactive_shell(false),
206 test_shell(false),
207 dump_heap_constants(false),
208 expected_to_throw(false),
209 mock_arraybuffer_allocator(false),
210 num_isolates(1),
211 compile_options(v8::ScriptCompiler::kNoCompileOptions),
212 isolate_sources(NULL),
213 icu_data_file(NULL),
214 natives_blob(NULL),
215 snapshot_blob(NULL) {}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000216
Ben Murdoch589d6972011-11-30 16:04:58 +0000217 ~ShellOptions() {
Ben Murdoch589d6972011-11-30 16:04:58 +0000218 delete[] isolate_sources;
219 }
220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 bool use_interactive_shell() {
222 return (interactive_shell || !script_executed) && !test_shell;
223 }
224
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000225 bool script_executed;
226 bool last_run;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 bool send_idle_notification;
228 bool invoke_weak_callbacks;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000229 bool stress_opt;
230 bool stress_deopt;
231 bool interactive_shell;
232 bool test_shell;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 bool dump_heap_constants;
234 bool expected_to_throw;
235 bool mock_arraybuffer_allocator;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000236 int num_isolates;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 v8::ScriptCompiler::CompileOptions compile_options;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000238 SourceGroup* isolate_sources;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 const char* icu_data_file;
240 const char* natives_blob;
241 const char* snapshot_blob;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000242};
243
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000244#ifdef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000245class Shell {
246#else
247class Shell : public i::AllStatic {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000248#endif // V8_SHARED
Ben Murdoch589d6972011-11-30 16:04:58 +0000249
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 static Local<UnboundScript> CompileString(
252 Isolate* isolate, Local<String> source, Local<Value> name,
253 v8::ScriptCompiler::CompileOptions compile_options);
254 static bool ExecuteString(Isolate* isolate,
255 Handle<String> source,
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 Handle<Value> name,
257 bool print_result,
258 bool report_exceptions);
Steve Block6ded16b2010-05-10 14:33:55 +0100259 static const char* ToCString(const v8::String::Utf8Value& value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 static void ReportException(Isolate* isolate, TryCatch* try_catch);
261 static Handle<String> ReadFile(Isolate* isolate, const char* name);
262 static Local<Context> CreateEvaluationContext(Isolate* isolate);
263 static int RunMain(Isolate* isolate, int argc, char* argv[]);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000264 static int Main(int argc, char* argv[]);
Ben Murdoch589d6972011-11-30 16:04:58 +0000265 static void Exit(int exit_code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400266 static void OnExit(Isolate* isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000267
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000268#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 static Handle<Array> GetCompletions(Isolate* isolate,
270 Handle<String> text,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000271 Handle<String> full);
Steve Blocka7e24c12009-10-30 11:49:00 +0000272 static int* LookupCounter(const char* name);
273 static void* CreateHistogram(const char* name,
274 int min,
275 int max,
276 size_t buckets);
277 static void AddHistogramSample(void* histogram, int sample);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 static void MapCounters(v8::Isolate* isolate, const char* name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000279
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 static Local<Object> DebugMessageDetails(Isolate* isolate,
281 Handle<String> message);
282 static Local<Value> DebugCommandToJSONRequest(Isolate* isolate,
283 Handle<String> command);
Steve Blocka7e24c12009-10-30 11:49:00 +0000284
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args);
286#endif // !V8_SHARED
Ben Murdochb0fe1622011-05-05 13:52:32 +0100287
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 static void RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args);
289 static void RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args);
290 static void RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args);
291 static void RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args);
292 static void RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
293 static void RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args);
294 static void RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args);
295 static void RealmSharedGet(Local<String> property,
296 const PropertyCallbackInfo<Value>& info);
297 static void RealmSharedSet(Local<String> property,
298 Local<Value> value,
299 const PropertyCallbackInfo<void>& info);
300
301 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
302 static void Write(const v8::FunctionCallbackInfo<v8::Value>& args);
303 static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
304 static void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
305 static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
306 static void ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
307 static Handle<String> ReadFromStdin(Isolate* isolate);
308 static void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
309 args.GetReturnValue().Set(ReadFromStdin(args.GetIsolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100310 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 static void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
312 static void ArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
313 static void Int8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
314 static void Uint8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
315 static void Int16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
316 static void Uint16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
317 static void Int32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
318 static void Uint32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
319 static void Float32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
320 static void Float64Array(const v8::FunctionCallbackInfo<v8::Value>& args);
321 static void Uint8ClampedArray(
322 const v8::FunctionCallbackInfo<v8::Value>& args);
323 static void ArrayBufferSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
324 static void ArraySubArray(const v8::FunctionCallbackInfo<v8::Value>& args);
325 static void ArraySet(const v8::FunctionCallbackInfo<v8::Value>& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 // The OS object on the global object contains methods for performing
327 // operating system calls:
328 //
329 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
330 // run the command, passing the arguments to the program. The standard output
331 // of the program will be picked up and returned as a multiline string. If
332 // timeout1 is present then it should be a number. -1 indicates no timeout
333 // and a positive number is used as a timeout in milliseconds that limits the
334 // time spent waiting between receiving output characters from the program.
335 // timeout2, if present, should be a number indicating the limit in
336 // milliseconds on the total running time of the program. Exceptions are
337 // thrown on timeouts or other errors or if the exit status of the program
338 // indicates an error.
339 //
340 // os.chdir(dir) changes directory to the given directory. Throws an
341 // exception/ on error.
342 //
343 // os.setenv(variable, value) sets an environment variable. Repeated calls to
344 // this method leak memory due to the API of setenv in the standard C library.
345 //
346 // os.umask(alue) calls the umask system call and returns the old umask.
347 //
348 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
349 // with the current umask. Intermediate directories are created if necessary.
350 // An exception is not thrown if the directory already exists. Analogous to
351 // the "mkdir -p" command.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352 static void OSObject(const v8::FunctionCallbackInfo<v8::Value>& args);
353 static void System(const v8::FunctionCallbackInfo<v8::Value>& args);
354 static void ChangeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
355 static void SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
356 static void UnsetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
357 static void SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args);
358 static void MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
359 static void RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 static void AddOSMethods(v8::Isolate* isolate,
362 Handle<ObjectTemplate> os_template);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100363
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 static const char* kPrompt;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000365 static ShellOptions options;
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 static Persistent<Context> evaluation_context_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000369#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000370 static Persistent<Context> utility_context_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 static CounterMap* counter_map_;
372 // We statically allocate a set of local counters to be used if we
373 // don't want to store the stats in a memory-mapped file
374 static CounterCollection local_counters_;
375 static CounterCollection* counters_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 static base::OS::MemoryMappedFile* counters_file_;
377 static base::Mutex context_mutex_;
378 static const base::TimeTicks kInitialTicks;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000379
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 static Counter* GetCounter(const char* name, bool is_histogram);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 static void InstallUtilityScript(Isolate* isolate);
382#endif // !V8_SHARED
383 static void Initialize(Isolate* isolate);
384 static void InitializeDebugger(Isolate* isolate);
385 static void RunShell(Isolate* isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000386 static bool SetOptions(int argc, char* argv[]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 static Handle<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate);
388 static Handle<FunctionTemplate> CreateArrayBufferTemplate(FunctionCallback);
389 static Handle<FunctionTemplate> CreateArrayTemplate(FunctionCallback);
390 static Handle<Value> CreateExternalArrayBuffer(Isolate* isolate,
391 Handle<Object> buffer,
392 int32_t size);
393 static Handle<Object> CreateExternalArray(Isolate* isolate,
394 Handle<Object> array,
395 Handle<Object> buffer,
396 ExternalArrayType type,
397 int32_t length,
398 int32_t byteLength,
399 int32_t byteOffset,
400 int32_t element_size);
401 static void CreateExternalArray(
402 const v8::FunctionCallbackInfo<v8::Value>& args,
403 ExternalArrayType type,
404 int32_t element_size);
405 static void ExternalArrayWeakCallback(Isolate* isolate,
406 Persistent<Object>* object,
407 uint8_t* data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000408};
409
410
Steve Blocka7e24c12009-10-30 11:49:00 +0000411} // namespace v8
412
413
414#endif // V8_D8_H_