Skip to content

Commit 16d6e87

Browse files
author
Mihail Slavchev
committed
Refactor Java packages/namespaces to use object accessors instead of proxies. This simplifies the code and makes it compatible with V8 5.x where info.This() has changed behavior for proxy callbacks.
1 parent 4d652cd commit 16d6e87

2 files changed

Lines changed: 37 additions & 48 deletions

File tree

runtime/src/main/jni/MetadataNode.cpp

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Local<Object> MetadataNode::CreateWrapper(Isolate *isolate)
150150
}
151151
else if (s_metadataReader.IsNodeTypePackage(nodeType))
152152
{
153-
obj = CreatePackageProxy(isolate);
153+
obj = CreatePackageObject(isolate);
154154
}
155155
else
156156
{
@@ -228,17 +228,23 @@ Local<Object> MetadataNode::CreateArrayWrapper(Isolate *isolate)
228228
return arr;
229229
}
230230

231-
Local<Object> MetadataNode::CreatePackageProxy(Isolate *isolate)
231+
Local<Object> MetadataNode::CreatePackageObject(Isolate *isolate)
232232
{
233-
EscapableHandleScope handleScope(isolate);
234-
235-
auto packageTemplate = ObjectTemplate::New();
236-
packageTemplate->SetNamedPropertyHandler(MetadataNode::PackageGetterCallback);
237-
238-
auto package = packageTemplate->NewInstance();
239-
SetPackageMetadata(isolate, package, this);
233+
auto packageObj = Object::New(isolate);
234+
auto ptrChildren = this->m_treeNode->children;
235+
if (ptrChildren != nullptr) {
236+
auto ctx = isolate->GetCurrentContext();
237+
auto extData = External::New(isolate, this);
238+
const auto &children = *ptrChildren;
239+
for (auto childNode: children) {
240+
packageObj->SetAccessor(ctx, ConvertToV8String(childNode->name),
241+
PackageGetterCallback,
242+
nullptr,
243+
extData);
244+
}
245+
}
240246

241-
return handleScope.Escape(package);
247+
return packageObj;
242248
}
243249

244250
void MetadataNode::SetClassAccessor(Local<Function>& ctorFunction)
@@ -913,22 +919,6 @@ void MetadataNode::SetInstanceMetadata(Isolate *isolate, Local<Object> value, Me
913919
value->SetHiddenValue(key, External::New(isolate, node));
914920
}
915921

916-
MetadataNode* MetadataNode::GetPackageMetadata(Isolate *isolate, const Local<Object>& value)
917-
{
918-
MetadataNode *node = nullptr;
919-
auto ext = value->GetHiddenValue(ConvertToV8String("tns::PackageMetadata"));
920-
if (!ext.IsEmpty())
921-
{
922-
node = reinterpret_cast<MetadataNode*>(ext.As<External>()->Value());
923-
}
924-
return node;
925-
}
926-
927-
void MetadataNode::SetPackageMetadata(Isolate *isolate, Local<Object> value, MetadataNode *node)
928-
{
929-
value->SetHiddenValue(ConvertToV8String("tns::PackageMetadata"), External::New(isolate, node));
930-
}
931-
932922
void MetadataNode::ExtendedClassConstructorCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
933923
{
934924
try
@@ -1290,25 +1280,30 @@ Local<Object> MetadataNode::GetImplementationObject(const Local<Object>& object)
12901280
return implementationObject;
12911281
}
12921282

