Advertisement
SforzandoCF

lx01

Mar 9th, 2025 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. module lx01.core
  2.  
  3. import lx01.io as IO
  4. import lx01.reflect as Ref
  5.  
  6. class System {
  7. private Ref.JavaBridge bridge = Ref.JavaBridge.new()
  8. private GarbageCollector gc = GarbageCollector.new()
  9.  
  10. private Thread[] hooks = Thread.new[]
  11.  
  12. void exit (int status) {
  13. bridge.execute("java.lang.System", "exit", Object.new[status])
  14. }
  15.  
  16. void runGarbageCollector () {
  17. gc.execute()
  18. hooks.forEach(t -> t.start)
  19. }
  20.  
  21. boolean addShutdownHook (Thread thread) {
  22. return hooks.add(thread)
  23. }
  24.  
  25. boolean removeShutdownHook (Thread thread) {
  26. return hooks.remove(thread)
  27. }
  28.  
  29. long execute (string command, optional string[] settings, optional IO.File directory) throws IO.IOException {
  30. execute(command.split(" "), settings.isPresent() ? {settings} | {null}, directory.isPresent() ? {directory} | {null})
  31. }
  32.  
  33. long execute (string[] command, optional string[] settings, optional IO.File directory) throws IO.IOException {
  34. string[] osettings = settings.isPresent() ? {settings} : {null}
  35. IO.File[] ofile = directory.isPresent() ? {directory} : {null}
  36. if (command == null or command.isEmpty()) throw NullValueException.new("No command supplied to execute")
  37. for (string s in command)
  38. if (s == null)
  39. throw NullValueException.new("Commands contain null values")
  40. for (string s in osettings)
  41. if (s == null)
  42. throw NullValueException.new("Settings contain null values")
  43. try {
  44. bridge.execute("java.lang.Runtime", "getRuntime").execute("exec", Object.new[command, osettings, ofile]).execute("pid").asLong()
  45. } catch Ref.BridgeInvocationException as bie {
  46. if (bie.getType() == "java.io.IOException")
  47. throw IO.IOException.new(bie.getMessage())
  48. if (bie.getType() == "java.lang.UnsupportedOperationException")
  49. throw NotSupportedException.new(bie.getMessage())
  50. if (bie.getType() == "java.lang.SecurityException")
  51. return -1
  52. throw Ref.JavaBridgeError.new()
  53. }
  54. }
  55.  
  56. long getAvailableMemory () {
  57. try {
  58. bridge.execute("java.lang.Runtime", "getRuntime").execute("freeMemory").asLong()
  59. } catch Ref.BridgeInvocationException as bie {
  60. throw Ref.JavaBridgeError.new()
  61. }
  62. }
  63.  
  64. long getActiveMemory () {
  65. try {
  66. getUsableMemory() - getAvailableMemory()
  67. } catch Ref.BridgeInvocationException as bie {
  68. throw Ref.JavaBridgeError.new()
  69. }
  70. }
  71.  
  72. long getUsableMemory () {
  73. try {
  74. bridge.execute("java.lang.Runtime", "getRuntime").execute("maxMemory").asLong()
  75. } catch Ref.BridgeInvocationException as bie {
  76. throw Ref.JavaBridgeError.new()
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement