Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion definitions/luau.luau
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ function luau.compile(source: string): Bytecode
error("not implemented")
end

function luau.load(bytecode: Bytecode, chunkname: string?, env: { [any]: any }?): (...any) -> ...any
function luau.load(bytecode: Bytecode, chunkname: string, env: { [any]: any }?): (...any) -> ...any
error("not implemented")
end

Expand Down
2 changes: 1 addition & 1 deletion examples/compile.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local luau = require("@lute/luau")
local luau = require("@std/luau")

local bytecode_container = luau.compile('return "Hello, world!"')

Expand Down
2 changes: 1 addition & 1 deletion examples/linter.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local fs = require("@std/fs")
local luau = require("@lute/luau")
local luau = require("@std/luau")

local pp = require("@batteries/pp")

Expand Down
2 changes: 1 addition & 1 deletion examples/parsing.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local luau = require("@lute/luau")
local luau = require("@std/luau")

local pretty = require("@batteries/pp")

Expand Down
8 changes: 3 additions & 5 deletions lute/luau/src/luau.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2657,12 +2657,10 @@ int compile_luau(lua_State* L)
int load_luau(lua_State* L)
{
const std::string* bytecode_string = static_cast<std::string*>(luaL_checkudata(L, 1, COMPILE_RESULT_TYPE));
const char* path = luaL_optlstring(L, 2, "=luau.load", nullptr);
std::string chunk_name = path;
if (chunk_name != "=luau.load")
chunk_name.insert(0, "@");
const char* path = luaL_checkstring(L, 2);
int envIndex = lua_isnoneornil(L, 3) ? 0 : 3;

luau_load(L, chunk_name.c_str(), bytecode_string->c_str(), bytecode_string->length(), lua_gettop(L) > 2 ? 3 : 0);
luau_load(L, path, bytecode_string->c_str(), bytecode_string->length(), envIndex);

return 1;
}
Expand Down
183 changes: 183 additions & 0 deletions lute/std/libs/luau.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
local luteLuau = require("@lute/luau")

local fs = require("@lute/fs")
local path = require("@std/path")

local luau = {}

-- Export all types from luteLuau
export type Position = luteLuau.Position
export type Location = luteLuau.Location

export type Whitespace = luteLuau.Whitespace
export type SingleLineComment = luteLuau.SingleLineComment
export type MultiLineComment = luteLuau.MultiLineComment

export type Trivia = luteLuau.Trivia

export type Token<Kind = string> = luteLuau.Token<Kind>

export type Eof = luteLuau.Eof

export type Pair<T, Separator> = luteLuau.Pair<T, Separator>
export type Punctuated<T, Separator = ","> = luteLuau.Punctuated<T, Separator>

export type AstLocal = luteLuau.AstLocal

export type AstExprGroup = luteLuau.AstExprGroup

export type AstExprConstantNil = luteLuau.AstExprConstantNil

export type AstExprConstantBool = luteLuau.AstExprConstantBool

export type AstExprConstantNumber = luteLuau.AstExprConstantNumber

export type AstExprConstantString = luteLuau.AstExprConstantString

export type AstExprLocal = luteLuau.AstExprLocal

export type AstExprGlobal = luteLuau.AstExprGlobal

export type AstExprVarargs = luteLuau.AstExprVarargs

export type AstExprCall = luteLuau.AstExprCall

export type AstExprIndexName = luteLuau.AstExprIndexName

export type AstExprIndexExpr = luteLuau.AstExprIndexExpr

export type AstFunctionBody = luteLuau.AstFunctionBody

export type AstExprAnonymousFunction = luteLuau.AstExprAnonymousFunction

export type AstExprTableItem = luteLuau.AstExprTableItem

export type AstExprTable = luteLuau.AstExprTable

export type AstExprUnary = luteLuau.AstExprUnary

export type AstExprBinary = luteLuau.AstExprBinary

export type AstExprInterpString = luteLuau.AstExprInterpString

export type AstExprTypeAssertion = luteLuau.AstExprTypeAssertion

export type AstExprIfElseIfs = luteLuau.AstExprIfElseIfs

export type AstExprIfElse = luteLuau.AstExprIfElse

export type AstExpr = luteLuau.AstExpr

export type AstStatBlock = luteLuau.AstStatBlock

export type AstStatElseIf = luteLuau.AstStatElseIf

export type AstStatIf = luteLuau.AstStatIf

export type AstStatWhile = luteLuau.AstStatWhile

export type AstStatRepeat = luteLuau.AstStatRepeat

