OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 | 47 |
48 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); | 48 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); |
49 void RunShell(v8::Handle<v8::Context> context); | 49 void RunShell(v8::Handle<v8::Context> context); |
50 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); | 50 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); |
51 bool ExecuteString(v8::Isolate* isolate, | 51 bool ExecuteString(v8::Isolate* isolate, |
52 v8::Handle<v8::String> source, | 52 v8::Handle<v8::String> source, |
53 v8::Handle<v8::Value> name, | 53 v8::Handle<v8::Value> name, |
54 bool print_result, | 54 bool print_result, |
55 bool report_exceptions); | 55 bool report_exceptions); |
56 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 56 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
57 v8::Handle<v8::Value> Read(const v8::Arguments& args); | 57 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
58 v8::Handle<v8::Value> Load(const v8::Arguments& args); | 58 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
59 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 59 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
60 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 60 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
61 v8::Handle<v8::String> ReadFile(const char* name); | 61 v8::Handle<v8::String> ReadFile(const char* name); |
62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
63 | 63 |
64 | 64 |
65 static bool run_shell; | 65 static bool run_shell; |
66 | 66 |
67 | 67 |
68 int main(int argc, char* argv[]) { | 68 int main(int argc, char* argv[]) { |
69 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 69 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Bind the 'version' function | 109 // Bind the 'version' function |
110 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 110 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); |
111 | 111 |
112 return v8::Context::New(isolate, NULL, global); | 112 return v8::Context::New(isolate, NULL, global); |
113 } | 113 } |
114 | 114 |
115 | 115 |
116 // The callback that is invoked by v8 whenever the JavaScript 'print' | 116 // The callback that is invoked by v8 whenever the JavaScript 'print' |
117 // function is called. Prints its arguments on stdout separated by | 117 // function is called. Prints its arguments on stdout separated by |
118 // spaces and ending with a newline. | 118 // spaces and ending with a newline. |
119 v8::Handle<v8::Value> Print(const v8::Arguments& args) { | 119 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
120 bool first = true; | 120 bool first = true; |
121 for (int i = 0; i < args.Length(); i++) { | 121 for (int i = 0; i < args.Length(); i++) { |
122 v8::HandleScope handle_scope(args.GetIsolate()); | 122 v8::HandleScope handle_scope(args.GetIsolate()); |
123 if (first) { | 123 if (first) { |
124 first = false; | 124 first = false; |
125 } else { | 125 } else { |
126 printf(" "); | 126 printf(" "); |
127 } | 127 } |
128 v8::String::Utf8Value str(args[i]); | 128 v8::String::Utf8Value str(args[i]); |
129 const char* cstr = ToCString(str); | 129 const char* cstr = ToCString(str); |
130 printf("%s", cstr); | 130 printf("%s", cstr); |
131 } | 131 } |
132 printf("\n"); | 132 printf("\n"); |
133 fflush(stdout); | 133 fflush(stdout); |
134 return v8::Undefined(); | |
135 } | 134 } |
136 | 135 |
137 | 136 |
138 // The callback that is invoked by v8 whenever the JavaScript 'read' | 137 // The callback that is invoked by v8 whenever the JavaScript 'read' |
139 // function is called. This function loads the content of the file named in | 138 // function is called. This function loads the content of the file named in |
140 // the argument into a JavaScript string. | 139 // the argument into a JavaScript string. |
141 v8::Handle<v8::Value> Read(const v8::Arguments& args) { | 140 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
142 if (args.Length() != 1) { | 141 if (args.Length() != 1) { |
143 return v8::ThrowException(v8::String::New("Bad parameters")); | 142 v8::ThrowException(v8::String::New("Bad parameters")); |
| 143 return; |
144 } | 144 } |
145 v8::String::Utf8Value file(args[0]); | 145 v8::String::Utf8Value file(args[0]); |
146 if (*file == NULL) { | 146 if (*file == NULL) { |
147 return v8::ThrowException(v8::String::New("Error loading file")); | 147 v8::ThrowException(v8::String::New("Error loading file")); |
| 148 return; |
148 } | 149 } |
149 v8::Handle<v8::String> source = ReadFile(*file); | 150 v8::Handle<v8::String> source = ReadFile(*file); |
150 if (source.IsEmpty()) { | 151 if (source.IsEmpty()) { |
151 return v8::ThrowException(v8::String::New("Error loading file")); | 152 v8::ThrowException(v8::String::New("Error loading file")); |
| 153 return; |
152 } | 154 } |
153 return source; | 155 args.GetReturnValue().Set(source); |
154 } | 156 } |
155 | 157 |
156 | 158 |
157 // The callback that is invoked by v8 whenever the JavaScript 'load' | 159 // The callback that is invoked by v8 whenever the JavaScript 'load' |
158 // function is called. Loads, compiles and executes its argument | 160 // function is called. Loads, compiles and executes its argument |
159 // JavaScript file. | 161 // JavaScript file. |
160 v8::Handle<v8::Value> Load(const v8::Arguments& args) { | 162 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
161 for (int i = 0; i < args.Length(); i++) { | 163 for (int i = 0; i < args.Length(); i++) { |
162 v8::HandleScope handle_scope(args.GetIsolate()); | 164 v8::HandleScope handle_scope(args.GetIsolate()); |
163 v8::String::Utf8Value file(args[i]); | 165 v8::String::Utf8Value file(args[i]); |
164 if (*file == NULL) { | 166 if (*file == NULL) { |
165 return v8::ThrowException(v8::String::New("Error loading file")); | 167 v8::ThrowException(v8::String::New("Error loading file")); |
| 168 return; |
166 } | 169 } |
167 v8::Handle<v8::String> source = ReadFile(*file); | 170 v8::Handle<v8::String> source = ReadFile(*file); |
168 if (source.IsEmpty()) { | 171 if (source.IsEmpty()) { |
169 return v8::ThrowException(v8::String::New("Error loading file")); | 172 v8::ThrowException(v8::String::New("Error loading file")); |
| 173 return; |
170 } | 174 } |
171 if (!ExecuteString(args.GetIsolate(), | 175 if (!ExecuteString(args.GetIsolate(), |
172 source, | 176 source, |
173 v8::String::New(*file), | 177 v8::String::New(*file), |
174 false, | 178 false, |
175 false)) { | 179 false)) { |
176 return v8::ThrowException(v8::String::New("Error executing file")); | 180 v8::ThrowException(v8::String::New("Error executing file")); |
| 181 return; |
177 } | 182 } |
178 } | 183 } |
179 return v8::Undefined(); | |
180 } | 184 } |
181 | 185 |
182 | 186 |
183 // The callback that is invoked by v8 whenever the JavaScript 'quit' | 187 // The callback that is invoked by v8 whenever the JavaScript 'quit' |
184 // function is called. Quits. | 188 // function is called. Quits. |
185 v8::Handle<v8::Value> Quit(const v8::Arguments& args) { | 189 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
186 // If not arguments are given args[0] will yield undefined which | 190 // If not arguments are given args[0] will yield undefined which |
187 // converts to the integer value 0. | 191 // converts to the integer value 0. |
188 int exit_code = args[0]->Int32Value(); | 192 int exit_code = args[0]->Int32Value(); |
189 fflush(stdout); | 193 fflush(stdout); |
190 fflush(stderr); | 194 fflush(stderr); |
191 exit(exit_code); | 195 exit(exit_code); |
192 return v8::Undefined(); | |
193 } | 196 } |
194 | 197 |
195 | 198 |
196 v8::Handle<v8::Value> Version(const v8::Arguments& args) { | 199 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
197 return v8::String::New(v8::V8::GetVersion()); | 200 args.GetReturnValue().Set(v8::String::New(v8::V8::GetVersion())); |
198 } | 201 } |
199 | 202 |
200 | 203 |
201 // Reads a file into a v8 string. | 204 // Reads a file into a v8 string. |
202 v8::Handle<v8::String> ReadFile(const char* name) { | 205 v8::Handle<v8::String> ReadFile(const char* name) { |
203 FILE* file = fopen(name, "rb"); | 206 FILE* file = fopen(name, "rb"); |
204 if (file == NULL) return v8::Handle<v8::String>(); | 207 if (file == NULL) return v8::Handle<v8::String>(); |
205 | 208 |
206 fseek(file, 0, SEEK_END); | 209 fseek(file, 0, SEEK_END); |
207 int size = ftell(file); | 210 int size = ftell(file); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 fprintf(stderr, "^"); | 345 fprintf(stderr, "^"); |
343 } | 346 } |
344 fprintf(stderr, "\n"); | 347 fprintf(stderr, "\n"); |
345 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 348 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
346 if (stack_trace.length() > 0) { | 349 if (stack_trace.length() > 0) { |
347 const char* stack_trace_string = ToCString(stack_trace); | 350 const char* stack_trace_string = ToCString(stack_trace); |
348 fprintf(stderr, "%s\n", stack_trace_string); | 351 fprintf(stderr, "%s\n", stack_trace_string); |
349 } | 352 } |
350 } | 353 } |
351 } | 354 } |
OLD | NEW |