some update

This commit is contained in:
blueloveTH 2022-11-13 13:58:31 +08:00
parent 25648a9639
commit 385002035c
3 changed files with 41 additions and 11 deletions

View File

@ -45,7 +45,7 @@ extern "C" {
__EXPORT __EXPORT
bool repl_input(const char* line){ bool repl_input(const char* line){
return pkpy_input_repl(_repl, line); return pkpy_repl_input(_repl, line);
} }
} }

View File

@ -547,7 +547,24 @@ void __addModuleSys(VM* vm){
vm->setAttr(mod, "version", vm->PyStr(PK_VERSION)); vm->setAttr(mod, "version", vm->PyStr(PK_VERSION));
} }
extern "C" { extern "C" {
__EXPORT
struct PyObjectDump: public PkExportedResource{
const char* type; // "int", "str", "float" ...
const char* string; // __str__ representation
const char* repr; // __repr__ representation
PyObjectDump(const char* _type, const char* _string, const char* _repr):
type(strdup(_type)), string(strdup(_string)), repr(strdup(_repr)){}
~PyObjectDump(){
delete[] type;
delete[] string;
delete[] repr;
}
};
__EXPORT __EXPORT
VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){ VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){
VM* vm = new VM(); VM* vm = new VM();
@ -570,9 +587,23 @@ extern "C" {
} }
__EXPORT __EXPORT
void pkpy_exec(VM* vm, const char* source){ bool pkpy_exec(VM* vm, const char* source){
_Code code = compile(vm, source, "main.py"); _Code code = compile(vm, source, "main.py");
if(code != nullptr) vm->exec(code); if(code == nullptr) return false;
return vm->exec(code) != nullptr;
}
__EXPORT
PyObjectDump* pkpy_eval(VM* vm, const char* source){
_Code code = compile(vm, source, "<eval>", EVAL_MODE);
if(code == nullptr) return nullptr;
PyVar ret = vm->exec(code);
if(ret == nullptr) return nullptr;
return new PyObjectDump(
ret->getTypeName().c_str(),
vm->PyStr_AS_C(vm->asStr(ret)).c_str(),
vm->PyStr_AS_C(vm->asRepr(ret)).c_str()
);
} }
__EXPORT __EXPORT
@ -581,16 +612,15 @@ extern "C" {
} }
__EXPORT __EXPORT
bool pkpy_input_repl(REPL* r, const char* line){ bool pkpy_repl_input(REPL* r, const char* line){
return r->input(line); return r->input(line);
} }
__EXPORT __EXPORT
void pkpy_add_module(VM* vm, const char* name, const char* source){ bool pkpy_add_module(VM* vm, const char* name, const char* source){
_Code code = compile(vm, source, name + _Str(".py")); _Code code = compile(vm, source, name + _Str(".py"));
if(code != nullptr){ if(code == nullptr) return false;
PyVar _m = vm->newModule(name); PyVar _m = vm->newModule(name);
vm->exec(code, _m); return vm->exec(code, _m) != nullptr;
}
} }
} }

View File

@ -435,7 +435,7 @@ public:
return call(getAttr(obj, func), args); return call(getAttr(obj, func), args);
} }
PyVar exec(const _Code& code, PyVar _module=nullptr){ PyVarOrNull exec(const _Code& code, PyVar _module=nullptr){
if(_module == nullptr) _module = _main; if(_module == nullptr) _module = _main;
try { try {
return _exec(code, _module); return _exec(code, _module);
@ -447,7 +447,7 @@ public:
_stderr(this, re.what()); _stderr(this, re.what());
} }
_stderr(this, "\n"); _stderr(this, "\n");
return None; return nullptr;
} }
} }