This commit is contained in:
blueloveTH 2023-02-09 15:32:55 +08:00
parent 998e3e4249
commit e421a06923
4 changed files with 12 additions and 17 deletions

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
const char* __BUILTINS_CODE = R"( const char* kBuiltinsCode = R"(
def print(*args, sep=' ', end='\n'): def print(*args, sep=' ', end='\n'):
s = sep.join([str(i) for i in args]) s = sep.join([str(i) for i in args])
__sys_stdout_write(s + end) __sys_stdout_write(s + end)
@ -370,7 +370,7 @@ class set:
return self._a.keys().__iter__() return self._a.keys().__iter__()
)"; )";
const char* __RANDOM_CODE = R"( const char* kRandomCode = R"(
import time as _time import time as _time
__all__ = ['Random', 'seed', 'random', 'randint', 'uniform'] __all__ = ['Random', 'seed', 'random', 'randint', 'uniform']

View File

@ -31,10 +31,6 @@
#define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!"); #define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!");
#endif #endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define PK_VERSION "0.8.3" #define PK_VERSION "0.8.3"
typedef int64_t i64; typedef int64_t i64;

View File

@ -15,7 +15,7 @@ struct Timer{
} }
}; };
#ifndef __NO_MAIN #ifndef __EMSCRIPTEN__
int main(int argc, char** argv){ int main(int argc, char** argv){
VM* vm = pkpy_new_vm(true); VM* vm = pkpy_new_vm(true);

View File

@ -503,8 +503,8 @@ void init_builtins(VM* _vm) {
#elif __APPLE__ #elif __APPLE__
#define __EXPORT __attribute__((visibility("default"))) __attribute__((used)) #define __EXPORT __attribute__((visibility("default"))) __attribute__((used))
#elif __EMSCRIPTEN__ #elif __EMSCRIPTEN__
#include <emscripten.h>
#define __EXPORT EMSCRIPTEN_KEEPALIVE #define __EXPORT EMSCRIPTEN_KEEPALIVE
#define __NO_MAIN
#else #else
#define __EXPORT #define __EXPORT
#endif #endif
@ -654,7 +654,7 @@ public:
virtual void* get() = 0; virtual void* get() = 0;
}; };
static std::vector<_PkExported*> _pkLookupTable; static std::vector<_PkExported*> _pk_lookup_table;
template<typename T> template<typename T>
class PkExported : public _PkExported{ class PkExported : public _PkExported{
@ -663,7 +663,7 @@ public:
template<typename... Args> template<typename... Args>
PkExported(Args&&... args) { PkExported(Args&&... args) {
_ptr = new T(std::forward<Args>(args)...); _ptr = new T(std::forward<Args>(args)...);
_pkLookupTable.push_back(this); _pk_lookup_table.push_back(this);
} }
~PkExported() override { delete _ptr; } ~PkExported() override { delete _ptr; }
@ -683,10 +683,10 @@ extern "C" {
/// If the pointer is not allocated by `pkpy_xxx_xxx`, the behavior is undefined. /// If the pointer is not allocated by `pkpy_xxx_xxx`, the behavior is undefined.
/// !!! /// !!!
void pkpy_delete(void* p){ void pkpy_delete(void* p){
for(int i = 0; i < _pkLookupTable.size(); i++){ for(int i = 0; i < _pk_lookup_table.size(); i++){
if(_pkLookupTable[i]->get() == p){ if(_pk_lookup_table[i]->get() == p){
delete _pkLookupTable[i]; delete _pk_lookup_table[i];
_pkLookupTable.erase(_pkLookupTable.begin() + i); _pk_lookup_table.erase(_pk_lookup_table.begin() + i);
return; return;
} }
} }
@ -761,11 +761,10 @@ extern "C" {
add_module_re(vm); add_module_re(vm);
add_module_dis(vm); add_module_dis(vm);
// add builtins | no exception handler | must succeed _Code code = vm->compile(kBuiltinsCode, "<builtins>", EXEC_MODE);
_Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE);
vm->_exec(code, vm->builtins, pkpy::make_shared<PyVarDict>()); vm->_exec(code, vm->builtins, pkpy::make_shared<PyVarDict>());
pkpy_vm_add_module(vm, "random", __RANDOM_CODE); pkpy_vm_add_module(vm, "random", kRandomCode);
return vm; return vm;
} }