From 88e1a56150c74449b2e062e1b75d9136ff359382 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 8 Dec 2022 12:47:31 +0800 Subject: [PATCH] pass by value --- src/builtins.h | 5 +++++ src/pocketpy.h | 9 +++------ src/safestl.h | 2 +- src/str.h | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/builtins.h b/src/builtins.h index 39589802..d26142c0 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -4,6 +4,11 @@ const char* __BUILTINS_CODE = R"( def len(x): return x.__len__() +def print(*args, sep=' ', end='\n'): + s = sep.join([str(i) for i in args]) + __sys_stdout_write(s + end) + + str.__mul__ = lambda self, n: ''.join([self for _ in range(n)]) def __str4split(self, sep): diff --git a/src/pocketpy.h b/src/pocketpy.h index 8fea2925..0f2127ce 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -39,12 +39,9 @@ void __initializeBuiltinFunctions(VM* _vm) { #undef BIND_NUM_ARITH_OPT #undef BIND_NUM_LOGICAL_OPT - _vm->bindBuiltinFunc("print", [](VM* vm, const pkpy::ArgList& args) { - _StrStream ss; - for(int i=0; iPyStr_AS_C(vm->asStr(args[i])) << " "; - } - (*vm->_stdout) << ss.str() << '\n'; + _vm->bindBuiltinFunc("__sys_stdout_write", [](VM* vm, const pkpy::ArgList& args) { + vm->__checkArgSize(args, 1); + (*vm->_stdout) << vm->PyStr_AS_C(args[0]); return vm->None; }); diff --git a/src/safestl.h b/src/safestl.h index 09e7df94..1701511c 100644 --- a/src/safestl.h +++ b/src/safestl.h @@ -58,7 +58,7 @@ public: } #endif - PyVarDict() : emhash8::HashMap<_Str, PyVar>(5) {} + PyVarDict() : emhash8::HashMap<_Str, PyVar>() {} }; diff --git a/src/str.h b/src/str.h index 6d78f28f..ab102d39 100644 --- a/src/str.h +++ b/src/str.h @@ -72,7 +72,7 @@ private: pkpy::shared_ptr<_StrMemory> _s; bool interned = false; public: - _Str(const _StrLiteral& s){ + _Str(_StrLiteral s){ construct(s); intern(); } @@ -95,7 +95,7 @@ public: this->_s = pkpy::make_shared<_StrMemory>(std::move(s)); } - void construct(const std::string_view& sv){ + void construct(std::string_view sv){ auto it = _strIntern.find(sv); if(it != _strIntern.end()){ this->_s = it->second;