From 8f5c4e38a849a758dcc6308dc5f7d17931752526 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 10 Nov 2022 17:02:02 +0800 Subject: [PATCH] some fix --- src/main.cpp | 2 +- src/pocketpy.h | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ff55667e..a2f85132 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ VM* newVM(){ } void REPL(){ - std::cout << "pocketpy 0.1.0" << std::endl; + std::cout << "pocketpy " PK_VERSION << std::endl; std::cout << "https://github.com/blueloveTH/pocketpy" << std::endl; int need_more_lines = 0; diff --git a/src/pocketpy.h b/src/pocketpy.h index e69a8acf..7a8d32f2 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -5,6 +5,9 @@ #include #include +#include + +#define PK_VERSION "0.2.0" #define BIND_NUM_ARITH_OPT(name, op) \ _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, PyVarList args){ \ @@ -493,14 +496,23 @@ void __runCodeBuiltins(VM* vm, const char* src){ #define __EXPORT #endif -#include + void __addModuleTime(VM* vm){ PyVar mod = vm->newModule("time"); vm->bindFunc(mod, "time", [](VM* vm, PyVarList args) { - return vm->PyFloat((_Int)std::time(nullptr)); + auto now = std::chrono::high_resolution_clock::now(); + return vm->PyFloat(std::chrono::duration_cast(now.time_since_epoch()).count() / 1000000.0); }); } +void __addModuleSys(VM* vm){ + PyVar mod = vm->newModule("sys"); + vm->bindFunc(mod, "getrefcount", [](VM* vm, PyVarList args) { + vm->__checkArgSize(args, 1); + return vm->PyInt(args[0].use_count()); + }); + vm->setAttr(mod, "version", vm->PyStr(PK_VERSION)); +} extern "C" { __EXPORT @@ -508,6 +520,7 @@ extern "C" { VM* vm = new VM(); __initializeBuiltinFunctions(vm); __runCodeBuiltins(vm, __BUILTINS_CODE); + __addModuleSys(vm); __addModuleTime(vm); vm->_stdout = _stdout; vm->_stderr = _stderr;