1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "src/allocation.h"
10 #include "src/hashmap.h"
11 #include "src/smart-pointers.h"
14 #include "include/v8.h"
21 // A single counter in a counter collection.
24 static const int kMaxNameSize = 64;
25 int32_t* Bind(const char* name, bool histogram);
26 int32_t* ptr() { return &count_; }
27 int32_t count() { return count_; }
28 int32_t sample_total() { return sample_total_; }
29 bool is_histogram() { return is_histogram_; }
30 void AddSample(int32_t sample);
33 int32_t sample_total_;
35 uint8_t name_[kMaxNameSize];
39 // A set of counters and associated information. An instance of this
40 // class is stored directly in the memory-mapped counters file if
41 // the --map-counters options is used
42 class CounterCollection {
45 Counter* GetNextCounter();
47 static const unsigned kMaxCounters = 512;
48 uint32_t magic_number_;
49 uint32_t max_counters_;
50 uint32_t max_name_size_;
51 uint32_t counters_in_use_;
52 Counter counters_[kMaxCounters];
58 CounterMap(): hash_map_(Match) { }
59 Counter* Lookup(const char* name) {
60 i::HashMap::Entry* answer = hash_map_.Lookup(
61 const_cast<char*>(name),
64 if (!answer) return NULL;
65 return reinterpret_cast<Counter*>(answer->value);
67 void Set(const char* name, Counter* value) {
68 i::HashMap::Entry* answer = hash_map_.Lookup(
69 const_cast<char*>(name),
72 DCHECK(answer != NULL);
73 answer->value = value;
77 explicit Iterator(CounterMap* map)
78 : map_(&map->hash_map_), entry_(map_->Start()) { }
79 void Next() { entry_ = map_->Next(entry_); }
80 bool More() { return entry_ != NULL; }
81 const char* CurrentKey() { return static_cast<const char*>(entry_->key); }
82 Counter* CurrentValue() { return static_cast<Counter*>(entry_->value); }
85 i::HashMap::Entry* entry_;
89 static int Hash(const char* name);
90 static bool Match(void* key1, void* key2);
98 enum Type { DUMB = 0, READLINE = 1 };
99 LineEditor(Type type, const char* name);
100 virtual ~LineEditor() { }
102 virtual Handle<String> Prompt(const char* prompt) = 0;
103 virtual bool Open(Isolate* isolate) { return true; }
104 virtual bool Close() { return true; }
105 virtual void AddHistory(const char* str) { }
107 const char* name() { return name_; }
108 static LineEditor* Get() { return current_; }
112 static LineEditor* current_;
130 void Begin(char** argv, int offset) {
131 argv_ = const_cast<const char**>(argv);
132 begin_offset_ = offset;
135 void End(int offset) { end_offset_ = offset; }
137 void Execute(Isolate* isolate);
140 void StartExecuteInThread();
141 void WaitForThread();
144 class IsolateThread : public base::Thread {
146 explicit IsolateThread(SourceGroup* group)
147 : base::Thread(GetThreadOptions()), group_(group) {}
150 group_->ExecuteInThread();
157 static base::Thread::Options GetThreadOptions();
158 void ExecuteInThread();
160 base::Semaphore next_semaphore_;
161 base::Semaphore done_semaphore_;
162 base::Thread* thread_;
165 void ExitShell(int exit_code);
166 Handle<String> ReadFile(Isolate* isolate, const char* name);
174 class BinaryResource : public v8::String::ExternalAsciiStringResource {
176 BinaryResource(const char* string, int length)
186 virtual const char* data() const { return data_; }
187 virtual size_t length() const { return length_; }
198 : script_executed(false),
200 send_idle_notification(false),
201 invoke_weak_callbacks(false),
204 interactive_shell(false),
206 dump_heap_constants(false),
207 expected_to_throw(false),
208 mock_arraybuffer_allocator(false),
210 compile_options(v8::ScriptCompiler::kNoCompileOptions),
211 isolate_sources(NULL),
214 snapshot_blob(NULL) {}
217 delete[] isolate_sources;
220 bool use_interactive_shell() {
221 return (interactive_shell || !script_executed) && !test_shell;
224 bool script_executed;
226 bool send_idle_notification;
227 bool invoke_weak_callbacks;
230 bool interactive_shell;
232 bool dump_heap_constants;
233 bool expected_to_throw;
234 bool mock_arraybuffer_allocator;
236 v8::ScriptCompiler::CompileOptions compile_options;
237 SourceGroup* isolate_sources;
238 const char* icu_data_file;
239 const char* natives_blob;
240 const char* snapshot_blob;
246 class Shell : public i::AllStatic {
250 static Local<UnboundScript> CompileString(
251 Isolate* isolate, Local<String> source, Local<Value> name,
252 v8::ScriptCompiler::CompileOptions compile_options);
253 static bool ExecuteString(Isolate* isolate,
254 Handle<String> source,
257 bool report_exceptions);
258 static const char* ToCString(const v8::String::Utf8Value& value);
259 static void ReportException(Isolate* isolate, TryCatch* try_catch);
260 static Handle<String> ReadFile(Isolate* isolate, const char* name);
261 static Local<Context> CreateEvaluationContext(Isolate* isolate);
262 static int RunMain(Isolate* isolate, int argc, char* argv[]);
263 static int Main(int argc, char* argv[]);
264 static void Exit(int exit_code);
265 static void OnExit();
268 static Handle<Array> GetCompletions(Isolate* isolate,
270 Handle<String> full);
271 static int* LookupCounter(const char* name);
272 static void* CreateHistogram(const char* name,
276 static void AddHistogramSample(void* histogram, int sample);
277 static void MapCounters(v8::Isolate* isolate, const char* name);
279 static Local<Object> DebugMessageDetails(Isolate* isolate,
280 Handle<String> message);
281 static Local<Value> DebugCommandToJSONRequest(Isolate* isolate,
282 Handle<String> command);
284 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args);
287 static void RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args);
288 static void RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args);
289 static void RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args);
290 static void RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args);
291 static void RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
292 static void RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args);
293 static void RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args);
294 static void RealmSharedGet(Local<String> property,
295 const PropertyCallbackInfo<Value>& info);
296 static void RealmSharedSet(Local<String> property,
298 const PropertyCallbackInfo<void>& info);
300 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
301 static void Write(const v8::FunctionCallbackInfo<v8::Value>& args);
302 static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
303 static void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
304 static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
305 static void ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
306 static Handle<String> ReadFromStdin(Isolate* isolate);
307 static void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
308 args.GetReturnValue().Set(ReadFromStdin(args.GetIsolate()));
310 static void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
311 static void ArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
312 static void Int8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
313 static void Uint8Array(const v8::FunctionCallbackInfo<v8::Value>& args);
314 static void Int16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
315 static void Uint16Array(const v8::FunctionCallbackInfo<v8::Value>& args);
316 static void Int32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
317 static void Uint32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
318 static void Float32Array(const v8::FunctionCallbackInfo<v8::Value>& args);
319 static void Float64Array(const v8::FunctionCallbackInfo<v8::Value>& args);
320 static void Uint8ClampedArray(
321 const v8::FunctionCallbackInfo<v8::Value>& args);
322 static void ArrayBufferSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
323 static void ArraySubArray(const v8::FunctionCallbackInfo<v8::Value>& args);
324 static void ArraySet(const v8::FunctionCallbackInfo<v8::Value>& args);
325 // The OS object on the global object contains methods for performing
326 // operating system calls:
328 // os.system("program_name", ["arg1", "arg2", ...], timeout1, timeout2) will
329 // run the command, passing the arguments to the program. The standard output
330 // of the program will be picked up and returned as a multiline string. If
331 // timeout1 is present then it should be a number. -1 indicates no timeout
332 // and a positive number is used as a timeout in milliseconds that limits the
333 // time spent waiting between receiving output characters from the program.
334 // timeout2, if present, should be a number indicating the limit in
335 // milliseconds on the total running time of the program. Exceptions are
336 // thrown on timeouts or other errors or if the exit status of the program
337 // indicates an error.
339 // os.chdir(dir) changes directory to the given directory. Throws an
340 // exception/ on error.
342 // os.setenv(variable, value) sets an environment variable. Repeated calls to
343 // this method leak memory due to the API of setenv in the standard C library.
345 // os.umask(alue) calls the umask system call and returns the old umask.
347 // os.mkdirp(name, mask) creates a directory. The mask (if present) is anded
348 // with the current umask. Intermediate directories are created if necessary.
349 // An exception is not thrown if the directory already exists. Analogous to
350 // the "mkdir -p" command.
351 static void OSObject(const v8::FunctionCallbackInfo<v8::Value>& args);
352 static void System(const v8::FunctionCallbackInfo<v8::Value>& args);
353 static void ChangeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
354 static void SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
355 static void UnsetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args);
356 static void SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args);
357 static void MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
358 static void RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args);
360 static void AddOSMethods(v8::Isolate* isolate,
361 Handle<ObjectTemplate> os_template);
363 static const char* kPrompt;
364 static ShellOptions options;
367 static Persistent<Context> evaluation_context_;
369 static Persistent<Context> utility_context_;
370 static CounterMap* counter_map_;
371 // We statically allocate a set of local counters to be used if we
372 // don't want to store the stats in a memory-mapped file
373 static CounterCollection local_counters_;
374 static CounterCollection* counters_;
375 static base::OS::MemoryMappedFile* counters_file_;
376 static base::Mutex context_mutex_;
377 static const base::TimeTicks kInitialTicks;
379 static Counter* GetCounter(const char* name, bool is_histogram);
380 static void InstallUtilityScript(Isolate* isolate);
382 static void Initialize(Isolate* isolate);
383 static void InitializeDebugger(Isolate* isolate);
384 static void RunShell(Isolate* isolate);
385 static bool SetOptions(int argc, char* argv[]);
386 static Handle<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate);
387 static Handle<FunctionTemplate> CreateArrayBufferTemplate(FunctionCallback);
388 static Handle<FunctionTemplate> CreateArrayTemplate(FunctionCallback);
389 static Handle<Value> CreateExternalArrayBuffer(Isolate* isolate,
390 Handle<Object> buffer,
392 static Handle<Object> CreateExternalArray(Isolate* isolate,
393 Handle<Object> array,
394 Handle<Object> buffer,
395 ExternalArrayType type,
399 int32_t element_size);
400 static void CreateExternalArray(
401 const v8::FunctionCallbackInfo<v8::Value>& args,
402 ExternalArrayType type,
403 int32_t element_size);
404 static void ExternalArrayWeakCallback(Isolate* isolate,
405 Persistent<Object>* object,