|
|
@@ -13,6 +13,7 @@
|
|
|
#include "debug.h"
|
|
|
#include "memory.h"
|
|
|
#include <stdarg.h>
|
|
|
+#include <time.h>
|
|
|
|
|
|
VM vm;
|
|
|
|
|
|
@@ -44,11 +45,26 @@ static void runtimeError(const char *format, ...) {
|
|
|
resetStack();
|
|
|
}
|
|
|
|
|
|
+static void defineNative(const char *name, NativeFn function) {
|
|
|
+ push(OBJ_VAL(copyString(name, (int) strlen(name))));
|
|
|
+ push(OBJ_VAL(newNative(function)));
|
|
|
+ tableSet(&vm.globals, AS_STRING(vm.stack[0]), vm.stack[1]);
|
|
|
+ pop();
|
|
|
+ pop();
|
|
|
+}
|
|
|
+
|
|
|
+static Value clockNative(int argCount, Value *args) {
|
|
|
+ return NUMBER_VAL((double) clock() / CLOCKS_PER_SEC);
|
|
|
+}
|
|
|
+
|
|
|
void initVM() {
|
|
|
resetStack();
|
|
|
vm.objects = NULL;
|
|
|
initTable(&vm.globals);
|
|
|
initTable(&vm.strings);
|
|
|
+
|
|
|
+ /// Native Function define.
|
|
|
+ defineNative("clock", clockNative);
|
|
|
}
|
|
|
|
|
|
static Value peek(int distance) {
|
|
|
@@ -76,6 +92,13 @@ static bool callValue(Value callee, int argCount) {
|
|
|
switch (OBJ_TYPE(callee)) {
|
|
|
case OBJ_FUNCTION:
|
|
|
return call(AS_FUNCTION(callee), argCount);
|
|
|
+ case OBJ_NATIVE: {
|
|
|
+ NativeFn fn = AS_NATIVE(callee);
|
|
|
+ Value result = fn(argCount, vm.stackTop - argCount);
|
|
|
+ vm.stackTop -= argCount + 1;
|
|
|
+ push(result);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
default:
|
|
|
break;// Non-callable object type.
|
|
|
}
|