From 88ce1b13d75fa40e3af5ca815b56541a2e2c91d0 Mon Sep 17 00:00:00 2001 From: king8fisher <140904760+king8fisher@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:39:38 -0500 Subject: [PATCH] declare LuaThread and LuaUserdata as interfaces --- core/5.0/global.d.ts | 8 ++++-- core/global.d.ts | 8 ++++-- test/__snapshots__/global.spec.ts.snap | 35 ++++++++++++++++++++++++++ test/global.spec.ts | 16 ++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/core/5.0/global.d.ts b/core/5.0/global.d.ts index b93aa0b..a1cce31 100644 --- a/core/5.0/global.d.ts +++ b/core/5.0/global.d.ts @@ -2,8 +2,12 @@ /** @noSelfInFile */ -type LuaThread = { readonly __internal__: unique symbol }; -type LuaUserdata = { readonly __internal__: unique symbol }; +interface LuaThread { + readonly __internal__: unique symbol; +} +interface LuaUserdata { + readonly __internal__: unique symbol; +} /** * A global variable (not a function) that holds a string containing the running diff --git a/core/global.d.ts b/core/global.d.ts index f80de47..f35a22f 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -4,8 +4,12 @@ /** @noSelfInFile */ -type LuaThread = { readonly __internal__: unique symbol }; -type LuaUserdata = { readonly __internal__: unique symbol }; +interface LuaThread { + readonly __internal__: unique symbol; +} +interface LuaUserdata { + readonly __internal__: unique symbol; +} /** * A global variable (not a function) that holds a string containing the running diff --git a/test/__snapshots__/global.spec.ts.snap b/test/__snapshots__/global.spec.ts.snap index bbfe81b..54e85cd 100644 --- a/test/__snapshots__/global.spec.ts.snap +++ b/test/__snapshots__/global.spec.ts.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Lua version 5.1 / global LuaThread and LuaUserdata can be extended 1`] = ` +"thread = coroutine.create(function() +end) +threadTag = thread.tag +userdataTag = userdata.tag" +`; + exports[`Lua version 5.1 / global assert 1`] = `"assert({bla = \\"not false\\"})"`; exports[`Lua version 5.1 / global assert 2`] = `"assert(false, \\"assert message\\")"`; @@ -104,6 +111,13 @@ exports[`Lua version 5.1 / global tostring 1`] = `"str = tostring(213.4)"`; exports[`Lua version 5.1 / global type 1`] = `"t = type(213.4)"`; +exports[`Lua version 5.2 / global LuaThread and LuaUserdata can be extended 1`] = ` +"thread = coroutine.create(function() +end) +threadTag = thread.tag +userdataTag = userdata.tag" +`; + exports[`Lua version 5.2 / global assert 1`] = `"assert({bla = \\"not false\\"})"`; exports[`Lua version 5.2 / global assert 2`] = `"assert(false, \\"assert message\\")"`; @@ -208,6 +222,13 @@ exports[`Lua version 5.2 / global tostring 1`] = `"str = tostring(213.4)"`; exports[`Lua version 5.2 / global type 1`] = `"t = type(213.4)"`; +exports[`Lua version 5.3 / global LuaThread and LuaUserdata can be extended 1`] = ` +"thread = coroutine.create(function() +end) +threadTag = thread.tag +userdataTag = userdata.tag" +`; + exports[`Lua version 5.3 / global assert 1`] = `"assert({bla = \\"not false\\"})"`; exports[`Lua version 5.3 / global assert 2`] = `"assert(false, \\"assert message\\")"`; @@ -312,6 +333,13 @@ exports[`Lua version 5.3 / global tostring 1`] = `"str = tostring(213.4)"`; exports[`Lua version 5.3 / global type 1`] = `"t = type(213.4)"`; +exports[`Lua version 5.4 / global LuaThread and LuaUserdata can be extended 1`] = ` +"thread = coroutine.create(function() +end) +threadTag = thread.tag +userdataTag = userdata.tag" +`; + exports[`Lua version 5.4 / global assert 1`] = `"assert({bla = \\"not false\\"})"`; exports[`Lua version 5.4 / global assert 2`] = `"assert(false, \\"assert message\\")"`; @@ -416,6 +444,13 @@ exports[`Lua version 5.4 / global tostring 1`] = `"str = tostring(213.4)"`; exports[`Lua version 5.4 / global type 1`] = `"t = type(213.4)"`; +exports[`Lua version JIT / global LuaThread and LuaUserdata can be extended 1`] = ` +"thread = coroutine.create(function() +end) +threadTag = thread.tag +userdataTag = userdata.tag" +`; + exports[`Lua version JIT / global assert 1`] = `"assert({bla = \\"not false\\"})"`; exports[`Lua version JIT / global assert 2`] = `"assert(false, \\"assert message\\")"`; diff --git a/test/global.spec.ts b/test/global.spec.ts index 7b55c96..a4a74e3 100644 --- a/test/global.spec.ts +++ b/test/global.spec.ts @@ -285,4 +285,20 @@ describeForEachLuaTarget('global', (target) => { expect(lua).toMatchSnapshot(); }); + + test('LuaThread and LuaUserdata can be extended', () => { + const lua = tstl( + target, + ` + interface LuaThread { readonly tag?: 'thread' } + interface LuaUserdata { readonly tag?: 'userdata' } + declare const userdata: LuaUserdata; + const thread = coroutine.create(() => {}); + const threadTag: 'thread' | undefined = thread.tag; + const userdataTag: 'userdata' | undefined = userdata.tag; + ` + ); + + expect(lua).toMatchSnapshot(); + }); });