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

Don't create external data if sebind isn't used. #15756

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 54 additions & 7 deletions native/cocos/bindings/jswrapper/v8/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ bool Class::init(const ccstd::string &clsName, Object *parent, Object *parentPro

v8::FunctionCallback ctorToSet = _constructor != nullptr ? _constructor : invalidConstructor;

_constructorTemplate.Reset(__isolate, v8::FunctionTemplate::New(__isolate, ctorToSet, createExternal(__isolate, data)));
v8::Local<v8::Value> externalData = v8::Local<v8::Value>();
if (data != nullptr) {
externalData = createExternal(__isolate, data);
}

_constructorTemplate.Reset(__isolate, v8::FunctionTemplate::New(__isolate, ctorToSet, externalData));
v8::MaybeLocal<v8::String> jsNameVal = v8::String::NewFromUtf8(__isolate, _name.c_str(), v8::NewStringType::kNormal);
if (jsNameVal.IsEmpty()) {
return false;
Expand Down Expand Up @@ -206,7 +211,13 @@ bool Class::defineFunction(const char *name, v8::FunctionCallback func, void *da
if (jsName.IsEmpty()) {
return false;
}
_constructorTemplate.Get(__isolate)->PrototypeTemplate()->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func, createExternal(__isolate, data)));

v8::Local<v8::Value> externalData = v8::Local<v8::Value>();
if (data != nullptr) {
externalData = createExternal(__isolate, data);
}

_constructorTemplate.Get(__isolate)->PrototypeTemplate()->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func, externalData));
return true;
}

Expand All @@ -217,8 +228,24 @@ bool Class::defineProperty(const char *name, v8::FunctionCallback getter, v8::Fu
}

auto prototypeTemplate = _constructorTemplate.Get(__isolate)->PrototypeTemplate();
auto externalData = createExternal(__isolate, data);
prototypeTemplate->SetAccessorProperty(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, getter, externalData), v8::FunctionTemplate::New(__isolate, setter, externalData));

v8::Local<v8::Value> externalData = v8::Local<v8::Value>();
if (data != nullptr) {
externalData = createExternal(__isolate, data);
}

v8::Local<v8::FunctionTemplate> localGetter = v8::Local<v8::FunctionTemplate>();
v8::Local<v8::FunctionTemplate> localSetter = v8::Local<v8::FunctionTemplate>();

if (getter != nullptr) {
localGetter = v8::FunctionTemplate::New(__isolate, getter, externalData);
}

if (setter != nullptr) {
localSetter = v8::FunctionTemplate::New(__isolate, setter, externalData);
}

prototypeTemplate->SetAccessorProperty(jsName.ToLocalChecked(), localGetter, localSetter);
return true;
}

Expand All @@ -235,7 +262,12 @@ bool Class::defineStaticFunction(const char *name, v8::FunctionCallback func, vo
if (jsName.IsEmpty()) {
return false;
}
_constructorTemplate.Get(__isolate)->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func, createExternal(__isolate, data)));

v8::Local<v8::Value> externalData = v8::Local<v8::Value>();
if (data != nullptr) {
externalData = createExternal(__isolate, data);
}
_constructorTemplate.Get(__isolate)->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func, externalData));
return true;
}

Expand All @@ -245,8 +277,23 @@ bool Class::defineStaticProperty(const char *name, v8::FunctionCallback getter,
return false;
}

auto externalData = createExternal(__isolate, data);
_constructorTemplate.Get(__isolate)->SetAccessorProperty(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, getter, externalData), v8::FunctionTemplate::New(__isolate, setter, externalData));
v8::Local<v8::Value> externalData = v8::Local<v8::Value>();
if (data != nullptr) {
externalData = createExternal(__isolate, data);
}

v8::Local<v8::FunctionTemplate> localGetter = v8::Local<v8::FunctionTemplate>();
v8::Local<v8::FunctionTemplate> localSetter = v8::Local<v8::FunctionTemplate>();

if (getter != nullptr) {
localGetter = v8::FunctionTemplate::New(__isolate, getter, externalData);
}

if (setter != nullptr) {
localSetter = v8::FunctionTemplate::New(__isolate, setter, externalData);
}

_constructorTemplate.Get(__isolate)->SetAccessorProperty(jsName.ToLocalChecked(), localGetter, localSetter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are duplicate codes, can wrap them into a function?

return true;
}

Expand Down