Skip to content

Commit

Permalink
Add mutliple values for data/args/rets (#140)
Browse files Browse the repository at this point in the history
* Add tuples

* Add typescript output

* Fix casing

* Add tooling output

* Fix tuple returns in typescript output

* Add docs

* Remove mention of tuples in docs

* Move push_parameters to mod.rs
  • Loading branch information
nezuo authored Nov 26, 2024
1 parent 93543cc commit eec53f0
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 188 deletions.
12 changes: 12 additions & 0 deletions docs/config/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const example = `event MyEvent = {
bar: u8,
},
}`

const dataExample = `event MyEvent = {
from: Server,
type: Reliable,
call: ManyAsync,
data: (boolean, u32, string)
}`
</script>

# Events
Expand Down Expand Up @@ -56,3 +63,8 @@ Use synchronous events with extreme caution.
### `data`

This field determines the data that is sent with the event. It can be any [Zap type](./types.md).

- If the event does not require any data, the `data` field should be excluded.
- You can pass multiple arguments to the event by separating each type with a comma and wrapping them all in parentheses:

<CodeBlock :code="dataExample" />
16 changes: 16 additions & 0 deletions docs/config/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const example = `funct Test = {
},
rets: enum { Success, Fail }
}`

const multiArgsRetsExample = `funct MultipleArgsRets = {
call: Async,
args: (boolean, u8),
rets: (boolean, string)
}`
</script>

# Functions
Expand Down Expand Up @@ -41,6 +47,16 @@ Use synchronous functions with extreme caution.

This field determines the data that is sent to the server. It can be any [Zap type](./types.md).

- If the client doesn't send any data, the `args` field should be excluded.
- You can pass multiple arguments to the function by separating each type with a comma and wrapping them all in parentheses:

<CodeBlock :code="multiArgsRetsExample" />

### `rets`

This field determines the data that is sent back to the client from the server. It can be any [Zap type](./types.md).

- If the server doesn't return any data, the `rets` field should be excluded.
- The function can return multiple values by separating each type with a comma and wrapping them all in parentheses:

<CodeBlock :code="multiArgsRetsExample" />
14 changes: 5 additions & 9 deletions docs/usage/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ event AnotherEvent = {
from: Client,
type: Reliable,
call: SingleAsync,
data: struct {
baz: boolean,
},
data: (boolean, u8)
}`
</script>

Expand Down Expand Up @@ -50,7 +48,7 @@ If your event's [call field](../config/events.md#call) is `SingleAsync` or `Sing
local Zap = require(Path.To.Zap)

-- only server listeners are given the player argument
Zap.AnotherEvent.SetCallback(function(Player, Data)
Zap.AnotherEvent.SetCallback(function(Player, Bool, Number)
-- Do something with the player and data
end)
```
Expand Down Expand Up @@ -81,14 +79,12 @@ Use `Sync` events only when performance is critical.

## Firing From the Client

The client only has a single function for firing events, `Fire`. This function takes the event's data as its only argument.
The client only has a single function for firing events, `Fire`. This function takes the event's data as it arguments.

```lua
local Zap = require(Path.To.Zap)

Zap.AnotherEvent.Fire({
baz = true,
})
Zap.AnotherEvent.Fire(true, 32)
```

## Firing From the Server
Expand Down Expand Up @@ -116,7 +112,7 @@ Zap.MyEvent.Fire(Player, {

### FireAll

The `FireAll` function takes the event's data as its only argument. It will fire the event to all players.
The `FireAll` function takes the event's data as its arguments. It will fire the event to all players.

```lua
local Zap = require(Path.To.Zap)
Expand Down
6 changes: 3 additions & 3 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ impl std::fmt::Display for YieldType {
pub struct FnDecl<'src> {
pub name: &'src str,
pub call: FnCall,
pub args: Option<Ty<'src>>,
pub rets: Option<Ty<'src>>,
pub args: Option<Vec<Ty<'src>>>,
pub rets: Option<Vec<Ty<'src>>>,
pub id: usize,
}

Expand All @@ -93,7 +93,7 @@ pub struct EvDecl<'src> {
pub from: EvSource,
pub evty: EvType,
pub call: EvCall,
pub data: Option<Ty<'src>>,
pub data: Option<Vec<Ty<'src>>>,
pub id: usize,
}

Expand Down
20 changes: 16 additions & 4 deletions zap/src/irgen/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ impl Gen for Des {
self.buf.push(stmt);
}

fn gen(mut self, var: Var, ty: &Ty) -> Vec<Stmt> {
self.push_ty(ty, var);
fn gen(mut self, var: Var, types: &[Ty]) -> Vec<Stmt> {
for (i, ty) in types.iter().enumerate() {
let var = if i > 0 {
Var::Name(match &var {
Var::Name(name) => format!("{name}{}", i + 1),
_ => unreachable!(),
})
} else {
var.clone()
};

self.push_ty(ty, var);
}

self.buf
}

Expand Down Expand Up @@ -419,11 +431,11 @@ impl Des {
}
}

pub fn gen(ty: &Ty, var: &str, checks: bool) -> Vec<Stmt> {
pub fn gen(types: &[Ty], var: &str, checks: bool) -> Vec<Stmt> {
Des {
checks,
buf: vec![],
var_occurrences: HashMap::new(),
}
.gen(var.into(), ty)
.gen(var.into(), types)
}
2 changes: 1 addition & 1 deletion zap/src/irgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod ser;

pub trait Gen {
fn push_stmt(&mut self, stmt: Stmt);
fn gen(self, var: Var, ty: &Ty<'_>) -> Vec<Stmt>;
fn gen(self, var: Var, types: &[Ty]) -> Vec<Stmt>;

fn push_local(&mut self, name: String, expr: Option<Expr>) {
self.push_stmt(Stmt::Local(name, expr))
Expand Down
20 changes: 16 additions & 4 deletions zap/src/irgen/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ impl Gen for Ser {
self.buf.push(stmt);
}

fn gen(mut self, var: Var, ty: &Ty) -> Vec<Stmt> {
self.push_ty(ty, var);
fn gen(mut self, var: Var, types: &[Ty]) -> Vec<Stmt> {
for (i, ty) in types.iter().enumerate() {
let var = if i > 0 {
Var::Name(match &var {
Var::Name(name) => format!("{name}{}", i + 1),
_ => unreachable!(),
})
} else {
var.clone()
};

self.push_ty(ty, var);
}

self.buf
}

Expand Down Expand Up @@ -357,11 +369,11 @@ impl Ser {
}
}

pub fn gen(ty: &Ty, var: &str, checks: bool) -> Vec<Stmt> {
pub fn gen(types: &[Ty], var: &str, checks: bool) -> Vec<Stmt> {
Ser {
checks,
buf: vec![],
var_occurrences: HashMap::new(),
}
.gen(var.into(), ty)
.gen(var.into(), types)
}
Loading

0 comments on commit eec53f0

Please sign in to comment.