export type AstStatBreak = luteLuau.AstStatBreak

export type AstStatContinue = luteLuau.AstStatContinue

export type AstStatReturn = luteLuau.AstStatReturn

export type AstStatExpr = luteLuau.AstStatExpr

export type AstStatLocal = luteLuau.AstStatLocal

export type AstStatFor = luteLuau.AstStatFor

export type AstStatForIn = luteLuau.AstStatForIn

export type AstStatAssign = luteLuau.AstStatAssign

export type AstStatCompoundAssign = luteLuau.AstStatCompoundAssign

export type AstAttribute = luteLuau.AstAttribute

export type AstStatFunction = luteLuau.AstStatFunction

export type AstStatLocalFunction = luteLuau.AstStatLocalFunction

export type AstStatTypeAlias = luteLuau.AstStatTypeAlias

export type AstStatTypeFunction = luteLuau.AstStatTypeFunction

export type AstStat = luteLuau.AstStat

export type AstGenericType = luteLuau.AstGenericType

export type AstGenericTypePack = luteLuau.AstGenericTypePack

export type AstTypeReference = luteLuau.AstTypeReference

export type AstTypeSingletonBool = luteLuau.AstTypeSingletonBool

export type AstTypeSingletonString = luteLuau.AstTypeSingletonString

export type AstTypeTypeof = luteLuau.AstTypeTypeof

export type AstTypeGroup = luteLuau.AstTypeGroup

export type AstTypeOptional = luteLuau.AstTypeOptional

export type AstTypeUnion = luteLuau.AstTypeUnion

export type AstTypeIntersection = luteLuau.AstTypeIntersection

export type AstTypeArray = luteLuau.AstTypeArray

export type AstTypeTableItem = luteLuau.AstTypeTableItem

export type AstTypeTable = luteLuau.AstTypeTable

export type AstTypeFunctionParameter = luteLuau.AstTypeFunctionParameter

export type AstTypeFunction = luteLuau.AstTypeFunction

export type AstType = luteLuau.AstType

export type AstTypePackExplicit = luteLuau.AstTypePackExplicit

export type AstTypePackGeneric = luteLuau.AstTypePackGeneric

export type AstTypePackVariadic = luteLuau.AstTypePackVariadic

export type AstTypePack = luteLuau.AstTypePack

export type ParseResult = luteLuau.ParseResult

export type Bytecode = luteLuau.Bytecode
Comment on lines +9 to +153
Copy link
Collaborator

Choose a reason for hiding this comment

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

all of these should become luacase ultimately, but it's okay if that doesn't happen in this PR.


function luau.parse(source: string): ParseResult
return luteLuau.parse(source)
end

function luau.parseexpr(source: string): AstExpr
return luteLuau.parseexpr(source)
end

function luau.compile(source: string): Bytecode
return luteLuau.compile(source)
end

function luau.load(bytecode: Bytecode, chunkname: string?, env: { [any]: any }?): (...any) -> ...any
return luteLuau.load(bytecode, if chunkname ~= nil then `@{chunkname}` else "=luau.load", env)
end

function luau.loadbypath(requirePath: path.pathlike): any
local requirePathStr = if typeof(requirePath) == "string" then requirePath else path.format(requirePath)

local migrationHandle = fs.open(requirePathStr, "r")

local migrationBytecode = luau.compile(fs.read(migrationHandle))

fs.close(migrationHandle)

return luau.load(migrationBytecode, requirePathStr)()
end

return luau
9 changes: 7 additions & 2 deletions lute/std/libs/path/posix/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ local function joinHelper(path: path, addend: pathlike): ()
end

function posix.join(...: pathlike): path
local parts = { ... }
local parts: { pathlike } = { ... }
if #parts == 0 then
return {
parts = {},
absolute = false,
}
end

local path = posix.parse(parts[1])
local path: path = if typeof(parts[1]) == "string"
then posix.parse(parts[1])
else {
parts = table.clone((parts[1] :: path).parts),
absolute = (parts[1] :: path).absolute,
}

for i = 2, #parts do
joinHelper(path, parts[i])
Expand Down
8 changes: 7 additions & 1 deletion lute/std/libs/path/win32/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ function win32.join(...: pathlike): path
}
end

local path = win32.parse(parts[1])
local path: path = if typeof(parts[1]) == "string"
then win32.parse(parts[1])
else {
parts = table.clone((parts[1] :: path).parts),
kind = (parts[1] :: path).kind,
driveLetter = (parts[1] :: path).driveLetter,
}

