Skip to content

Commit

Permalink
Merge pull request #234 from rustyscreeps/clean-up-macro-usage
Browse files Browse the repository at this point in the history
Clean up some internal macro syntax
  • Loading branch information
daboross authored Aug 23, 2019
2 parents 6e201b5 + bb67d06 commit b2f11d2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 90 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
- Fix typos in JavaScript code for `game::market::get_order` and `Nuke::launch_room_name`
- Add support for accessing intershard resource amounts, which currently only includes subscription
tokens, under `game::resources`.
- Remove remaining usages of internal `get_from_js!` macro, as it was minimally useful
- Improve syntax and consistency of some internal macros

0.6.0 (2019-08-15)
==================
Expand Down
63 changes: 12 additions & 51 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,53 +87,6 @@ macro_rules! js_unwrap_ref {
)
}

/// Creates a getter method to unwrap a field of a javascript object.
///
/// Macro Syntax (`$name` are expressions):
///
/// ```ignore
/// get_from_js!($method_name -> {$js_statement} -> $rust_type)
/// get_from_js!($method_name($param1, $param2, ...) -> {$js_statement} -> $rust_type)
/// ```
///
/// Building on top of `js_unwrap!()`, this creates an accessor to a javascript
/// object method or attribute.
///
/// # Example
/// ```
/// get_from_js!(
/// limit -> {
/// Game.cpu.limit
/// } -> u32
/// )
/// ```
///
/// Will become:
/// ```
/// pub fn limit() -> u32 {
/// js_unwrap!(Game.cpu.limit)
/// }
/// ```
/// which would best be used inside the implementation for `cpu` in this case.
macro_rules! get_from_js {
($name:ident -> { $js_side:expr } -> $rust_ret_type:ty) => (
get_from_js!($name() -> { $js_side } -> $rust_ret_type);
);
(
$name:ident(
$($param_ident:ident: $param_ty:ty),*
) -> {
$($js_side:tt)*
} -> $rust_ret_type:ty
) => (
pub fn $name(
$($param_ident: $param_ty),*
) -> $rust_ret_type {
js_unwrap!($($js_side)*)
}
)
}

/// Macro used to encapsulate all screeps game objects
///
/// Macro syntax:
Expand Down Expand Up @@ -245,7 +198,7 @@ macro_rules! simple_accessors {
/// (See `reference_wrapper` macro) which will support all `StructureProperties`
/// methods.
macro_rules! impl_structure_properties {
( $( $struct_name:ty ),+ ) => {$(
( $( $struct_name:ty ),+ $(,)? ) => {$(
unsafe impl StructureProperties for $struct_name {}
)*};
}
Expand All @@ -265,7 +218,7 @@ macro_rules! impl_structure_properties {
/// }
/// ```
macro_rules! impl_has_id {
($($struct_name:ty);* $(;)*) => {$(
($($struct_name:ty),+ $(,)?) => {$(
unsafe impl HasId for $struct_name {}

impl PartialEq for $struct_name {
Expand Down Expand Up @@ -534,8 +487,12 @@ macro_rules! match_some_structure_variants {

/// Implements `Iterator` for `js_vec::IntoIter` or `js_vec::Iter`, using
/// `FromExpectedType` and panicking on incorrect types.
///
/// Accepts a list of types to implement the traits for. Each type must be a
/// single ident, optionally followed by `<'lifetime_param>` where
/// `lifetime_param` is a single named lifetime.
macro_rules! impl_js_vec_iterators_from_expected_type_panic {
($($name:ident $(<$single_life_param:lifetime>)*),* $(,)*) => {
($($name:ident $(<$single_life_param:lifetime>)*),+ $(,)?) => {
$(
impl<$($single_life_param, )* T> Iterator for $name<$($single_life_param, )* T>
where
Expand Down Expand Up @@ -579,8 +536,12 @@ macro_rules! impl_js_vec_iterators_from_expected_type_panic {
}

/// Implements `Iterator` for `js_vec::IntoIter` or `js_vec::Iter`.
///
/// Accepts a list of types to implement the traits for. Each type must be a
/// single ident, optionally followed by `<'lifetime_param>` where
/// `lifetime_param` is a single named lifetime.
macro_rules! impl_js_vec_iterators_from_expected_type_with_result {
($($name:ident $(<$single_life_param:lifetime>)*),* $(,)*) => {
($($name:ident $(<$single_life_param:lifetime>)*),+ $(,)?) => {
$(
impl<$($single_life_param, )* T> Iterator for $name<$($single_life_param, )* T>
where
Expand Down
58 changes: 29 additions & 29 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,35 @@ pub unsafe trait HasId: RoomObjectProperties {
}

impl_has_id! {
ConstructionSite;
Creep;
Mineral;
Nuke;
Resource;
Source;
OwnedStructure;
Structure;
StructureContainer;
StructureController;
StructureExtension;
StructureExtractor;
StructureKeeperLair;
StructureLab;
StructureLink;
StructureNuker;
StructureObserver;
StructurePowerBank;
StructurePowerSpawn;
StructurePortal;
StructureRampart;
StructureRoad;
StructureSpawn;
StructureStorage;
StructureTerminal;
StructureTower;
StructureWall;
Tombstone;
PowerCreep;
ConstructionSite,
Creep,
Mineral,
Nuke,
Resource,
Source,
OwnedStructure,
Structure,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePowerBank,
StructurePowerSpawn,
StructurePortal,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall,
Tombstone,
PowerCreep,
}

/// Trait for all wrappers over Screeps JavaScript objects extending
Expand Down
22 changes: 12 additions & 10 deletions src/raw_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub struct ForeignSegment {

js_deserializable!(ForeignSegment);

get_from_js!(get_active_segments -> {
Object.keys(RawMemory.segments).map(Number)
} -> Vec<u32>);
pub fn get_active_segments() -> Vec<u32> {
js_unwrap!(Object.keys(RawMemory.segments).map(Number))
}

/// Sets active segments (max 10 ids).
pub fn set_active_segments(ids: &[u32]) {
Expand All @@ -30,9 +30,9 @@ pub fn set_active_segments(ids: &[u32]) {
}
}

get_from_js!(get_segment(id: u32) -> {
RawMemory.segments[@{id}]
} -> Option<String>);
pub fn get_segment(id: u32) -> Option<String> {
js_unwrap!(RawMemory.segments[@{id}])
}

pub fn set_segment(id: u32, data: &str) {
js! { @(no_return)
Expand All @@ -53,9 +53,9 @@ pub fn drop_segment(id: u32) {
}
}

get_from_js!(get_foreign_segment -> {
RawMemory.foreignSegment
} -> ForeignSegment);
pub fn get_foreign_segment() -> ForeignSegment {
js_unwrap!(RawMemory.foreignSegment)
}

/// Implements `RawMemory.setActiveForeignSegment`
///
Expand Down Expand Up @@ -92,7 +92,9 @@ pub fn set_public_segments(ids: &[u32]) {
}
}

get_from_js!(get -> {RawMemory.get()} -> String);
pub fn get() -> String {
js_unwrap!(RawMemory.get())
}

pub fn set(value: &str) {
js! { @(no_return)
Expand Down

0 comments on commit b2f11d2

Please sign in to comment.