Skip to content

Conversation

@pilaoda
Copy link
Contributor

@pilaoda pilaoda commented Aug 23, 2025

Implementation about #1478 #1502
all luaLibImport options are supported.

Copy link
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this relates to #1478, nothing has changed there, has it?

wrt #1502, the idea is the user can supply their own lualib implementation, I'm not sure simply recompiling the existing lualib with plugins is what we're after. If I wanted to completely replace a lualib file how would I do it?

I'm not sure what the reason would be for wanting this recompilation mechanism?

transpiledFiles = [];
}

const proxyEmitHost =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this does or why it's needed

Copy link
Contributor Author

@pilaoda pilaoda Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we write lualib_module_info.json when we recompile lualib_bundle.lua, this proxy make sure we don't overwrite the exists one, but collect by our recompile instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you solve this in lualib.ts where you start the new transpile (with this explanation as comment because code is very hard to understand), instead of here in the global transpile process?

feature: LuaLibFeature,
luaTarget: LuaTarget,
emitHost: EmitHost,
useCache = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get what the point of this getCache in these functions is, why would we no longer want the cache? In my mind either we read from disk or we recompile, but either way we want to cache the result there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because every time we recompile, we need new fresh LuaLib code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand this: Either we read the file from disk and we want to cache the data we read, or we recompile and we want to cache the compilation result. Why would we ever want to skip the cache in either case? I don't see why the lualib would change after first compilation within a single tstl run

@pilaoda
Copy link
Contributor Author

pilaoda commented Aug 23, 2025

#1502, I explain that, user may want to recompile # into Len() in all files including LuaLib.
also in #1478, with this option, every try include in Promise can be compiled into xpcall.
in #535, user can compile all array in LuaLib to Array.New()

all of these 3 issuss can be perfectly resolve by this recompileLuaLib option.

@pilaoda
Copy link
Contributor Author

pilaoda commented Aug 23, 2025

I write so many plugins and copy tons of code in tstl just to cover every array usage in LuaLib, now I decide to support recompile LuaLib to free me from such troubles

@pilaoda
Copy link
Contributor Author

pilaoda commented Aug 24, 2025

Here we have 2 user cases:

  1. If I wanted to completely replace a lualib file how would I do it?
  1. if I just want plugins can also apply to LuaLib Compilation,

with proposal in #973: lualib?: Partial<Record<LuaLibFeature, string>>

  • for user case 1
    • it's straightforward and maintainable
    • you maintain lua code without type-safety.
  • for user case 2
    • it would be very bother to rewrite all of those LuaLib implentation.
    • It's Not Maintainable and no type-safety.

with this option: recompileLuaLib

  • for user case 1
    • you can achive this with a plugin detect and replace the lualib function. this option just let you copy your replacement into a plugin.
    • it's maintainable, support typescript.
const plugin: tstl.Plugin = {
  beforeEmit(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.EmitFile[]) {
    for (const file of result) {
     if (file.outputPath == "xxx/lualib/Class") {
      file.code = "local MyCustomClass = {};";
      // or compile typescript
      file.code = tstl.transpileString("const MyCustomClass = {};", options).file!.lua!;
     }
    }
  },
};
  • for user case 2
    • it's straightforward and maintainable

Sorry I don't make those pro and con very clear last night, hope I done it now.

@pilaoda pilaoda marked this pull request as draft August 24, 2025 14:09
@pilaoda pilaoda marked this pull request as ready for review August 24, 2025 14:43
@pilaoda
Copy link
Contributor Author

pilaoda commented Aug 24, 2025

I realized that I had made a mistake about #1478. The implementation of Promise does not use try, but instead uses pcall.
My memory is jumbled, hahaha.

However, this does not affect #535 and #1502 related to Array that need this option.

feature: LuaLibFeature,
luaTarget: LuaTarget,
emitHost: EmitHost,
useCache = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand this: Either we read the file from disk and we want to cache the data we read, or we recompile and we want to cache the compilation result. Why would we ever want to skip the cache in either case? I don't see why the lualib would change after first compilation within a single tstl run

.expectToHaveNoDiagnostics()
.getLuaResult();
const lualubBundle = transpiledFiles.find(f => path.basename(f.outPath) === "lualib_bundle.lua");
expect(lualubBundle?.lua).toContain(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test with bundling?

transpiledFiles = [];
}

const proxyEmitHost =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you solve this in lualib.ts where you start the new transpile (with this explanation as comment because code is very hard to understand), instead of here in the global transpile process?

add test LuaLib recompile with bundling option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants