Lines Matching refs:v8

28 #include <include/v8.h>
51 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate);
52 void RunShell(v8::Handle<v8::Context> context);
53 int RunMain(v8::Isolate* isolate, int argc, char* argv[]);
54 bool ExecuteString(v8::Isolate* isolate,
55 v8::Handle<v8::String> source,
56 v8::Handle<v8::Value> name,
59 void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
60 void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
61 void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
62 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
63 void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
64 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
65 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
71 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
83 v8::V8::InitializeICU();
84 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
85 v8::V8::InitializePlatform(platform);
86 v8::V8::Initialize();
87 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
89 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
90 v8::Isolate* isolate = v8::Isolate::New();
94 v8::Isolate::Scope isolate_scope(isolate);
95 v8::HandleScope handle_scope(isolate);
96 v8::Handle<v8::Context> context = CreateShellContext(isolate);
101 v8::Context::Scope context_scope(context);
105 v8::V8::Dispose();
106 v8::V8::ShutdownPlatform();
113 const char* ToCString(const v8::String::Utf8Value& value) {
120 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) {
122 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
124 global->Set(v8::String::NewFromUtf8(isolate, "print"),
125 v8::FunctionTemplate::New(isolate, Print));
127 global->Set(v8::String::NewFromUtf8(isolate, "read"),
128 v8::FunctionTemplate::New(isolate, Read));
130 global->Set(v8::String::NewFromUtf8(isolate, "load"),
131 v8::FunctionTemplate::New(isolate, Load));
133 global->Set(v8::String::NewFromUtf8(isolate, "quit"),
134 v8::FunctionTemplate::New(isolate, Quit));
136 global->Set(v8::String::NewFromUtf8(isolate, "version"),
137 v8::FunctionTemplate::New(isolate, Version));
139 return v8::Context::New(isolate, NULL, global);
143 // The callback that is invoked by v8 whenever the JavaScript 'print'
146 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
149 v8::HandleScope handle_scope(args.GetIsolate());
155 v8::String::Utf8Value str(args[i]);
164 // The callback that is invoked by v8 whenever the JavaScript 'read'
167 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
170 v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters"));
173 v8::String::Utf8Value file(args[0]);
176 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
179 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
182 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
189 // The callback that is invoked by v8 whenever the JavaScript 'load'
192 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
194 v8::HandleScope handle_scope(args.GetIsolate());
195 v8::String::Utf8Value file(args[i]);
198 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
201 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
204 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
209 v8::String::NewFromUtf8(args.GetIsolate(), *file),
213 v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file"));
220 // The callback that is invoked by v8 whenever the JavaScript 'quit'
222 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
232 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
234 v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion()));
238 // Reads a file into a v8 string.
239 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
241 if (file == NULL) return v8::Handle<v8::String>();
254 v8::Handle<v8::String> result =
255 v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
262 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
276 v8::Handle<v8::String> file_name =
277 v8::String::NewFromUtf8(isolate, "unnamed");
278 v8::Handle<v8::String> source =
279 v8::String::NewFromUtf8(isolate, argv[++i]);
283 v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str);
284 v8::Handle<v8::String> source = ReadFile(isolate, str);
297 void RunShell(v8::Handle<v8::Context> context) {
298 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
301 v8::Context::Scope context_scope(context);
302 v8::Local<v8::String> name(
303 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)"));
309 v8::HandleScope handle_scope(context->GetIsolate());
311 v8::String::NewFromUtf8(context->GetIsolate(), str),
320 // Executes a string within the current v8 context.
321 bool ExecuteString(v8::Isolate* isolate,
322 v8::Handle<v8::String> source,
323 v8::Handle<v8::Value> name,
326 v8::HandleScope handle_scope(isolate);
327 v8::TryCatch try_catch;
328 v8::ScriptOrigin origin(name);
329 v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
336 v8::Handle<v8::Value> result = script->Run();
348 v8::String::Utf8Value str(result);
358 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
359 v8::HandleScope handle_scope(isolate);
360 v8::String::Utf8Value exception(try_catch->Exception());
362 v8::Handle<v8::Message> message = try_catch->Message();
369 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
374 v8::String::Utf8Value sourceline(message->GetSourceLine());
387 v8::String::Utf8Value stack_trace(try_catch->StackTrace());