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
const char* __BUILTINS_CODE = R"(
const char* kBuiltinsCode = R"(
def print(*args, sep=' ', end='\n'):
s = sep.join([str(i) for i in args])
__sys_stdout_write(s + end)
@ -370,7 +370,7 @@ class set:
return self._a.keys().__iter__()
)";
const char* __RANDOM_CODE = R"(
const char* kRandomCode = R"(
import time as _time
__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()!");
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define PK_VERSION "0.8.3"
typedef int64_t i64;

View File

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

View File

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