Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module lx01.core
- import lx01.reflect as Ref
- module root class RootObject {
- RootObject new () {}
- void init () {}
- }
- class Object extends RootObject {
- Object new () {
- int size = getDataSize()
- Ref.Pointer dataPointer = Pointer.new(size)
- Object obj = System.createObject(dataPointer)
- return obj
- }
- private int getDataSize () {
- Ref.Class this = System.getCallerClass()
- int sum = 0
- for Ref.Variable v : this.getVariables() {
- switch v.type():
- case Ref.Type.Byte {
- sum += 1
- } case Ref.Type.Short {
- sum += 2
- } case Ref.Type.Int {
- sum += 4
- } case Ref.Type.Long {
- sum += 8
- } case Ref.Type.Float {
- sum += 4
- } case Ref.Type.Double {
- sum += 8
- } case Ref.Type.Char {
- sum += 4
- } case Ref.Type.String {
- sum += 2
- } case Ref.Type.Object {
- sum += v.getTypeClass().getSize()
- } case Ref.Type.Array {
- sum += 3
- }
- }
- }
- instance boolean equals (Object other) {
- return other.getClass() == instance.getClass() & other.getHashCode() == this.getHashCode()
- }
- instance Class getClass () {
- return System.getCallerClass()
- }
- instance int getHashCode () {
- return System.getDataPointer(instance).getHashCode()
- }
- final instance void wake () {
- Ref.Monitor.get(instance).wakeAll()
- }
- instance string toString () {
- return "name=" + instance.getClass().toString() + ",pointer=" + instance.getHashCode()
- }
- final instance void sleep () {
- Ref.Monitor.get(instance).addWaitHook(Ref.Function.getCaller()).wait()
- }
- final instance void sleep (long milliseconds) {
- Object caller = Ref.Function.getCaller()
- System.scheduleAfter(milliseconds, () -> {
- Ref.Monitor.get(instance).wake(caller)
- })
- instance.sleep()
- }
- }
- root class System {
- private Map<Object, Ref.Pointer> pointers = Maps.create()
- optional(extra) <T> T[] copyContents (T[] data, optional(extra) int offset, optional(extra) int length) {
- int oOffset = extra.isPresent() ? offset : 0
- int oLength = extra.isPresent() ? length : data.length
- T[] new = T.new[]
- for int i from 0 to oLength {
- new[i] = data[i + oOffset]
- }
- return new
- }
- java(java.base:java.lang.System) int getTime () {
- return java.currentTimeMillis()
- }
- optional(gError) java(java.base:java.lang.System) void exit (optional(gError) Error error) {
- int errorCode = gError.isPresent() ? error.getErrorCode() : 0
- java.exit(errorCode)
- }
- Ref.Class getCallerClass () {
- return Ref.Function.getCaller().getContainer()
- }
- Object createObject (Ref.Pointer target) {
- if pointers.hasValue(target) return
- Object obj = new Object
- pointers.put(obj, target)
- return obj.init()
- }
- Ref.Pointer getDataPointer (Object obj) {
- return pointers.get(obj)
- }
- void scheduleAt (long millis, Task task) {
- Thread.new(() -> {
- while (System.getTime() < millis) {
- Thread.wait(1)
- }
- task.execute()
- }).start()
- }
- void scheduleAfter (long millis, Task task) {
- scheduleAt(millis + getTime(), task)
- }
- }
- class Thread {
- private instance java(java.base:java.lang.Thread) bridge
- private final instance Task task
- private final instance String name
- Thread new () {
- Thread this = (Thread) super.new()
- this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread", () -> {
- lx01.object.task.execute();
- }, 0L)
- this.task = () -> {}
- this.name = ""
- }
- Thread new (string name) {
- Thread this = (Thread) super.new()
- this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread: " + lx01.params.name, () -> {
- lx01.object.task.execute();
- }, 0L)
- this.task = () -> {}
- this.name = name
- }
- Thread new (Task task) {
- Thread this = (Thread) super.new()
- this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread", () -> {
- lx01.object.task.execute();
- }, 0L)
- this.task = task
- this.name = ""
- }
- Thread new (string name, Task task) {
- Thread this = (Thread) super.new()
- this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread: " + lx01.params.name, () -> {
- lx01.object.task.execute();
- }, 0L)
- this.task = task
- this.name = name
- }
- java(java.base:java.lang.Thread) Thread thisThread () {
- Thread this = byPlatform(java.currentThread())
- if this == null throw new IllegalStateException("Current thread is not an LX01 thread")
- return this
- }
- instance Thread createCopy () {
- return Thread.new(instance.name, instance.task)
- }
- instance void printStackTrace () {
- instance.printStackTrace(Console.getError())
- }
- instance void printStackTrace (TextOutput out) {
- out.print(instance.getStackTrace().toString())
- }
- java(java.base:java.lang.Thread) Thread[] getActiveThreads () {
- Thread[] threads = Thread.new[]
- java[] array = java.new[java.activeCount]
- int retrieved = 0
- while array.length >= retrieved
- retrieved = java.enumerate(array)
- for java j : array {
- Thread t = byPlatform(j)
- if (t == null) continue
- threads.add(t)
- }
- return threads
- }
- Map<Thread, StackTrace> getAllStackTraces () {
- Map<Thread, StackTrace> traces = Maps.create()
- for (Thread t : getActiveThreads())
- traces.put(t, t.getStackTrace())
- return traces
- }
- instance void getExceptionManager () {
- return ExceptionHandler.fromJava(instance.bridge.getUncaughtExceptionHandler())
- }
- instance long getThreadID () {
- return instance.bridge.threadId()
- }
- instance string getName () {
- return instance.name
- }
- java(java.base:java.lang.Thread.State) instance ThreadState getState () {
- java state = instance.bridge.getState()
- switch state:
- case java.BLOCKED {
- return ThreadState.WAITING
- } case java.NEW {
- return ThreadState.NOT_STARTED
- } case java.RUNNABLE {
- return ThreadState.RUNNING
- } case java.TERMINATED {
- return ThreadState.STOPPED
- } case java.WAITING {
- return ThreadState.WAITING
- } case java.TIMED_WAITING {
- return ThreadState.WAITING
- } alt case {
- return ThreadState.UNKNOWN
- }
- }
- instance boolean isMonitorOwner (Object monitored) {
- return Ref.Monitor.get(monitored).getOwnerThread() == instance
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement