Implement array! and json! proc macros to make creation/instantiation easier #4515
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR merges two proc macros that make
js_sys::Array
andjs_sys::Object
creation and instantiation easier.Currently, to create an
Object
, you must use theReflect
API. Retrieving a property requires aReflect::get
call for every attempted retrieval. Likewise, to add a key-value pair to anObject
, we must make aReflect::set
call for every property we would like to set. If we are adding anObject
to anotherObject
, thatObject
must be instantiated the same way before finally setting it in the targetObject
. This process is very cumbersome and quickly clutters the code.The
json!
andarray!
proc macros address this problem.json!
allows the creation of ajs_sys::Object
using JavaScript object literal syntax:Similarly, the
array!
macro facilitates the creation ofjs_sys::Array
s using JavaScript array literal syntax:Both macros support the use of variables in the JSON as long as the underlying type implements
Into<JsValue>
. Comments are also supported. I have been thinking about how useful a macro like this could be for quite some time, and I finally decided to implement it. Please check out the README.md for more information, as I've only covered the basics here. Let me know your thoughts!