Skip to content

Commit 2275171

Browse files
committed
fix error 'length is read-only`. Use defineProperty
1 parent a3f99cc commit 2275171

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

test-app/runtime/src/main/cpp/napi/common/native_api_util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ namespace napi_util {
166166
return napi_define_properties(env, object, 1, &desc);
167167
}
168168

169+
inline napi_status define_property_value(napi_env env, napi_value object, const char *propertyName,
170+
napi_value value = nullptr, napi_property_attributes attributes = napi_default_jsproperty, void *data = nullptr) {
171+
return napi_util::define_property(env, object, propertyName, value, nullptr, nullptr, data, attributes);
172+
}
173+
174+
inline napi_status define_property_get_set(napi_env env, napi_value object, const char *propertyName,
175+
napi_callback getter, napi_callback setter, napi_property_attributes attributes = napi_default_jsproperty, void *data = nullptr) {
176+
return napi_util::define_property(env, object, propertyName, nullptr, getter, setter, data, attributes);
177+
}
178+
169179
inline void setPrototypeOf(napi_env env, napi_value object, napi_value prototype) {
170180
napi_value global, global_object, set_proto;
171181

test-app/runtime/src/main/cpp/runtime/metadata/MetadataNode.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ std::vector<MetadataNode::MethodCallbackData *> MetadataNode::SetClassMembersFro
10851085
napi_create_function(env, methodName.c_str(), methodName.size(), MethodCallback,
10861086
callbackData, &method);
10871087

1088-
napi_set_named_property(env, prototype, methodName.c_str(), method);
1088+
napi_util::define_property_value(env, prototype, methodName.c_str(), method, napi_default_method);
10891089
lastMethodName = methodName;
10901090
collectedExtensionMethods.emplace(methodName, callbackData);
10911091

@@ -1112,7 +1112,7 @@ std::vector<MetadataNode::MethodCallbackData *> MetadataNode::SetClassMembersFro
11121112
napi_value method;
11131113
napi_create_function(env, methodName.c_str(), methodName.size(), MethodCallback,
11141114
callbackData, &method);
1115-
napi_set_named_property(env, prototype, methodName.c_str(), method);
1115+
napi_util::define_property_value(env, prototype, methodName.c_str(), method, napi_default_method);
11161116
collectedExtensionMethods.emplace(methodName, callbackData);
11171117
}
11181118

@@ -1199,7 +1199,8 @@ std::vector<MetadataNode::MethodCallbackData *> MetadataNode::SetClassMembersFro
11991199
napi_value method;
12001200
napi_create_function(env, methodName.c_str(), methodName.size(), MethodCallback,
12011201
callbackData, &method);
1202-
napi_set_named_property(env, constructor, methodName.c_str(), method);
1202+
1203+
napi_util::define_property_value(env, constructor, methodName.c_str(), method, napi_default_method);
12031204
lastMethodName = methodName;
12041205
}
12051206
callbackData->candidates.push_back(std::move(entry));
@@ -1234,6 +1235,7 @@ std::vector<MetadataNode::MethodCallbackData *> MetadataNode::SetClassMembersFro
12341235
ArgConverter::convertToJsString(env, tname));
12351236

12361237
SetClassAccessor(env, constructor);
1238+
12371239
return instanceMethodData;
12381240
}
12391241

@@ -1992,7 +1994,7 @@ napi_value MetadataNode::MethodCallback(napi_env env, napi_callback_info info) {
19921994

19931995

19941996
if (argc == 0 && methodName == PROP_KEY_VALUEOF) {
1995-
return jsThis;
1997+
return jsThis;
19961998
} else {
19971999
// Runtime::GetRuntime(env)->clearPendingError();
19982000
bool isFromInterface = initialCallbackData->node->IsNodeTypeInterface();

0 commit comments

Comments
 (0)