@@ -69,6 +69,7 @@ pub const NodeValue = struct {
6969 return v ;
7070 }
7171
72+ /// Casts the value to an NodeObject. Fails is the typeof != .Object.
7273 pub fn asObject (self : NodeValue.Self ) ! NodeObject {
7374 if (self .typeof () == .Object ) {
7475 return NodeObject {
@@ -80,6 +81,7 @@ pub const NodeValue = struct {
8081 return error .NodeValueIsNoObject ;
8182 }
8283
84+ /// Casts the value to an NodeArray. Fails is the typeof != .Array.
8385 pub fn asArray (self : NodeValue.Self ) ! NodeArray {
8486 if (self .typeof () == .Object ) {
8587 return NodeObject {
@@ -91,19 +93,6 @@ pub const NodeValue = struct {
9193 return error .NodeValueIsNoObject ;
9294 }
9395
94- pub fn asFunction (self : NodeValue.Self ) ! NodeFunction {
95- if (try self .typeof () == .Function ) {
96- return NodeFunction {
97- .napi_env = self .napi_env ,
98- .napi_value = self .napi_value ,
99- };
100- }
101-
102- return error .NodeValueIsNoFunction ;
103- }
104-
105- // TODO: more as... methods
106-
10796 pub fn isArray (self : NodeValue.Self ) ! bool {
10897 return self .is (c .napi_is_array );
10998 }
@@ -136,9 +125,9 @@ pub const NodeValue = struct {
136125 return self .is (c .napi_is_promise );
137126 }
138127
139- fn is (self : NodeValue.Self , f : anytype ) ! bool {
128+ fn is (self : NodeValue.Self , is_fn : anytype ) ! bool {
140129 var b : bool = undefined ;
141- s2e (f (self .napi_env , self .napi_value , & b ));
130+ s2e (is_fn (self .napi_env , self .napi_value , & b ));
142131 return b ;
143132 }
144133};
@@ -157,9 +146,50 @@ pub const NodeObject = struct {
157146 };
158147 }
159148
160- // set prop
149+ /// Gets the value of a property.
150+ pub fn get (self : NodeObject.Self , property_name : []const u8 ) ! NodeValue {
151+ var v : c.napi_value = undefined ;
152+ try s2e (c .napi_get_named_property (self .napi_env , self .napi_value , property_name .ptr , & v ));
153+ return NodeValue { .napi_env = self .napi_env , .napi_value = v };
154+ }
155+
156+ /// Sets the value of a property.
157+ pub fn set (self : NodeObject.Self , property_name : []const u8 , value : NodeValue ) ! NodeValue {
158+ try s2e (c .napi_set_named_property (self .napi_env , self .napi_value , property_name .ptr , value .napi_value ));
159+ return value ;
160+ }
161+
162+ /// Gets a NodeValue indicating wether the object has a property with the specified name.
163+ pub fn has (self : NodeObject.Self , property_name : []const u8 ) ! bool {
164+ var v : bool = undefined ;
165+ try s2e (c .napi_has_named_property (self .napi_env , self .napi_value , property_name .ptr , & v ));
166+ return v ;
167+ }
168+
169+ /// Gets a NodeValue indicating wether the object has the named own property. key must be a string or a symbol, or an error will be thrown.
170+ pub fn hasOwn (self : NodeObject.Self , property_name : []const u8 ) ! bool {
171+ var v : bool = undefined ;
172+ try s2e (c .napi_has_own_property (self .napi_env , self .napi_value , try Convert .nodeFromNative (self .napi_env , property_name ), & v ));
173+ return v ;
174+ }
175+ /// Gets the value of a property.
176+ pub fn delete (self : NodeObject.Self , property_name : []const u8 ) ! bool {
177+ var v : bool = undefined ;
178+ try s2e (c .napi_delete_property (
179+ self .napi_env ,
180+ self .napi_value ,
181+ try Convert .nodeFromNative (self .napi_env , property_name ),
182+ & v ,
183+ ));
184+ return v ;
185+ }
161186
162- // get prop
187+ /// Returns the names of the enumerable properties as a NodeArray of strings. Symbol keys will not be included.
188+ pub fn getPropertyNames (self : NodeObject.Self ) ! NodeArray {
189+ var v : c.napi_value = undefined ;
190+ try s2e (c .napi_get_property_names (self .napi_env , self .napi_value , & v ));
191+ return NodeArray { .napi_env = self .napi_env , .napi_value = v };
192+ }
163193};
164194
165195/// Represents a JS function.
0 commit comments