From 3cebeb19fde0c20fe2b9f9dbb49b6ef2a51f2675 Mon Sep 17 00:00:00 2001 From: Robert Borghese <28355157+SomeRanDev@users.noreply.github.com> Date: Mon, 18 Dec 2023 06:01:06 -0500 Subject: [PATCH] Add @:wrapPublicOnly --- src/gdcompiler/GDCompiler.hx | 15 ++++++++++----- src/gdcompiler/config/Meta.hx | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/gdcompiler/GDCompiler.hx b/src/gdcompiler/GDCompiler.hx index 7536acb..76d3d2e 100644 --- a/src/gdcompiler/GDCompiler.hx +++ b/src/gdcompiler/GDCompiler.hx @@ -57,7 +57,7 @@ class GDCompiler extends reflaxe.DirectToStringCompiler { A stack used to track any overrides to the "self" keyword. If empty, "self" will be used. **/ - var selfStack: Array = []; + var selfStack: Array<{ selfName: String, publicOnly: Bool }> = []; public function new() { super(); @@ -346,7 +346,10 @@ func _exit_tree(): var gdScriptVal = if(f.expr != null) { if(isWrapper) { - selfStack.push(wrapperSelfName); + selfStack.push({ + selfName: wrapperSelfName, + publicOnly: classType.hasMeta(Meta.WrapPublicOnly) + }); } // Compile function @@ -806,7 +809,7 @@ func _exit_tree(): case TNull: return "null"; case TThis: { if(selfStack.length > 0) { - return selfStack[selfStack.length - 1]; + return selfStack[selfStack.length - 1].selfName; } return "self"; } @@ -901,13 +904,15 @@ func _exit_tree(): switch(fa) { // Check if this is a self.field with BypassWrapper - case FInstance(_, _, clsFieldRef): { + case FInstance(_, _, clsFieldRef) if(selfStack.length > 0): { final isSelfAccess = switch(e.expr) { case TConst(TThis): true; case _: false; } if(isSelfAccess) { - bypassSelf = clsFieldRef.get().hasMeta(Meta.BypassWrapper); + final selfData = selfStack[selfStack.length - 1]; + final field = clsFieldRef.get(); + bypassSelf = field.hasMeta(Meta.BypassWrapper) || (selfData.publicOnly && !field.isPublic); } } diff --git a/src/gdcompiler/config/Meta.hx b/src/gdcompiler/config/Meta.hx index cf3048c..a3bab96 100644 --- a/src/gdcompiler/config/Meta.hx +++ b/src/gdcompiler/config/Meta.hx @@ -29,9 +29,20 @@ enum abstract Meta(String) from String to String { If added to a class, that class will be treated as a wrapper class. This means instead of using `self`, all instance functions will be provided a `self` replacement argument to use as `self`. + + Class variables will still be implemented entirely in GDScript. **/ var Wrapper = ":wrapper"; + /** + @:wrapPublicOnly + + If added to a class with `@:wrapper`, this will make it so only public function are + "wrapped". Private functions will be generated and called entirely within + GDScript. + **/ + var WrapPublicOnly = ":wrapPublicOnly"; + /** @:bypass_wrapper