Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Unified Diff: src/d8.cc

Issue 18404: Added preemption option to d8 (Closed)
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index 24eb7225ffc9de445c86a71acc014f3efe83ef63..8dfed58cd5f1cf9e409a7b1204422e5fde754ab7 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -33,6 +33,7 @@
#include "debug.h"
#include "api.h"
#include "natives.h"
+#include "platform.h"
namespace v8 {
@@ -383,10 +384,9 @@ void Shell::OnExit() {
}
-// Reads a file into a v8 string.
-Handle<String> Shell::ReadFile(const char* name) {
+static char* ReadChars(const char *name, int* size_out) {
FILE* file = i::OS::FOpen(name, "rb");
- if (file == NULL) return Handle<String>();
+ if (file == NULL) return NULL;
fseek(file, 0, SEEK_END);
int size = ftell(file);
@@ -399,7 +399,17 @@ Handle<String> Shell::ReadFile(const char* name) {
i += read;
}
fclose(file);
- Handle<String> result = String::New(chars, size);
+ *size_out = size;
+ return chars;
+}
+
+
+// Reads a file into a v8 string.
+Handle<String> Shell::ReadFile(const char* name) {
+ int size = 0;
+ char* chars = ReadChars(name, &size);
+ if (chars == NULL) return Handle<String>();
+ Handle<String> result = String::New(chars);
delete[] chars;
return result;
}
@@ -423,6 +433,37 @@ void Shell::RunShell() {
}
+class ShellThread : public i::Thread {
+ public:
+ ShellThread(int no, i::Vector<const char> files)
+ : no_(no), files_(files) { }
+ virtual void Run();
+ private:
+ int no_;
+ i::Vector<const char> files_;
+};
+
+
+void ShellThread::Run() {
+ char* ptr = const_cast<char*>(files_.start());
+ while (ptr) {
Erik Corry 2009/01/20 13:28:31 Test against NULL should be explicit.
Christian Plesner Hansen 2009/01/20 14:13:46 Fixed (times 3).
+ // For each newline-separated line.
+ char *filename = ptr;
+ char* next = ::strchr(ptr, '\n');
+ if (next) {
Erik Corry 2009/01/20 13:28:31 Test against NULL should be explicit.
+ *next = '\0';
+ ptr = (next + 1);
+ } else {
+ ptr = next;
Erik Corry 2009/01/20 13:28:31 ptr = NULL would be clearer.
+ }
+ Locker locker;
+ HandleScope scope;
+ Handle<String> str = Shell::ReadFile(filename);
+ Shell::ExecuteString(str, String::New(filename), false, false);
+ }
+}
+
+
int Shell::Main(int argc, char* argv[]) {
i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
if (i::FLAG_help) {
@@ -430,6 +471,7 @@ int Shell::Main(int argc, char* argv[]) {
}
Initialize();
bool run_shell = (argc == 1);
+ i::List<i::Thread*> threads(1);
Context::Scope context_scope(evaluation_context_);
Erik Corry 2009/01/20 13:28:31 This should be protected by a locker. Also the pl
Christian Plesner Hansen 2009/01/20 14:13:46 I don't think so -- there can only be one thread d
for (int i = 1; i < argc; i++) {
char* str = argv[i];
@@ -449,6 +491,16 @@ int Shell::Main(int argc, char* argv[]) {
if (!ExecuteString(source, file_name, false, true))
return 1;
i++;
+ } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
+ Locker locker;
+ Locker::StartPreemption(10);
Erik Corry 2009/01/20 13:28:31 You only need to do this once, not once per file.
Christian Plesner Hansen 2009/01/20 14:13:46 Right, but I don't want to enable it without the -
+ int size = 0;
+ const char *files = ReadChars(argv[++i], &size);
+ if (files == NULL) return 1;
+ ShellThread *thread =
+ new ShellThread(threads.length(), i::Vector<const char>(files, size));
+ thread->Start();
+ threads.Add(thread);
} else {
// Use all other arguments as names of files to load and run.
Erik Corry 2009/01/20 13:28:31 If the user uses both -p files and straight js fil
Christian Plesner Hansen 2009/01/20 14:13:46 Fixed
HandleScope handle_scope;
@@ -466,6 +518,11 @@ int Shell::Main(int argc, char* argv[]) {
v8::Debug::AddDebugEventListener(HandleDebugEvent);
if (run_shell)
RunShell();
Erik Corry 2009/01/20 13:28:31 and here.
Christian Plesner Hansen 2009/01/20 14:13:46 Fixed
+ for (int i = 0; i < threads.length(); i++) {
+ Thread *thread = threads[i];
+ thread->Join();
+ delete thread;
+ }
OnExit();
return 0;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698