Skip to content

Commit

Permalink
Merge branch 'main' into opt-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev committed Feb 3, 2024
2 parents 279c0bb + 990f7f7 commit 452c365
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 85 deletions.
2 changes: 1 addition & 1 deletion docs/config/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const example = `funct Test = {
foo: u8,
bar: string
},
rets: enum { "Success", "Fail" }
rets: enum { Success, Fail }
}`
</script>

Expand Down
4 changes: 1 addition & 3 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,7 @@ impl<'src> Struct<'src> {

if let Some(ty_max) = ty_max {
if let Some(current_max) = max {
if ty_max > current_max {
max = Some(ty_max);
}
max = Some(ty_max + current_max);
}
} else {
max = None;
Expand Down
10 changes: 10 additions & 0 deletions zap/src/output/luau/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,16 @@ impl<'src> ClientOutput<'src> {
}
}

self.push_line("return function()");
self.indent();

self.push_line(&format!(
"table.remove(events[{id}], table.find(events[{id}], {callback}))"
));

self.dedent();
self.push_line("end");

self.dedent();
self.push_line("end,");
}
Expand Down
132 changes: 52 additions & 80 deletions zap/src/output/luau/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,31 @@ pub trait Output {
}

fn push_line_indent(&mut self, s: &str) {
self.push_indent();
self.push(s);
self.push("\n");
self.push_line(s);
self.indent();
}

fn push_dedent_line(&mut self, s: &str) {
self.dedent();
self.push_indent();
self.push(s);
self.push("\n");
self.push_line(s);
}

fn push_dedent_line_indent(&mut self, s: &str) {
self.dedent();
self.push_indent();
self.push(s);
self.push("\n");
self.push_line(s);
self.indent();
}