for i = 2, #parts do
joinHelper(path, parts[i])
Expand Down
2 changes: 1 addition & 1 deletion lute/std/libs/syntax/parser.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--!strict

local luau = require("@lute/luau")
local luau = require("@std/luau")

--- Parses Luau source code into an AstStatBlock
local function parse(source: string): luau.AstStatBlock
Expand Down
2 changes: 1 addition & 1 deletion lute/std/libs/syntax/printer.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--!strict
local luau = require("@lute/luau")
local luau = require("@std/luau")
local visitor = require("./visitor")

local function exhaustiveMatch(value: never): never
Expand Down
2 changes: 1 addition & 1 deletion lute/std/libs/syntax/visitor.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--!strict

local luau = require("@lute/luau")
local luau = require("@std/luau")

export type Visitor = {
visitBlock: (luau.AstStatBlock) -> boolean,
Expand Down
68 changes: 68 additions & 0 deletions tests/loadbypath.test.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local fs = require("@lute/fs")
local process = require("@lute/process")
local path = require("@std/path")
local test = require("@std/test")

local REQUIRER_CONTENTS = [[
local args = { ... }
assert(#args == 2, "Expected one argument: path to Luau script to require")

local luau = require("@std/luau")
local result = luau.loadbypath(args[2])
print(result)
]]

local REQUIREE_CONTENTS = [[return "Success"]]

local lutePath = process.execpath()

local tmpDirStr = ".tmp"
local tmpDirExists = fs.exists(tmpDirStr)
local testDir = path.join(tmpDirStr, "lute_require_test")
local testDirStr = path.format(testDir)

test.suite("Lute CLI", function(suite)
suite:beforeall(function()
if not tmpDirExists then
fs.mkdir(tmpDirStr)
end

if not fs.exists(testDirStr) then
fs.mkdir(testDirStr)
end
end)

suite:case("help1", function(check)
-- Setup
-- Create files
local requirerPath = path.format(path.join(testDir, "requirer.luau"))
local requirerFile = fs.open(requirerPath, "w+")
fs.write(requirerFile, REQUIRER_CONTENTS)
fs.close(requirerFile)

local requireePath = path.format(path.join(testDir, "requiree.luau"))
local requireeFile = fs.open(requireePath, "w+")
fs.write(requireeFile, REQUIREE_CONTENTS)
fs.close(requireeFile)

local result = process.run({ lutePath, requirerPath, requireePath })
check.eq(result.exitcode, 0)
check.eq(result.stdout, "Success\n")

-- Cleanup
fs.remove(requirerPath)
fs.remove(requireePath)
end)

suite:afterall(function()
if fs.exists(testDirStr) then
fs.rmdir(testDirStr)
end

if not tmpDirExists then
fs.rmdir(tmpDirStr)
end
end)
end)

test.run()
4 changes: 4 additions & 0 deletions tests/path.posix.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ test.suite("PathPosixJoinSuite", function(suite)
assert.eq(result.parts[2], "user")
assert.eq(result.parts[3], "documents")
assert.eq(result.parts[4], "file.txt")
assert.eq(#pathobj.parts, 2) -- original pathobj should be unchanged
assert.eq(pathobj.parts[1], "home")
assert.eq(pathobj.parts[2], "user")
assert.eq(pathobj.absolute, true)
end)

suite:case("join_error_on_absolute_addend", function(assert)
Expand Down
5 changes: 5 additions & 0 deletions tests/path.win32.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ test.suite("PathWin32JoinSuite", function(suite)
assert.eq(result.parts[2], "username")
assert.eq(result.parts[3], "Documents")
assert.eq(result.parts[4], "file.txt")
assert.eq(#pathobj.parts, 2) -- original pathobj should be unchanged
assert.eq(pathobj.parts[1], "Users")
assert.eq(pathobj.parts[2], "username")
assert.eq(pathobj.kind, "absolute")
assert.eq(pathobj.driveLetter, "C")
end)

suite:case("join_error_on_absolute_addend", function(assert)
Expand Down
4 changes: 2 additions & 2 deletions tests/testAstSerializer.test.luau
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local asserts = require("@std/assert")
local fs = require("@std/fs")
local luau = require("@lute/luau")
local luau = require("@std/luau")
local path = require("@std/path")
local test = require("@std/test")

local parser = require("@std/syntax/parser")
local printer = require("@std/syntax/printer")
local T = require("@lute/luau")
local T = require("@std/luau")

local function assertEqualsLocation(
assert: asserts.Asserts,
Expand Down
Loading