Modules and Projects
Module System
In LuneScript, one .lns file corresponds to one module.
Folder hierarchies are represented by dot . delimiters (e.g., foo/bar.lns →
foo.bar).
// foo/bar.lns
pub fn func() { print("foo.bar"); }
Project Settings (lune.js)
A LuneScript project is recognized with the directory containing a file named lune.js (or
lune.lua) as its root.
This file is written in JSON format.
{
"cmd_option": [ "--valid-luaval" ]
}
Even a single empty JSON {} works, but by specifying cmd_option, you can manage
compilation options applicable to the entire project.
lune.js is located becomes the starting point of the module path.If
foo/lune.js exists, foo/bar/mod.lns is treated as bar.mod.
Import
To use other modules, use import.
import foo.bar;
foo.bar.func();
import foo.bar as baz;
baz.func();
Using External Lua Modules
When using existing Lua modules, perform type definition using the module keyword.
module Math require "math" {
pub static let pi: real;
pub static fn sin( x:real ): real;
}
print( Math.sin( Math.pi / 2.0 ) );
Glue Code Generation
There is a feature that automatically generates Glue code for interconnecting with Lua (C language)
modules.
Specify the glue keyword and a prefix in the module definition.
module External require "test.ext" glue "hoge_" {
pub static fn test(): int;
}
This generates C-language header and source files with hoge_ as a prefix, allowing for smooth
interconnection with the C-side implementation.
Provide
Used to expose a specific symbol as the module's return value for compatibility with Lua.
pub fn func(): int { return 10; }
provide func;
This results in the function func being returned directly instead of a table when
require("this_mod") is called from Lua.
Subfile (File Splitting)
A feature that splits one module into multiple files. It makes large modules easier to manage.
Main File
// @lnsFront: skip
// main.lns
subfile use sub1;
subfile use sub2;
pub let val = 100;
Sub File
// @lnsFront: skip
// sub1.lns
subfile owner main;
pub fn func() {
print( val ); // Can access members of main
}
Files specified with subfile use are combined and compiled as if they were part of the
main file.