Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Procedural Macro for Native Classes #22

Merged
merged 4 commits into from
Aug 13, 2022

Conversation

Redfire75369
Copy link
Owner

@Redfire75369 Redfire75369 commented Aug 11, 2022

Resolves: #9

The addition of this new procedural macro allows easier creation of native functions to reduce boilerplate and chances for type errors.
It currently supports:

  • Methods
  • Accessors
  • Accessors for Public Properties
  • Static Methods,
  • Static Value Properties
  • Static Accessors

Below is an example of code using this macro:

#[ion::js_class]
mod switch {
	use ion::Result;
	use mozjs::conversions::ConversionBehavior::Clamp;

	#[derive(Clone, Debug)]
	pub struct Switch {
		b: bool,
		#[convert(Clamp)]
		pub i: i32,
	}

	impl Switch {
		pub const INT: i32 = 32;
		pub const DOUBLE: f64 = 4.20;
		pub const STRING: &'static str = "SPIDER\0";

		#[constructor]
		fn new(b: bool) -> Result<Switch> {
			Ok(Switch { b, i: 144 })
		}

		fn print(#[this] this: &mut Switch) -> Result<()> {
			eprintln!("SWITCH: {:?}", this);
			Ok(())
		}

		fn switch(#[this] this: &mut Switch) -> Result<()> {
			this.b = !this.b;
			Ok(())
		}

		fn spiderfire() -> Result<String> {
			Ok(String::from("SPIDERFIRE"))
		}

		#[get]
		fn b(#[this] this: &Switch) -> Result<bool> {
			Ok(this.b)
		}

		#[set]
		fn set_b(#[this] this: &mut Switch, b: bool) -> Result<()> {
			this.b = b;
			Ok(())
		}

		#[get]
		fn get_a() -> Result<i32> {
			Ok(Switch::INT)
		}

		#[set]
		fn c(_: bool) -> Result<()> {
			eprintln!("Pranked!");
			Ok(())
		}
	}
}

The rough equivalent of this code in TypeScript would be

class Switch {
	static INT = 32;
	static DOUBLE = 4.20;
	static STRING = "SPIDER";
	
	constructor(b: boolean) {
		self._b = b;
		self._i = 144;
	}
	
	print() {
		console.log(this);
	}
	
	switch() {
		this.b = !this.b;
	}
	
	static spiderfire() {
		return "SPIDERFIRE";
	}
	
	get b() {
		return this._b;
	}
	
	set b(b: boolean) {
		this.b = b;
	}
	
	static get a() {
		return Switch.INT;
	}
	
	static set c(_: any) {
		console.log("Pranked!");
	}
	
	get i() {
		return this._i;
	}
	
	set i(i: number) {
		this._i = i;
	}
}

Added Trait for Initialising Classes

Added Support for Methods and Constants on Native Classes
Cleaned and Refactored Code
Refactored Accessor Code
@Redfire75369 Redfire75369 self-assigned this Aug 11, 2022
@Redfire75369 Redfire75369 added Feature: Native/API Requests, Additions and Improvements to the Ion API and Interfacing with Native JSAPI RFC Request for Comment Progress: Partial labels Aug 11, 2022
@Redfire75369 Redfire75369 marked this pull request as ready for review August 13, 2022 03:41
Replaced Formatting with String Literals
Updated Method Predicate to use Signature
@Redfire75369 Redfire75369 merged commit e89e585 into master Aug 13, 2022
@Redfire75369 Redfire75369 deleted the feature/macro/js-class branch August 13, 2022 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Native/API Requests, Additions and Improvements to the Ion API and Interfacing with Native JSAPI Progress: Complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Macro for Native Classes
1 participant