This commit is contained in:
blueloveTH 2023-02-01 14:59:54 +08:00
parent 956c0e24e3
commit d7c37373e4
12 changed files with 10966 additions and 184 deletions

View File

@ -1,8 +1,10 @@
with open("src/opcodes.h", "rt", encoding='utf-8') as f: with open("src/opcodes.h", "rt", encoding='utf-8') as f:
OPCODES_TEXT = f.read() OPCODES_TEXT = f.read()
with open("src/_bindings.h", "rt", encoding='utf-8') as f:
_BINDINGS_TEXT = f.read()
pipeline = [ pipeline = [
["hash_table8.hpp", "__stl__.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"], ["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"],
["obj.h", "iter.h", "parser.h", "ref.h", "codeobject.h"], ["obj.h", "iter.h", "parser.h", "ref.h", "codeobject.h"],
["vm.h", "compiler.h", "repl.h"], ["vm.h", "compiler.h", "repl.h"],
["pocketpy.h"] ["pocketpy.h"]
@ -30,6 +32,7 @@ def remove_copied_include(text):
text text
) )
text = text.replace('#include "opcodes.h"', OPCODES_TEXT) text = text.replace('#include "opcodes.h"', OPCODES_TEXT)
text = text.replace('#include "_bindings.h"', _BINDINGS_TEXT)
return text return text
for seq in pipeline: for seq in pipeline:

File diff suppressed because it is too large Load Diff

@ -1 +1 @@
Subproject commit e84dedd36affa57df6b9bf845b456df2de5de872 Subproject commit bd9b1dc1aee9ef625ce922cc127ef7456c36169c

52
scripts/moc.py Normal file
View File

@ -0,0 +1,52 @@
mapping = {
'int': 'i64',
'float': 'f64',
'str': 'const char*',
'bool': 'bool',
'None': 'void'
}
def args(n, a: list, res: list, first=True):
if n == 0:
res.append(tuple(a))
return
for p_ret, c_ret in mapping.items():
if not first and p_ret == 'None':
continue
a.append(p_ret)
args(n-1, a, res, first=False)
a.pop()
data = []
for n in [4,3,2,1]:
res = []
args(n, [], res)
for p_ret,*p_args in res:
c_args = ['VM*'] + [mapping[i] for i in p_args]
c_ret = mapping[p_ret]
name = f'__f_{p_ret}__{"_".join(p_args)}'
s = f'typedef {c_ret} (*{name})({", ".join(c_args)});'
s += '\n'
impl = []
for i, p_arg in enumerate(p_args):
impl.append( f'{mapping[p_arg]} _{i} = vm->Py{p_arg.capitalize()}_AS_C(args[{i}]);' )
call_impl = f'f({", ".join(["vm"] + [f"_{j}" for j in range(len(p_args))])})';
if p_ret == 'None':
impl.append( call_impl + ';' )
impl.append( 'return vm->None;' )
else:
impl.append ( f'{mapping[p_ret]} ret = {call_impl};' )
impl.append( f'return vm->Py{p_ret.capitalize()}(ret);' )
impl = '\n'.join([' '*8 + i for i in impl])
s += f'''__EXPORT\nvoid pkpy_vm_bind{name}(VM* vm, const char* mod, const char* name, {name} f) {{
PyVar obj = vm->new_module_if_not_existed(mod);
vm->bindFunc<{len(p_args)}>(obj, name, [f](VM* vm, const pkpy::ArgList& args) {{
{impl}
}});
}}''' + '\n'
data.append(s)
with open('src/_bindings.h', 'w') as f:
text = '\n'.join(data)
f.write('extern "C" {\n' + text + '}')

5391
src/_bindings.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -33,10 +33,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.0" #define PK_VERSION "0.8.0"
typedef int64_t i64; typedef int64_t i64;

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "__stl__.h" #include "common.h"
namespace pkpy{ namespace pkpy{
template <typename T> template <typename T>

View File

@ -7,8 +7,8 @@ struct BaseRef;
class VM; class VM;
class Frame; class Frame;
typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::ArgList&); //typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::ArgList&);
//typedef std::function<PyVar(VM*, const pkpy::ArgList&)> _CppFunc; typedef std::function<PyVar(VM*, const pkpy::ArgList&)> _CppFuncRaw;
typedef pkpy::shared_ptr<CodeObject> _Code; typedef pkpy::shared_ptr<CodeObject> _Code;
struct _CppFunc { struct _CppFunc {

View File

@ -805,3 +805,5 @@ extern "C" {
return strdup(ss.str().c_str()); return strdup(ss.str().c_str());
} }
} }
#include "_bindings.h"

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "__stl__.h" #include "common.h"
#include "memory.h" #include "memory.h"
#include "str.h" #include "str.h"

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "__stl__.h" #include "common.h"
typedef std::stringstream _StrStream; typedef std::stringstream _StrStream;
@ -133,6 +133,10 @@ public:
return *this; return *this;
} }
operator const char*() const {
return c_str();
}
~_Str(){ ~_Str(){
if(_u8_index != nullptr) delete _u8_index; if(_u8_index != nullptr) delete _u8_index;
} }

View File

@ -603,6 +603,12 @@ public:
return obj; return obj;
} }
PyVar new_module_if_not_existed(_Str name) {
PyVar* it = _modules.try_get(name);
if(it != nullptr) return *it;
return new_module(name);
}
PyVarOrNull getattr(const PyVar& obj, const _Str& name, bool throw_err=true) { PyVarOrNull getattr(const PyVar& obj, const _Str& name, bool throw_err=true) {
PyVarDict::iterator it; PyVarDict::iterator it;
PyObject* cls; PyObject* cls;