Skip to content

Commit

Permalink
adjust code to more closely match documented semantics
Browse files Browse the repository at this point in the history
Instead of hardcoding the arguments within the loader, pass them
through the searcher, as specified by the documented behavior:

#689 (comment)

> quoted from https://www.lua.org/manual/5.4/manual.html#pdf-require
>
>> Once a loader is found, require calls the loader with two arguments:
>> modname and an extra value, a loader data, also returned by the
>> searcher. The loader data can be any value useful to the module;
>> for the default searchers, it indicates where the loader was found.
>> (For instance, if the loader came from a file, this extra value is
>> the file path.)

Thanks @fperrad for the research!
  • Loading branch information
hishamhm committed Sep 5, 2023
1 parent 3ce3592 commit c7f8fe8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
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(module_name, found_filename)
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
12 changes: 6 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,11 @@ 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(module_name, found_filename)
return function(modname: string, loader_data: string): any
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

0 comments on commit c7f8fe8

Please sign in to comment.