Upgrade V8 to version 4.9.385.28

https://chromium.googlesource.com/v8/v8/+/4.9.385.28

FPIIM-449

Change-Id: I4b2e74289d4bf3667f2f3dc8aa2e541f63e26eb4
diff --git a/samples/shell.cc b/samples/shell.cc
index 1a08aff..b89ffdd 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -44,20 +44,19 @@
  */
 
 
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate);
-void RunShell(v8::Handle<v8::Context> context);
-int RunMain(v8::Isolate* isolate, int argc, char* argv[]);
-bool ExecuteString(v8::Isolate* isolate,
-                   v8::Handle<v8::String> source,
-                   v8::Handle<v8::Value> name,
-                   bool print_result,
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate);
+void RunShell(v8::Local<v8::Context> context, v8::Platform* platform);
+int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
+            char* argv[]);
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+                   v8::Local<v8::Value> name, bool print_result,
                    bool report_exceptions);
 void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
 void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
 void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
 void Version(const v8::FunctionCallbackInfo<v8::Value>& args);
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
 
 
@@ -77,27 +76,30 @@
 
 int main(int argc, char* argv[]) {
   v8::V8::InitializeICU();
+  v8::V8::InitializeExternalStartupData(argv[0]);
   v8::Platform* platform = v8::platform::CreateDefaultPlatform();
   v8::V8::InitializePlatform(platform);
   v8::V8::Initialize();
   v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
   ShellArrayBufferAllocator array_buffer_allocator;
-  v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
-  v8::Isolate* isolate = v8::Isolate::New();
+  v8::Isolate::CreateParams create_params;
+  create_params.array_buffer_allocator = &array_buffer_allocator;
+  v8::Isolate* isolate = v8::Isolate::New(create_params);
   run_shell = (argc == 1);
   int result;
   {
     v8::Isolate::Scope isolate_scope(isolate);
     v8::HandleScope handle_scope(isolate);
-    v8::Handle<v8::Context> context = CreateShellContext(isolate);
+    v8::Local<v8::Context> context = CreateShellContext(isolate);
     if (context.IsEmpty()) {
       fprintf(stderr, "Error creating context\n");
       return 1;
     }
     v8::Context::Scope context_scope(context);
-    result = RunMain(isolate, argc, argv);
-    if (run_shell) RunShell(context);
+    result = RunMain(isolate, platform, argc, argv);
+    if (run_shell) RunShell(context, platform);
   }
+  isolate->Dispose();
   v8::V8::Dispose();
   v8::V8::ShutdownPlatform();
   delete platform;
@@ -113,24 +115,31 @@
 
 // Creates a new execution environment containing the built-in
 // functions.
-v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) {
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
   // Create a template for the global object.
-  v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
+  v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
   // Bind the global 'print' function to the C++ Print callback.
-  global->Set(v8::String::NewFromUtf8(isolate, "print"),
-              v8::FunctionTemplate::New(isolate, Print));
+  global->Set(
+      v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
+          .ToLocalChecked(),
+      v8::FunctionTemplate::New(isolate, Print));
   // Bind the global 'read' function to the C++ Read callback.
-  global->Set(v8::String::NewFromUtf8(isolate, "read"),
+  global->Set(v8::String::NewFromUtf8(
+                  isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(),
               v8::FunctionTemplate::New(isolate, Read));
   // Bind the global 'load' function to the C++ Load callback.
-  global->Set(v8::String::NewFromUtf8(isolate, "load"),
+  global->Set(v8::String::NewFromUtf8(
+                  isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(),
               v8::FunctionTemplate::New(isolate, Load));
   // Bind the 'quit' function
-  global->Set(v8::String::NewFromUtf8(isolate, "quit"),
+  global->Set(v8::String::NewFromUtf8(
+                  isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(),
               v8::FunctionTemplate::New(isolate, Quit));
   // Bind the 'version' function
-  global->Set(v8::String::NewFromUtf8(isolate, "version"),
-              v8::FunctionTemplate::New(isolate, Version));
+  global->Set(
+      v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal)
+          .ToLocalChecked(),
+      v8::FunctionTemplate::New(isolate, Version));
 
   return v8::Context::New(isolate, NULL, global);
 }
@@ -163,19 +172,22 @@
 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
   if (args.Length() != 1) {
     args.GetIsolate()->ThrowException(
-        v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters"));
+        v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters",
+                                v8::NewStringType::kNormal).ToLocalChecked());
     return;
   }
   v8::String::Utf8Value file(args[0]);
   if (*file == NULL) {
     args.GetIsolate()->ThrowException(
-        v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+        v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+                                v8::NewStringType::kNormal).ToLocalChecked());
     return;
   }
-  v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
-  if (source.IsEmpty()) {
+  v8::Local<v8::String> source;
+  if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
     args.GetIsolate()->ThrowException(
-        v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+        v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+                                v8::NewStringType::kNormal).ToLocalChecked());
     return;
   }
   args.GetReturnValue().Set(source);
@@ -191,22 +203,21 @@
     v8::String::Utf8Value file(args[i]);
     if (*file == NULL) {
       args.GetIsolate()->ThrowException(
-          v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+          v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+                                  v8::NewStringType::kNormal).ToLocalChecked());
       return;
     }
-    v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file);
-    if (source.IsEmpty()) {
+    v8::Local<v8::String> source;
+    if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
       args.GetIsolate()->ThrowException(
-           v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file"));
+          v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
+                                  v8::NewStringType::kNormal).ToLocalChecked());
       return;
     }
