Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module lx01.core
- import lx01.io as IO
- import lx01.reflect as Ref
- class System {
- private Ref.JavaBridge bridge = Ref.JavaBridge.new()
- private GarbageCollector gc = GarbageCollector.new()
- private Thread[] hooks = Thread.new[]
- void exit (int status) {
- bridge.execute("java.lang.System", "exit", Object.new[status])
- }
- void runGarbageCollector () {
- gc.execute()
- hooks.forEach(t -> t.start)
- }
- boolean addShutdownHook (Thread thread) {
- return hooks.add(thread)
- }
- boolean removeShutdownHook (Thread thread) {
- return hooks.remove(thread)
- }
- long execute (string command, optional string[] settings, optional IO.File directory) throws IO.IOException {
- execute(command.split(" "), settings.isPresent() ? {settings} | {null}, directory.isPresent() ? {directory} | {null})
- }
- long execute (string[] command, optional string[] settings, optional IO.File directory) throws IO.IOException {
- string[] osettings = settings.isPresent() ? {settings} : {null}
- IO.File[] ofile = directory.isPresent() ? {directory} : {null}
- if (command == null or command.isEmpty()) throw NullValueException.new("No command supplied to execute")
- for (string s in command)
- if (s == null)
- throw NullValueException.new("Commands contain null values")
- for (string s in osettings)
- if (s == null)
- throw NullValueException.new("Settings contain null values")
- try {
- bridge.execute("java.lang.Runtime", "getRuntime").execute("exec", Object.new[command, osettings, ofile]).execute("pid").asLong()
- } catch Ref.BridgeInvocationException as bie {
- if (bie.getType() == "java.io.IOException")
- throw IO.IOException.new(bie.getMessage())
- if (bie.getType() == "java.lang.UnsupportedOperationException")
- throw NotSupportedException.new(bie.getMessage())
- if (bie.getType() == "java.lang.SecurityException")
- return -1
- throw Ref.JavaBridgeError.new()
- }
- }
- long getAvailableMemory () {
- try {
- bridge.execute("java.lang.Runtime", "getRuntime").execute("freeMemory").asLong()
- } catch Ref.BridgeInvocationException as bie {
- throw Ref.JavaBridgeError.new()
- }
- }
- long getActiveMemory () {
- try {
- getUsableMemory() - getAvailableMemory()
- } catch Ref.BridgeInvocationException as bie {
- throw Ref.JavaBridgeError.new()
- }
- }
- long getUsableMemory () {
- try {
- bridge.execute("java.lang.Runtime", "getRuntime").execute("maxMemory").asLong()
- } catch Ref.BridgeInvocationException as bie {
- throw Ref.JavaBridgeError.new()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement