Skip to content

Commit

Permalink
feat: add remaining compiler options
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Jul 5, 2024
1 parent 4d20904 commit d5000ed
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
48 changes: 47 additions & 1 deletion src/main/java/net/hollowcube/luau/compiler/LuauCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;

import java.nio.charset.StandardCharsets;
import java.util.List;

@SuppressWarnings("preview")
public sealed interface LuauCompiler permits LuauCompilerImpl {
Expand Down Expand Up @@ -42,7 +43,52 @@ sealed interface Builder permits LuauCompilerImpl.BuilderImpl {

@NotNull Builder coverageLevel(@NotNull CoverageLevel level);

//todo vectorLib, vectorCtor, vectorType, mutableGlobals, userdataTypes
/**
* name of the library providing vector type operations
*/
@NotNull Builder vectorLib(@NotNull String vectorLib);

/**
* global builtin to construct vectors; disabled by default
*/
@NotNull Builder vectorCtor(@NotNull String vectorCtor);

/**
* vector type name for type tables; disabled by default
*/
@NotNull Builder vectorType(@NotNull String vectorType);

/**
* List of globals that are mutable; disables the import optimization for fields accessed through these
* <br/>
* Replaces any previously set value.
*/
default @NotNull Builder mutableGlobals(@NotNull String... mutableGlobals) {
return mutableGlobals(List.of(mutableGlobals));
}

/**
* List of globals that are mutable; disables the import optimization for fields accessed through these
* <br/>
* Replaces any previously set value.
*/
@NotNull Builder mutableGlobals(@NotNull List<String> mutableGlobals);

/**
* userdata types that will be included in the type information
* <br/>
* Replaces any previously set value.
*/
default @NotNull Builder userdataTypes(@NotNull String... userdataTypes) {
return userdataTypes(List.of(userdataTypes));
}

/**
* userdata types that will be included in the type information
* <br/>
* Replaces any previously set value.
*/
@NotNull Builder userdataTypes(@NotNull List<String> userdataTypes);

@NotNull LuauCompiler build();

Expand Down
73 changes: 71 additions & 2 deletions src/main/java/net/hollowcube/luau/compiler/LuauCompilerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
import net.hollowcube.luau.internal.compiler.luacode_h;
import net.hollowcube.luau.util.NativeLibraryLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.nio.charset.StandardCharsets;
import java.util.List;

@SuppressWarnings("preview")
record LuauCompilerImpl(
@NotNull OptimizationLevel optimizationLevel,
@NotNull DebugLevel debugLevel,
@NotNull TypeInfoLevel typeInfoLevel,
@NotNull CoverageLevel coverageLevel
@NotNull CoverageLevel coverageLevel,
@Nullable String vectorLib,
@Nullable String vectorCtor,
@Nullable String vectorType,
@NotNull List<String> mutableGlobals,
@NotNull List<String> userdataTypes
) implements LuauCompiler {
static {
NativeLibraryLoader.loadLibrary("compiler");
Expand Down Expand Up @@ -62,6 +69,27 @@ public byte[] compile(byte @NotNull [] source) throws LuauCompileException {
lua_CompileOptions.debugLevel(opts, debugLevel.ordinal());
lua_CompileOptions.typeInfoLevel(opts, typeInfoLevel.ordinal());
lua_CompileOptions.coverageLevel(opts, coverageLevel.ordinal());
if (vectorLib != null) lua_CompileOptions.vectorLib(opts, arena.allocateUtf8String(vectorLib));
if (vectorCtor != null) lua_CompileOptions.vectorCtor(opts, arena.allocateUtf8String(vectorCtor));
if (vectorType != null) lua_CompileOptions.vectorType(opts, arena.allocateUtf8String(vectorType));
if (!mutableGlobals.isEmpty()) {
// size + 1 because the array is null terminated.
final MemorySegment mutableGlobals = arena.allocateArray(ValueLayout.ADDRESS, this.mutableGlobals.size() + 1);
for (int i = 0; i < this.mutableGlobals.size(); i++) {
final MemorySegment str = arena.allocateUtf8String(this.mutableGlobals.get(i));
mutableGlobals.setAtIndex(ValueLayout.ADDRESS, i, str);
}
lua_CompileOptions.mutableGlobals(opts, mutableGlobals);
}
if (!userdataTypes.isEmpty()) {
// size + 1 because the array is null terminated.
final MemorySegment userdataTypes = arena.allocateArray(ValueLayout.ADDRESS, this.userdataTypes.size() + 1);
for (int i = 0; i < this.userdataTypes.size(); i++) {
final MemorySegment str = arena.allocateUtf8String(this.userdataTypes.get(i));
userdataTypes.setAtIndex(ValueLayout.ADDRESS, i, str);
}
lua_CompileOptions.userdataTypes(opts, userdataTypes);
}
return opts;
}

Expand All @@ -78,6 +106,11 @@ static final class BuilderImpl implements Builder {
private DebugLevel debugLevel = DebugLevel.BACKTRACE;
private TypeInfoLevel typeInfoLevel = TypeInfoLevel.NATIVE_MODULES;
private CoverageLevel coverageLevel = CoverageLevel.NONE;
private String vectorLib = null;
private String vectorCtor = null;
private String vectorType = null;
private List<String> mutableGlobals = List.of();
private List<String> userdataTypes = List.of();

@Override
public @NotNull Builder optimizationLevel(@NotNull OptimizationLevel level) {
Expand All @@ -103,9 +136,45 @@ static final class BuilderImpl implements Builder {
return this;
}

@Override
public @NotNull Builder vectorLib(@NotNull String vectorLib) {
this.vectorLib = vectorLib;
return this;
}

@Override
public @NotNull Builder vectorCtor(@NotNull String vectorCtor) {
this.vectorCtor = vectorCtor;
return this;
}

@Override
public @NotNull Builder vectorType(@NotNull String vectorType) {
this.vectorType = vectorType;
return this;
}

@Override
public @NotNull Builder mutableGlobals(@NotNull List<String> mutableGlobals) {
this.mutableGlobals = mutableGlobals;
return this;
}

@Override
public @NotNull Builder userdataTypes(@NotNull List<String> userdataTypes) {
this.userdataTypes = userdataTypes;
return this;
}

@Override
public @NotNull LuauCompiler build() {
return new LuauCompilerImpl(optimizationLevel, debugLevel, typeInfoLevel, coverageLevel);
return new LuauCompilerImpl(
optimizationLevel, debugLevel,
typeInfoLevel, coverageLevel,
vectorLib, vectorCtor,
vectorType, mutableGlobals,
userdataTypes
);
}
}

Expand Down

0 comments on commit d5000ed

Please sign in to comment.