fn push_range_check(&mut self, range: Range, val: &str) {
if let Some(exact) = range.exact() {
self.push_line(&format!("assert({val} == {exact})"));
self.push_line(&format!("assert({val} == {exact}, \"value not {exact}\")"));
} else {
if let Some(min) = range.min() {
self.push_line(&format!("assert({val} >= {min})"));
self.push_line(&format!("assert({val} >= {min}, \"value outside range\")"));
}

if let Some(max) = range.max() {
self.push_line(&format!("assert({val} <= {max})"));
self.push_line(&format!("assert({val} <= {max}, \"value outside range\")"));
}
}
}
Expand All @@ -67,12 +61,12 @@ pub trait Output {
Ty::Str(len) => {
if let Some(exact) = len.exact() {
if checks {
self.push_line(&format!("assert(#{from} == {exact})"));
self.push_line(&format!("assert(#{from} == {exact}, \"length not {exact}\")"));
}

self.push_line(&format!("alloc({exact})"));
self.push_line(&format!(
"buffer.writeu16(outgoing_buff, outgoing_apos, {from}, {exact})"
"buffer.writestring(outgoing_buff, outgoing_apos, {from}, {exact})"
));
} else {
self.push_line(&format!("local len = #{from}"));
Expand All @@ -81,40 +75,45 @@ pub trait Output {
self.push_range_check(*len, "len");
}

self.push_line("alloc(len)");
self.push_line("alloc(len + 2)");
self.push_line("buffer.writeu16(outgoing_buff, outgoing_apos, len)");
self.push_line(&format!(
"buffer.writestring(outgoing_buff, outgoing_apos, {from}, len)"
"buffer.writestring(outgoing_buff, outgoing_apos + 2, {from}, len)"
));
}
}

Ty::Buf(len) => {
if let Some(exact) = len.exact() {
if checks {
self.push_line(&format!("assert(#{from} == {exact})"));
self.push_line(&format!(
"assert(buffer.len({from}) == {exact}, \"length not {exact}\")"
));
}

self.push_line(&format!("alloc({exact})"));
self.push_line(&format!(
"buffer.copy(outgoing_buff, outgoing_apos, {from}, 0, {exact})"
));
} else {
self.push_line(&format!("local len = #{from}"));
self.push_line(&format!("local len = buffer.len({from})"));

if checks {
self.push_range_check(*len, "len");
}

self.push_line("alloc(len)");
self.push_line(&format!("buffer.copy(outgoing_buff, outgoing_apos, {from}, 0, len)"));
self.push_line("alloc(len + 2)");
self.push_line("buffer.writeu16(outgoing_buff, outgoing_apos, len)");
self.push_line(&format!(
"buffer.copy(outgoing_buff, outgoing_apos + 2, {from}, 0, len)"
));
}
}

Ty::Arr(ty, len) => {
if let Some(exact) = len.exact() {
if checks {
self.push_line(&format!("assert(#{from} == {exact})"));
self.push_line(&format!("assert(#{from} == {exact}, \"length not {exact}\")"));
}

self.push_line_indent(&format!("for i = 1, {exact} do"));
Expand All @@ -128,8 +127,9 @@ pub trait Output {
self.push_range_check(*len, "len");
}

self.push_line("alloc(len)");
self.push_line("alloc(2)");
self.push_line("buffer.writeu16(outgoing_buff, outgoing_apos, len)");

self.push_line_indent("for i = 1, len do");
self.push_line(&format!("local value = {from}[i]"));
self.push_ser("value", ty, checks);
Expand Down Expand Up @@ -160,14 +160,14 @@ pub trait Output {
self.push_dedent_line("end");
}

Ty::Ref(name) => {
self.push_line(&format!("types.write_{name}({from})"));
}
Ty::Ref(name) => self.push_line(&format!("types.write_{name}({from})")),

Ty::Enum(enum_ty) => match enum_ty {
Enum::Unit(enumerators) => {
let numty = NumTy::from_f64(0.0, enumerators.len() as f64 - 1.0);

self.push_line(&format!("alloc({})", numty.size()));

for (i, enumerator) in enumerators.iter().enumerate() {
if i == 0 {
self.push_line_indent(&format!("if {from} == \"{enumerator}\" then"));
Expand Down Expand Up @@ -215,7 +215,10 @@ pub trait Output {

Ty::Instance(class) => {
if checks && class.is_some() {
self.push_line(&format!("assert({from}:IsA(\"{}\"))", class.unwrap()));
self.push_line(&format!(
"assert({from}:IsA(\"{class}\"), \"instance is not a {class}\")",
class = class.unwrap()
));
}

self.push_line(&format!("table.insert(outgoing_inst, {from})"));
Expand Down Expand Up @@ -243,47 +246,25 @@ pub trait Output {
self.push_line(&format!(
"local axis_alignment = table.find(CFrameSpecialCases, {from}.Rotation)"
));
self.push_line("assert(axis_alignment)");
self.push_line("assert(axis_alignment, \"invalid axis alignment\")");
self.push_line("alloc(1)");
self.push_line("buffer.writeu8(outgoing_buff, outgoing_apos, axis_alignment)");

self.push_line("alloc(12)");
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos, {from}.Position.X)"
));
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos + 4, {from}.Position.Y)"
));
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos + 8, {from}.Position.Z)"
));
self.push_ser(&format!("{from}.Position"), &Ty::Vector3, checks);
}

Ty::CFrame => {
self.push_line(&format!("local axis, angle = {from}:ToAxisAngle()"));
self.push_line("axis = axis * angle");

self.push_line("alloc(12)");
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos, {from}.Position.X)"
));
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos + 4, {from}.Position.Y)"
));
self.push_line(&format!(
"buffer.writef32(outgoing_buff, outgoing_apos + 8, {from}.Position.Z)"
));

self.push_line("alloc(12)");
self.push_line("buffer.writef32(outgoing_buff, outgoing_apos, axis.X)");
self.push_line("buffer.writef32(outgoing_buff, outgoing_apos + 4, axis.Y)");
self.push_line("buffer.writef32(outgoing_buff, outgoing_apos + 8, axis.Z)");
self.push_ser(&format!("{from}.Position"), &Ty::Vector3, checks);
self.push_ser("axis", &Ty::Vector3, checks);
}

Ty::Boolean => {
self.push_line("alloc(1)");
self.push_line(&format!(
"buffer.writeu8(outgoing_buff, outgoing_apos, {from} and 1 or 0)"
"buffer.writeu8(outgoing_buff, outgoing_apos, if {from} then 1 else 0)"
))
}

Expand Down Expand Up @@ -390,7 +371,10 @@ pub trait Output {
self.push_line(&format!("{into} = incoming_inst[incoming_ipos]"));

if checks && class.is_some() {
self.push_line(&format!("assert({into} == nil or {into}:IsA(\"{}\"))", class.unwrap()));
self.push_line(&format!(
"assert({into} == nil or {into}:IsA(\"{class}\"), \"Expected {into} to be nil or an instance of {class}\")",
class = class.unwrap(),
));
}
} else {
self.push_des(into, ty, checks);
Expand All @@ -399,16 +383,14 @@ pub trait Output {
self.push_dedent_line("end");
}

Ty::Ref(name) => {
self.push_line(&format!("{into} = types.read_{name}()"));
}
Ty::Ref(name) => self.push_line(&format!("{into} = types.read_{name}()")),

Ty::Enum(enum_ty) => match enum_ty {
Enum::Unit(enumerators) => {
let numty = NumTy::from_f64(0.0, enumerators.len() as f64 - 1.0);

self.push_line(&format!(
"local enum_index = buffer.read{numty}(incoming_buff, {})",
"local enum_index = buffer.read{numty}(incoming_buff, read({}))",
numty.size()
));

Expand All @@ -432,7 +414,7 @@ pub trait Output {

self.push_line(&format!("{into} = {{}}"));
self.push_line(&format!(
"local enum_index = buffer.read{numty}(incoming_buff, {})",
"local enum_index = buffer.read{numty}(incoming_buff, read({}))",
numty.size()
));

Expand Down Expand Up @@ -469,12 +451,15 @@ pub trait Output {
self.push_line(&format!("{into} = incoming_inst[incoming_ipos]"));

if checks && class.is_some() {
self.push_line(&format!("assert({into} and {into}:IsA(\"{}\"))", class.unwrap()));
self.push_line(&format!(
"assert({into} and {into}:IsA(\"{class}\"), \"instance does not exist or is not a {class}\")",
class = class.unwrap(),
));
} else {
// we always assert that the instance is not nil
// because roblox will sometimes just turn instances into nil
// Ty::Opt covers the nil-able cases
self.push_line(&format!("assert({into})"));
self.push_line(&format!("assert({into}, \"instance does not exist\")"));
}
}

Expand All @@ -497,29 +482,18 @@ pub trait Output {
Ty::AlignedCFrame => {
self.push_line("local axis_alignment = buffer.readu8(incoming_buff, read(1))");

self.push_line_indent("local pos = Vector3.new(");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4))");
self.push_dedent_line(")");
self.push_line("local pos");
self.push_des("pos", &Ty::Vector3, checks);

self.push_line(&format!(
"{into} = CFrame.new(pos) * CFrameSpecialCases[axis_alignment]"
));
}

Ty::CFrame => {
self.push_line_indent("local pos = Vector3.new(");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4))");
self.push_dedent_line(")");

self.push_line_indent("local axis_angle = Vector3.new(");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4)),");
self.push_line("buffer.readf32(incoming_buff, read(4))");
self.push_dedent_line(")");
self.push_line("local pos, axis_angle");
self.push_des("pos", &Ty::Vector3, checks);
self.push_des("axis_angle", &Ty::Vector3, checks);

self.push_line("local angle = axis_angle.Magnitude");

Expand All @@ -530,9 +504,7 @@ pub trait Output {
self.push_dedent_line("end");
}

Ty::Boolean => {
self.push_line(&format!("{into} = buffer.readu8(incoming_buff, read(1)) == 1"));
}
Ty::Boolean => self.push_line(&format!("{into} = buffer.readu8(incoming_buff, read(1)) == 1")),

Ty::Unknown => {
self.push_line("incoming_ipos += 1");
Expand Down
12 changes: 11 additions & 1 deletion zap/src/output/luau/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,21 @@ impl<'a> ServerOutput<'a> {
self.push_ty(ty);
}

self.push(") -> ())\n");
self.push(") -> ()): () -> ()\n");
self.indent();

self.push_line(&format!("table.insert(events[{id}], {callback})"));

self.push_line("return function()");
self.indent();

self.push_line(&format!(
"table.remove(events[{id}], table.find(events[{id}], {callback}))"
));

self.dedent();
self.push_line("end");

self.dedent();
self.push_line("end,");
}
Expand Down
4 changes: 4 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ impl<'src> Converter<'src> {
SyntaxEnumKind::Tagged { tag, variants } => {
let tag_name = Self::str(tag);

if variants.is_empty() {
self.report(Report::AnalyzeEmptyEnum { span });
}

let variants = variants
.iter()
.map(|(variant_name, variant_struct)| {
Expand Down

0 comments on commit 452c365

Please sign in to comment.