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

Use require wherever possible #27

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

tomodachi94
Copy link

@tomodachi94 tomodachi94 commented Dec 9, 2022

This PR is in a highly unfinished state. I don't recommend merging it until it's ready.

Checklist:

  • Move files to .lua extension
  • Have APIs (technically will be modules after this) return all of their functions inside of a table
  • Add a shim for os.loadAPI that transparently requires modules when os.loadAPI() is called
  • Figure out how to get require added on every boot
  • Insert the necessary code to allow require to work: package.path = package.path .. ";/modules/?;/modules/?.lua;/modules/?/init.lua"
  • Test everything and fix all the bugs

Closes #21

Modules:

  • autostartup
  • dns
  • events
  • graph
  • graphmanager
  • input
  • log
  • messagebus
  • net
  • queuemanager
  • serializer
  • util
  • wire

@tomodachi94
Copy link
Author

cc @danports

@tomodachi94
Copy link
Author

@danports should we rename util to prismutil or something? I feel like it is super ambiguous.

@danports
Copy link
Owner

danports commented Dec 9, 2022

I agree that util isn't a great name. Looking at the functions in that package, I'd probably favor a name related to tables (tablehelper?) - admittedly still pretty generic, but then most of the API names in prism-core are like that. I'd suggest tackling the rename in a follow up PR though so the scope of this PR doesn't grow too large.

@tomodachi94
Copy link
Author

Also, what are your thoughts about automatic linting? I can also setup some CI to do linting in a separate PR.

@danports
Copy link
Owner

danports commented Dec 9, 2022

Re: linting, my feeling is that it depends on how strict it is. Certainly a CI pipeline that checks for syntax errors at a bare minimum would be better than what we have now (nothing).

@tomodachi94
Copy link
Author

Re: linting, my feeling is that it depends on how strict it is. Certainly a CI pipeline that checks for syntax errors at a bare minimum would be better than what we have now (nothing).

I've got a working CI setup that uses Selene, a configurable linter. In its default configuration, it checks for all sorts of things, and can check even more if you configure it to do so. It also has support for knowing about custom globals (ex. ComputerCraft APIs). To top it all off, it's written in Rust.

@danports
Copy link
Owner

danports commented Dec 9, 2022

Sounds good. I have pushed broken code to git before, though fortunately I've caught it by other means (e.g. canary deployments) before I had to fix hundreds of broken computers! 😅 Anything to prevent that would be helpful.

@tomodachi94
Copy link
Author

We can squash the Convert... commits into one when we're finished, I'm doing atomic commits in case I make a mistake.

Also removed trailing whitespace and deleted the `require` for an unused library.
By default, package.path doesn't search for /modules. This changes that.
@tomodachi94
Copy link
Author

Alright, all of the APIs have been converted to modules.

@tomodachi94
Copy link
Author

tomodachi94 commented Dec 9, 2022

@danports for the require shim, I think we'll need to introduce a script that runs everything that is present inside of the startup.d folder, but as such, we'd also need to modify railmanager in the prism-rails repository to use startup.d.

@tomodachi94
Copy link
Author

tomodachi94 commented Dec 9, 2022

I've just written a script that does the heavy lifting of loading startup.d. It's available here. I've added it to this PR so we can automatically add cc-require on old versions. It will also help with the os.loadAPI shim.

(We'd need to migrate a single script in prism-rails to use this, but otherwise it wouldn't break anything.)

@tomodachi94
Copy link
Author

Only thing that appears to be left is the os.loadAPI shim. (Maybe we should do some testing too.)

This uses https://github.com/tomodachi94/cc-require, a fork of
https://github.com/oddstr13/cc-require.

Startupmgr is a script that executes everything in '/startup.d',
and is useful for executing multiple scripts on boot.
@danports
Copy link
Owner

I am wondering whether it would be better to handle the require polyfill at the package/API level, or at the ROM/BIOS level. I see you did something similar for pastebin. Could we use a similar approach here? The other option would be to do something like os.loadAPI("require") or dofile("cc-require") at the top of every startup file, which seems feasible but a little inelegant. What do you think?

Also, yes, I'll do some testing on my server on this branch once it's ready.

@danports
Copy link
Owner

OK, I could see how you could do this without adding a line to the top of the files by moving all of the existing startup files into startup.d for all packages with such an entry point, which is probably better regardless, but I'm still wondering whether this should be in the library or the ROM. Given that require is present in the ROM in later CC versions/forks, I tend to think the polyfill should be at the ROM level too.

@tomodachi94
Copy link
Author

...I'm still wondering whether this should be in the library or the ROM. Given that require is present in the ROM in later CC versions/forks, I tend to think the polyfill should be at the ROM level too.

I agree that it should, and cc-require is intended to be used as a resource pack that modifies ROM (but it works in non-ROM too).

My modification to cc-require detects that if require as a function already exists, it will abort. In other words, if we have the patch on the builtin side, ROM side and on the non-ROM side, the builtin side will take precedence, followed by ROM, followed by local.

To summarize, I think it should remain in the library but a ROM patch should be available.

@danports
Copy link
Owner

Yeah, having both library and ROM options makes a lot of sense. So the idea is that requiem is an optional package that can be installed by the end user on computers that would need it? We wouldn't need to take requiem as a dependency for every package that uses require, right?

@tomodachi94
Copy link
Author

tomodachi94 commented Dec 10, 2022

Yeah, having both library and ROM options makes a lot of sense. So the idea is that requiem is an optional package that can be installed by the end user on computers that would need it? We wouldn't need to take requiem as a dependency for every package that uses require, right?

Yep, pretty much. Originally, I was going to add the os.loadAPI shim to requiem, but having it in a separate package makes a lot more sense now.

This is needed to retain compatibility with
unpublished scripts that use os.loadAPI().
@tomodachi94
Copy link
Author

tomodachi94 commented Dec 10, 2022

@danports I've finished the os.loadAPI shim, this PR is ready for testing. I'm going to make an equivalent PR on prism-rails that should get merged after this one.

@danports
Copy link
Owner

Thanks! I'll take a look this weekend and see if I can test both this and the prism-rails PR. Some code in amber will likely need updating as well.

@tomodachi94 tomodachi94 marked this pull request as ready for review December 10, 2022 19:44
@danports
Copy link
Owner

Looking at this now...I think I need to figure out a solution for the amber client before I can go much further since I'll be able to pull the updated prism-core packages with the old amber but then if the amber client isn't compatible with those new packages, I'll have a broken installation. The autostartup solution is fine for daemon-type apps, but I'm not sure that's the right approach conceptually for command line apps like amber (well, amber is also a library, but let's leave that aside for now), since the startup.d directory mixes blocking (e.g. message loops) and non-blocking code. It seems like the options are:

  1. Add require-specific code to amber (and any other command line apps that don't use autostartup).
  2. Separate blocking and non-blocking code in autostartup.

What are your thoughts? I'm leaning towards 1️⃣ because I'm not sure it really makes sense to take a dependency on autostartup in command line apps when all we really want is require support (i.e. just one of the potential startup scripts). Maybe what we really want is something like a polyfills.d that gets loaded for command line apps and before startup.d for daemon apps. 🤔

@@ -0,0 +1,3 @@
package.path = package.path .. ";/modules/?;/modules/?.lua;/modules/?/init.lua"
local dns = require("dns")

Copy link
Owner

Choose a reason for hiding this comment

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

It seems like the rest of this file's contents are missing? Ditto for every other package converted to a module - it seems like only the new modules have any actual code.

Copy link
Author

@tomodachi94 tomodachi94 Dec 11, 2022

Choose a reason for hiding this comment

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

-.- Not sure what happened here. I'll try to fix this tomorrow.

Copy link
Author

Choose a reason for hiding this comment

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

I'll work on fix later today.

@@ -0,0 +1,3 @@
{
description = "Execute all scripts in the /startup.d folder"
Copy link
Owner

Choose a reason for hiding this comment

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

I'm wondering whether we should merge startupmgr with autostartup. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

Maybe. What are their differences?

@tomodachi94
Copy link
Author

In regards to Amber, I think we should do #1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

require polyfill
2 participants