-    if (!ExecuteString(args.GetIsolate(),
-                       source,
-                       v8::String::NewFromUtf8(args.GetIsolate(), *file),
-                       false,
-                       false)) {
+    if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
       args.GetIsolate()->ThrowException(
-          v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file"));
+          v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file",
+                                  v8::NewStringType::kNormal).ToLocalChecked());
       return;
     }
   }
@@ -218,7 +229,8 @@
 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
   // If not arguments are given args[0] will yield undefined which
   // converts to the integer value 0.
-  int exit_code = args[0]->Int32Value();
+  int exit_code =
+      args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0);
   fflush(stdout);
   fflush(stderr);
   exit(exit_code);
@@ -227,35 +239,40 @@
 
 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
   args.GetReturnValue().Set(
-      v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion()));
+      v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(),
+                              v8::NewStringType::kNormal).ToLocalChecked());
 }
 
 
 // Reads a file into a v8 string.
-v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
+v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
   FILE* file = fopen(name, "rb");
-  if (file == NULL) return v8::Handle<v8::String>();
+  if (file == NULL) return v8::MaybeLocal<v8::String>();
 
   fseek(file, 0, SEEK_END);
-  int size = ftell(file);
+  size_t size = ftell(file);
   rewind(file);
 
   char* chars = new char[size + 1];
   chars[size] = '\0';
-  for (int i = 0; i < size;) {
-    int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
-    i += read;
+  for (size_t i = 0; i < size;) {
+    i += fread(&chars[i], 1, size - i, file);
+    if (ferror(file)) {
+      fclose(file);
+      return v8::MaybeLocal<v8::String>();
+    }
   }
   fclose(file);
-  v8::Handle<v8::String> result =
-      v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
+  v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8(
+      isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size));
   delete[] chars;
   return result;
 }
 
 
 // Process remaining command line arguments and execute files
-int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
+int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
+            char* argv[]) {
   for (int i = 1; i < argc; i++) {
     const char* str = argv[i];
     if (strcmp(str, "--shell") == 0) {
@@ -269,20 +286,31 @@
               "Warning: unknown flag %s.\nTry --help for options\n", str);
     } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
       // Execute argument given to -e option directly.
-      v8::Handle<v8::String> file_name =
-          v8::String::NewFromUtf8(isolate, "unnamed");
-      v8::Handle<v8::String> source =
-          v8::String::NewFromUtf8(isolate, argv[++i]);
-      if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
+      v8::Local<v8::String> file_name =
+          v8::String::NewFromUtf8(isolate, "unnamed",
+                                  v8::NewStringType::kNormal).ToLocalChecked();
+      v8::Local<v8::String> source;
+      if (!v8::String::NewFromUtf8(isolate, argv[++i],
+                                   v8::NewStringType::kNormal)
+               .ToLocal(&source)) {
+        return 1;
+      }
+      bool success = ExecuteString(isolate, source, file_name, false, true);
+      while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
+      if (!success) return 1;
     } else {
       // Use all other arguments as names of files to load and run.
-      v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str);
-      v8::Handle<v8::String> source = ReadFile(isolate, str);
-      if (source.IsEmpty()) {
+      v8::Local<v8::String> file_name =
+          v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
+              .ToLocalChecked();
+      v8::Local<v8::String> source;
+      if (!ReadFile(isolate, str).ToLocal(&source)) {
         fprintf(stderr, "Error reading '%s'\n", str);
         continue;
       }
-      if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
+      bool success = ExecuteString(isolate, source, file_name, false, true);
+      while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
+      if (!success) return 1;
     }
   }
   return 0;
