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

fix: populate ... for required modules #693

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions spec/cli/run_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ describe("tl run", function()
]], output)
end)

it("passes standard arguments to required chunks", function()
local dir_name = util.write_tmp_dir(finally, {
["ld.tl"] = [[
require("foo")
print("Done")
]],
["foo.tl"] = [[
print(...)
]]
})
local pd, output
util.do_in(dir_name, function()
pd = io.popen(util.tl_cmd("run", "ld.tl"), "r")
output = pd:read("*a")
end)
util.assert_popen_close(0, pd:close())
util.assert_line_by_line(util.os_path([[
foo ./foo.tl
Done
]]), output)
end)

describe("-l --require", function()
it("can require a module from the CLI like Lua", function()
local dir_name = util.write_tmp_dir(finally, {
Expand Down
10 changes: 5 additions & 5 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5487,14 +5487,14 @@ local function init_globals(lax)
}),
["loaders"] = a_type({
typename = "array",
elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY }) }),
elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY, ANY }) }),
}),
["loadlib"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING }), rets = TUPLE({ FUNCTION }) }),
["path"] = STRING,
["preload"] = TABLE,
["searchers"] = a_type({
typename = "array",
elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY }) }),
elements = a_type({ typename = "function", args = TUPLE({ STRING }), rets = TUPLE({ ANY, ANY }) }),
}),
["searchpath"] = a_type({ typename = "function", args = TUPLE({ STRING, STRING, OPT(STRING), OPT(STRING) }), rets = TUPLE({ STRING, STRING }) }),
},
Expand Down Expand Up @@ -10824,11 +10824,11 @@ local function tl_package_loader(module_name)
local code = assert(tl.pretty_print_ast(program, env.gen_target, true))
local chunk, err = load(code, "@" .. found_filename, "t")
if chunk then
return function()
local ret = chunk()
return function(modname, loader_data)
local ret = chunk(modname, loader_data)
package.loaded[module_name] = ret
return ret
end
end, found_filename
else
error("Internal Compiler Error: Teal generator produced invalid Lua. Please report a bug at https://github.com/teal-language/tl\n\n" .. err)
end
Expand Down
15 changes: 9 additions & 6 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -5487,14 +5487,14 @@ local function init_globals(lax: boolean): {string:Variable}, {string:Type}
},
["loaders"] = a_type {
typename = "array",
elements = a_type { typename = "function", args = TUPLE { STRING }, rets = TUPLE { ANY } }
elements = a_type { typename = "function", args = TUPLE { STRING }, rets = TUPLE { ANY, ANY } }
},
["loadlib"] = a_type { typename = "function", args = TUPLE { STRING, STRING }, rets = TUPLE { FUNCTION } },
["path"] = STRING,
["preload"] = TABLE,
["searchers"] = a_type {
typename = "array",
elements = a_type { typename = "function", args = TUPLE { STRING }, rets = TUPLE { ANY } }
elements = a_type { typename = "function", args = TUPLE { STRING }, rets = TUPLE { ANY, ANY } }
},
["searchpath"] = a_type { typename = "function", args = TUPLE { STRING, STRING, OPT(STRING), OPT(STRING) }, rets = TUPLE { STRING, STRING } },
},
Expand Down Expand Up @@ -10791,7 +10791,7 @@ tl.gen = function(input: string, env: Env): string, Result
return code, result
end

local function tl_package_loader(module_name: string): any
local function tl_package_loader(module_name: string): any, any
local found_filename, fd, tried = tl.search_module(module_name, false)
if found_filename then
local input = read_full_file(fd)
Expand Down Expand Up @@ -10824,11 +10824,14 @@ local function tl_package_loader(module_name: string): any
local code = assert(tl.pretty_print_ast(program, env.gen_target, true))
local chunk, err = load(code, "@" .. found_filename, "t")
if chunk then
return function(): any
local ret = chunk()
return function(modname: string, loader_data: string): any
if loader_data == nil then
loader_data = found_filename
end
local ret = chunk(modname, loader_data)
package.loaded[module_name] = ret
return ret
end
end, found_filename
else
error("Internal Compiler Error: Teal generator produced invalid Lua. Please report a bug at https://github.com/teal-language/tl\n\n" .. err)
end
Expand Down