This commit is contained in:
blueloveTH 2022-12-06 04:45:36 +08:00
parent d65636babb
commit 29c2618c9e
10 changed files with 29 additions and 20 deletions

View File

@ -1 +1 @@
g++ -o pocketpy src/main.cpp --std=c++17 -O1 -pthread
g++ -o pocketpy src/main.cpp --std=c++17 -O1 -pthread -Wall -Wno-sign-compare -Wno-unused-variable

View File

@ -32,10 +32,15 @@ PocketPy is a lightweight Python interpreter for game engines.
For features that are PocketPy specific, see [Extra Features](https://pocketpy.dev/extras/goto).
## Introduction
<p>
<a title="Pub" href="https://pub.dev/packages/pocketpy" ><img src="https://img.shields.io/pub/v/pocketpy" /></a>
</p>
This plugin provides object-oriented interfaces including full functionality of PocketPy [C-API](https://pocketpy.dev/c-api/vm).
It also provides `JsonRpcServer` class and `os` module bindings.
Run the following script to install this plugin.
```
flutter pub add pocketpy
```

View File

@ -2970,13 +2970,14 @@ struct _Slice {
class _Iterator {
protected:
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
VM* vm;
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
public:
virtual PyVar next() = 0;
virtual bool hasNext() = 0;
_Pointer var;
_Iterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
virtual ~_Iterator() = default;
};
typedef pkpy::shared_ptr<Function> _Func;
@ -3055,7 +3056,7 @@ public:
class StringIterator : public _Iterator {
private:
size_t index = 0;
int index = 0;
_Str str;
public:
StringIterator(VM* vm, PyVar _ref) : _Iterator(vm, _ref) {
@ -3389,6 +3390,7 @@ struct BasePointer {
virtual PyVar get(VM*, Frame*) const = 0;
virtual void set(VM*, Frame*, PyVar) const = 0;
virtual void del(VM*, Frame*) const = 0;
virtual ~BasePointer() = default;
};
enum NameScope {
@ -3713,6 +3715,7 @@ private:
int ip = 0;
std::stack<int> forLoops; // record the FOR_ITER bytecode index
public:
const CodeObject* code;
PyVar _module;
PyVarDict f_locals;
@ -3722,8 +3725,6 @@ public:
return _module->attribs;
}
const CodeObject* code;
Frame(const CodeObject* code, PyVar _module, const PyVarDict& locals)
: code(code), _module(_module), f_locals(locals) {
@ -3815,13 +3816,13 @@ public:
pkpy::ArgList popNValuesReversed(VM* vm, int n){
pkpy::ArgList v(n);
for(int i=n-1; i>=0; i--) v._index(i) = std::move(popValue(vm));
for(int i=n-1; i>=0; i--) v._index(i) = popValue(vm);
return v;
}
pkpy::ArgList __popNReversed(int n){
pkpy::ArgList v(n);
for(int i=n-1; i>=0; i--) v._index(i) = std::move(__pop());
for(int i=n-1; i>=0; i--) v._index(i) = __pop();
return v;
}
};
@ -4361,7 +4362,7 @@ public:
try {
return _exec(code, _module, {});
} catch (const std::exception& e) {
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
if(dynamic_cast<const _Error*>(&e)){
*_stderr << e.what() << '\n';
}else{
auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());
@ -6012,7 +6013,7 @@ _Code compile(VM* vm, const char* source, _Str filename, CompileMode mode=EXEC_M
try{
return compiler.__fillCode();
}catch(std::exception& e){
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
if(dynamic_cast<const _Error*>(&e)){
(*vm->_stderr) << e.what() << '\n';
}else{
auto ce = CompileError("UnexpectedError", e.what(), compiler.getLineSnapshot());

View File

@ -127,6 +127,7 @@ private:
int ip = 0;
std::stack<int> forLoops; // record the FOR_ITER bytecode index
public:
const CodeObject* code;
PyVar _module;
PyVarDict f_locals;
@ -136,8 +137,6 @@ public:
return _module->attribs;
}
const CodeObject* code;
Frame(const CodeObject* code, PyVar _module, const PyVarDict& locals)
: code(code), _module(_module), f_locals(locals) {
@ -229,13 +228,13 @@ public:
pkpy::ArgList popNValuesReversed(VM* vm, int n){
pkpy::ArgList v(n);
for(int i=n-1; i>=0; i--) v._index(i) = std::move(popValue(vm));
for(int i=n-1; i>=0; i--) v._index(i) = popValue(vm);
return v;
}
pkpy::ArgList __popNReversed(int n){
pkpy::ArgList v(n);
for(int i=n-1; i>=0; i--) v._index(i) = std::move(__pop());
for(int i=n-1; i>=0; i--) v._index(i) = __pop();
return v;
}
};

View File

@ -1028,7 +1028,7 @@ _Code compile(VM* vm, const char* source, _Str filename, CompileMode mode=EXEC_M
try{
return compiler.__fillCode();
}catch(std::exception& e){
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
if(dynamic_cast<const _Error*>(&e)){
(*vm->_stderr) << e.what() << '\n';
}else{
auto ce = CompileError("UnexpectedError", e.what(), compiler.getLineSnapshot());

View File

@ -43,7 +43,7 @@ public:
class StringIterator : public _Iterator {
private:
size_t index = 0;
int index = 0;
_Str str;
public:
StringIterator(VM* vm, PyVar _ref) : _Iterator(vm, _ref) {

View File

@ -3,19 +3,21 @@
#include "pocketpy.h"
//#define PK_DEBUG_TIME
#define PK_DEBUG_THREADED
#define PK_DEBUG_TIME
//#define PK_DEBUG_THREADED
struct Timer{
const char* title;
Timer(const char* title) : title(title) {}
void run(std::function<void()> f){
#ifdef PK_DEBUG_TIME
auto start = std::chrono::high_resolution_clock::now();
f();
auto end = std::chrono::high_resolution_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000000.0;
#ifdef PK_DEBUG_TIME
std::cout << title << ": " << elapsed << " s" << std::endl;
#else
f();
#endif
}
};

View File

@ -55,13 +55,14 @@ struct _Slice {
class _Iterator {
protected:
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
VM* vm;
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
public:
virtual PyVar next() = 0;
virtual bool hasNext() = 0;
_Pointer var;
_Iterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
virtual ~_Iterator() = default;
};
typedef pkpy::shared_ptr<Function> _Func;

View File

@ -8,6 +8,7 @@ struct BasePointer {
virtual PyVar get(VM*, Frame*) const = 0;
virtual void set(VM*, Frame*, PyVar) const = 0;
virtual void del(VM*, Frame*) const = 0;
virtual ~BasePointer() = default;
};
enum NameScope {

View File

@ -538,7 +538,7 @@ public:
try {
return _exec(code, _module, {});
} catch (const std::exception& e) {
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
if(dynamic_cast<const _Error*>(&e)){
*_stderr << e.what() << '\n';
}else{
auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());