1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00
Commit Graph

57 Commits

Author SHA1 Message Date
Jonathan Coates
7e74725de7 [skip ci] Waluaigi POC
See 12ef20d0fc/README.md
for the performance numbers, which are pretty untenable.
2021-05-03 17:54:11 +01:00
Jonathan Coates
9d1ee6f61d Remove m_ (#658)
IT'S GONE!

Not looking forward to the merge conflicts on this one.
2021-01-15 16:35:49 +00:00
Jonathan Coates
e4b0a5b3ce 2020 -> 2021
Oh, the most useless part of my build process.
2021-01-06 17:13:40 +00:00
SquidDev
183b342071 Bump for 1.91.0 2020-08-23 15:35:58 +01:00
SquidDev
b9ff9b7f90 Allow returning lua functions
Not sure how this will play with persistence when it happens (badly,
most likely), but it's not a bad idea to support it.

Closes #466
2020-06-03 21:44:08 +01:00
SquidDev
5a816917d5 Normalise config names 2020-05-15 23:04:04 +01:00
SquidDev
7af63d052d Make many more http options domain-specific
timetout, max_upload, max_download and max_websocket_message may now be
configured on a domain-by-domain basis. This uses the same system that
we use for the block/allow-list from before:

Example:

    [[http.rules]]
        host = "*"
        action = "allow"
	max_upload = 4194304
	max_download = 16777216
	timeout = 30000
2020-05-15 23:04:04 +01:00
Jonathan Coates
d5f82fa458
Replace getMethodNames/callMethod with annotations (#447)
When creating a peripheral or custom Lua object, one must implement two
methods:

 - getMethodNames(): String[] - Returns the name of the methods
 - callMethod(int, ...): Object[] - Invokes the method using an index in
   the above array.

This has a couple of problems:
 - It's somewhat unwieldy to use - you need to keep track of array
   indices, which leads to ugly code.
 - Functions which yield (for instance, those which run on the main
   thread) are blocking. This means we need to spawn new threads for
   each CC-side yield.

We replace this system with a few changes:

 - @LuaFunction annotation: One may annotate a public instance method
   with this annotation. This then exposes a peripheral/lua object
   method.

   Furthermore, this method can accept and return a variety of types,
   which often makes functions cleaner (e.g. can return an int rather
   than an Object[], and specify and int argument rather than
   Object[]).

 - MethodResult: Instead of returning an Object[] and having blocking
   yields, functions return a MethodResult. This either contains an
   immediate return, or an instruction to yield with some continuation
   to resume with.

   MethodResult is then interpreted by the Lua runtime (i.e. Cobalt),
   rather than our weird bodgey hacks before. This means we no longer
   spawn new threads when yielding within CC.

 - Methods accept IArguments instead of a raw Object array. This has a
   few benefits:
   - Consistent argument handling - people no longer need to use
     ArgumentHelper (as it doesn't exist!), or even be aware of its
     existence - you're rather forced into using it.
   - More efficient code in some cases. We provide a Cobalt-specific
     implementation of IArguments, which avoids the boxing/unboxing when
     handling numbers and binary strings.
2020-05-15 13:21:16 +01:00
SquidDev
cb8135a0d1 Bump Cobalt version
- Remove stub for table.pack/table.unpack.
 - Remove Lua 5.3 bitlib stub. We're not on 5.3, there's no
   point emulating it.
 - Change peripheral.call to correctly adjust the error level. This is a
   terrible hack, but I believe the only good option.

It'd be good to remove load as well, but it's a little more complex due
to our injecting of _ENV.

Closes #363
2020-04-16 10:48:26 +01:00
SquidDev
4c8fd4fc35 Allow returning collections and arrays from Java
Closes #344
2020-01-13 13:20:15 +00:00
SquidDev
93a9ebc4f6 Happy new year 2020-01-01 00:09:18 +00:00
SquidDev
c311cdc6f5 Make our Javadoc validation a little stricter
I'm not sure there's much utility in this, but still feels worth doing.
2019-10-27 15:16:47 +00:00
SquidDev
a0e7c4a74c Add a little bit of source code checking to Gradle
- Adds a CheckStyle configuration which is pretty similar to CC's
   existing one.
 - Add the Gradle license plugin.
 - Ensure the existing source code is compatible with these additional
   checks.

See #239
2019-06-08 00:28:03 +01:00
SquidDev
f93da7ea51 Bump Cobalt version
I promise! The joys of using -SNAPSHOT I guess...

This will now correctly cause orphaned threads to be cleaned up,
reducing the risk of thread saturation.
2019-04-18 09:49:52 +01:00
SquidDev
173ea72001 Turn inspections up to 11
OK, so let's get this out of the way, there's some actual changes mixed
in here too. I'm really sorry:
 - Turtles can now not be renamed with unnamed item tags (previously it
   would clear the name, this seemed a little unideal).
 - commands.getBlock(s)Data will also include NBT.

Now, onto the horror story which is these inspection changes:
 - Make a lot of methods static
 - Typo fixes
 - Make utility classes final + private constructor
 - Lots of reformatting (ifs -> ternary, invert control flow, etc...)
 - ???
 - Profit!

I'm so going to regret this - can pretty much guarantee this is going to
break something.
2019-03-29 21:26:21 +00:00
SquidDev
853e2622a1 An initial prototype of main thread rate limiting
Unlike ComputerThread, we do not have a single source of tasks, and so
need a smarter way to handle scheduling and rate limiting. This
introduces a cooldown system, which works on both a global and
per-computer level:

Each computer is allowed to do some work for 5ms. If they go over that
budget, then they are marked as "hot", and will not execute work on the
next tick, until they have cooled down. This ensures that _on average_
computers perform at most 5ms of work per tick.

Obviously this is a rather large time span, so we also apply a global
10ms to all computers. This uses the same cooldown principle, meaning we
keep to an average of 10ms, even if we go over budget.
2019-03-26 11:21:16 +00:00
SquidDev
5b942ff9c1 Some changes to Lua machines and string loading
- Share the ILuaContext across all method calls, as well as shifting it
   into an anonymous class.
 - Move the load/loadstring prefixing into bios.lua
 - Be less militant in prefixing chunk names:
   - load will no longer do any auto-prefixing.
   - loadstring will not prefix when there no chunk name is supplied.
     Before we would do `"=" .. supplied_program`, which made no sense.
2019-03-10 12:24:55 +00:00
SquidDev
b3e6a53868 Allow pausing Lua machines
TimeoutState now introduces a TIMESLICE, which is the maximum period of
time a computer can run before we will look into pausing it.

When we have executed a task for more than this period, and if there are
other computers waiting to execute work, then we will suspend the
machine.

Suspending the machine sets a flag on the ComputerExecutor, and pauses
the "cumulative" time - the time spent handling this particular event.
When resuming the machine, we restart our timer and resume the machine.
2019-02-28 16:49:06 +00:00
SquidDev
218f8e53bb Further improvements to computer execution
Oh goodness, when will it end?

 - Computer errors are shown in red.

 - Lua machine operations provide whether they succeeded, and an
   optional error message (reason bios failed to load, timeout error,
   another Lua error), which is then shown to the user.

 - Clear the Cobalt "thrown soft abort" flag when resuming, rather than
   every n instructions.

 - Computers will clear their "should start" flag once the time has
   expired, irrespective of whether it turned on or not. Before
   computers would immediately restart after shutting down if the flag
   had been set much earlier.

Errors within the Lua machine are displayed in a more friendly
2019-02-28 15:44:43 +00:00
SquidDev
d02575528b Fix leaking file descriptors when closing
When closing a BufferedWriter, we close the underlying writer. As we're
using channels, this is an instance of sun.nio.cs.StreamEncoder. This
will attempt to flush the pending character.

However, if throwing an exception within .write errors, the flush will
fail and so the underlying stream is not closed. This was causing us to
leak file descriptors.

We fix this by introducing ChannelWrappers - this holds the wrapper
object (say, a BufferedWriter) and underlying channel. When closed, we
dispose of the wrapper, and then the channel. You could think of this as
doing a nested try-with-resources, rather than a single one.

Note, this is not related to JDK-6378948 - this occurs in the underlying
stream encoder instead.
2019-02-28 11:24:12 +00:00
SquidDev
c78adb2cdc Several improvements to the computer thread rework
- TimeoutState uses nanoseconds rather than milliseconds. While this is
   slightly less efficient on Windows, it's a) not the bottleneck of Lua
   execution and b) we need a monotonic counter, otherwise we could
   fail to terminate computers if the time changes.
 - Add an exception handler to all threads.
 - Document several classes a little better - I'm not sure how useful
   all of these are, but _hopefully_ it'll make the internals a little
   more accessible.
2019-02-27 20:56:45 +00:00
SquidDev
06e76f9b15 Rewrite ComputerThread and the timeout system
- Instead of setting soft/hard timeouts on the ILuaMachine, we instead
   provide it with a TimeoutState instance. This holds the current abort
   flags, which can then be polled within debug hooks.

   This means the Lua machine has to do less state management, but also
   allows a more flexible implementation of aborts.

 - Soft aborts are now handled by the TimeoutState - we track when the
   task was started, and now only need to check we're more than 7s since
   then.

   Note, these timers work with millisecond granularity, rather than
   nano, as this invokes substantially less overhead.

 - Instead of having n runners being observed with n managers, we now
   have n runners and 1 manager (or Monitor).

   The runners are now responsible for pulling work from the queue. When
   the start to execute a task, they set the time execution commenced.
   The monitor then just checks each runner every 0.1s and handles hard
   aborts (or killing the thread if need be).
2019-02-26 13:46:10 +00:00
SquidDev
6d383d005c A couple of minor tweaks
- Rename unload -> close to be a little more consistent
 - Make pollAndResetChanged be atomic, so we don't need to aquire a lock
 - Get the computer queue from the task owner, rather than a separate
   argument.
2019-02-26 12:43:45 +00:00
SquidDev
fb9c125ab8 Update Cobalt version for latest bugfixes/changes
- Fix load not unwinding stack
 - Allow transferring control between coroutines when unwinding is
   disabled

Fixes #128
2019-02-25 14:18:45 +00:00
SquidDev
12e82afad2 Bump Cobalt version to enable single-threading
The latest version of Cobalt has several major changes, which I'm
looking forward to taking advantage of in the coming months:

 - The Lua interpreter has been split up from the actual LuaClosure
   instance. It now runs multiple functions within one loop, handling
   pushing/popping and resuming method calls correctly.

   This means we have a theoretically infinite call depth, as we're no
   longer bounded by Java's stack size. In reality, this is limited to
   32767 (Short.MAX_VALUE), as that's a mostly equivalent to the limits
   PUC Lua exposes.

 - The stack is no longer unwound in the event of errors. This both
   simplifies error handling (not that CC:T needs to care about that)
   but also means one can call debug.traceback on a now-dead coroutine
   (which is more useful for debugging than using xpcall).

 - Most significantly, coroutines are no longer each run on a dedicated
   thread. Instead, yielding or resuming throws an exception to unwind
   the Java stack and switches to a different coroutine.

   In order to preserve compatability with CC's assumption about LuaJ's
   threading model (namely that yielding blocks the thread), we also
   provide a yieldBlock method (which CC:T consumes). This suspends the
   current thread and switches execution to a new thread (see
   SquidDev/Cobalt@b5ddf164f1 for more
   details). While this does mean we need to use more than 1 thread,
   it's still /substantially/ less than would otherwise be needed.

We've been running these changes on SwitchCraft for a few days now and
haven't seen any issues. One nice thing to observe is that the number of
CC thread has gone down from ~1.9k to ~100 (of those, ~70 are dedicated
to running coroutines). Similarly, the server has gone from generating
~15k threads over its lifetime, to ~3k. While this is still a lot, it's
a substantial improvement.
2019-02-10 22:02:30 +00:00
SquidDev
f450c0156b Recomment out a global
It's actually rather worrying this was exposed, you could cause some
serious issues with it.
2019-02-07 20:22:00 +00:00
SquidDev
1c9110b927 Happy new year! 2019-01-01 01:10:18 +00:00
SquidDev
2032e7a83a Reformat everything
It's been a long time comin'
But tonight is the end of the war, my friend
Tomorrow only one style will remain.
2018-12-23 17:46:58 +00:00
SquidDev
8ae65d1bdd A couple of minor cleanups on the last commit
- Remove a redundant logger
 - Provide a getter for the ComputerCraft thread group. This allows us
   to monitor child threads within prometheus.
 - Replace a deprecated call with a fastutils alternative.
2018-11-22 12:48:06 +00:00
SquidDev
71ee692da0 Some more improvements to threading
- Put all ComputerCraft threads into a thread group
 - Be a little more aggressive in when we cleanup/abandon threads.
2018-11-22 12:09:24 +00:00
SquidDev
93cb6547bd Improvements for coroutine creation
- Keep track of the number of created and destroyed coroutines for each
   computer.
 - Run coroutines with a thread pool executor, which will keep stale
   threads around for 60 seconds. This substantially reduces the
   pressure from short-lived coroutines.
 - Update to the latest Cobalt version.
2018-11-21 16:47:06 +00:00
SquidDev
e555f9f7f0 Merge pull request #575 from SquidDev-CC/ComputerCraft/feature/file-seeking
Rewrite file systems to use ByteChannels
2018-10-24 12:20:53 +01:00
SquidDev
518eefbe10 Rewrite file systems to use ByteChannels
This replaces the existing IMount openFor* method with openChannelFor*
ones, which return an appropriate byte channel instead.

As channels are not correctly closed when GCed, we introduce a
FileSystemWrapper. We store a weak reference to this, and when it is
GCed or the file closed, we will remove it from our "open file" set and
ensure any underlying buffers are closed.

While this change may seem a little odd, it does introduce some
benefits:

 - We can replace JarMount with a more general FileSystemMount. This
   does assume a read-only file system, but could technically be used
   for other sources.

 - Add support for seekable (binary) handles. We can now look for
   instances of SeekableByteChannel and dynamically add it. This works
   for all binary filesystem and HTTP streams.

 - Rewrite the io library to more accurately emulate PUC Lua's
   implementation. We do not correctly implement some elements (most
   noticably "*n", but it's a definite improvement.
2018-09-26 10:00:17 +01:00
SquidDev
add86ea100 Merge pull request #491 from SquidDev-CC/ComputerCraft/feature/api-api
Provide an API for registering custom APIs
2017-12-01 19:18:40 +00:00
SquidDev
55847460c5 Provide an API for registering custom APIs
ILuaAPI has been moved to dan200.computercraft.api.lua. One creates
a new API by registering an instance of ILuaAPIFactory. This takes an
instance of IComputerSystem and returns such an API.

IComputerSystem is an extension of IComputerAccess, with methods to
access additional information about the the computer, such as its label
and filesystem.
2017-11-19 18:23:38 +00:00
SquidDev
ed8e9d7817 Add support for enabling Lua's debug library
Whilst I'm pretty sure this is safe for general use, I'm disabling this
by default for now. I may consider enabling it in the future if no
issues are found.
2017-11-15 12:18:10 +00:00
SquidDev
540e2e25aa Merge pull request #163 from SquidDev-CC/ComputerCraft/feature/cobalt
Replace LuaJ with Cobalt
2017-11-14 21:48:47 +00:00
SquidDev
060fb21bdb Merge pull request #298 from SquidDev-CC/ComputerCraft/feature/luaj-bit32
Replace BitAPI with a LuaJ implementation of bit32
2017-11-14 21:32:19 +00:00
SquidDev
0bd0f4d313 Prefix all loaded strings with "="
Whilst this is not consistent with normal Lua, this is required in order
to remain compatible with LuaJ.
2017-11-14 18:41:01 +00:00
SquidDev
73873eb8cb Change timeout system to occur on instructions instead of API calls
This means loops which do not touch CC specific methods will still
produce an error, rather than terminating the computer.
2017-11-14 18:41:00 +00:00
SquidDev
bb741975b7 Migrate LuaJLuaMachine to use Cobalt 2017-11-14 18:41:00 +00:00
SquidDev
d29ffed383 Java 8. Java 8. Does whatever Java 8 can.
Default methods, everywhere.
Arrow types, switch on strings.
Lambdas!
Here comes Java 8.
2017-07-25 21:10:48 +01:00
SquidDev
6fca136327 Move Bit32 library to LuaJ sources 2017-06-28 23:05:48 +01:00
SquidDev
084bbe8480 Replace BitAPI with a LuaJ implementation of bit32 2017-06-17 21:39:12 +01:00
Daniel Ratcliffe
ba4b1e21fe Merge pull request #238 from SquidDev-CC/feature/binary-handles
Refactor the filesystem and HTTP code
2017-05-16 20:02:34 +01:00
SquidDev
efb0065ebd Add config option to disable certain error messages 2017-05-16 15:59:09 +01:00
SquidDev
a0b6cbb671 Remove ILogger facade
Just use global Log4j logger on the ComputerCraft class.
2017-05-16 15:49:55 +01:00
SquidDev
44ba4069c1 Print stack traces/add logging messages in several places
This will hopefully make it easier to track down various issues which
might otherwise go unnoticed or provide little information.

The main areas modified are those that external APIs may provide values
for or interact with: various providers and ILuaObject/IPeripheral
implementations. However, we do also log in a couple of other places
which indicate a problem with this, or another, mod.
2017-05-16 15:49:55 +01:00
SquidDev
6ccffe9742 Refactor the filesystem and HTTP code
- Move the encoding/decoding from the Filesystem implementation to the
   individual handles.
 - Move each handle into an core.apis.handles package from the main fs
   API.
 - Move the HTTP response to inherit from these handles.
 - Allow binary handles' read function to accept a number, specifying
   how many characters to read - these will be returned as a Lua string.
 - Add readAll to binary handles
 - Allow binary handles' write function to accept a string which is
   decoded into the individual bytes.
 - Add "binary" argument to http.request and friends in order to return
   a binary handle.
 - Ensure file handles are open when reading from/writing to them.
 - Return the error message when opening a file fails.
2017-05-13 22:47:28 +01:00
ObloxCC
7f754f33bb Updated (C) from 2016 to 2017 (#229)
* Updated (C)

* Fixed Whitespace

bugs, bugs, and more bugs

* Fixed Whitespaces 1/2

* Fixed Whitespaces 2/2

* Fixed Whitespaces 3/2
2017-05-13 19:20:39 +01:00