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

Module Declaration for Native Modules #1225

Open
flakey5 opened this issue Apr 7, 2024 · 1 comment
Open

Module Declaration for Native Modules #1225

flakey5 opened this issue Apr 7, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@flakey5
Copy link

flakey5 commented Apr 7, 2024

Background

Hey all! I'm working on a small side project and one of the things I'd like to do involves porting some Lua code over to Luau. The current code exposes a lot of native modules that can be used via require() statements. E.g.

local someLibrary = require('some-c-library')

Luau doesn't like this since it can't find the module. From what I've seen looking at docs and rfcs, I don't think there's currently a way to do this that Luau likes. It looks like the current declaration syntax only works for declaring globals.

Proposal

Adding modules to the current declaration syntax. Something like,

-- types.d.luau
-- Module exporting a table
declare module 'some-c-library' {
  sum: (a: number, b: number) -> number
    -- ...other module exports here
}

-- Module exporting a string
declare module 'some-other-library' string

-- some-source-file.luau
local someLibrary = require('some-c-library')
print(someLibrary.sum(1, 2))
print(require('some-other-library'))

This would,

  • Declare the module so that require() resolves it and the type it returns
  • Throw at runtime if the vm can't resolve the require()
@flakey5 flakey5 added the enhancement New feature or request label Apr 7, 2024
@Mooshua
Copy link

Mooshua commented Apr 8, 2024

Luau doesn't like this since it can't find the module. From what I've seen looking at docs and rfcs, I don't think there's currently a way to do this that Luau likes. It looks like the current declaration syntax only works for declaring globals.

The way to do this in a way that luau "likes" is to create a module resolver compatible with the Analysis library. This allows the analyzer to discover built-in module types at compile time. You can check the CLI implementation at struct CliFileResolver : Luau::FileResolver in Analyze.cpp.

Note that this means you will probably need to ship your own analysis binaries that use your fancy custom file resolver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants