mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
up
This commit is contained in:
parent
aab4ff7647
commit
4eff4ea076
12
src/main.cpp
12
src/main.cpp
@ -1,19 +1,17 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
|
||||||
using namespace pkpy;
|
|
||||||
|
|
||||||
#ifndef __EMSCRIPTEN__
|
#ifndef __EMSCRIPTEN__
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
VM* vm = pkpy_new_vm(true);
|
pkpy::VM* vm = pkpy_new_vm(true);
|
||||||
vm->bind_builtin_func<0>("input", [](VM* vm, Args& args){
|
vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::Args& args){
|
||||||
static std::string line;
|
static std::string line;
|
||||||
std::getline(std::cin, line);
|
std::getline(std::cin, line);
|
||||||
return vm->PyStr(line);
|
return vm->PyStr(line);
|
||||||
});
|
});
|
||||||
if(argc == 1){
|
if(argc == 1){
|
||||||
REPL* repl = pkpy_new_repl(vm);
|
pkpy::REPL* repl = pkpy_new_repl(vm);
|
||||||
bool need_more_lines = false;
|
bool need_more_lines = false;
|
||||||
while(true){
|
while(true){
|
||||||
(*vm->_stdout) << (need_more_lines ? "... " : ">>> ");
|
(*vm->_stdout) << (need_more_lines ? "... " : ">>> ");
|
||||||
@ -35,8 +33,8 @@ int main(int argc, char** argv){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
PyVarOrNull ret = nullptr;
|
pkpy::PyVarOrNull ret = nullptr;
|
||||||
ret = vm->exec(src.c_str(), filename, EXEC_MODE);
|
ret = vm->exec(src.c_str(), filename, pkpy::EXEC_MODE);
|
||||||
pkpy_delete(vm);
|
pkpy_delete(vm);
|
||||||
return ret != nullptr ? 0 : 1;
|
return ret != nullptr ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
@ -807,6 +807,10 @@ void VM::post_init(){
|
|||||||
this->_exec(code, this->builtins);
|
this->_exec(code, this->builtins);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace pkpy
|
||||||
|
|
||||||
|
/*************************GLOBAL NAMESPACE*************************/
|
||||||
|
|
||||||
class PkExportedBase{
|
class PkExportedBase{
|
||||||
public:
|
public:
|
||||||
virtual ~PkExportedBase() = default;
|
virtual ~PkExportedBase() = default;
|
||||||
@ -852,8 +856,8 @@ extern "C" {
|
|||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Run a given source on a virtual machine.
|
/// Run a given source on a virtual machine.
|
||||||
void pkpy_vm_exec(VM* vm, const char* source){
|
void pkpy_vm_exec(pkpy::VM* vm, const char* source){
|
||||||
vm->exec(source, "main.py", EXEC_MODE);
|
vm->exec(source, "main.py", pkpy::EXEC_MODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
@ -861,11 +865,11 @@ extern "C" {
|
|||||||
///
|
///
|
||||||
/// Return `__repr__` of the result.
|
/// Return `__repr__` of the result.
|
||||||
/// If the variable is not found, return `nullptr`.
|
/// If the variable is not found, return `nullptr`.
|
||||||
char* pkpy_vm_get_global(VM* vm, const char* name){
|
char* pkpy_vm_get_global(pkpy::VM* vm, const char* name){
|
||||||
PyVar* val = vm->_main->attr().try_get(name);
|
pkpy::PyVar* val = vm->_main->attr().try_get(name);
|
||||||
if(val == nullptr) return nullptr;
|
if(val == nullptr) return nullptr;
|
||||||
try{
|
try{
|
||||||
Str _repr = vm->PyStr_AS_C(vm->asRepr(*val));
|
pkpy::Str _repr = vm->PyStr_AS_C(vm->asRepr(*val));
|
||||||
return strdup(_repr.c_str());
|
return strdup(_repr.c_str());
|
||||||
}catch(...){
|
}catch(...){
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -877,11 +881,11 @@ extern "C" {
|
|||||||
///
|
///
|
||||||
/// Return `__repr__` of the result.
|
/// Return `__repr__` of the result.
|
||||||
/// If there is any error, return `nullptr`.
|
/// If there is any error, return `nullptr`.
|
||||||
char* pkpy_vm_eval(VM* vm, const char* source){
|
char* pkpy_vm_eval(pkpy::VM* vm, const char* source){
|
||||||
PyVarOrNull ret = vm->exec(source, "<eval>", EVAL_MODE);
|
pkpy::PyVarOrNull ret = vm->exec(source, "<eval>", pkpy::EVAL_MODE);
|
||||||
if(ret == nullptr) return nullptr;
|
if(ret == nullptr) return nullptr;
|
||||||
try{
|
try{
|
||||||
Str _repr = vm->PyStr_AS_C(vm->asRepr(ret));
|
pkpy::Str _repr = vm->PyStr_AS_C(vm->asRepr(ret));
|
||||||
return strdup(_repr.c_str());
|
return strdup(_repr.c_str());
|
||||||
}catch(...){
|
}catch(...){
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -890,26 +894,26 @@ extern "C" {
|
|||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Create a REPL, using the given virtual machine as the backend.
|
/// Create a REPL, using the given virtual machine as the backend.
|
||||||
REPL* pkpy_new_repl(VM* vm){
|
pkpy::REPL* pkpy_new_repl(pkpy::VM* vm){
|
||||||
return PKPY_ALLOCATE(REPL, vm);
|
return PKPY_ALLOCATE(pkpy::REPL, vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Input a source line to an interactive console. Return true if need more lines.
|
/// Input a source line to an interactive console. Return true if need more lines.
|
||||||
bool pkpy_repl_input(REPL* r, const char* line){
|
bool pkpy_repl_input(pkpy::REPL* r, const char* line){
|
||||||
return r->input(line);
|
return r->input(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Add a source module into a virtual machine.
|
/// Add a source module into a virtual machine.
|
||||||
void pkpy_vm_add_module(VM* vm, const char* name, const char* source){
|
void pkpy_vm_add_module(pkpy::VM* vm, const char* name, const char* source){
|
||||||
vm->_lazy_modules[name] = source;
|
vm->_lazy_modules[name] = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Create a virtual machine.
|
/// Create a virtual machine.
|
||||||
VM* pkpy_new_vm(bool use_stdio){
|
pkpy::VM* pkpy_new_vm(bool use_stdio){
|
||||||
return PKPY_ALLOCATE(VM, use_stdio);
|
return PKPY_ALLOCATE(pkpy::VM, use_stdio);
|
||||||
}
|
}
|
||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
@ -918,13 +922,13 @@ extern "C" {
|
|||||||
/// After this operation, both stream will be cleared.
|
/// After this operation, both stream will be cleared.
|
||||||
///
|
///
|
||||||
/// Return a json representing the result.
|
/// Return a json representing the result.
|
||||||
char* pkpy_vm_read_output(VM* vm){
|
char* pkpy_vm_read_output(pkpy::VM* vm){
|
||||||
if(vm->use_stdio) return nullptr;
|
if(vm->use_stdio) return nullptr;
|
||||||
StrStream* s_out = (StrStream*)(vm->_stdout);
|
pkpy::StrStream* s_out = (pkpy::StrStream*)(vm->_stdout);
|
||||||
StrStream* s_err = (StrStream*)(vm->_stderr);
|
pkpy::StrStream* s_err = (pkpy::StrStream*)(vm->_stderr);
|
||||||
Str _stdout = s_out->str();
|
pkpy::Str _stdout = s_out->str();
|
||||||
Str _stderr = s_err->str();
|
pkpy::Str _stderr = s_err->str();
|
||||||
StrStream ss;
|
pkpy::StrStream ss;
|
||||||
ss << '{' << "\"stdout\": " << _stdout.escape(false);
|
ss << '{' << "\"stdout\": " << _stdout.escape(false);
|
||||||
ss << ", " << "\"stderr\": " << _stderr.escape(false) << '}';
|
ss << ", " << "\"stderr\": " << _stderr.escape(false) << '}';
|
||||||
s_out->str(""); s_err->str("");
|
s_out->str(""); s_err->str("");
|
||||||
@ -955,19 +959,19 @@ extern "C" {
|
|||||||
|
|
||||||
__EXPORT
|
__EXPORT
|
||||||
/// Bind a function to a virtual machine.
|
/// Bind a function to a virtual machine.
|
||||||
char* pkpy_vm_bind(VM* vm, const char* mod, const char* name, int ret_code){
|
char* pkpy_vm_bind(pkpy::VM* vm, const char* mod, const char* name, int ret_code){
|
||||||
if(!f_int || !f_float || !f_bool || !f_str || !f_None) return nullptr;
|
if(!f_int || !f_float || !f_bool || !f_str || !f_None) return nullptr;
|
||||||
static int kGlobalBindId = 0;
|
static int kGlobalBindId = 0;
|
||||||
for(int i=0; mod[i]; i++) if(mod[i] == ' ') return nullptr;
|
for(int i=0; mod[i]; i++) if(mod[i] == ' ') return nullptr;
|
||||||
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
|
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
|
||||||
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
|
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
|
||||||
PyVar obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod);
|
pkpy::PyVar obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod);
|
||||||
vm->bind_func<-1>(obj, name, [ret_code, f_header](VM* vm, const Args& args){
|
vm->bind_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, const pkpy::Args& args){
|
||||||
StrStream ss;
|
pkpy::StrStream ss;
|
||||||
ss << f_header;
|
ss << f_header;
|
||||||
for(int i=0; i<args.size(); i++){
|
for(int i=0; i<args.size(); i++){
|
||||||
ss << ' ';
|
ss << ' ';
|
||||||
PyVar x = vm->call(args[i], __json__);
|
pkpy::PyVar x = vm->call(args[i], pkpy::__json__);
|
||||||
ss << vm->PyStr_AS_C(x);
|
ss << vm->PyStr_AS_C(x);
|
||||||
}
|
}
|
||||||
char* packet = strdup(ss.str().c_str());
|
char* packet = strdup(ss.str().c_str());
|
||||||
@ -988,6 +992,4 @@ extern "C" {
|
|||||||
});
|
});
|
||||||
return strdup(f_header.c_str());
|
return strdup(f_header.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
|
Loading…
x
Reference in New Issue
Block a user