Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: Add a way to create a function without a prototype #7586

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions deps/v8/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,7 @@ class PropertyCallbackInfo {

typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);

enum class ConstructorBehavior { kThrow, kAllow };

/**
* A JavaScript function object (ECMA-262, 15.3).
Expand All @@ -3255,6 +3256,11 @@ class V8_EXPORT Function : public Object {
FunctionCallback callback,
Local<Value> data = Local<Value>(),
int length = 0);
static MaybeLocal<Function> New(Local<Context> context,
FunctionCallback callback,
Local<Value> data,
int length,
ConstructorBehavior behavior);
static V8_DEPRECATE_SOON(
"Use maybe version",
Local<Function> New(Isolate* isolate, FunctionCallback callback,
Expand Down Expand Up @@ -4478,6 +4484,9 @@ class V8_EXPORT FunctionTemplate : public Template {
Isolate* isolate, FunctionCallback callback = 0,
Local<Value> data = Local<Value>(),
Local<Signature> signature = Local<Signature>(), int length = 0);
static Local<FunctionTemplate> New(
Isolate* isolate, FunctionCallback callback, Local<Value> data,
Local<Signature> signature, int length, ConstructorBehavior behavior);

/**
* Creates a function template with a fast handler. If a fast handler is set,
Expand Down
30 changes: 24 additions & 6 deletions deps/v8/src/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1153,14 +1153,26 @@ Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate,
v8::Local<Value> data,
v8::Local<Signature> signature,
int length) {
return New(
isolate, callback, data, signature, length, ConstructorBehavior::kAllow);
}

Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate,
FunctionCallback callback,
v8::Local<Value> data,
v8::Local<Signature> signature,
int length,
ConstructorBehavior behavior) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(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;
}


Expand Down Expand Up @@ -4449,15 +4461,21 @@ Local<v8::Value> Object::CallAsConstructor(int argc,
MaybeLocal<Function> Function::New(Local<Context> context,
FunctionCallback callback, Local<Value> data,
int length) {
return New(context, callback, data, length, ConstructorBehavior::kAllow);
}

MaybeLocal<Function> Function::New(Local<Context> context,
FunctionCallback callback, Local<Value> 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<Signature>(), length, true)
->GetFunction(context);
auto tmpl = FunctionTemplateNew(isolate, callback, nullptr, data,
Local<Signature>(), length, true);
if (behavior == ConstructorBehavior::kThrow) tmpl->RemovePrototype();
return tmpl->GetFunction(context);
}


Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
Local<Value> data, int length) {
return Function::New(v8_isolate->GetCurrentContext(), callback, data, length)
Expand Down