Version 12.3.195.1 (cherry-pick)

Merged 2e0e30ed641858f391df5ce63f8a85759f338123

Revert "[handles] Process non-nestable tasks with trivial stack in samples/shell"

Change-Id: Ic79bc6884799c13da77510615befc7aa763f2089
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5296754
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Owners-Override: Matthias Liedtke <mliedtke@chromium.org>
Cr-Commit-Position: refs/heads/12.3.195@{#2}
Cr-Branched-From: 50d0ce8de0e0470f5d598cf7429bbf5aa96d2a07-refs/heads/main@{#92311}
diff --git a/include/v8-version.h b/include/v8-version.h
index a4b7885..5490ebc 100644
--- a/include/v8-version.h
+++ b/include/v8-version.h
@@ -11,7 +11,7 @@
 #define V8_MAJOR_VERSION 12
 #define V8_MINOR_VERSION 3
 #define V8_BUILD_NUMBER 195
-#define V8_PATCH_LEVEL 0
+#define V8_PATCH_LEVEL 1
 
 // Use 1 for candidates and 0 otherwise.
 // (Boolean macro values are not supported by all preprocessors.)
diff --git a/samples/shell.cc b/samples/shell.cc
index a92bc2b..835dfcb 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -48,11 +48,11 @@
  * For a more sophisticated shell, consider using the debug shell D8.
  */
 
-v8::Global<v8::Context> CreateShellContext(v8::Isolate* isolate);
-void RunShell(v8::Isolate* isolate, const v8::Global<v8::Context>& context,
-              v8::Platform* platform);
-int RunMain(v8::Isolate* isolate, const v8::Global<v8::Context>& context,
-            v8::Platform* platform, int argc, char* argv[]);
+
+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);
@@ -64,8 +64,10 @@
 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
 
+
 static bool run_shell;
 
+
 int main(int argc, char* argv[]) {
   v8::V8::InitializeICUDefaultLocation(argv[0]);
   v8::V8::InitializeExternalStartupData(argv[0]);
@@ -80,13 +82,16 @@
   run_shell = (argc == 1);
   int result;
   {
-    v8::Global<v8::Context> context = CreateShellContext(isolate);
+    v8::Isolate::Scope isolate_scope(isolate);
+    v8::HandleScope handle_scope(isolate);
+    v8::Local<v8::Context> context = CreateShellContext(isolate);
     if (context.IsEmpty()) {
       fprintf(stderr, "Error creating context\n");
       return 1;
     }
-    result = RunMain(isolate, context, platform.get(), argc, argv);
-    if (run_shell) RunShell(isolate, context, platform.get());
+    v8::Context::Scope context_scope(context);
+    result = RunMain(isolate, platform.get(), argc, argv);
+    if (run_shell) RunShell(context, platform.get());
   }
   isolate->Dispose();
   v8::V8::Dispose();
@@ -95,16 +100,16 @@
   return result;
 }
 
+
 // Extracts a C string from a V8 Utf8Value.
 const char* ToCString(const v8::String::Utf8Value& value) {
   return *value ? *value : "<string conversion failed>";
 }
 
+
 // Creates a new execution environment containing the built-in
 // functions.
-v8::Global<v8::Context> CreateShellContext(v8::Isolate* isolate) {
-  v8::Isolate::Scope isolate_scope(isolate);
-  v8::HandleScope handle_scope(isolate);
+v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
   // Create a template for the global object.
   v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
   // Bind the global 'print' function to the C++ Print callback.
@@ -117,11 +122,10 @@
   global->Set(isolate, "quit", v8::FunctionTemplate::New(isolate, Quit));
   // Bind the 'version' function
   global->Set(isolate, "version", v8::FunctionTemplate::New(isolate, Version));
-  // Return the context.
-  v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global);
-  return v8::Global<v8::Context>(isolate, context);
+  return v8::Context::New(isolate, NULL, global);
 }
 
+
 // The callback that is invoked by v8 whenever the JavaScript 'print'
 // function is called.  Prints its arguments on stdout separated by
 // spaces and ending with a newline.
@@ -151,7 +155,7 @@
     return;
   }
   v8::String::Utf8Value file(info.GetIsolate(), info[0]);
