-
Notifications
You must be signed in to change notification settings - Fork 31
Feature: add support for user input in @std/io
#437
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6518a5a
initial lute sys/stdio work
annieetang 1b762cb
working version with Luau::Variant - tysm varun
annieetang db20879
stylua
annieetang 6d88010
add example luau file
annieetang 3e48ba6
add to std/io possible passing in a string prompt that's temporarily …
annieetang 60986c2
add examples and comments
annieetang 5c74acc
stylua again
annieetang 7b15549
separate examples out with prompt and without
annieetang 81158c2
renaming and addressing comments!
annieetang 87fd9a5
fixes
annieetang a4f992e
rm test file and small fixes
annieetang be42918
more fixes
annieetang 3c7a50e
rewrite luaL_error handling
annieetang 7537f8a
Merge branch 'primary' of https://github.com/luau-lang/lute into anni…
annieetang 0bc8102
remove comment
annieetang 642de28
extract status to make more readable
annieetang 6450ff3
rm iohandle destructor
annieetang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| local io = {} | ||
|
|
||
| function io.input(): string | ||
| error("unimplemented") | ||
| end | ||
|
|
||
| return io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| -- User input can be provided in multiple ways: | ||
| -- 1. lute user_input.luau | ||
| -- 2. echo "Hello!" | lute user_input_no_prompt.luau | ||
| -- 3. lute user_input_no_prompt.luau < input_text.txt | ||
|
|
||
| local io = require("@std/io") | ||
|
|
||
| -- Get user input | ||
| local input = io.input() | ||
| print(input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| -- User input can be provided in multiple ways: | ||
| -- 1. lute user_input.luau | ||
| -- 2. echo "Hello!" | lute user_input_with_prompt.luau | ||
| -- 3. lute user_input_with_prompt.luau < input_text.txt | ||
|
|
||
| local io = require("@std/io") | ||
|
|
||
| -- Get user input with prompt | ||
| local name = io.input("Please enter your name: ") | ||
| print(name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| add_library(Lute.IO STATIC) | ||
|
|
||
| target_sources(Lute.IO PRIVATE | ||
| include/lute/io.h | ||
|
|
||
| src/io.cpp | ||
| ) | ||
|
|
||
| target_compile_features(Lute.IO PUBLIC cxx_std_17) | ||
| target_include_directories(Lute.IO PUBLIC "include" ${LIBUV_INCLUDE_DIR}) | ||
| target_link_libraries(Lute.IO PRIVATE Lute.Runtime Luau.VM uv_a) | ||
| target_compile_options(Lute.IO PRIVATE ${LUTE_OPTIONS}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
|
|
||
| #include "lua.h" | ||
| #include "lualib.h" | ||
|
|
||
| // open the library as a standard global luau library | ||
| int luaopen_io(lua_State* L); | ||
| // open the library as a table on top of the stack | ||
| int luteopen_io(lua_State* L); | ||
|
|
||
| namespace io | ||
| { | ||
|
|
||
| int read(lua_State* L); | ||
|
|
||
| static const luaL_Reg lib[] = { | ||
| {"read", read}, | ||
|
|
||
| {nullptr, nullptr} | ||
| }; | ||
|
|
||
| } // namespace io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| #include "lute/io.h" | ||
| #include "Luau/Variant.h" | ||
| #include "lute/runtime.h" | ||
|
|
||
| #include <uv.h> | ||
| #include <memory> | ||
|
|
||
| #include "lua.h" | ||
| #include "lualib.h" | ||
|
|
||
|
|
||
| namespace io | ||
| { | ||
|
|
||
| struct IOHandle | ||
| { | ||
| Luau::Variant<uv_pipe_t, uv_tty_t> streamVariant; | ||
| uv_loop_t* loop = nullptr; | ||
| ResumeToken resumeToken; | ||
| std::shared_ptr<IOHandle> self; | ||
| std::vector<char> buffer; | ||
|
|
||
| ~IOHandle() {} | ||
annieetang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| void closeHandles() | ||
| { | ||
| auto closeCb = [](uv_handle_t* handle) | ||
| { | ||
| IOHandle* ioh = static_cast<IOHandle*>(handle->data); | ||
| ioh->self.reset(); | ||
| }; | ||
|
|
||
| uv_stream_t *stream = getStream(); | ||
| uv_read_stop(stream); | ||
| uv_close((uv_handle_t*)stream, closeCb); | ||
| } | ||
|
|
||
| uv_stream_t* getStream() { | ||
| if (auto* tty = streamVariant.get_if<uv_tty_t>()) | ||
| return (uv_stream_t*)tty; | ||
| if (auto* pipe = streamVariant.get_if<uv_pipe_t>()) | ||
| return (uv_stream_t*)pipe; | ||
| return nullptr; | ||
| } | ||
| }; | ||
|
|
||
| static void allocBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf) | ||
| { | ||
| IOHandle* ioh = static_cast<IOHandle*>(handle->data); | ||
| ioh->buffer.resize(suggestedSize); | ||
| buf->base = ioh->buffer.data(); | ||
| buf->len = ioh->buffer.size(); | ||
| } | ||
|
|
||
| // IOHandle is closed immediately after one read since we don't need a long running stream for this API. | ||
| static void onTtyRead(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) | ||
| { | ||
| IOHandle* handle = static_cast<IOHandle*>(stream->data); | ||
Vighnesh-V marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (nread > 0) | ||
| { | ||
| handle->resumeToken->complete( | ||
| [data = std::string(buf->base, nread)](lua_State* L) -> int | ||
| { | ||
| lua_pushlstring(L, data.c_str(), data.size()); | ||
| return 1; | ||
| } | ||
| ); | ||
| } | ||
| else if (nread < 0) | ||
| { | ||
| handle->resumeToken->fail(uv_strerror(nread)); | ||
| } | ||
|
|
||
| handle->closeHandles(); | ||
| } | ||
|
|
||
| int read(lua_State* L) | ||
| { | ||
| auto handle = std::make_shared<IOHandle>(); | ||
| handle->loop = uv_default_loop(); | ||
| handle->resumeToken = getResumeToken(L); | ||
| handle->self = handle; | ||
|
|
||
| uv_handle_type ht = uv_guess_handle(fileno(stdin)); | ||
| if (ht == UV_TTY) | ||
| { | ||
| uv_tty_t& tty = handle->streamVariant.emplace<uv_tty_t>(); | ||
| int status = uv_tty_init(handle->loop, &tty, fileno(stdin), 0); | ||
| if (status < 0) | ||
| luaL_error(L, "Failed to initialize TTY: %s", uv_strerror(status)); | ||
| } | ||
| else if (ht == UV_NAMED_PIPE || ht == UV_FILE) | ||
| { | ||
| uv_pipe_t& pipe = handle->streamVariant.emplace<uv_pipe_t>(); | ||
| int status = uv_pipe_init(handle->loop, static_cast<uv_pipe_t*>(&pipe), 0); | ||
| if (status < 0) | ||
| luaL_error(L, "Failed to initialize pipe: %s", uv_strerror(status)); | ||
| uv_pipe_open(static_cast<uv_pipe_t*>(&pipe), fileno(stdin)); | ||
| } | ||
| else | ||
| { | ||
| luaL_error(L, "Unsupported stdin type"); | ||
| } | ||
|
|
||
| uv_stream_t *stream = handle->getStream(); | ||
| stream->data = handle.get(); | ||
| uv_read_start(stream, allocBuffer, onTtyRead); | ||
| return lua_yield(L, 0); | ||
| } | ||
|
|
||
| } // namespace io | ||
|
|
||
| int luaopen_io(lua_State* L) | ||
| { | ||
| luaL_register(L, "io", io::lib); | ||
| return 1; | ||
| } | ||
|
|
||
| int luteopen_io(lua_State* L) | ||
| { | ||
| lua_createtable(L, 0, std::size(io::lib)); | ||
|
|
||
| for (auto& [name, func] : io::lib) | ||
| { | ||
| if (!name || !func) | ||
| break; | ||
|
|
||
| lua_pushcfunction(L, func, name); | ||
| lua_setfield(L, -2, name); | ||
| } | ||
|
|
||
| lua_setreadonly(L, -1, 1); | ||
|
|
||
| return 1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --!strict | ||
| -- @std/io | ||
| -- stdlib for `@lute/io` | ||
| -- Provides standard input, output, and other I/O utility functions | ||
|
|
||
| local io = require("@lute/io") | ||
|
|
||
| -- User input can be provided via command line stdin, piped input, or redirection from a file. See `examples/user_input.luau`. | ||
| function input(prompt: string?): string | ||
| if prompt then | ||
| -- We temporarily use `print` for prompt until we have proper stdout support, so there is a `\n` between prompt and input. | ||
| print(prompt) | ||
| end | ||
| return io.read() | ||
| end | ||
|
|
||
| return table.freeze({ | ||
| input = input, | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.