Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ static void createInstanceOfFunction(LuaTranslator tr) {
* typecasting handles in unbounded quantities.
*/
static void createObjectIndexFunctions(LuaTranslator tr) {
LuaVariable objectIndexMap = LuaAst.LuaVariable("__wurst_objectIndexMap", LuaAst.LuaExprNull());
tr.luaModel.add(objectIndexMap);
// The maps are made by the bootstrap; until then they read as nil, so they need no declaration.
LuaVariable objectIndexMap = LuaAst.LuaVariable("__wurst_objectIndexMap", LuaAst.LuaNoExpr());
tr.deferMainInit(LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(objectIndexMap), LuaAst.LuaTableConstructor(LuaAst.LuaTableFields(
LuaAst.LuaTableNamedField("counter", LuaAst.LuaExprIntVal("0"))
))));

LuaVariable numberWrapperMap = LuaAst.LuaVariable("__wurst_number_wrapper_map", LuaAst.LuaExprNull());
tr.luaModel.add(numberWrapperMap);
LuaVariable numberWrapperMap = LuaAst.LuaVariable("__wurst_number_wrapper_map", LuaAst.LuaNoExpr());
tr.deferMainInit(LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(numberWrapperMap), LuaAst.LuaTableConstructor(LuaAst.LuaTableFields(
LuaAst.LuaTableNamedField("counter", LuaAst.LuaExprIntVal("0"))
))));
Expand Down Expand Up @@ -129,8 +128,8 @@ static OldGenericsHelpers createOldGenericsCastFunctions(LuaTranslator tr) {
}

