From df54db77625e0e51b1d016fb8a1f89f7458a0584 Mon Sep 17 00:00:00 2001
From: Ryan Dahl <ry@tinyclouds.org>
Date: Fri, 1 Feb 2019 23:26:33 -0500
Subject: [PATCH] Fix v8_str internalize bug

---
 libdeno/binding.cc      | 11 +++++------
 libdeno/internal.h      |  6 ++----
 libdeno/libdeno_test.cc |  9 +++++++++
 libdeno/modules.cc      |  2 +-
 4 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/libdeno/binding.cc b/libdeno/binding.cc
index 6c636774a9..d7fab65193 100644
--- a/libdeno/binding.cc
+++ b/libdeno/binding.cc
@@ -262,8 +262,8 @@ deno_mod DenoIsolate::RegisterModule(const char* name, const char* source) {
   auto context = context_.Get(isolate_);
   v8::Context::Scope context_scope(context);
 
-  v8::Local<v8::String> name_str = v8_str(name, true);
-  v8::Local<v8::String> source_str = v8_str(source, true);
+  v8::Local<v8::String> name_str = v8_str(name);
+  v8::Local<v8::String> source_str = v8_str(source);
 
   auto origin = ModuleOrigin(isolate_, name_str);
   v8::ScriptCompiler::Source source_(source_str, origin);
@@ -341,8 +341,8 @@ bool Execute(v8::Local<v8::Context> context, const char* js_filename,
   v8::HandleScope handle_scope(isolate);
   v8::Context::Scope context_scope(context);
 
-  auto source = v8_str(js_source, true);
-  auto name = v8_str(js_filename, true);
+  auto source = v8_str(js_source);
+  auto name = v8_str(js_filename);
 
   v8::TryCatch try_catch(isolate);
 
@@ -423,8 +423,7 @@ void HostInitializeImportMetaObjectCallback(v8::Local<v8::Context> context,
 
   const char* url = info->name.c_str();
 
-  meta->CreateDataProperty(context, v8_str("url"), v8_str(url, true))
-      .ToChecked();
+  meta->CreateDataProperty(context, v8_str("url"), v8_str(url)).ToChecked();
 }
 
 void DenoIsolate::AddIsolate(v8::Isolate* isolate) {
diff --git a/libdeno/internal.h b/libdeno/internal.h
index 022fd8f1e9..14a9cd43f5 100644
--- a/libdeno/internal.h
+++ b/libdeno/internal.h
@@ -123,11 +123,9 @@ struct InternalFieldData {
   uint32_t data;
 };
 
-static inline v8::Local<v8::String> v8_str(const char* x,
-                                           bool internalize = false) {
+static inline v8::Local<v8::String> v8_str(const char* x) {
   return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
-                                 internalize ? v8::NewStringType::kInternalized
-                                             : v8::NewStringType::kNormal)
+                                 v8::NewStringType::kNormal)
       .ToLocalChecked();
 }
 
diff --git a/libdeno/libdeno_test.cc b/libdeno/libdeno_test.cc
index 282a124ba9..b14b4758bf 100644
--- a/libdeno/libdeno_test.cc
+++ b/libdeno/libdeno_test.cc
@@ -280,3 +280,12 @@ TEST(LibDenoTest, Shared) {
   EXPECT_EQ(s[2], 44);
   deno_delete(d);
 }
+
+TEST(LibDenoTest, Utf8Bug) {
+  Deno* d = deno_new(deno_config{0, empty, empty, nullptr});
+  // The following is a valid UTF-8 javascript which just defines a string
+  // literal. We had a bug where libdeno would choke on this.
+  deno_execute(d, nullptr, "a.js", "x = \"\xEF\xBF\xBD\"");
+  EXPECT_EQ(nullptr, deno_last_exception(d));
+  deno_delete(d);
+}
diff --git a/libdeno/modules.cc b/libdeno/modules.cc
index 961686acaa..851ade8c8f 100644
--- a/libdeno/modules.cc
+++ b/libdeno/modules.cc
@@ -102,7 +102,7 @@ v8::MaybeLocal<v8::Module> ResolveCallback(Local<Context> context,
         char buf[64 * 1024];
         snprintf(buf, sizeof(buf), "Cannot resolve module \"%s\" from \"%s\"",
                  req_str.c_str(), referrer_info->name.c_str());
-        isolate->ThrowException(deno::v8_str(buf, true));
+        isolate->ThrowException(deno::v8_str(buf));
         break;
       } else {
         Local<Module> child_mod = info->handle.Get(isolate);