From fcae9ae023d0b57e38aefacacb7be5c76283d7c0 Mon Sep 17 00:00:00 2001 From: Jochen Eisinger Date: Thu, 7 Jul 2016 17:17:25 +0200 Subject: [PATCH] deps: Add a way to create a function without a prototype This is an non-ABI breaking version port of https://chromium.googlesource.com/v8/v8/+/46428e45e9982a7490685ff1af6ffe680096c1a4 which adds an API that will be used by the inspector code. --- deps/v8/include/v8.h | 9 +++++++++ deps/v8/src/api.cc | 30 ++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/deps/v8/include/v8.h b/deps/v8/include/v8.h index 2c2399543938ef..8b7b7c2cc48c3b 100644 --- a/deps/v8/include/v8.h +++ b/deps/v8/include/v8.h @@ -3241,6 +3241,7 @@ class PropertyCallbackInfo { typedef void (*FunctionCallback)(const FunctionCallbackInfo& info); +enum class ConstructorBehavior { kThrow, kAllow }; /** * A JavaScript function object (ECMA-262, 15.3). @@ -3255,6 +3256,11 @@ class V8_EXPORT Function : public Object { FunctionCallback callback, Local data = Local(), int length = 0); + static MaybeLocal New(Local context, + FunctionCallback callback, + Local data, + int length, + ConstructorBehavior behavior); static V8_DEPRECATE_SOON( "Use maybe version", Local New(Isolate* isolate, FunctionCallback callback, @@ -4478,6 +4484,9 @@ class V8_EXPORT FunctionTemplate : public Template { Isolate* isolate, FunctionCallback callback = 0, Local data = Local(), Local signature = Local(), int length = 0); + static Local New( + Isolate* isolate, FunctionCallback callback, Local data, + Local signature, int length, ConstructorBehavior behavior); /** * Creates a function template with a fast handler. If a fast handler is set, diff --git a/deps/v8/src/api.cc b/deps/v8/src/api.cc index f0045cdb96327f..bf351548430a21 100644 --- a/deps/v8/src/api.cc +++ b/deps/v8/src/api.cc @@ -1153,14 +1153,26 @@ Local FunctionTemplate::New(Isolate* isolate, v8::Local data, v8::Local signature, int length) { + return New( + isolate, callback, data, signature, length, ConstructorBehavior::kAllow); +} + +Local FunctionTemplate::New(Isolate* isolate, + FunctionCallback callback, + v8::Local data, + v8::Local signature, + int length, + ConstructorBehavior behavior) { i::Isolate* i_isolate = reinterpret_cast(isolate); // Changes to the environment cannot be captured in the snapshot. Expect no // function templates when the isolate is created for serialization. DCHECK(!i_isolate->serializer_enabled()); LOG_API(i_isolate, "FunctionTemplate::New"); ENTER_V8(i_isolate); - return FunctionTemplateNew(i_isolate, callback, nullptr, data, signature, - length, false); + auto tmpl = FunctionTemplateNew(i_isolate, callback, nullptr, data, signature, + length, false); + if (behavior == ConstructorBehavior::kThrow) tmpl->RemovePrototype(); + return tmpl; } @@ -4449,15 +4461,21 @@ Local Object::CallAsConstructor(int argc, MaybeLocal Function::New(Local context, FunctionCallback callback, Local data, int length) { + return New(context, callback, data, length, ConstructorBehavior::kAllow); +} + +MaybeLocal Function::New(Local context, + FunctionCallback callback, Local data, + int length, ConstructorBehavior behavior) { i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate(); LOG_API(isolate, "Function::New"); ENTER_V8(isolate); - return FunctionTemplateNew(isolate, callback, nullptr, data, - Local(), length, true) - ->GetFunction(context); + auto tmpl = FunctionTemplateNew(isolate, callback, nullptr, data, + Local(), length, true); + if (behavior == ConstructorBehavior::kThrow) tmpl->RemovePrototype(); + return tmpl->GetFunction(context); } - Local Function::New(Isolate* v8_isolate, FunctionCallback callback, Local data, int length) { return Function::New(v8_isolate->GetCurrentContext(), callback, data, length)