Skip to content

Commit f43259a

Browse files
committed
Add Value override for has/get/setProperty, and deleteProperty API
1 parent a58f8c1 commit f43259a

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

API/hermes_node_api/hermes_node_api.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,11 +4538,13 @@ napi_status NAPI_CDECL napi_delete_property(
45384538
if (objRes.getStatus() == vm::ExecutionStatus::EXCEPTION) {
45394539
return env->setJSException();
45404540
}
4541+
vm::Handle<vm::JSObject> objHandle =
4542+
env->runtime_.makeHandle<vm::JSObject>(*objRes);
45414543

4542-
vm::CallResult<bool> cr = vm::JSObject::deleteComputed(
4543-
env->runtime_.makeHandle<vm::JSObject>(*objRes),
4544+
vm::CallResult<bool> cr = objHandle->deleteComputed(
4545+
objHandle,
45444546
env->runtime_,
4545-
asHandle(key));
4547+
asHandle(key),vm::PropOpFlags().plusThrowOnError());
45464548
if (cr.getStatus() == vm::ExecutionStatus::EXCEPTION) {
45474549
return env->setJSException();
45484550
}

API/hermes_node_api_jsi/NodeApiJsiRuntime.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,19 @@ class NodeApiJsiRuntime : public jsi::Runtime {
269269
override;
270270
jsi::Value getProperty(const jsi::Object &obj, const jsi::String &name)
271271
override;
272+
273+
#if JSI_VERSION >= 21
274+
jsi::Value getProperty(const jsi::Object &obj, const jsi::Value &name) override;
275+
#endif
276+
272277
bool hasProperty(const jsi::Object &obj, const jsi::PropNameID &name)
273278
override;
274279
bool hasProperty(const jsi::Object &obj, const jsi::String &name) override;
280+
281+
#if JSI_VERSION >= 21
282+
bool hasProperty(const jsi::Object &obj, const jsi::Value &name) override;
283+
#endif
284+
275285
void setPropertyValue(
276286
JSI_CONST_10 jsi::Object &obj,
277287
const jsi::PropNameID &name,
@@ -281,6 +291,19 @@ class NodeApiJsiRuntime : public jsi::Runtime {
281291
const jsi::String &name,
282292
const jsi::Value &value) override;
283293

294+
#if JSI_VERSION >= 21
295+
void setPropertyValue(
296+
const jsi::Object &obj,
297+
const jsi::Value &name,
298+
const jsi::Value &value) override;
299+
300+
void deleteProperty(const jsi::Object &obj, const jsi::PropNameID &name)
301+
override;
302+
void deleteProperty(const jsi::Object &obj, const jsi::String &name) override;
303+
304+
void deleteProperty(const jsi::Object &obj, const jsi::Value &name) override;
305+
#endif
306+
284307
bool isArray(const jsi::Object &obj) const override;
285308
bool isArrayBuffer(const jsi::Object &obj) const override;
286309
bool isFunction(const jsi::Object &obj) const override;
@@ -921,6 +944,7 @@ class NodeApiJsiRuntime : public jsi::Runtime {
921944
napi_value getProperty(napi_value object, napi_value propertyId) const;
922945
void setProperty(napi_value object, napi_value propertyId, napi_value value)
923946
const;
947+
void deleteProperty(napi_value object, napi_value propertyId) const;
924948
void setProperty(
925949
napi_value object,
926950
napi_value propertyId,
@@ -1741,6 +1765,15 @@ jsi::Value NodeApiJsiRuntime::getProperty(
17411765
return toJsiValue(getProperty(getNodeApiValue(obj), getNodeApiValue(name)));
17421766
}
17431767

1768+
#if JSI_VERSION >= 21
1769+
jsi::Value NodeApiJsiRuntime::getProperty(
1770+
const jsi::Object &obj,
1771+
const jsi::Value &name) {
1772+
NodeApiScope scope{*this};
1773+
return toJsiValue(getProperty(getNodeApiValue(obj), getNodeApiValue(name)));
1774+
}
1775+
#endif
1776+
17441777
bool NodeApiJsiRuntime::hasProperty(
17451778
const jsi::Object &obj,
17461779
const jsi::PropNameID &name) {
@@ -1755,6 +1788,15 @@ bool NodeApiJsiRuntime::hasProperty(
17551788
return hasProperty(getNodeApiValue(obj), getNodeApiValue(name));
17561789
}
17571790

1791+
#if JSI_VERSION >= 21
1792+
bool NodeApiJsiRuntime::hasProperty(
1793+
const jsi::Object& obj,
1794+
const jsi::Value& name) {
1795+
NodeApiScope scope{*this};
1796+
return hasProperty(getNodeApiValue(obj), getNodeApiValue(name));
1797+
}
1798+
#endif
1799+
17581800
void NodeApiJsiRuntime::setPropertyValue(
17591801
JSI_CONST_10 jsi::Object &obj,
17601802
const jsi::PropNameID &name,
@@ -1773,6 +1815,38 @@ void NodeApiJsiRuntime::setPropertyValue(
17731815
getNodeApiValue(obj), getNodeApiValue(name), getNodeApiValue(value));
17741816
}
17751817

1818+
#if JSI_VERSION >= 21
1819+
void NodeApiJsiRuntime::setPropertyValue(
1820+
const jsi::Object& obj,
1821+
const jsi::Value& name,
1822+
const jsi::Value& value) {
1823+
NodeApiScope scope{*this};
1824+
setProperty(
1825+
getNodeApiValue(obj), getNodeApiValue(name), getNodeApiValue(value));
1826+
}
1827+
1828+
void NodeApiJsiRuntime::deleteProperty(
1829+
const jsi::Object &obj,
1830+
const jsi::PropNameID &name) {
1831+
NodeApiScope scope{*this};
1832+
deleteProperty(getNodeApiValue(obj), getNodeApiValue(name));
1833+
}
1834+
1835+
void NodeApiJsiRuntime::deleteProperty(
1836+
const jsi::Object& obj,
1837+
const jsi::String& name) {
1838+
NodeApiScope scope{*this};
1839+
deleteProperty(getNodeApiValue(obj), getNodeApiValue(name));
1840+
}
1841+
1842+
void NodeApiJsiRuntime::deleteProperty(
1843+
const jsi::Object &obj,
1844+
const jsi::Value &name) {
1845+
NodeApiScope scope{*this};
1846+
deleteProperty(getNodeApiValue(obj), getNodeApiValue(name));
1847+
}
1848+
#endif
1849+
17761850
bool NodeApiJsiRuntime::isArray(const jsi::Object &obj) const {
17771851
NodeApiScope scope{*this};
17781852
return isArray(getNodeApiValue(obj));
@@ -2862,6 +2936,14 @@ void NodeApiJsiRuntime::setProperty(
28622936
CHECK_NAPI(jsrApi_->napi_set_property(env_, object, propertyId, value));
28632937
}
28642938

2939+
//Deletes object property value.
2940+
void NodeApiJsiRuntime::deleteProperty(
2941+
napi_value object,
2942+
napi_value propertyId) const {
2943+
bool result{};
2944+
CHECK_NAPI(jsrApi_->napi_delete_property(env_, object, propertyId,&result));
2945+
}
2946+
28652947
// Sets object property value with the provided property accessibility
28662948
// attributes.
28672949
void NodeApiJsiRuntime::setProperty(

unittests/NodeApi/test/js-native-api/test_object/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ assert.strictEqual(newObject.test_string, 'test string');
232232
const obj = {};
233233

234234
Object.defineProperty(obj, 'foo', { configurable: false });
235-
assert.strictEqual(test_object.Delete(obj, 'foo'), false);
235+
assert.throws(() => {
236+
test_object.Delete(obj, 'foo');
237+
}, /Property is not configurable/);
236238
assert.strictEqual('foo' in obj, true);
237239
}
238240

0 commit comments

Comments
 (0)