From a87a161e5791de722f002f787f79c5eff1757e58 Mon Sep 17 00:00:00 2001 From: Jotham Wong <45916998+JothamWong@users.noreply.github.com> Date: Wed, 17 Apr 2024 01:19:14 +0800 Subject: [PATCH] Add time.Sleep() (#19) * Add time.Sleep() * Temporarily bandaid GC tests Note that adding to the standard library increases the min number of words that Ooga needs to operate --- src/tests/test.ts | 6 +++--- src/vm/oogavm-machine.ts | 9 +++++---- src/vm/oogavm-typechecker.ts | 1 + std/ooga-std.ooga | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/tests/test.ts b/src/tests/test.ts index a5eb3a2..9aba2b9 100644 --- a/src/tests/test.ts +++ b/src/tests/test.ts @@ -477,7 +477,7 @@ x; `, 5, '', - 300 + 400 ); // Testing mark and sweep, with exactly one variable that should be freed @@ -497,7 +497,7 @@ x; `, 5, '', - 220 + 400 ); // Testing structs (no methods for now) @@ -1044,7 +1044,7 @@ go googa(x, y); `, true, '15', - 206 + 400 ); // Test various array expressions diff --git a/src/vm/oogavm-machine.ts b/src/vm/oogavm-machine.ts index dd203f7..ebfa73d 100644 --- a/src/vm/oogavm-machine.ts +++ b/src/vm/oogavm-machine.ts @@ -307,9 +307,10 @@ export const builtinMappings = { isAtomicSection = false; return True; }, - // "make": () => { - // // TODO: Support channels as priority number 1 - // } + getTime: () => { + // Get unix time in millis + return TSValueToAddress(Date.now()); + } }; class Builtin { @@ -1245,7 +1246,6 @@ function runInstruction() { // printStringPoolMapping(); } -// TODO: Switch to low level memory representation export function run(numWords = 1000000) { initialize(numWords); while (running) { @@ -1267,6 +1267,7 @@ export function run(numWords = 1000000) { log('Program value is ' + returnValue); log('After STD initialization: '); printHeapUsage(); + debugHeap(); log('Return value: ' + returnValue); return returnValue; } diff --git a/src/vm/oogavm-typechecker.ts b/src/vm/oogavm-typechecker.ts index b23939f..64c7aad 100644 --- a/src/vm/oogavm-typechecker.ts +++ b/src/vm/oogavm-typechecker.ts @@ -120,6 +120,7 @@ const global_type_frame = { blockThread: new FunctionType([], new NullType()), getThreadID: new FunctionType([], new IntegerType()), oogaError: new FunctionType([], new NullType()), + getTime: new FunctionType([], new IntegerType()), }; const empty_type_environment = null; diff --git a/std/ooga-std.ooga b/std/ooga-std.ooga index f450c1a..0e37b59 100644 --- a/std/ooga-std.ooga +++ b/std/ooga-std.ooga @@ -63,3 +63,19 @@ func (m *Mutex) Unlock() { } var fmt format = format{} + + +type Time struct { +} + +// Duration to sleep for in milliseconds +func (t *Time) Sleep(duration int) { + startTime := getTime(); + for { + if getTime() - startTime >= duration { + break; + } + } +} + +var time Time = Time{};