Skip to content

Commit e56b36f

Browse files
committed
feat: toStringRepr to map to luaL_tolstring
1 parent e6e7422 commit e56b36f

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ tasks.named<JextractTask>("jextract") {
241241
"luaL_checkbuffer", "luaL_checkstack", "luaL_checktype",
242242
"luaL_checkany", "luaL_checkudata", "luaL_checkoption",
243243

244+
"luaL_tolstring",
245+
244246
"luaL_where",
245247

246248
"luaopen_base", "luaopen_coroutine", "luaopen_table",

src/generated/java/net/hollowcube/luau/internal/vm/lua_h.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5755,7 +5755,7 @@ public double apply(Object... x0) {
57555755
if (TRACE_DOWNCALLS) {
57565756
traceDowncall("lua_clock", x0);
57575757
}
5758-
return (double) spreader.invokeExact(x0);
5758+
return (double)spreader.invokeExact(x0);
57595759
} catch(IllegalArgumentException | ClassCastException ex$) {
57605760
throw ex$; // rethrow IAE from passing wrong number/type of args
57615761
} catch (Throwable ex$) {

src/generated/java/net/hollowcube/luau/internal/vm/lualib_h.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,66 @@ public static int luaL_checkoption(MemorySegment L, int narg, MemorySegment def,
15561556
}
15571557
}
15581558

1559+
private static class luaL_tolstring {
1560+
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
1561+
lualib_h.C_POINTER,
1562+
lualib_h.C_POINTER,
1563+
lualib_h.C_INT,
1564+
lualib_h.C_POINTER
1565+
);
1566+
1567+
public static final MemorySegment ADDR = lualib_h.findOrThrow("luaL_tolstring");
1568+
1569+
public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
1570+
}
1571+
1572+
/**
1573+
* Function descriptor for:
1574+
* {@snippet lang=c :
1575+
* extern const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
1576+
* }
1577+
*/
1578+
public static FunctionDescriptor luaL_tolstring$descriptor() {
1579+
return luaL_tolstring.DESC;
1580+
}
1581+
1582+
/**
1583+
* Downcall method handle for:
1584+
* {@snippet lang=c :
1585+
* extern const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
1586+
* }
1587+
*/
1588+
public static MethodHandle luaL_tolstring$handle() {
1589+
return luaL_tolstring.HANDLE;
1590+
}
1591+
1592+
/**
1593+
* Address for:
1594+
* {@snippet lang=c :
1595+
* extern const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
1596+
* }
1597+
*/
1598+
public static MemorySegment luaL_tolstring$address() {
1599+
return luaL_tolstring.ADDR;
1600+
}
1601+
1602+
/**
1603+
* {@snippet lang=c :
1604+
* extern const char *luaL_tolstring(lua_State *L, int idx, size_t *len)
1605+
* }
1606+
*/
1607+
public static MemorySegment luaL_tolstring(MemorySegment L, int idx, MemorySegment len) {
1608+
var mh$ = luaL_tolstring.HANDLE;
1609+
try {
1610+
if (TRACE_DOWNCALLS) {
1611+
traceDowncall("luaL_tolstring", L, idx, len);
1612+
}
1613+
return (MemorySegment)mh$.invokeExact(L, idx, len);
1614+
} catch (Throwable ex$) {
1615+
throw new AssertionError("should not reach here", ex$);
1616+
}
1617+
}
1618+
15591619
private static class luaL_newstate {
15601620
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
15611621
lualib_h.C_POINTER );

src/main/java/net/hollowcube/luau/LuaState.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ Access Functions (Stack -> Java)
124124
@NotNull ByteBuffer toBuffer(int index); //todo type
125125
Object toPointer(int index); //todo type
126126

127+
/**
128+
* Converts the value to its string representation, including calling __tostring metamethods.
129+
*
130+
* <p>Maps to luaL_tolstring.</p>
131+
*/
132+
@NotNull String toStringRepr(int index);
133+
127134
/*
128135
Push Functions (Java -> Stack)
129136
*/
@@ -264,8 +271,8 @@ static double clock() {
264271
*/
265272

266273
int stackDepth();
267-
// int getInfo(); //todo args here
268-
// int getArgument(); //todo args here
274+
// int getInfo(); //todo args here
275+
// int getArgument(); //todo args here
269276
// getLocal
270277
// setLocal
271278
// getUpValue

src/main/java/net/hollowcube/luau/LuaStateImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public LuaStateImpl() {
8383
@Override
8484
public void close() {
8585
if (isThread)
86-
throw new IllegalStateException("cannot close a thread directly, it will be closed when lua garbage collects it.");
86+
throw new IllegalStateException(
87+
"cannot close a thread directly, it will be closed when lua garbage collects it.");
8788
lua_h.lua_close(L);
8889
closeInternal();
8990
}
@@ -427,6 +428,11 @@ public Object toPointer(int index) {
427428
throw new UnsupportedOperationException("todo");
428429
}
429430

431+
@Override
432+
public @NotNull String toStringRepr(int index) {
433+
return readLString(len -> luaL_tolstring(L, index, len));
434+
}
435+
430436
@Override
431437
public void pushNil() {
432438
lua_pushnil(L);

0 commit comments

Comments
 (0)