Skip to content

Commit

Permalink
Add @:wrapPublicOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeRanDev committed Dec 18, 2023
1 parent 8733596 commit 3cebeb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/gdcompiler/GDCompiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = [];
var selfStack: Array<{ selfName: String, publicOnly: Bool }> = [];

public function new() {
super();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/gdcompiler/config/Meta.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cebeb1

Please sign in to comment.