Commit 46428e45 authored by jochen's avatar jochen Committed by Commit bot

Make it possible to create a v8::Function directly w/o a prototype

BUG=chromium:625823
R=verwaest@chromium.org

Review-Url: https://codereview.chromium.org/2123143002
Cr-Commit-Position: refs/heads/master@{#37549}
parent a5fa2984
...@@ -3259,6 +3259,7 @@ class PropertyCallbackInfo { ...@@ -3259,6 +3259,7 @@ class PropertyCallbackInfo {
typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info); typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
enum class ConstructorBehavior { kThrow, kAllow };
/** /**
* A JavaScript function object (ECMA-262, 15.3). * A JavaScript function object (ECMA-262, 15.3).
...@@ -3269,10 +3270,10 @@ class V8_EXPORT Function : public Object { ...@@ -3269,10 +3270,10 @@ class V8_EXPORT Function : public Object {
* Create a function in the current execution context * Create a function in the current execution context
* for a given FunctionCallback. * for a given FunctionCallback.
*/ */
static MaybeLocal<Function> New(Local<Context> context, static MaybeLocal<Function> New(
FunctionCallback callback, Local<Context> context, FunctionCallback callback,
Local<Value> data = Local<Value>(), Local<Value> data = Local<Value>(), int length = 0,
int length = 0); ConstructorBehavior behavior = ConstructorBehavior::kAllow);
static V8_DEPRECATE_SOON( static V8_DEPRECATE_SOON(
"Use maybe version", "Use maybe version",
Local<Function> New(Isolate* isolate, FunctionCallback callback, Local<Function> New(Isolate* isolate, FunctionCallback callback,
...@@ -4493,7 +4494,8 @@ class V8_EXPORT FunctionTemplate : public Template { ...@@ -4493,7 +4494,8 @@ class V8_EXPORT FunctionTemplate : public Template {
static Local<FunctionTemplate> New( static Local<FunctionTemplate> New(
Isolate* isolate, FunctionCallback callback = 0, Isolate* isolate, FunctionCallback callback = 0,
Local<Value> data = Local<Value>(), Local<Value> data = Local<Value>(),
Local<Signature> signature = Local<Signature>(), int length = 0); Local<Signature> signature = Local<Signature>(), int length = 0,
ConstructorBehavior behavior = ConstructorBehavior::kAllow);
/** Get a template included in the snapshot by index. */ /** Get a template included in the snapshot by index. */
static MaybeLocal<FunctionTemplate> FromSnapshot(Isolate* isolate, static MaybeLocal<FunctionTemplate> FromSnapshot(Isolate* isolate,
......
...@@ -1186,19 +1186,18 @@ static Local<FunctionTemplate> FunctionTemplateNew( ...@@ -1186,19 +1186,18 @@ static Local<FunctionTemplate> FunctionTemplateNew(
return Utils::ToLocal(obj); return Utils::ToLocal(obj);
} }
Local<FunctionTemplate> FunctionTemplate::New(
Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate, Isolate* isolate, FunctionCallback callback, v8::Local<Value> data,
FunctionCallback callback, v8::Local<Signature> signature, int length, ConstructorBehavior behavior) {
v8::Local<Value> data,
v8::Local<Signature> signature,
int length) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
// Changes to the environment cannot be captured in the snapshot. Expect no // Changes to the environment cannot be captured in the snapshot. Expect no
// function templates when the isolate is created for serialization. // function templates when the isolate is created for serialization.
LOG_API(i_isolate, FunctionTemplate, New); LOG_API(i_isolate, FunctionTemplate, New);
ENTER_V8(i_isolate); ENTER_V8(i_isolate);
return FunctionTemplateNew(i_isolate, callback, nullptr, data, signature, auto templ = FunctionTemplateNew(i_isolate, callback, nullptr, data,
length, false); signature, length, false);
if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
return templ;
} }
MaybeLocal<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate, MaybeLocal<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate,
...@@ -4497,22 +4496,23 @@ Local<v8::Value> Object::CallAsConstructor(int argc, ...@@ -4497,22 +4496,23 @@ Local<v8::Value> Object::CallAsConstructor(int argc,
RETURN_TO_LOCAL_UNCHECKED(CallAsConstructor(context, argc, argv_cast), Value); RETURN_TO_LOCAL_UNCHECKED(CallAsConstructor(context, argc, argv_cast), Value);
} }
MaybeLocal<Function> Function::New(Local<Context> context, MaybeLocal<Function> Function::New(Local<Context> context,
FunctionCallback callback, Local<Value> data, FunctionCallback callback, Local<Value> data,
int length) { int length, ConstructorBehavior behavior) {
i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate(); i::Isolate* isolate = Utils::OpenHandle(*context)->GetIsolate();
LOG_API(isolate, Function, New); LOG_API(isolate, Function, New);
ENTER_V8(isolate); ENTER_V8(isolate);
return FunctionTemplateNew(isolate, callback, nullptr, data, auto templ = FunctionTemplateNew(isolate, callback, nullptr, data,
Local<Signature>(), length, true) Local<Signature>(), length, true);
->GetFunction(context); if (behavior == ConstructorBehavior::kThrow) templ->RemovePrototype();
return templ->GetFunction(context);
} }
Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback, Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
Local<Value> data, int length) { Local<Value> data, int length) {
return Function::New(v8_isolate->GetCurrentContext(), callback, data, length) return Function::New(v8_isolate->GetCurrentContext(), callback, data, length,
ConstructorBehavior::kAllow)
.FromMaybe(Local<Function>()); .FromMaybe(Local<Function>());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment