Lines Matching refs:v8
1 // Copyright 2011 the V8 project authors. All rights reserved.
28 #include <v8.h>
41 * based on V8. This includes initializing V8 with command line options,
48 v8::Persistent<v8::Context> CreateShellContext();
49 void RunShell(v8::Handle<v8::Context> context);
51 bool ExecuteString(v8::Handle<v8::String> source,
52 v8::Handle<v8::Value> name,
55 v8::Handle<v8::Value> Print(const v8::Arguments& args);
56 v8::Handle<v8::Value> Read(const v8::Arguments& args);
57 v8::Handle<v8::Value> Load(const v8::Arguments& args);
58 v8::Handle<v8::Value> Quit(const v8::Arguments& args);
59 v8::Handle<v8::Value> Version(const v8::Arguments& args);
60 v8::Handle<v8::String> ReadFile(const char* name);
61 void ReportException(v8::TryCatch* handler);
68 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
70 v8::HandleScope handle_scope;
71 v8::Persistent<v8::Context> context = CreateShellContext();
81 v8::V8::Dispose();
86 // Extracts a C string from a V8 Utf8Value.
87 const char* ToCString(const v8::String::Utf8Value& value) {
94 v8::Persistent<v8::Context> CreateShellContext() {
96 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
98 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
100 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
102 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load));
104 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit));
106 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version));
108 return v8::Context::New(NULL, global);
112 // The callback that is invoked by v8 whenever the JavaScript 'print'
115 v8::Handle<v8::Value> Print(const v8::Arguments& args) {
118 v8::HandleScope handle_scope;
124 v8::String::Utf8Value str(args[i]);
130 return v8::Undefined();
134 // The callback that is invoked by v8 whenever the JavaScript 'read'
137 v8::Handle<v8::Value> Read(const v8::Arguments& args) {
139 return v8::ThrowException(v8::String::New("Bad parameters"));
141 v8::String::Utf8Value file(args[0]);
143 return v8::ThrowException(v8::String::New("Error loading file"));
145 v8::Handle<v8::String> source = ReadFile(*file);
147 return v8::ThrowException(v8::String::New("Error loading file"));
153 // The callback that is invoked by v8 whenever the JavaScript 'load'
156 v8::Handle<v8::Value> Load(const v8::Arguments& args) {
158 v8::HandleScope handle_scope;
159 v8::String::Utf8Value file(args[i]);
161 return v8::ThrowException(v8::String::New("Error loading file"));
163 v8::Handle<v8::String> source = ReadFile(*file);
165 return v8::ThrowException(v8::String::New("Error loading file"));
167 if (!ExecuteString(source, v8::String::New(*file), false, false)) {
168 return v8::ThrowException(v8::String::New("Error executing file"));
171 return v8::Undefined();
175 // The callback that is invoked by v8 whenever the JavaScript 'quit'
177 v8::Handle<v8::Value> Quit(const v8::Arguments& args) {
184 return v8::Undefined();
188 v8::Handle<v8::Value> Version(const v8::Arguments& args) {
189 return v8::String::New(v8::V8::GetVersion());
193 // Reads a file into a v8 string.
194 v8::Handle<v8::String> ReadFile(const char* name) {
196 if (file == NULL) return v8::Handle<v8::String>();
209 v8::Handle<v8::String> result = v8::String::New(chars, size);
229 v8::Handle<v8::String> file_name = v8::String::New("unnamed");
230 v8::Handle<v8::String> source = v8::String::New(argv[++i]);
234 v8::Handle<v8::String> file_name = v8::String::New(str);
235 v8::Handle<v8::String> source = ReadFile(str);
248 void RunShell(v8::Handle<v8::Context> context) {
249 printf("V8 version %s [sample shell]\n", v8::V8::GetVersion());
252 v8::Context::Scope context_scope(context);
253 v8::Local<v8::String> name(v8::String::New("(shell)"));
259 v8::HandleScope handle_scope;
260 ExecuteString(v8::String::New(str), name, true, true);
266 // Executes a string within the current v8 context.
267 bool ExecuteString(v8::Handle<v8::String> source,
268 v8::Handle<v8::Value> name,
271 v8::HandleScope handle_scope;
272 v8::TryCatch try_catch;
273 v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
280 v8::Handle<v8::Value> result = script->Run();
292 v8::String::Utf8Value str(result);
302 void ReportException(v8::TryCatch* try_catch) {
303 v8::HandleScope handle_scope;
304 v8::String::Utf8Value exception(try_catch->Exception());
306 v8::Handle<v8::Message> message = try_catch->Message();
308 // V8 didn't provide any extra information about this error; just
313 v8::String::Utf8Value filename(message->GetScriptResourceName());
318 v8::String::Utf8Value sourceline(message->GetSourceLine());
331 v8::String::Utf8Value stack_trace(try_catch->StackTrace());