Skip to content

Commit

Permalink
Add time.Sleep() (#19)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JothamWong authored Apr 16, 2024
1 parent 2492269 commit a87a161
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ x;
`,
5,
'',
300
400
);

// Testing mark and sweep, with exactly one variable that should be freed
Expand All @@ -497,7 +497,7 @@ x;
`,
5,
'',
220
400
);

// Testing structs (no methods for now)
Expand Down Expand Up @@ -1044,7 +1044,7 @@ go googa(x, y);
`,
true,
'15',
206
400
);

// Test various array expressions
Expand Down
9 changes: 5 additions & 4 deletions src/vm/oogavm-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1245,7 +1246,6 @@ function runInstruction() {
// printStringPoolMapping();
}

// TODO: Switch to low level memory representation
export function run(numWords = 1000000) {
initialize(numWords);
while (running) {
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/vm/oogavm-typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions std/ooga-std.ooga
Original file line number Diff line number Diff line change
Expand Up @@ -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{};

0 comments on commit a87a161

Please sign in to comment.