Advertisement
SforzandoCF

lx01

Mar 9th, 2025 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. module lx01.core
  2.  
  3. import lx01.reflect as Ref
  4.  
  5. module root class RootObject {
  6. RootObject new () {}
  7.  
  8. void init () {}
  9. }
  10.  
  11. class Object extends RootObject {
  12. Object new () {
  13. int size = getDataSize()
  14. Ref.Pointer dataPointer = Pointer.new(size)
  15. Object obj = System.createObject(dataPointer)
  16. return obj
  17. }
  18.  
  19. private int getDataSize () {
  20. Ref.Class this = System.getCallerClass()
  21. int sum = 0
  22. for Ref.Variable v : this.getVariables() {
  23. switch v.type():
  24. case Ref.Type.Byte {
  25. sum += 1
  26. } case Ref.Type.Short {
  27. sum += 2
  28. } case Ref.Type.Int {
  29. sum += 4
  30. } case Ref.Type.Long {
  31. sum += 8
  32. } case Ref.Type.Float {
  33. sum += 4
  34. } case Ref.Type.Double {
  35. sum += 8
  36. } case Ref.Type.Char {
  37. sum += 4
  38. } case Ref.Type.String {
  39. sum += 2
  40. } case Ref.Type.Object {
  41. sum += v.getTypeClass().getSize()
  42. } case Ref.Type.Array {
  43. sum += 3
  44. }
  45. }
  46. }
  47.  
  48. instance boolean equals (Object other) {
  49. return other.getClass() == instance.getClass() & other.getHashCode() == this.getHashCode()
  50. }
  51.  
  52. instance Class getClass () {
  53. return System.getCallerClass()
  54. }
  55.  
  56. instance int getHashCode () {
  57. return System.getDataPointer(instance).getHashCode()
  58. }
  59.  
  60. final instance void wake () {
  61. Ref.Monitor.get(instance).wakeAll()
  62. }
  63.  
  64. instance string toString () {
  65. return "name=" + instance.getClass().toString() + ",pointer=" + instance.getHashCode()
  66. }
  67.  
  68. final instance void sleep () {
  69. Ref.Monitor.get(instance).addWaitHook(Ref.Function.getCaller()).wait()
  70. }
  71.  
  72. final instance void sleep (long milliseconds) {
  73. Object caller = Ref.Function.getCaller()
  74. System.scheduleAfter(milliseconds, () -> {
  75. Ref.Monitor.get(instance).wake(caller)
  76. })
  77. instance.sleep()
  78. }
  79. }
  80.  
  81. root class System {
  82. private Map<Object, Ref.Pointer> pointers = Maps.create()
  83. optional(extra) <T> T[] copyContents (T[] data, optional(extra) int offset, optional(extra) int length) {
  84. int oOffset = extra.isPresent() ? offset : 0
  85. int oLength = extra.isPresent() ? length : data.length
  86. T[] new = T.new[]
  87. for int i from 0 to oLength {
  88. new[i] = data[i + oOffset]
  89. }
  90. return new
  91. }
  92.  
  93. java(java.base:java.lang.System) int getTime () {
  94. return java.currentTimeMillis()
  95. }
  96.  
  97. optional(gError) java(java.base:java.lang.System) void exit (optional(gError) Error error) {
  98. int errorCode = gError.isPresent() ? error.getErrorCode() : 0
  99. java.exit(errorCode)
  100. }
  101.  
  102. Ref.Class getCallerClass () {
  103. return Ref.Function.getCaller().getContainer()
  104. }
  105.  
  106. Object createObject (Ref.Pointer target) {
  107. if pointers.hasValue(target) return
  108. Object obj = new Object
  109. pointers.put(obj, target)
  110. return obj.init()
  111. }
  112.  
  113. Ref.Pointer getDataPointer (Object obj) {
  114. return pointers.get(obj)
  115. }
  116.  
  117. void scheduleAt (long millis, Task task) {
  118. Thread.new(() -> {
  119. while (System.getTime() < millis) {
  120. Thread.wait(1)
  121. }
  122. task.execute()
  123. }).start()
  124. }
  125.  
  126. void scheduleAfter (long millis, Task task) {
  127. scheduleAt(millis + getTime(), task)
  128. }
  129. }
  130.  
  131. class Thread {
  132. private instance java(java.base:java.lang.Thread) bridge
  133.  
  134. private final instance Task task
  135. private final instance String name
  136.  
  137. Thread new () {
  138. Thread this = (Thread) super.new()
  139. this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread", () -> {
  140. lx01.object.task.execute();
  141. }, 0L)
  142. this.task = () -> {}
  143. this.name = ""
  144. }
  145.  
  146. Thread new (string name) {
  147. Thread this = (Thread) super.new()
  148. this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread: " + lx01.params.name, () -> {
  149. lx01.object.task.execute();
  150. }, 0L)
  151. this.task = () -> {}
  152. this.name = name
  153. }
  154.  
  155. Thread new (Task task) {
  156. Thread this = (Thread) super.new()
  157. this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread", () -> {
  158. lx01.object.task.execute();
  159. }, 0L)
  160. this.task = task
  161. this.name = ""
  162. }
  163.  
  164. Thread new (string name, Task task) {
  165. Thread this = (Thread) super.new()
  166. this.bridge = java(com.sforzando.lx01.LX01Constants.getThreadGroup(), "LX01 thread: " + lx01.params.name, () -> {
  167. lx01.object.task.execute();
  168. }, 0L)
  169. this.task = task
  170. this.name = name
  171. }
  172.  
  173. java(java.base:java.lang.Thread) Thread thisThread () {
  174. Thread this = byPlatform(java.currentThread())
  175. if this == null throw new IllegalStateException("Current thread is not an LX01 thread")
  176. return this
  177. }
  178.  
  179. instance Thread createCopy () {
  180. return Thread.new(instance.name, instance.task)
  181. }
  182.  
  183. instance void printStackTrace () {
  184. instance.printStackTrace(Console.getError())
  185. }
  186.  
  187. instance void printStackTrace (TextOutput out) {
  188. out.print(instance.getStackTrace().toString())
  189. }
  190.  
  191. java(java.base:java.lang.Thread) Thread[] getActiveThreads () {
  192. Thread[] threads = Thread.new[]
  193. java[] array = java.new[java.activeCount]
  194. int retrieved = 0
  195. while array.length >= retrieved
  196. retrieved = java.enumerate(array)
  197. for java j : array {
  198. Thread t = byPlatform(j)
  199. if (t == null) continue
  200. threads.add(t)
  201. }
  202. return threads
  203. }
  204.  
  205. Map<Thread, StackTrace> getAllStackTraces () {
  206. Map<Thread, StackTrace> traces = Maps.create()
  207. for (Thread t : getActiveThreads())
  208. traces.put(t, t.getStackTrace())
  209. return traces
  210. }
  211.  
  212. instance void getExceptionManager () {
  213. return ExceptionHandler.fromJava(instance.bridge.getUncaughtExceptionHandler())
  214. }
  215.  
  216. instance long getThreadID () {
  217. return instance.bridge.threadId()
  218. }
  219.  
  220. instance string getName () {
  221. return instance.name
  222. }
  223.  
  224. java(java.base:java.lang.Thread.State) instance ThreadState getState () {
  225. java state = instance.bridge.getState()
  226. switch state:
  227. case java.BLOCKED {
  228. return ThreadState.WAITING
  229. } case java.NEW {
  230. return ThreadState.NOT_STARTED
  231. } case java.RUNNABLE {
  232. return ThreadState.RUNNING
  233. } case java.TERMINATED {
  234. return ThreadState.STOPPED
  235. } case java.WAITING {
  236. return ThreadState.WAITING
  237. } case java.TIMED_WAITING {
  238. return ThreadState.WAITING
  239. } alt case {
  240. return ThreadState.UNKNOWN
  241. }
  242. }
  243.  
  244. instance boolean isMonitorOwner (Object monitored) {
  245. return Ref.Monitor.get(monitored).getOwnerThread() == instance
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement