luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau.org
MIT License
3.98k stars 373 forks source link

Add io.read and io.write capabilities. #1169

Open creepersaur opened 7 months ago

creepersaur commented 7 months ago

I know lua has io.read and io.write, but luau, being rebuilt from scratch it seems, doesn't. Roblox doesn't need io perhaps but it is much better looking at luau which is faster than base lua.

Currently, it's not possible to take user input through the terminal/console. And I would love if this was added.

UtoECat commented 7 months ago

All I/O support has been removed to provide sandboxing capabilities.

There are certain problems with the fact that several unsafe luau scripts will consume input from different threads at the same time, for example, or if the native application that embeds luau needs that input.

Additionally, both io.read() and io.write() can cause the thread to block until the operation completes or new data is received, which is not always acceptable.

If your task really requires terminal I/O and you understand all the implications, you can wrap the C getchar()/putchar() functions quite easily and add the wrapped functions to a global table. It won't take more than 10-20 lines of C/C++ code.

creepersaur commented 7 months ago

All I/O support has been removed to provide sandboxing capabilities.

There are certain problems with the fact that several unsafe luau scripts will consume input from different threads at the same time, for example, or if the native application that embeds luau needs that input.

Additionally, both io.read() and io.write() can cause the thread to block until the operation completes or new data is received, which is not always acceptable.

If your task really requires terminal I/O and you understand all the implications, you can wrap the C getchar()/putchar() functions quite easily and add the wrapped functions to a global table. It won't take more than 10-20 lines of C/C++ code.

I was hoping it would be easier to take input, as I would assume most people who use Roblox or luau haven't done much C/C++ or know how to wrap lua with it (this includes me.) And it would make sense pausing a thread until I/O actions are completed.

Could you provide a resource for me to look through with regards to this though?

UtoECat commented 7 months ago

as I would assume most people who use Roblox or luau haven't done much C/C++ or know how to wrap lua with it (this includes me.) Could you provide a resource for me to look through with regards to this though?

Wait, are you using the luau CLI utility?

I think it would make a little more sense to have these functions included with the CLI... But at the same time, this can lead to some confusion, because the embedded luau does not have such functions.

Anyway, back to your situation. If you find it difficult to work with C/C++ code and the LUA C API, I would suggest using regular lua or luajit. The lack of IO functions is not as critical as the lack of ability to dynamically load native code (compiled C/C++/any language) libraries to expand the capabilities of your scripts... And Luau does not support dynamic loading of theese libraries.

creepersaur commented 7 months ago

Wait, are you using the luau CLI utility? I'm using the luau.exe file from the luau-lang.org website. I'm finding it really hard (and annoying) to build luajit. As they say "only source code" is available and you have to build the luajit.exe yourself.

And embedded luau doesn't need IO support, which I agree, but it generally is rather useful. Python is multipurpose and has IO, I wished it was faster though.

Is there a public luajit.exe file? I'm getting errors trying to build from source and it would be way simpler using an exe.

vegorov-rbx commented 7 months ago

While we might add more os library methods/IO/dynamic modules in the future specifically for luau build as an executable, right now we don't have this on our roadmap.

Like it was mentioned, users building Luau from source can add any methods they need to the global environment.

You can also look at this project that already extends default Luau environment with IO features and other things: https://github.com/lune-org/lune (note, this is not a project managed/supported by the Luau org)

cloewen8 commented 3 months ago

Same problem here, different use case. I'm looking into using Luau in a music player addon. I want to allow users to quickly automate tasks without needing to write a custom C++ addon.

Writing files (and requiring native modules) are common tasks. I can write my own functions for this, but it would be nice to have a starting point. Even if Lua's existing libraries where simply modernized enough for Luau to function.

TheSandvichMaker commented 3 months ago

I just want to echo the sentiment. I was looking if I could use luau as a drop-in replacement for lua.exe in my project, ideally I just want a single executable with no other dependencies. I'm using lua for compile time text processing. I don't really have an issue building luau from source to add what I need, but it would be great for the luau standalone executable to have file IO capabilities out of the box.