From 2db0386e651ae65e2d2a31c593f4ff640edf8b6d Mon Sep 17 00:00:00 2001 From: Martok Date: Thu, 19 Jan 2023 21:26:49 +0100 Subject: [PATCH] No issue - implement js::NativeDefineDataProperty helper --- js/src/vm/NativeObject.cpp | 35 +++++++++++++++++++++++++++++++++++ js/src/vm/NativeObject.h | 12 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/js/src/vm/NativeObject.cpp b/js/src/vm/NativeObject.cpp index 53f7c0bfac..7418d81edf 100644 --- a/js/src/vm/NativeObject.cpp +++ b/js/src/vm/NativeObject.cpp @@ -1675,6 +1675,41 @@ js::NativeDefineProperty(ExclusiveContext* cx, HandleNativeObject obj, PropertyN return NativeDefineProperty(cx, obj, id, value, getter, setter, attrs); } +bool +js::NativeDefineDataProperty(JSContext* cx, Handle obj, HandleId id, HandleValue value, + unsigned attrs, ObjectOpResult& result) +{ + Rooted desc(cx); + desc.initFields(nullptr, value, attrs, nullptr, nullptr); + return NativeDefineProperty(cx, obj, id, desc, result); +} + +bool +js::NativeDefineDataProperty(JSContext* cx, Handle obj, HandleId id, HandleValue value, + unsigned attrs) +{ + ObjectOpResult result; + if (!NativeDefineDataProperty(cx, obj, id, value, attrs, result)) { + return false; + } + if (!result) { + // Off-thread callers should not get here: they must call this + // function only with known-valid arguments. Populating a new + // PlainObject with configurable properties is fine. + MOZ_ASSERT(!cx->isHelperThreadContext()); + result.reportError(cx, obj, id); + return false; + } + return true; +} + +bool +js::NativeDefineDataProperty(JSContext* cx, Handle obj, PropertyName* name, HandleValue value, + unsigned attrs) +{ + RootedId id(cx, NameToId(name)); + return NativeDefineDataProperty(cx, obj, id, value, attrs); +} /*** [[HasProperty]] *****************************************************************************/ diff --git a/js/src/vm/NativeObject.h b/js/src/vm/NativeObject.h index abc84c9fd1..cf6d684ac0 100644 --- a/js/src/vm/NativeObject.h +++ b/js/src/vm/NativeObject.h @@ -1399,6 +1399,18 @@ NativeDefineProperty(ExclusiveContext* cx, HandleNativeObject obj, PropertyName* HandleValue value, JSGetterOp getter, JSSetterOp setter, unsigned attrs); +bool +NativeDefineDataProperty(JSContext* cx, Handle obj, HandleId id, HandleValue value, + unsigned attrs, ObjectOpResult& result); + +extern bool +NativeDefineDataProperty(JSContext* cx, Handle obj, HandleId id, + HandleValue value, unsigned attrs); + +extern bool +NativeDefineDataProperty(JSContext* cx, Handle obj, PropertyName* name, + HandleValue value, unsigned attrs); + extern bool NativeHasProperty(JSContext* cx, HandleNativeObject obj, HandleId id, bool* foundp);