This commit is contained in:
blueloveTH 2024-03-24 18:01:03 +08:00
parent efc5755d78
commit dc20c56391
6 changed files with 32 additions and 36 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,6 @@
#include "common.h"
#include "vm.h"
#include "_generated.h"
namespace pkpy {

View File

@ -26,6 +26,8 @@
#include "config.h"
#include "export.h"
#include "_generated.h"
#ifdef min
#undef min
#endif

View File

@ -16,23 +16,16 @@ def generate_python_sources():
const_char_array.append(str(c - 256))
const_char_array.append('0')
const_char_array = ','.join(const_char_array)
sources[key] = '(const char[]){' + const_char_array + '}'
sources[key] = '{' + const_char_array + '}'
header = '''#pragma once
// generated by prebuild.py
#include <map>
#include <string>
namespace pkpy{
inline std::map<std::string, const char*> kPythonLibs = {
'''
for key, value in sources.items():
header += f' {{ "{key}", {value} }},\n'
header += ''' };
} // namespace pkpy
'''
header += f' inline const char kPythonLibs_{key}[] = {value};\n'
header += '}\n'
return header
# use LF line endings instead of CRLF

View File

@ -552,7 +552,7 @@ namespace pkpy
PyObject *mod = vm->new_module("collections");
PyDeque::register_class(vm, mod);
PyDequeIter::register_class(vm, mod);
CodeObject_ code = vm->compile(kPythonLibs["collections"], "collections.py", EXEC_MODE);
CodeObject_ code = vm->compile(kPythonLibs_collections, "collections.py", EXEC_MODE);
vm->_exec(code, mod);
}
} // namespace pkpypkpy

View File

@ -1534,14 +1534,21 @@ void VM::post_init(){
add_module_base64(this);
add_module_operator(this);
for(const char* name: {"this", "functools", "heapq", "bisect", "pickle", "_long", "colorsys", "typing", "datetime", "cmath"}){
_lazy_modules[name] = kPythonLibs[name];
}
_lazy_modules["this"] = kPythonLibs_this;
_lazy_modules["functools"] = kPythonLibs_functools;
_lazy_modules["heapq"] = kPythonLibs_heapq;
_lazy_modules["bisect"] = kPythonLibs_bisect;
_lazy_modules["pickle"] = kPythonLibs_pickle;
_lazy_modules["_long"] = kPythonLibs__long;
_lazy_modules["colorsys"] = kPythonLibs_colorsys;
_lazy_modules["typing"] = kPythonLibs_typing;
_lazy_modules["datetime"] = kPythonLibs_datetime;
_lazy_modules["cmath"] = kPythonLibs_cmath;
try{
CodeObject_ code = compile(kPythonLibs["builtins"], "<builtins>", EXEC_MODE);
CodeObject_ code = compile(kPythonLibs_builtins, "<builtins>", EXEC_MODE);
this->_exec(code, this->builtins);
code = compile(kPythonLibs["_set"], "<set>", EXEC_MODE);
code = compile(kPythonLibs__set, "<set>", EXEC_MODE);
this->_exec(code, this->builtins);
}catch(const Exception& e){
std::cerr << e.summary() << std::endl;