Skip to content

Lualib as module#1213

Closed
GlassBricks wants to merge 12 commits intoTypeScriptToLua:masterfrom
GlassBricks:lualib-module
Closed

Lualib as module#1213
GlassBricks wants to merge 12 commits intoTypeScriptToLua:masterfrom
GlassBricks:lualib-module

Conversation

@GlassBricks
Copy link
Copy Markdown
Contributor

@GlassBricks GlassBricks commented Feb 3, 2022

This implements lualib as modules (lualib source files and lualib_bundle are now modules), removes use of global variables.

  • Creates a new "internal" compiler option lualibCompilation for compiling lualib.
  • In this mode, lualib source files are modules, with the following restrictions:
    • No export default or export =
    • ltems can only be imported from other lualib source files
      As this mode is meant to be "internal", these restrictions are currently not enforced (no checking or diagnostics).
  • LuaLib feature dependencies are now automatically determined (instead of manually defined), and written to a file called lualib_modules_info.json.
  • Fixes a number of issues with lualib features not being loaded.

Additional details

Each lualib module compilations's output is such that it declares local variables that correspond to the module's exports, potentially wrapping module statements in a do block if nessecary to avoid leaking variables out of scope. See sourceFile.ts for more info.
This allows lualib modules to have non-exported members, i.e. be written like a normal module.
The result can be directly inlined for lualibImport: "inline", or for assembling lualib_bundle.

Possible future changes

  • Change polyfilled global objects to actual objects, so that const func = Object.assign or const foo = console works.
  • Change lualib/builtin handling to be more declarative; Allow plugins to supply lualib functions or to modify lualib/builtin behavior.
  • Assemble a "custom" lualib_bundle for projects, which contains only used features.
  • Make it so that multiple tstl compiled projects (bundles) running in the same environment use the same lualib.

EDIT: reflect latest commits.
Thanks to @Zamiell for testing these changes in an existing project.

@@ -1,4 +1,4 @@
function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
export function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
Copy link
Copy Markdown
Member

@lolleko lolleko Feb 6, 2022

Choose a reason for hiding this comment

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

Is it possible to make Array a class like set/map?
To give people the ability to overwrite array built ins.

EDIT: same applies to Object

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that might cause problems when interacting with existing lua code. Could be done in separate PR, see #262

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think it would be annoying for end users to have to mix Array and LuaArray, for example my game API returns lots of LuaArray

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Out of scope for this PR, this should be considered in a separate PR.

@Zamiell
Copy link
Copy Markdown
Contributor

Zamiell commented Feb 8, 2022

I tried out this PR today with my mod, and everything seems to be working great so far.
There are no more global variables, as promised!
I will update this if I find any issues in the coming days.

@@ -1,4 +1,4 @@
function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
export function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Out of scope for this PR, this should be considered in a separate PR.

const superInfo = getOrUpdate(classSuperInfos, context, () => []);
superInfo.push({ className, extendedTypeNode });

if (extendedType) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you sure this can be removed? Why does this not break promises construction?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The check for lualib type has been moved to transformIdentifier. For a class extends, transformIdentifier is eventually called in createClassSetup.

const relativeOutputPath = getEmitPathRelativeToOutDir(file, program);
export function getEmitPath(file: string | ProcessedFile, program: ts.Program): string {
const outDir = getEmitOutDir(program);
if (typeof file !== "string" && file.isRawFile) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ew, why

code: string;
sourceMap?: string;
sourceFiles?: ts.SourceFile[];
isRawFile?: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this even required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is for the json file. Before getEmitPath expects absolute paths and turns filenames into lua, and the json file name is not absolute. This allows treating the json file differently.
If you can think of a better way to handle this let me know.

"lualibuser.lua",
`
require("lualib_bundle")
local __TS__ArrayPush = require("lualib_bundle").__TS__ArrayPush
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this optimized into one statement here, but not in the cases in the snapshot below?

@GlassBricks GlassBricks requested a review from Perryvw February 27, 2022 20:08
@GlassBricks
Copy link
Copy Markdown
Contributor Author

Closing in favor of #1224

@GlassBricks GlassBricks deleted the lualib-module branch March 16, 2022 16:26
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.

4 participants