-  if (*file == nullptr) {
+  if (*file == NULL) {
     info.GetIsolate()->ThrowError("Error loading file");
     return;
   }
@@ -171,7 +175,7 @@
   for (int i = 0; i < info.Length(); i++) {
     v8::HandleScope handle_scope(info.GetIsolate());
     v8::String::Utf8Value file(info.GetIsolate(), info[i]);
-    if (*file == nullptr) {
+    if (*file == NULL) {
       info.GetIsolate()->ThrowError("Error loading file");
       return;
     }
@@ -199,8 +203,6 @@
   exit(exit_code);
 }
 
-// The callback that is invoked by v8 whenever the JavaScript 'version'
-// function is called.  Returns a string containing the current V8 version.
 void Version(const v8::FunctionCallbackInfo<v8::Value>& info) {
   info.GetReturnValue().Set(
       v8::String::NewFromUtf8(info.GetIsolate(), v8::V8::GetVersion())
@@ -210,7 +212,7 @@
 // Reads a file into a v8 string.
 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
   FILE* file = fopen(name, "rb");
-  if (file == nullptr) return {};
+  if (file == NULL) return v8::MaybeLocal<v8::String>();
 
   fseek(file, 0, SEEK_END);
   size_t size = ftell(file);
@@ -222,7 +224,7 @@
     i += fread(&chars[i], 1, size - i, file);
     if (ferror(file)) {
       fclose(file);
-      return {};
+      return v8::MaybeLocal<v8::String>();
     }
   }
   fclose(file);
@@ -232,9 +234,10 @@
   return result;
 }
 
+
 // Process remaining command line arguments and execute files
-int RunMain(v8::Isolate* isolate, const v8::Global<v8::Context>& context,
-            v8::Platform* platform, 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) {
@@ -248,41 +251,25 @@
               "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.
-      bool success;
-      {
-        // Enter the execution environment before evaluating any code.
-        v8::HandleScope handle_scope(isolate);
-        v8::Context::Scope context_scope(context.Get(isolate));
-        v8::Local<v8::String> file_name =
-            v8::String::NewFromUtf8Literal(isolate, "unnamed");
-        v8::Local<v8::String> source;
-        if (!v8::String::NewFromUtf8(isolate, argv[++i]).ToLocal(&source)) {
-          return 1;
-        }
-        success = ExecuteString(isolate, source, file_name, false, true);
+      v8::Local<v8::String> file_name =
+          v8::String::NewFromUtf8Literal(isolate, "unnamed");
+      v8::Local<v8::String> source;
+      if (!v8::String::NewFromUtf8(isolate, argv[++i]).ToLocal(&source)) {
+        return 1;
       }
-      // It is important not to pump the message loop when there are v8::Local
-      // handles on the stack, as this may trigger a stackless GC.
+      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.
-      bool success;
-      {
-        // Enter the execution environment before evaluating any code.
-        v8::HandleScope handle_scope(isolate);
-        v8::Context::Scope context_scope(context.Get(isolate));
-        v8::Local<v8::String> file_name =
-            v8::String::NewFromUtf8(isolate, str).ToLocalChecked();
-        v8::Local<v8::String> source;
-        if (!ReadFile(isolate, str).ToLocal(&source)) {
-          fprintf(stderr, "Error reading '%s'\n", str);
-          continue;
-        }
-        success = ExecuteString(isolate, source, file_name, false, true);
+      v8::Local<v8::String> file_name =
+          v8::String::NewFromUtf8(isolate, str).ToLocalChecked();
+      v8::Local<v8::String> source;
+      if (!ReadFile(isolate, str).ToLocal(&source)) {
+        fprintf(stderr, "Error reading '%s'\n", str);
+        continue;
       }
-      // It is important not to pump the message loop when there are v8::Local
-      // handles on the stack, as this may trigger a stackless GC.
+      bool success = ExecuteString(isolate, source, file_name, false, true);
       while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
       if (!success) return 1;
     }
@@ -290,33 +277,32 @@
   return 0;
 }
 
+
 // The read-eval-execute loop of the shell.
-void RunShell(v8::Isolate* isolate, const v8::Global<v8::Context>& context,
-              v8::Platform* platform) {
+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::NewFromUtf8Literal(context->GetIsolate(), "(shell)"));
   while (true) {
     char buffer[kBufferSize];
     fprintf(stderr, "> ");
     char* str = fgets(buffer, kBufferSize, stdin);
-    if (str == nullptr) break;
-    {
-      // Enter the execution environment before evaluating any code.
-      v8::HandleScope handle_scope(isolate);
-      v8::Context::Scope context_scope(context.Get(isolate));
-      v8::Local<v8::String> name(
-          v8::String::NewFromUtf8Literal(isolate, "(shell)"));
-      ExecuteString(isolate,
-                    v8::String::NewFromUtf8(isolate, str).ToLocalChecked(),
-                    name, true, true);
-    }
-    // It is important not to pump the message loop when there are v8::Local
-    // handles on the stack, as this may trigger a stackless GC.
-    while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
+    if (str == NULL) break;
+    v8::HandleScope handle_scope(context->GetIsolate());
+    ExecuteString(
+        context->GetIsolate(),
+        v8::String::NewFromUtf8(context->GetIsolate(), str).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::Local<v8::String> source,
                    v8::Local<v8::Value> name, bool print_result,
@@ -353,6 +339,7 @@
   }
 }
 
+
 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
   v8::HandleScope handle_scope(isolate);
   v8::String::Utf8Value exception(isolate, try_catch->Exception());