static void createStringIndexFunctions(LuaTranslator tr) {
LuaVariable map = LuaAst.LuaVariable("__wurst_string_index_map", LuaAst.LuaExprNull());
tr.luaModel.add(map);
// made by the bootstrap, like the object index maps
LuaVariable map = LuaAst.LuaVariable("__wurst_string_index_map", LuaAst.LuaNoExpr());
tr.deferMainInit(LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(map), LuaAst.LuaTableConstructor(LuaAst.LuaTableFields(
LuaAst.LuaTableNamedField("counter", LuaAst.LuaExprIntVal("0")),
LuaAst.LuaTableNamedField("byString", LuaAst.LuaTableConstructor(LuaAst.LuaTableFields())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ private void createBootstrapFunction() {
if (mainIm == null && confIm == null) {
return;
}
LuaVariable doneFlag = LuaAst.LuaVariable(uniqueName("__wurst_bootstrap_done"), LuaAst.LuaExprNull());
luaModel.add(doneFlag);
// nil until the bootstrap runs, which is how a global Lua has not seen reads: it needs no declaration
LuaVariable doneFlag = LuaAst.LuaVariable(uniqueName("__wurst_bootstrap_done"), LuaAst.LuaNoExpr());
LuaFunction boot = LuaAst.LuaFunction(uniqueName("__wurst_init_bootstrap"), LuaAst.LuaParams(), LuaAst.LuaStatements());
bootstrapFunction = boot;
boot.getBody().add(LuaAst.LuaLiteral("if " + doneFlag.getName() + " then return end"));
Expand Down Expand Up @@ -2693,8 +2693,8 @@ private void translateGlobal(ImVar v) {
if (v.getType() instanceof ImArrayType || v.getType() instanceof ImArrayTypeMulti) {
localizableStorageTables.add(lv);
}
lv.setInitialValue(LuaAst.LuaExprNull());
luaModel.add(lv);
// Not declared at the top of the script: a global Lua has not seen reads as nil, and the bootstrap, which
// main runs first, gives it its default.
deferMainInit(LuaAst.LuaAssignment(LuaAst.LuaExprVarAccess(lv), defaultValue(v.getType())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2814,13 +2814,7 @@ public void nestedConcreteGenericStaticStorageCompilesInLua() throws IOException
);

String compiled = compiledLua("nestedConcreteGenericStaticStorageCompilesInLua");
java.util.regex.Matcher storageDeclarations = java.util.regex.Pattern
.compile("(?m)^(List_store\\S*) = nil$")
.matcher(compiled);
List<String> storageNames = new ArrayList<>();
while (storageDeclarations.find()) {
storageNames.add(storageDeclarations.group(1));
}
List<String> storageNames = bootstrapDefaults(compiled, "List_store");
assertEquals("each concrete List instantiation needs independent static storage",
3, storageNames.size());
assertEquals("each structural List specialization must emit one storage slot",
Expand All @@ -2846,15 +2840,9 @@ public void genericStaticsAreIndependentWithoutTupleInstantiation() throws IOExc
);

String compiled = compiledLua("genericStaticsAreIndependentWithoutTupleInstantiation");
java.util.regex.Matcher storageDeclarations = java.util.regex.Pattern
.compile("(?m)^Slot_value_\\S* = nil$")
.matcher(compiled);
int storages = 0;
while (storageDeclarations.find()) {
storages++;
}
assertEquals("each concrete Slot instantiation needs its own static",
2, storages);
List<String> storages = bootstrapDefaults(compiled, "Slot_value_");
assertEquals("each concrete Slot instantiation needs its own static: " + storages,
2, storages.size());
}

@Test
Expand Down Expand Up @@ -2896,15 +2884,9 @@ public void detachedGenericStaticInitializerRemainsMetadataOnly() {
LuaCompilationUnit luaCode = compiler.transformProgToLua();
StringBuilder output = new StringBuilder();
luaCode.print(output, 0);
java.util.regex.Matcher declarations = java.util.regex.Pattern
.compile("(?m)^Slot_value_\\S* = nil$")
.matcher(output);
int storages = 0;
while (declarations.find()) {
storages++;
}
assertEquals("both concrete static specializations must survive detached initialization",
2, storages);
List<String> storages = bootstrapDefaults(output.toString(), "Slot_value_");
assertEquals("both concrete static specializations must survive detached initialization: " + storages,
2, storages.size());
}

@Test
Expand Down Expand Up @@ -3013,13 +2995,8 @@ public void inheritedGenericStaticUsesDeclaringOwnerSpecialization() throws IOEx
);

String compiled = compiledLua("inheritedGenericStaticUsesDeclaringOwnerSpecialization");
java.util.regex.Matcher declarations = java.util.regex.Pattern
.compile("(?m)^Base_value_\\S* = nil$").matcher(compiled);
int storages = 0;
while (declarations.find()) {
storages++;
}
assertEquals("each inherited Base<T> static needs independent storage", 2, storages);
List<String> storages = bootstrapDefaults(compiled, "Base_value_");
assertEquals("each inherited Base<T> static needs independent storage: " + storages, 2, storages.size());
}

@Test
Expand Down Expand Up @@ -5699,6 +5676,22 @@ private static String functionBody(String compiled, String functionName) {
return compiled.substring(start, end);
}

/**
* The globals whose name starts with {@code prefix} which the bootstrap gives their default, in order, once
* for each assignment: every global the script uses gets exactly one there.
*/
private static List<String> bootstrapDefaults(String compiled, String prefix) {
String bootstrap = topLevelFunctionBodyWithPrefix(compiled, "__wurst_init_bootstrap");
java.util.regex.Matcher defaults = java.util.regex.Pattern
.compile("(?m)^\\t(" + java.util.regex.Pattern.quote(prefix) + "\\S*) = ")
.matcher(bootstrap);
List<String> names = new ArrayList<>();
while (defaults.find()) {
names.add(defaults.group(1));
}
return names;
}

private static String topLevelFunctionBodyWithPrefix(String compiled, String functionNamePrefix) {
int start = compiled.indexOf("function " + functionNamePrefix);
assertTrue("expected function starting with " + functionNamePrefix, start >= 0);
Expand Down Expand Up @@ -6238,6 +6231,44 @@ public void classesWhichOnlyAllocateShareOneAllocator() throws IOException {
assertFalse("which allocates in place:\n" + grid, grid.contains("__wurst_allocObject"));
}

/**
* A global is given its default by the bootstrap which main runs first, and a global Lua has not seen reads as
* nil, so the script declares none of them as nil at its top: each declaration was a table write at load which
* changed nothing.
*/
@Test
public void globalsAreNotDeclaredAsNilAtTheTopOfTheScript() throws IOException {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class A",
"int count = 0",
"real ratio = 0",
"boolean flag = false",
"string name = null",
"A obj = null",
"int array counts",
"function bump()",
" count++",
" ratio += 0.5",
" counts[2] += 3",
"init",
" bump()",
" if count == 1 and ratio == 0.5 and not flag and name == null and obj == null",
" and counts[2] == 3 and counts[1] == 0",
" name = \"set\"",
" obj = new A",
" if name == \"set\" and obj != null",
" testSuccess()");
String compiled = compiledLua("globalsAreNotDeclaredAsNilAtTheTopOfTheScript");
java.util.regex.Matcher nilDeclaration = java.util.regex.Pattern
.compile("(?m)^[A-Za-z_][A-Za-z0-9_]* = nil$").matcher(compiled);
assertFalse("a global declared as nil:\n" + compiled, nilDeclaration.find());
String bootstrap = topLevelFunctionBodyWithPrefix(compiled, "__wurst_init_bootstrap");
assertTrue("the bootstrap gives the globals their defaults:\n" + bootstrap,
bootstrap.contains("Test_count = 0") && bootstrap.contains("Test_flag = false"));
}

/**
* A class table carries its supertypes for {@code instanceof} and its type id for {@code x.typeId}, and nothing
* else reads them: a program which does neither gets neither, and no instanceof helper.
Expand Down
Loading