Remove prototypes from v8 functions that aren't constructors

BUG=chromium:625823
R=haraken@chromium.org,thestig@chromium.org

Review-Url: https://codereview.chromium.org/2123153002
diff --git a/fpdfsdk/jsapi/fxjs_v8.cpp b/fpdfsdk/jsapi/fxjs_v8.cpp
index 283fac9..3297b4b 100644
--- a/fpdfsdk/jsapi/fxjs_v8.cpp
+++ b/fpdfsdk/jsapi/fxjs_v8.cpp
@@ -243,13 +243,14 @@
   CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
   CFXJS_ObjDefinition* pObjDef =
       CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID);
+  v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
+      pIsolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature());
+  fun->RemovePrototype();
   pObjDef->GetInstanceTemplate()->Set(
       v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(),
                               v8::NewStringType::kNormal)
           .ToLocalChecked(),
-      v8::FunctionTemplate::New(pIsolate, pMethodCall, v8::Local<v8::Value>(),
-                                pObjDef->GetSignature()),
-      v8::ReadOnly);
+      fun, v8::ReadOnly);
 }
 
 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
@@ -301,11 +302,14 @@
   v8::Isolate::Scope isolate_scope(pIsolate);
   v8::HandleScope handle_scope(pIsolate);
   CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
-  GetGlobalObjectTemplate(pIsolate)
-      ->Set(v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(),
-                                    v8::NewStringType::kNormal)
-                .ToLocalChecked(),
-            v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly);
+  v8::Local<v8::FunctionTemplate> fun =
+      v8::FunctionTemplate::New(pIsolate, pMethodCall);
+  fun->RemovePrototype();
+  GetGlobalObjectTemplate(pIsolate)->Set(
+      v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(),
+                              v8::NewStringType::kNormal)
+          .ToLocalChecked(),
+      fun, v8::ReadOnly);
 }
 
 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
@@ -314,11 +318,14 @@
   v8::Isolate::Scope isolate_scope(pIsolate);
   v8::HandleScope handle_scope(pIsolate);
   CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode();
-  GetGlobalObjectTemplate(pIsolate)
-      ->SetAccessorProperty(v8::String::NewFromUtf8(pIsolate, bsConst.c_str(),
-                                                    v8::NewStringType::kNormal)
-                                .ToLocalChecked(),
-                            v8::FunctionTemplate::New(pIsolate, pConstGetter));
+  v8::Local<v8::FunctionTemplate> fun =
+      v8::FunctionTemplate::New(pIsolate, pConstGetter);
+  fun->RemovePrototype();
+  GetGlobalObjectTemplate(pIsolate)->SetAccessorProperty(
+      v8::String::NewFromUtf8(pIsolate, bsConst.c_str(),
+                              v8::NewStringType::kNormal)
+          .ToLocalChecked(),
+      fun);
 }
 
 void FXJS_InitializeRuntime(
diff --git a/fxjse/class.cpp b/fxjse/class.cpp
index 1dba7f0..a4ed091 100644
--- a/fxjse/class.cpp
+++ b/fxjse/class.cpp
@@ -104,6 +104,9 @@
 
 static void FXJSE_V8ConstructorCallback_Wrapper(
     const v8::FunctionCallbackInfo<v8::Value>& info) {
+  if (!info.IsConstructCall()) {
+    return;
+  }
   const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition =
       static_cast<FXJSE_CLASS_DESCRIPTOR*>(
           info.Data().As<v8::External>()->Value());
@@ -228,13 +231,14 @@
   }
   if (lpClassDefinition->methNum) {
     for (int32_t i = 0; i < lpClassDefinition->methNum; i++) {
+      v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
+          pIsolate, FXJSE_V8FunctionCallback_Wrapper,
+          v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
+                                          lpClassDefinition->methods + i)));
+      fun->RemovePrototype();
       hObjectTemplate->Set(
           v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name),
-          v8::FunctionTemplate::New(
-              pIsolate, FXJSE_V8FunctionCallback_Wrapper,
-              v8::External::New(pIsolate,
-                                const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
-                                    lpClassDefinition->methods + i))),
+          fun,
           static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
     }
   }
@@ -260,12 +264,12 @@
     }
   }
   if (bIsJSGlobal) {
-    hObjectTemplate->Set(
-        v8::String::NewFromUtf8(pIsolate, "toString"),
-        v8::FunctionTemplate::New(
-            pIsolate, FXJSE_Context_GlobalObjToString,
-            v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(
-                                            lpClassDefinition))));
+    v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
+        pIsolate, FXJSE_Context_GlobalObjToString,
+        v8::External::New(
+            pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)));
+    fun->RemovePrototype();
+    hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun);
   }
   pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate);
   lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass));