@@ -271,9 +271,138 @@ public void intCasting() throws IOException {
271271 assertFunctionBodyContains (compiled , "testEnum" , "zeroInt = zeroEnum" , true );
272272 assertFunctionBodyContains (compiled , "testEnum" , "zeroEnum2 = zeroInt" , true );
273273 // classes are cast with objectToIndex and objectFromIndex in lua
274- assertFunctionBodyContains (compiled , "testClass" , "objectToIndex " , true );
275- assertFunctionBodyContains (compiled , "testClass" , "objectFromIndex " , true );
274+ assertFunctionBodyContains (compiled , "testClass" , "__wurst_objectToIndex " , true );
275+ assertFunctionBodyContains (compiled , "testClass" , "__wurst_objectFromIndex " , true );
276276 assertFunctionBodyContains (compiled , "testClass" , "cInt = cObj" , false );
277277 assertFunctionBodyContains (compiled , "testClass" , "cObj2 = cInt" , false );
278278 }
279+
280+ @ Test
281+ public void configEntrypointNotRenamedWhenUserHasConfigFunction () throws IOException {
282+ test ().testLua (true ).lines (
283+ "package Test" ,
284+ "function config()" ,
285+ " skip" ,
286+ "init" ,
287+ " config()"
288+ );
289+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_configEntrypointNotRenamedWhenUserHasConfigFunction.lua" ), Charsets .UTF_8 );
290+ assertTrue (compiled .contains ("function config(" ));
291+ assertFalse (compiled .contains ("function config2(" ));
292+ assertTrue (compiled .contains ("function config1(" ));
293+ assertTrue (compiled .contains ("config1()" ));
294+ }
295+
296+ @ Test
297+ public void objectIndexFunctionsDoNotCollideWithUserFunctions () throws IOException {
298+ test ().testLua (true ).lines (
299+ "package Test" ,
300+ "function objectToIndex(int i) returns int" ,
301+ " return i + 1" ,
302+ "function objectFromIndex(int i) returns int" ,
303+ " return i - 1" ,
304+ "native takesInt(int i)" ,
305+ "class C" ,
306+ "function testClass()" ,
307+ " let cObj = new C()" ,
308+ " let cInt = cObj castTo int" ,
309+ " let cObj2 = cInt castTo C" ,
310+ " takesInt(objectToIndex(3))" ,
311+ " takesInt(objectFromIndex(5))" ,
312+ " if cObj2 == null" ,
313+ " skip" ,
314+ "init" ,
315+ " testClass()"
316+ );
317+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_objectIndexFunctionsDoNotCollideWithUserFunctions.lua" ), Charsets .UTF_8 );
318+ assertTrue (compiled .contains ("function objectToIndex(" ));
319+ assertTrue (compiled .contains ("function objectFromIndex(" ));
320+ assertFunctionBodyContains (compiled , "testClass" , "__wurst_objectToIndex" , true );
321+ assertFunctionBodyContains (compiled , "testClass" , "__wurst_objectFromIndex" , true );
322+ }
323+
324+ @ Test
325+ public void reflectionNativesStubbedForLua () throws IOException {
326+ test ().testLua (true ).lines (
327+ "package Test" ,
328+ "native typeIdToTypeName(int typeId) returns string" ,
329+ "native maxTypeId() returns int" ,
330+ "native instanceCount(int typeId) returns int" ,
331+ "native maxInstanceCount(int typeId) returns int" ,
332+ "class A" ,
333+ "init" ,
334+ " let name = typeIdToTypeName(A.typeId)" ,
335+ " let m = maxTypeId()" ,
336+ " let c = instanceCount(A.typeId)" ,
337+ " let mc = maxInstanceCount(A.typeId)"
338+ );
339+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_reflectionNativesStubbedForLua.lua" ), Charsets .UTF_8 );
340+ assertFalse (compiled .contains ("The native 'typeIdToTypeName' is not implemented." ));
341+ assertFalse (compiled .contains ("The native 'maxTypeId' is not implemented." ));
342+ assertFalse (compiled .contains ("The native 'instanceCount' is not implemented." ));
343+ assertFalse (compiled .contains ("The native 'maxInstanceCount' is not implemented." ));
344+ assertTrue (compiled .contains ("typeIdToTypeName = function" ));
345+ assertTrue (compiled .contains ("maxTypeId = function" ));
346+ assertTrue (compiled .contains ("instanceCount = function" ));
347+ assertTrue (compiled .contains ("maxInstanceCount = function" ));
348+ }
349+
350+ @ Test
351+ public void reflectionNativeStubBodiesReturnDefaults () throws IOException {
352+ test ().testLua (true ).lines (
353+ "package Test" ,
354+ "native typeIdToTypeName(int typeId) returns string" ,
355+ "native maxTypeId() returns int" ,
356+ "native instanceCount(int typeId) returns int" ,
357+ "native maxInstanceCount(int typeId) returns int" ,
358+ "class A" ,
359+ "init" ,
360+ " let _n = typeIdToTypeName(A.typeId)" ,
361+ " let _m = maxTypeId()" ,
362+ " let _c = instanceCount(A.typeId)" ,
363+ " let _mc = maxInstanceCount(A.typeId)"
364+ );
365+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_reflectionNativeStubBodiesReturnDefaults.lua" ), Charsets .UTF_8 );
366+ assertTrue (compiled .contains ("typeIdToTypeName = function" ));
367+ assertTrue (compiled .contains ("return \" \" " ));
368+ assertTrue (compiled .contains ("maxTypeId = function" ));
369+ assertTrue (compiled .contains ("instanceCount = function" ));
370+ assertTrue (compiled .contains ("maxInstanceCount = function" ));
371+ assertTrue (compiled .contains ("return 0" ));
372+ }
373+
374+ @ Test
375+ public void reflectionNativeStubsAreGuardedByExistingDefinitions () throws IOException {
376+ test ().testLua (true ).lines (
377+ "package Test" ,
378+ "native typeIdToTypeName(int typeId) returns string" ,
379+ "native maxTypeId() returns int" ,
380+ "native instanceCount(int typeId) returns int" ,
381+ "native maxInstanceCount(int typeId) returns int" ,
382+ "class A" ,
383+ "init" ,
384+ " let _n = typeIdToTypeName(A.typeId)" ,
385+ " let _m = maxTypeId()" ,
386+ " let _c = instanceCount(A.typeId)" ,
387+ " let _mc = maxInstanceCount(A.typeId)"
388+ );
389+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_reflectionNativeStubsAreGuardedByExistingDefinitions.lua" ), Charsets .UTF_8 );
390+ assertTrue (compiled .contains ("if typeIdToTypeName then" ));
391+ assertTrue (compiled .contains ("if maxTypeId then" ));
392+ assertTrue (compiled .contains ("if instanceCount then" ));
393+ assertTrue (compiled .contains ("if maxInstanceCount then" ));
394+ }
395+
396+ @ Test
397+ public void stdLibInitUsesTriggerEvaluateGuardInMain () throws IOException {
398+ test ().testLua (true ).withStdLib ().lines (
399+ "package Test" ,
400+ "init" ,
401+ " skip"
402+ );
403+ String compiled = Files .toString (new File ("test-output/lua/LuaTranslationTests_stdLibInitUsesTriggerEvaluateGuardInMain.lua" ), Charsets .UTF_8 );
404+ assertTrue (compiled .contains ("if not(TriggerEvaluate(" ));
405+ assertTrue (compiled .contains ("TriggerClearConditions" ));
406+ }
407+
279408}
0 commit comments