Skip to content

Commit

Permalink
Fix global methods declaration
Browse files Browse the repository at this point in the history
Resolves: #36
  • Loading branch information
matshou committed Feb 4, 2021
1 parent 7ce1ba0 commit 65f1486
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/yooksi/pz/zdoc/compile/JavaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

public class JavaCompiler implements ICompiler<ZomboidJavaDoc> {

public static final String GLOBAL_OBJECT_CLASS = "zombie.Lua.LuaManager.GlobalObject";
private static final File SERIALIZE_LUA = new File("serialize.lua");

private final Set<Class<?>> exposedJavaClasses;
Expand Down Expand Up @@ -240,7 +241,7 @@ static HashSet<Class<?>> getExposedJava() throws ReflectiveOperationException {
exposer, "exposed", true
);
// class containing global exposed methods
result.add(Utils.getClassForName("zombie.Lua.LuaManager.GlobalObject"));
result.add(Utils.getClassForName(GLOBAL_OBJECT_CLASS));
return result;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/yooksi/pz/zdoc/doc/ZomboidLuaDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jetbrains.annotations.UnmodifiableView;

import io.yooksi.pz.zdoc.Main;
import io.yooksi.pz.zdoc.compile.JavaCompiler;
import io.yooksi.pz.zdoc.compile.LuaCompiler;
import io.yooksi.pz.zdoc.element.IMember;
import io.yooksi.pz.zdoc.element.lua.Annotated;
Expand Down Expand Up @@ -112,9 +113,14 @@ public void writeToFile(File file) throws IOException {
ZomboidLuaDoc.appendComments(sb, method);
ZomboidLuaDoc.appendAnnotations(sb, method);

sb.append("function ").append(clazz.getConventionalName());
sb.append(':').append(method.getName()).append('(');
sb.append("function ");

// global methods need to be declared outside tables
String parentType = clazz.getParentType();
if (parentType == null || !parentType.equals(JavaCompiler.GLOBAL_OBJECT_CLASS)) {
sb.append(clazz.getConventionalName()).append(':');
}
sb.append(method.getName()).append('(');
method.appendParameterSignature(sb);
sb.append(") end\n\n");
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/io/yooksi/pz/zdoc/doc/ZomboidLuaDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableList;

import io.yooksi.pz.zdoc.TestWorkspace;
import io.yooksi.pz.zdoc.compile.JavaCompiler;
import io.yooksi.pz.zdoc.element.lua.*;
import io.yooksi.pz.zdoc.element.mod.AccessModifierKey;
import io.yooksi.pz.zdoc.element.mod.MemberModifier;
Expand Down Expand Up @@ -389,6 +390,53 @@ void shouldWriteZomboidLuaDocMethodsWithVariadicArgumentsToFile() throws IOExcep
Assertions.assertEquals(expectedResult, writeToFileAndRead(zDoc));
}

@Test
void shouldNotWriteGlobalZomboidLuaDocMethodsAsPartOfTableToFile() throws IOException {

Set<LuaMethod> luaMethods = new LinkedHashSet<>();
luaMethods.add(LuaMethod.Builder.create("firstMethod").build()
);
luaMethods.add(LuaMethod.Builder.create("secondMethod")
.withModifier(new MemberModifier(AccessModifierKey.PUBLIC)).build()
);
luaMethods.add(LuaMethod.Builder.create("thirdMethod")
.withModifier(new MemberModifier(AccessModifierKey.PRIVATE))
.withReturnType(new LuaType("String")).build()
);
luaMethods.add(LuaMethod.Builder.create("fourthMethod").withOwner(TEST_LUA_CLASS)
.withModifier(new MemberModifier(AccessModifierKey.DEFAULT))
.withReturnType(new LuaType("Object")).withParams(ImmutableList.of(
new LuaParameter(new LuaType("int"), "param1"),
new LuaParameter(new LuaType("boolean"), "param2"))).build()
);
ZomboidLuaDoc zDoc = new ZomboidLuaDoc(
new LuaClass("LuaManager_GlobalObject", JavaCompiler.GLOBAL_OBJECT_CLASS),
new ArrayList<>(), luaMethods
);

List<String> expectedResult = ImmutableList.of(
"---@class LuaManager_GlobalObject : " + JavaCompiler.GLOBAL_OBJECT_CLASS,
"LuaManager_GlobalObject = {}",
"",
"---@return void",
"function firstMethod() end",
"",
"---@public",
"---@return void",
"function secondMethod() end",
"",
"---@private",
"---@return String",
"function thirdMethod() end",
"",
"---@param param1 int",
"---@param param2 boolean",
"---@return Object",
"function fourthMethod(param1, param2) end"
);
Assertions.assertEquals(expectedResult, writeToFileAndRead(zDoc));
}

@Test
@SuppressWarnings("ConstantConditions")
void shouldThrowExceptionWhenModifyingImmutableFields() {
Expand Down

0 comments on commit 65f1486

Please sign in to comment.