Typechecker & requiring native modules #687
Replies: 1 comment 3 replies
-
I believe this is correct - definition files are mainly there to be loaded into the global scope. Not sure if they can be tied to a particular module. Although, you may still be able to get somewhere. This is how I would think to do it: When you create a Frontend, you specify a FileResolver: https://github.com/Roblox/luau/blob/master/Analysis/include/Luau/FileResolver.h For this, there are 2 key methods:
Here, if we are given the managed file name return {} :: {
do_something: (name: string) -> ()
} (and you can also export any type defs as necessary) Put together into something like static const std::string kBuiltinFooModule = R"BUILTIN_SRC(
--!strict
export type Value = string
return {} :: {
do_something: (name: string) -> ()
}
)BUILTIN_SRC";
struct BuiltinsFileResolver : Luau::FileResolver {
std::optional<SourceCode> readSource(const ModuleName& name)
{
if (name == "@foo")
return Luau::SourceCode{kBuiltinFooModule, Luau::SourceCode::Module};
return std::nullopt
}
std::optional<ModuleInfo> resolveModule(const ModuleInfo* context, AstExpr* expr)
{
if (Luau::AstExprConstantString* expr = node->as<Luau::AstExprConstantString>())
{
if (expr->value.data == "foo")
return Luau::ModuleInfo{"@foo"};
}
return std::nullopt;
}
} Its no longer really using type definitions syntax, just general Luau code. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! We have a bunch of native (C++) modules that are optionally required by scripts. How could I register them to the typechecker?
For example, in a script we could have something like this:
Type definition file for module 'foo':
I've tried injecting the type definition file into frontend.moduleResolver.modules map but when the script tries to require "foo", the typechecker raises this error:
Cannot require module foo: Module is not a ModuleScript. It cannot be required.
I checked the code in TypeChecker::checkRequire and it leads me to think only Lua source code modules are supported. Am I out of luck?
Beta Was this translation helpful? Give feedback.
All reactions