OLD | NEW |
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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 } | 160 } |
161 v8::String::Utf8Value str(args[i]); | 161 v8::String::Utf8Value str(args[i]); |
162 const char* cstr = ToCString(str); | 162 const char* cstr = ToCString(str); |
163 printf("%s", cstr); | 163 printf("%s", cstr); |
164 } | 164 } |
165 return Undefined(); | 165 return Undefined(); |
166 } | 166 } |
167 | 167 |
168 | 168 |
169 Handle<Value> Shell::Read(const Arguments& args) { | 169 Handle<Value> Shell::Read(const Arguments& args) { |
170 if (args.Length() != 1) { | |
171 return ThrowException(String::New("Bad parameters")); | |
172 } | |
173 String::Utf8Value file(args[0]); | 170 String::Utf8Value file(args[0]); |
174 if (*file == NULL) { | 171 if (*file == NULL) { |
175 return ThrowException(String::New("Error loading file")); | 172 return ThrowException(String::New("Error loading file")); |
176 } | 173 } |
177 Handle<String> source = ReadFile(*file); | 174 Handle<String> source = ReadFile(*file); |
178 if (source.IsEmpty()) { | 175 if (source.IsEmpty()) { |
179 return ThrowException(String::New("Error loading file")); | 176 return ThrowException(String::New("Error loading file")); |
180 } | 177 } |
181 return source; | 178 return source; |
182 } | 179 } |
183 | 180 |
184 | 181 |
| 182 Handle<Value> Shell::ReadLine(const Arguments& args) { |
| 183 char line_buf[256]; |
| 184 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { |
| 185 return ThrowException(String::New("Error reading line")); |
| 186 } |
| 187 int len = strlen(line_buf); |
| 188 if (line_buf[len - 1] == '\n') { |
| 189 --len; |
| 190 } |
| 191 return String::New(line_buf, len); |
| 192 } |
| 193 |
| 194 |
185 Handle<Value> Shell::Load(const Arguments& args) { | 195 Handle<Value> Shell::Load(const Arguments& args) { |
186 for (int i = 0; i < args.Length(); i++) { | 196 for (int i = 0; i < args.Length(); i++) { |
187 HandleScope handle_scope; | 197 HandleScope handle_scope; |
188 String::Utf8Value file(args[i]); | 198 String::Utf8Value file(args[i]); |
189 if (*file == NULL) { | 199 if (*file == NULL) { |
190 return ThrowException(String::New("Error loading file")); | 200 return ThrowException(String::New("Error loading file")); |
191 } | 201 } |
192 Handle<String> source = ReadFile(*file); | 202 Handle<String> source = ReadFile(*file); |
193 if (source.IsEmpty()) { | 203 if (source.IsEmpty()) { |
194 return ThrowException(String::New("Error loading file")); | 204 return ThrowException(String::New("Error loading file")); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 V8::SetCreateHistogramFunction(CreateHistogram); | 407 V8::SetCreateHistogramFunction(CreateHistogram); |
398 V8::SetAddHistogramSampleFunction(AddHistogramSample); | 408 V8::SetAddHistogramSampleFunction(AddHistogramSample); |
399 } | 409 } |
400 | 410 |
401 // Initialize the global objects | 411 // Initialize the global objects |
402 HandleScope scope; | 412 HandleScope scope; |
403 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); | 413 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); |
404 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); | 414 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); |
405 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); | 415 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); |
406 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); | 416 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); |
| 417 global_template->Set(String::New("readline"), |
| 418 FunctionTemplate::New(ReadLine)); |
407 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); | 419 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); |
408 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); | 420 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); |
409 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); | 421 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); |
410 | 422 |
411 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); | 423 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); |
412 AddOSMethods(os_templ); | 424 AddOSMethods(os_templ); |
413 global_template->Set(String::New("os"), os_templ); | 425 global_template->Set(String::New("os"), os_templ); |
414 | 426 |
415 utility_context_ = Context::New(NULL, global_template); | 427 utility_context_ = Context::New(NULL, global_template); |
416 utility_context_->SetSecurityToken(Undefined()); | 428 utility_context_->SetSecurityToken(Undefined()); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 // Prepare the context for this thread. | 601 // Prepare the context for this thread. |
590 Locker locker; | 602 Locker locker; |
591 HandleScope scope; | 603 HandleScope scope; |
592 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); | 604 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); |
593 global_template->Set(String::New("print"), | 605 global_template->Set(String::New("print"), |
594 FunctionTemplate::New(Shell::Print)); | 606 FunctionTemplate::New(Shell::Print)); |
595 global_template->Set(String::New("write"), | 607 global_template->Set(String::New("write"), |
596 FunctionTemplate::New(Shell::Write)); | 608 FunctionTemplate::New(Shell::Write)); |
597 global_template->Set(String::New("read"), | 609 global_template->Set(String::New("read"), |
598 FunctionTemplate::New(Shell::Read)); | 610 FunctionTemplate::New(Shell::Read)); |
| 611 global_template->Set(String::New("readline"), |
| 612 FunctionTemplate::New(Shell::ReadLine)); |
599 global_template->Set(String::New("load"), | 613 global_template->Set(String::New("load"), |
600 FunctionTemplate::New(Shell::Load)); | 614 FunctionTemplate::New(Shell::Load)); |
601 global_template->Set(String::New("yield"), | 615 global_template->Set(String::New("yield"), |
602 FunctionTemplate::New(Shell::Yield)); | 616 FunctionTemplate::New(Shell::Yield)); |
603 global_template->Set(String::New("version"), | 617 global_template->Set(String::New("version"), |
604 FunctionTemplate::New(Shell::Version)); | 618 FunctionTemplate::New(Shell::Version)); |
605 | 619 |
606 char* ptr = const_cast<char*>(files_.start()); | 620 char* ptr = const_cast<char*>(files_.start()); |
607 while ((ptr != NULL) && (*ptr != '\0')) { | 621 while ((ptr != NULL) && (*ptr != '\0')) { |
608 // For each newline-separated line. | 622 // For each newline-separated line. |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 return 0; | 764 return 0; |
751 } | 765 } |
752 | 766 |
753 | 767 |
754 } // namespace v8 | 768 } // namespace v8 |
755 | 769 |
756 | 770 |
757 int main(int argc, char* argv[]) { | 771 int main(int argc, char* argv[]) { |
758 return v8::Shell::Main(argc, argv); | 772 return v8::Shell::Main(argc, argv); |
759 } | 773 } |
OLD | NEW |