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

Side by Side Diff: src/d8.cc

Issue 162004: Add a readline() command to d8. This reads a single line, stripping the... Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 4 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/d8.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 v8::String::Utf8Value str(args[i]); 157 v8::String::Utf8Value str(args[i]);
158 const char* cstr = ToCString(str); 158 const char* cstr = ToCString(str);
159 printf("%s", cstr); 159 printf("%s", cstr);
160 } 160 }
161 printf("\n"); 161 printf("\n");
162 return Undefined(); 162 return Undefined();
163 } 163 }
164 164
165 165
166 Handle<Value> Shell::Read(const Arguments& args) { 166 Handle<Value> Shell::Read(const Arguments& args) {
167 if (args.Length() != 1) {
168 return ThrowException(String::New("Bad parameters"));
169 }
170 String::Utf8Value file(args[0]); 167 String::Utf8Value file(args[0]);
171 if (*file == NULL) { 168 if (*file == NULL) {
172 return ThrowException(String::New("Error loading file")); 169 return ThrowException(String::New("Error loading file"));
173 } 170 }
174 Handle<String> source = ReadFile(*file); 171 Handle<String> source = ReadFile(*file);
175 if (source.IsEmpty()) { 172 if (source.IsEmpty()) {
176 return ThrowException(String::New("Error loading file")); 173 return ThrowException(String::New("Error loading file"));
177 } 174 }
178 return source; 175 return source;
179 } 176 }
180 177
181 178
179 Handle<Value> Shell::ReadLine(const Arguments& args) {
180 char line_buf[256];
181 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
182 return ThrowException(String::New("Error reading line"));
183 }
184 int len = strlen(line_buf);
185 if (line_buf[len - 1] == '\n') {
186 --len;
187 }
188 return String::New(line_buf, len);
189 }
190
191
182 Handle<Value> Shell::Load(const Arguments& args) { 192 Handle<Value> Shell::Load(const Arguments& args) {
183 for (int i = 0; i < args.Length(); i++) { 193 for (int i = 0; i < args.Length(); i++) {
184 HandleScope handle_scope; 194 HandleScope handle_scope;
185 String::Utf8Value file(args[i]); 195 String::Utf8Value file(args[i]);
186 if (*file == NULL) { 196 if (*file == NULL) {
187 return ThrowException(String::New("Error loading file")); 197 return ThrowException(String::New("Error loading file"));
188 } 198 }
189 Handle<String> source = ReadFile(*file); 199 Handle<String> source = ReadFile(*file);
190 if (source.IsEmpty()) { 200 if (source.IsEmpty()) {
191 return ThrowException(String::New("Error loading file")); 201 return ThrowException(String::New("Error loading file"));
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 V8::SetCounterFunction(LookupCounter); 403 V8::SetCounterFunction(LookupCounter);
394 V8::SetCreateHistogramFunction(CreateHistogram); 404 V8::SetCreateHistogramFunction(CreateHistogram);
395 V8::SetAddHistogramSampleFunction(AddHistogramSample); 405 V8::SetAddHistogramSampleFunction(AddHistogramSample);
396 } 406 }
397 407
398 // Initialize the global objects 408 // Initialize the global objects
399 HandleScope scope; 409 HandleScope scope;
400 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 410 Handle<ObjectTemplate> global_template = ObjectTemplate::New();
401 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); 411 global_template->Set(String::New("print"), FunctionTemplate::New(Print));
402 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); 412 global_template->Set(String::New("read"), FunctionTemplate::New(Read));
413 global_template->Set(String::New("readline"),
414 FunctionTemplate::New(ReadLine));
403 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); 415 global_template->Set(String::New("load"), FunctionTemplate::New(Load));
404 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); 416 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
405 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); 417 global_template->Set(String::New("version"), FunctionTemplate::New(Version));
406 418
407 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); 419 Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
408 AddOSMethods(os_templ); 420 AddOSMethods(os_templ);
409 global_template->Set(String::New("os"), os_templ); 421 global_template->Set(String::New("os"), os_templ);
410 422
411 utility_context_ = Context::New(NULL, global_template); 423 utility_context_ = Context::New(NULL, global_template);
412 utility_context_->SetSecurityToken(Undefined()); 424 utility_context_->SetSecurityToken(Undefined());
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 595
584 void ShellThread::Run() { 596 void ShellThread::Run() {
585 // Prepare the context for this thread. 597 // Prepare the context for this thread.
586 Locker locker; 598 Locker locker;
587 HandleScope scope; 599 HandleScope scope;
588 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 600 Handle<ObjectTemplate> global_template = ObjectTemplate::New();
589 global_template->Set(String::New("print"), 601 global_template->Set(String::New("print"),
590 FunctionTemplate::New(Shell::Print)); 602 FunctionTemplate::New(Shell::Print));
591 global_template->Set(String::New("read"), 603 global_template->Set(String::New("read"),
592 FunctionTemplate::New(Shell::Read)); 604 FunctionTemplate::New(Shell::Read));
605 global_template->Set(String::New("readline"),
606 FunctionTemplate::New(Shell::ReadLine));
593 global_template->Set(String::New("load"), 607 global_template->Set(String::New("load"),
594 FunctionTemplate::New(Shell::Load)); 608 FunctionTemplate::New(Shell::Load));
595 global_template->Set(String::New("yield"), 609 global_template->Set(String::New("yield"),
596 FunctionTemplate::New(Shell::Yield)); 610 FunctionTemplate::New(Shell::Yield));
597 global_template->Set(String::New("version"), 611 global_template->Set(String::New("version"),
598 FunctionTemplate::New(Shell::Version)); 612 FunctionTemplate::New(Shell::Version));
599 613
600 char* ptr = const_cast<char*>(files_.start()); 614 char* ptr = const_cast<char*>(files_.start());
601 while ((ptr != NULL) && (*ptr != '\0')) { 615 while ((ptr != NULL) && (*ptr != '\0')) {
602 // For each newline-separated line. 616 // For each newline-separated line.
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 return 0; 758 return 0;
745 } 759 }
746 760
747 761
748 } // namespace v8 762 } // namespace v8
749 763
750 764
751 int main(int argc, char* argv[]) { 765 int main(int argc, char* argv[]) {
752 return v8::Shell::Main(argc, argv); 766 return v8::Shell::Main(argc, argv);
753 } 767 }
OLDNEW
« no previous file with comments | « src/d8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698