@@ -290,47 +318,49 @@
 
 
 // The read-eval-execute loop of the shell.
-void RunShell(v8::Handle<v8::Context> context) {
+void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) {
   fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
   static const int kBufferSize = 256;
   // Enter the execution environment before evaluating any code.
   v8::Context::Scope context_scope(context);
   v8::Local<v8::String> name(
-      v8::String::NewFromUtf8(context->GetIsolate(), "(shell)"));
+      v8::String::NewFromUtf8(context->GetIsolate(), "(shell)",
+                              v8::NewStringType::kNormal).ToLocalChecked());
   while (true) {
     char buffer[kBufferSize];
     fprintf(stderr, "> ");
     char* str = fgets(buffer, kBufferSize, stdin);
     if (str == NULL) break;
     v8::HandleScope handle_scope(context->GetIsolate());
-    ExecuteString(context->GetIsolate(),
-                  v8::String::NewFromUtf8(context->GetIsolate(), str),
-                  name,
-                  true,
-                  true);
+    ExecuteString(
+        context->GetIsolate(),
+        v8::String::NewFromUtf8(context->GetIsolate(), str,
+                                v8::NewStringType::kNormal).ToLocalChecked(),
+        name, true, true);
+    while (v8::platform::PumpMessageLoop(platform, context->GetIsolate()))
+      continue;
   }
   fprintf(stderr, "\n");
 }
 
 
 // Executes a string within the current v8 context.
-bool ExecuteString(v8::Isolate* isolate,
-                   v8::Handle<v8::String> source,
-                   v8::Handle<v8::Value> name,
-                   bool print_result,
+bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source,
+                   v8::Local<v8::Value> name, bool print_result,
                    bool report_exceptions) {
   v8::HandleScope handle_scope(isolate);
-  v8::TryCatch try_catch;
+  v8::TryCatch try_catch(isolate);
   v8::ScriptOrigin origin(name);
-  v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin);
-  if (script.IsEmpty()) {
+  v8::Local<v8::Context> context(isolate->GetCurrentContext());
+  v8::Local<v8::Script> script;
+  if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) {
     // Print errors that happened during compilation.
     if (report_exceptions)
       ReportException(isolate, &try_catch);
     return false;
   } else {
-    v8::Handle<v8::Value> result = script->Run();
-    if (result.IsEmpty()) {
+    v8::Local<v8::Value> result;
+    if (!script->Run(context).ToLocal(&result)) {
       assert(try_catch.HasCaught());
       // Print errors that happened during execution.
       if (report_exceptions)
@@ -355,7 +385,7 @@
   v8::HandleScope handle_scope(isolate);
   v8::String::Utf8Value exception(try_catch->Exception());
   const char* exception_string = ToCString(exception);
-  v8::Handle<v8::Message> message = try_catch->Message();
+  v8::Local<v8::Message> message = try_catch->Message();
   if (message.IsEmpty()) {
     // V8 didn't provide any extra information about this error; just
     // print the exception.
@@ -363,25 +393,30 @@
   } else {
     // Print (filename):(line number): (message).
     v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
+    v8::Local<v8::Context> context(isolate->GetCurrentContext());
     const char* filename_string = ToCString(filename);
-    int linenum = message->GetLineNumber();
+    int linenum = message->GetLineNumber(context).FromJust();
     fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
     // Print line of source code.
-    v8::String::Utf8Value sourceline(message->GetSourceLine());
+    v8::String::Utf8Value sourceline(
+        message->GetSourceLine(context).ToLocalChecked());
     const char* sourceline_string = ToCString(sourceline);
     fprintf(stderr, "%s\n", sourceline_string);
     // Print wavy underline (GetUnderline is deprecated).
-    int start = message->GetStartColumn();
+    int start = message->GetStartColumn(context).FromJust();
     for (int i = 0; i < start; i++) {
       fprintf(stderr, " ");
     }
-    int end = message->GetEndColumn();
+    int end = message->GetEndColumn(context).FromJust();
     for (int i = start; i < end; i++) {
       fprintf(stderr, "^");
     }
     fprintf(stderr, "\n");
-    v8::String::Utf8Value stack_trace(try_catch->StackTrace());
-    if (stack_trace.length() > 0) {
+    v8::Local<v8::Value> stack_trace_string;
+    if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
+        stack_trace_string->IsString() &&
+        v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) {
+      v8::String::Utf8Value stack_trace(stack_trace_string);
       const char* stack_trace_string = ToCString(stack_trace);
       fprintf(stderr, "%s\n", stack_trace_string);
     }