-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Object::get_property_names() and Object::get_own_property_nam…
…es() This change allows the customization of the behavior of v8::Object::GetOwnPropertyNames() and v8::Object::GetPropertyNames() by accepting all the options that the raw V8 API supports. Fixes: #740 Signed-off-by: Darshan Sen <raisinten@gmail.com>
- Loading branch information
Showing
7 changed files
with
450 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::PropertyFilter; | ||
use crate::ONLY_ENUMERABLE; | ||
use crate::SKIP_SYMBOLS; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
#[repr(C)] | ||
pub enum KeyConversionMode { | ||
/// kConvertToString will convert integer indices to strings. | ||
ConvertToString, | ||
/// kKeepNumbers will return numbers for integer indices. | ||
KeepNumbers, | ||
NoNumbers, | ||
} | ||
|
||
/// Keys/Properties filter enums: | ||
/// | ||
/// KeyCollectionMode limits the range of collected properties. kOwnOnly limits | ||
/// the collected properties to the given Object only. kIncludesPrototypes will | ||
/// include all keys of the objects's prototype chain as well. | ||
#[derive(Debug, Clone, Copy)] | ||
#[repr(C)] | ||
pub enum KeyCollectionMode { | ||
/// OwnOnly limits the collected properties to the given Object only. | ||
OwnOnly, | ||
/// kIncludesPrototypes will include all keys of the objects's prototype chain | ||
/// as well. | ||
IncludePrototypes, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
#[repr(C)] | ||
pub enum IndexFilter { | ||
/// kIncludesIndices allows for integer indices to be collected. | ||
IncludeIndices, | ||
/// kSkipIndices will exclude integer indices from being collected. | ||
SkipIndices, | ||
} | ||
|
||
pub struct GetPropertyNamesArgs { | ||
pub mode: KeyCollectionMode, | ||
pub property_filter: PropertyFilter, | ||
pub index_filter: IndexFilter, | ||
pub key_conversion: KeyConversionMode, | ||
} | ||
|
||
pub struct GetPropertyNamesArgsBuilder { | ||
mode: KeyCollectionMode, | ||
property_filter: PropertyFilter, | ||
index_filter: IndexFilter, | ||
key_conversion: KeyConversionMode, | ||
} | ||
|
||
impl Default for GetPropertyNamesArgsBuilder { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl GetPropertyNamesArgsBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
mode: KeyCollectionMode::IncludePrototypes, | ||
property_filter: ONLY_ENUMERABLE | SKIP_SYMBOLS, | ||
index_filter: IndexFilter::IncludeIndices, | ||
key_conversion: KeyConversionMode::KeepNumbers, | ||
} | ||
} | ||
|
||
pub fn build(&self) -> GetPropertyNamesArgs { | ||
GetPropertyNamesArgs { | ||
mode: self.mode, | ||
property_filter: self.property_filter, | ||
index_filter: self.index_filter, | ||
key_conversion: self.key_conversion, | ||
} | ||
} | ||
|
||
pub fn mode( | ||
&mut self, | ||
mode: KeyCollectionMode, | ||
) -> &mut GetPropertyNamesArgsBuilder { | ||
self.mode = mode; | ||
self | ||
} | ||
|
||
pub fn property_filter( | ||
&mut self, | ||
property_filter: PropertyFilter, | ||
) -> &mut GetPropertyNamesArgsBuilder { | ||
self.property_filter = property_filter; | ||
self | ||
} | ||
|
||
pub fn index_filter( | ||
&mut self, | ||
index_filter: IndexFilter, | ||
) -> &mut GetPropertyNamesArgsBuilder { | ||
self.index_filter = index_filter; | ||
self | ||
} | ||
|
||
pub fn key_conversion( | ||
&mut self, | ||
key_conversion: KeyConversionMode, | ||
) -> &mut Self { | ||
self.key_conversion = key_conversion; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.