Skip to content

Commit 0fbc289

Browse files
committed
feat: Add std.time module with native time functions and register it in the standard library.
1 parent 4a43e8b commit 0fbc289

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/stdlib/stdlib_core.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ void register_string_natives(VM* vm);
2626
void register_convert_natives(VM* vm);
2727
void register_system_natives(VM* vm);
2828

29+
// Exposed natives
30+
extern Value native_clock(int argCount, Value* args);
31+
32+
static Value native_len(int argCount, Value* args) {
33+
if (argCount < 1) return NUMBER_VAL(0);
34+
if (IS_STRING(args[0])) return NUMBER_VAL((double)AS_STRING(args[0])->length);
35+
// TODO: List/Map support
36+
return NUMBER_VAL(0);
37+
}
38+
2939
static void registerModule(VM* vm, const char* name, ObjModule* module) {
3040
ObjString* nameObj = copyString(name, (int)strlen(name));
3141
push(vm, OBJ_VAL(nameObj));
@@ -177,4 +187,8 @@ void registerStdLib(VM* vm) {
177187
tableSet(&vm->globals, stdName, OBJ_VAL(stdMod));
178188
pop(vm); // stdMod
179189
pop(vm); // stdName
190+
191+
// Register simple globals for convenience
192+
defineNative(vm, "clock", native_clock);
193+
defineNative(vm, "len", native_len);
180194
}

src/stdlib/time_native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static Value native_now(int argCount, Value* args) {
4040
}
4141

4242
// clock() -> Number (CPU time in seconds)
43-
static Value native_clock(int argCount, Value* args) {
43+
Value native_clock(int argCount, Value* args) {
4444
(void)argCount; (void)args;
4545
return NUMBER_VAL((double)clock() / CLOCKS_PER_SEC);
4646
}

0 commit comments

Comments
 (0)