1293-
void MetadataNode::PackageGetterCallback(Local<String> property, const PropertyCallbackInfo<Value>& info)
1283+
void MetadataNode::PackageGetterCallback(Local<Name> property, const PropertyCallbackInfo<Value>& info)
12941284
{
12951285
try
12961286
{
1297-
string propName = ConvertToString(property);
1287+
if (property.IsEmpty() || !property->IsString()) {
1288+
return;
1289+
}
1290+
1291+
auto strProperty = property.As<String>();
1292+
1293+
string propName = ConvertToString(strProperty);
12981294

12991295
if (propName.empty())
13001296
return;
13011297

1302-
auto isolate = Isolate::GetCurrent();
1303-
HandleScope handleScope(isolate);
1298+
auto isolate = info.GetIsolate();
13041299

13051300
auto thiz = info.This();
13061301

1307-
auto cachedItem = thiz->GetHiddenValue(property);
1302+
auto cachedItem = thiz->GetHiddenValue(strProperty);
13081303

13091304
if (cachedItem.IsEmpty())
13101305
{
1311-
auto node = GetPackageMetadata(isolate, thiz);
1306+
auto node = reinterpret_cast<MetadataNode*>(info.Data().As<External>()->Value());
13121307

13131308
uint8_t nodeType = s_metadataReader.GetNodeType(node->m_treeNode);
13141309

@@ -1321,7 +1316,7 @@ void MetadataNode::PackageGetterCallback(Local<String> property, const PropertyC
13211316
{
13221317
auto childNode = MetadataNode::GetOrCreateInternal(child.treeNode);
13231318
cachedItem = childNode->CreateWrapper(isolate);
1324-
thiz->SetHiddenValue(property, cachedItem);
1319+
thiz->SetHiddenValue(strProperty, cachedItem);
13251320
}
13261321
}
13271322

@@ -1678,15 +1673,13 @@ MetadataEntry MetadataNode::GetChildMetadataForPackage(MetadataNode *node, const
16781673
child.treeNode = treeNodeChild;
16791674

16801675
uint8_t childNodeType = s_metadataReader.GetNodeType(treeNodeChild);
1681-
if (s_metadataReader.IsNodeTypeInterface(childNodeType))
1682-
{
1683-
bool isPrefix;
1684-
string declaringType = s_metadataReader.ReadInterfaceImplementationTypeName(treeNodeChild, isPrefix);
1685-
child.declaringType = isPrefix
1686-
? (declaringType + s_metadataReader.ReadTypeName(child.treeNode))
1687-
:
1688-
declaringType;
1689-
}
1676+
if (s_metadataReader.IsNodeTypeInterface(childNodeType)) {
1677+
bool isPrefix;
1678+
string declaringType = s_metadataReader.ReadInterfaceImplementationTypeName(treeNodeChild, isPrefix);
1679+
child.declaringType = isPrefix
1680+
? (declaringType + s_metadataReader.ReadTypeName(child.treeNode))
1681+
: declaringType;
1682+
}
16901683
}
16911684
}
16921685

runtime/src/main/jni/MetadataNode.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace tns
7676

7777
MetadataNode(MetadataTreeNode *treeNode);
7878

79-
v8::Local<v8::Object> CreatePackageProxy(v8::Isolate *isolate);
79+
v8::Local<v8::Object> CreatePackageObject(v8::Isolate *isolate);
8080

8181
v8::Local<v8::Function> GetConstructorFunction(v8::Isolate *isolate);
8282
v8::Local<v8::FunctionTemplate> GetConstructorFunctionTemplate(v8::Isolate *isolate, MetadataTreeNode *treeNode);
@@ -108,10 +108,6 @@ namespace tns
108108

109109
static void SetTypeMetadata(v8::Isolate *isolate, v8::Local<v8::Function> value, TypeMetadata *data);
110110

111-
static MetadataNode* GetPackageMetadata(v8::Isolate *isolate, const v8::Local<v8::Object>& value);
112-
113-
static void SetPackageMetadata(v8::Isolate *isolate, v8::Local<v8::Object> value, MetadataNode *node);
114-
115111
static std::string CreateFullClassName(const std::string& className, const std::string& extendNameAndLocation);
116112
static void MethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
117113
static void InterfaceConstructorCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
@@ -133,7 +129,7 @@ namespace tns
133129
static void SuperAccessorGetterCallback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
134130
static void ArrayLengthGetterCallack(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
135131

136-
static void PackageGetterCallback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info);
132+
static void PackageGetterCallback(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info);
137133

138134
static void ArrayIndexedPropertyGetterCallback(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info);
139135
static void ArrayIndexedPropertySetterCallback(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info);

0 commit comments

Comments
 (0)