Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Lualib override plugins #973

Open
Perryvw opened this issue Jan 29, 2021 · 2 comments
Open

Proposal: Lualib override plugins #973

Perryvw opened this issue Jan 29, 2021 · 2 comments
Labels
discussion Open for discussion and ideas enhancement

Comments

@Perryvw
Copy link
Member

Perryvw commented Jan 29, 2021

In the last 2 weeks we received two requests for the ability to modify lualib output with plugins. One for supplying an environment-specific implementation of __TS__New, and another for a specialized ArrayForEach implementation for an environment's special tables.

Other use cases for overriding lualib functions might be using computation/memory/garbage optimized versions of lualib functions.

I think this is a 'quick win' as it seems relatively simple to implement this as follows:

Add a record to the plugin interface that allows supplying a string for a lualib feature:

export interface Plugin {
    /**
     * An augmentation to the map of visitors that transform TypeScript AST to Lua AST.
     *
     * Key is a `SyntaxKind` of a processed node.
     */
    visitors?: Visitors;

    /**
     * A function that converts Lua AST to a string.
     *
     * At most one custom printer can be provided across all plugins.
     */
    printer?: Printer;
+
+    /**
+     * Provide a custom string that will be emitted as implementation for a lualib feature.
+     */
+    lualib?: Partial<Record<LuaLibFeature, string>>
}

Then a plugin could look like this:

const plugin = {
    lualib: {
        [LuaLibFeature.Delete]: `
            function __TS__Delete(target, key)
                target[key] = nil
            end
        `
    }
}

Or even:

const plugin = {
    lualib: {
        ...garbageCollectorOptimizedLualibCollectionFromNpmPackage,
        ...lualibFunctionsForEnvironment
    }
}

We then pass these plugins to the printer and override the lualib methods it wants to emit.

What I'm not sure about yet is if these should be supplied as Lua string or something else (TS/Lua AST/TS AST). Also leaving in the function header like I did in the example could potentially lead to typos and other issues, although we should easily be able to put in a diagnostic for that.

@Perryvw Perryvw added enhancement discussion Open for discussion and ideas labels Jan 29, 2021
@GlassBricks
Copy link
Contributor

GlassBricks commented Aug 23, 2021

It could also be nice if plugins can also control the outputting of lualib feature itself (for optimizations or modifications), beyond just the lualib function contents.

This could potentially be expanded to allowing plugins to override builtin function behavior in general.

@GlassBricks
Copy link
Contributor

I think its better if lualib functions can be supplied as TS code, and tstl can compile them in a separate "instance", like how lualib is currently compiled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Open for discussion and ideas enhancement
Projects
None yet
Development

No branches or pull requests

2 participants