Update CHANGELOG.md
This commit is contained in:
blueloveTH 2022-12-05 23:16:04 +08:00
parent 5fe61c2e11
commit 35aca12e50
5 changed files with 32 additions and 29 deletions

View File

@ -1,3 +1,3 @@
## 0.4.7 ## 0.4.7+1
The initial version. Hello, world! The initial version. Hello, world!

View File

@ -2,7 +2,7 @@
PocketPy is a lightweight Python interpreter for game engines. PocketPy is a lightweight Python interpreter for game engines.
![logo](https://pocketpy.dev/static/logo_flat.png) ![](https://pocketpy.dev/static/logo_flat.png)
## Features ## Features

View File

@ -6,10 +6,10 @@ import 'dart:io';
import 'package:pocketpy/pocketpy.dart'; import 'package:pocketpy/pocketpy.dart';
class JsonRpcError { class _JsonRpcError {
final Map<String, dynamic> payload = {}; final Map<String, dynamic> payload = {};
JsonRpcError(int code, String message, {dynamic data}) { _JsonRpcError(int code, String message, {dynamic data}) {
payload['code'] = code; payload['code'] = code;
payload['message'] = message; payload['message'] = message;
if (data != null) { if (data != null) {
@ -33,6 +33,7 @@ class JsonRpcServer {
registerOS(this); registerOS(this);
} }
/// Register a JSONRPC handler.
void register(String name, FutureOr<dynamic> Function(List) method) { void register(String name, FutureOr<dynamic> Function(List) method) {
_methods[name] = method; _methods[name] = method;
} }
@ -43,14 +44,15 @@ class JsonRpcServer {
if (json == null) throw Exception("JSONRPC request is null"); if (json == null) throw Exception("JSONRPC request is null");
var request = jsonDecode(json); var request = jsonDecode(json);
var f = _methods[request['method']]; var f = _methods[request['method']];
if (f == null) throw JsonRpcError(-32601, "Method not found"); if (f == null) throw _JsonRpcError(-32601, "Method not found");
try { try {
return f(request['params'] as List); return f(request['params'] as List);
} catch (e) { } catch (e) {
throw JsonRpcError(-32000, e.toString()); throw _JsonRpcError(-32000, e.toString());
} }
} }
/// Dispatch a JSONRPC request.
FutureOr<void> dispatch(ThreadedVM vm) async { FutureOr<void> dispatch(ThreadedVM vm) async {
onPreDispatch?.call(); onPreDispatch?.call();
try { try {
@ -58,12 +60,13 @@ class JsonRpcServer {
if (ret is Future<dynamic>) ret = await ret; if (ret is Future<dynamic>) ret = await ret;
vm.write_jsonrpc_response(jsonEncode({"result": ret})); vm.write_jsonrpc_response(jsonEncode({"result": ret}));
onPostDispatch?.call(); onPostDispatch?.call();
} on JsonRpcError catch (e) { } on _JsonRpcError catch (e) {
vm.write_jsonrpc_response(jsonEncode({"error": e.payload})); vm.write_jsonrpc_response(jsonEncode({"error": e.payload}));
return; return;
} }
} }
/// Attach the JsonRpcServer into a ThreadedVM. Once the ThreadedVM encounters JSONRPC request, it takes care of it automatically. This process will be stopped when the whole execution is done.
Future<void> attach(ThreadedVM vm, Future<void> attach(ThreadedVM vm,
{Duration? spinFreq = const Duration(milliseconds: 20)}) async { {Duration? spinFreq = const Duration(milliseconds: 20)}) async {
while (vm.state.index <= ThreadState.running.index) { while (vm.state.index <= ThreadState.running.index) {

View File

@ -7,7 +7,7 @@ import 'package:ffi/ffi.dart';
export 'jsonrpc.dart'; export 'jsonrpc.dart';
class Bindings class _Bindings
{ {
static ffi.DynamicLibrary _load() { static ffi.DynamicLibrary _load() {
String _libName = "pocketpy"; String _libName = "pocketpy";
@ -53,11 +53,11 @@ class PyOutput {
: stdout = json['stdout'], stderr = json['stderr']; : stdout = json['stdout'], stderr = json['stderr'];
} }
class Str { class _Str {
static final Finalizer<ffi.Pointer<Utf8>> finalizer = Finalizer((p) => calloc.free(p)); static final Finalizer<ffi.Pointer<Utf8>> finalizer = Finalizer((p) => calloc.free(p));
late final ffi.Pointer<Utf8> _p; late final ffi.Pointer<Utf8> _p;
Str(String s) { _Str(String s) {
_p = s.toNativeUtf8(); _p = s.toNativeUtf8();
finalizer.attach(this, _p); finalizer.attach(this, _p);
} }
@ -70,35 +70,35 @@ class VM {
VM() { VM() {
if (this is ThreadedVM) { if (this is ThreadedVM) {
pointer = Bindings.pkpy_new_tvm(false); pointer = _Bindings.pkpy_new_tvm(false);
} else { } else {
pointer = Bindings.pkpy_new_vm(false); pointer = _Bindings.pkpy_new_vm(false);
} }
} }
void dispose() { void dispose() {
Bindings.pkpy_delete(pointer); _Bindings.pkpy_delete(pointer);
} }
PyOutput read_output() { PyOutput read_output() {
var _o = Bindings.pkpy_vm_read_output(pointer); var _o = _Bindings.pkpy_vm_read_output(pointer);
String _j = _o.toDartString(); String _j = _o.toDartString();
var ret = PyOutput.fromJson(cvt.jsonDecode(_j)); var ret = PyOutput.fromJson(cvt.jsonDecode(_j));
Bindings.pkpy_delete(_o); _Bindings.pkpy_delete(_o);
return ret; return ret;
} }
/// Add a source module into a virtual machine. Return `true` if there is no complie error. /// Add a source module into a virtual machine. Return `true` if there is no complie error.
bool add_module(String name, String source) bool add_module(String name, String source)
{ {
var ret = Bindings.pkpy_vm_add_module(pointer, Str(name).p, Str(source).p); var ret = _Bindings.pkpy_vm_add_module(pointer, _Str(name).p, _Str(source).p);
return ret; return ret;
} }
/// Evaluate an expression. Return a json representing the result. If there is any error, return `nullptr`. /// Evaluate an expression. Return a json representing the result. If there is any error, return `nullptr`.
String? eval(String source) String? eval(String source)
{ {
var ret = Bindings.pkpy_vm_eval(pointer, Str(source).p); var ret = _Bindings.pkpy_vm_eval(pointer, _Str(source).p);
if (ret == ffi.nullptr) return null; if (ret == ffi.nullptr) return null;
String s = ret.toDartString(); String s = ret.toDartString();
calloc.free(ret); calloc.free(ret);
@ -108,14 +108,14 @@ class VM {
/// Run a given source on a virtual machine. Return `true` if there is no compile error. /// Run a given source on a virtual machine. Return `true` if there is no compile error.
bool exec(String source) bool exec(String source)
{ {
var ret = Bindings.pkpy_vm_exec(pointer, Str(source).p); var ret = _Bindings.pkpy_vm_exec(pointer, _Str(source).p);
return ret; return ret;
} }
/// Get a global variable of a virtual machine. Return a json representing the result. If the variable is not found, return `nullptr`. /// Get a global variable of a virtual machine. Return a json representing the result. If the variable is not found, return `nullptr`.
String? get_global(String name) String? get_global(String name)
{ {
var ret = Bindings.pkpy_vm_get_global(pointer, Str(name).p); var ret = _Bindings.pkpy_vm_get_global(pointer, _Str(name).p);
if (ret == ffi.nullptr) return null; if (ret == ffi.nullptr) return null;
String s = ret.toDartString(); String s = ret.toDartString();
calloc.free(ret); calloc.free(ret);
@ -127,19 +127,19 @@ class VM {
enum ThreadState { ready, running, suspended, finished } enum ThreadState { ready, running, suspended, finished }
class ThreadedVM extends VM { class ThreadedVM extends VM {
ThreadState get state => ThreadState.values[Bindings.pkpy_tvm_get_state(pointer)]; ThreadState get state => ThreadState.values[_Bindings.pkpy_tvm_get_state(pointer)];
/// Run a given source on a threaded virtual machine. The excution will be started in a new thread. Return `true` if there is no compile error. /// Run a given source on a threaded virtual machine. The excution will be started in a new thread. Return `true` if there is no compile error.
bool exec_async(String source) bool exec_async(String source)
{ {
var ret = Bindings.pkpy_tvm_exec_async(pointer, Str(source).p); var ret = _Bindings.pkpy_tvm_exec_async(pointer, _Str(source).p);
return ret; return ret;
} }
/// Read the current JSONRPC request from shared string buffer. /// Read the current JSONRPC request from shared string buffer.
String? read_jsonrpc_request() String? read_jsonrpc_request()
{ {
var ret = Bindings.pkpy_tvm_read_jsonrpc_request(pointer); var ret = _Bindings.pkpy_tvm_read_jsonrpc_request(pointer);
if (ret == ffi.nullptr) return null; if (ret == ffi.nullptr) return null;
String s = ret.toDartString(); String s = ret.toDartString();
calloc.free(ret); calloc.free(ret);
@ -149,19 +149,19 @@ class ThreadedVM extends VM {
/// Set the state of a threaded virtual machine to `THREAD_READY`. The current state should be `THREAD_FINISHED`. /// Set the state of a threaded virtual machine to `THREAD_READY`. The current state should be `THREAD_FINISHED`.
void reset_state() void reset_state()
{ {
Bindings.pkpy_tvm_reset_state(pointer); _Bindings.pkpy_tvm_reset_state(pointer);
} }
/// Emit a KeyboardInterrupt signal to stop a running threaded virtual machine. /// Emit a KeyboardInterrupt signal to stop a running threaded virtual machine.
void terminate() void terminate()
{ {
Bindings.pkpy_tvm_terminate(pointer); _Bindings.pkpy_tvm_terminate(pointer);
} }
/// Write a JSONRPC response to shared string buffer. /// Write a JSONRPC response to shared string buffer.
void write_jsonrpc_response(String value) void write_jsonrpc_response(String value)
{ {
Bindings.pkpy_tvm_write_jsonrpc_response(pointer, Str(value).p); _Bindings.pkpy_tvm_write_jsonrpc_response(pointer, _Str(value).p);
} }
} }
@ -170,17 +170,17 @@ class REPL {
late final ffi.Pointer pointer; late final ffi.Pointer pointer;
REPL(VM vm) { REPL(VM vm) {
pointer = Bindings.pkpy_new_repl(vm.pointer); pointer = _Bindings.pkpy_new_repl(vm.pointer);
} }
void dispose() { void dispose() {
Bindings.pkpy_delete(pointer); _Bindings.pkpy_delete(pointer);
} }
/// Input a source line to an interactive console. Return `0` if need more lines, `1` if execution happened, `2` if execution skipped (compile error or empty input). /// Input a source line to an interactive console. Return `0` if need more lines, `1` if execution happened, `2` if execution skipped (compile error or empty input).
int input(String line) int input(String line)
{ {
var ret = Bindings.pkpy_repl_input(pointer, Str(line).p); var ret = _Bindings.pkpy_repl_input(pointer, _Str(line).p);
return ret; return ret;
} }

View File

@ -1,6 +1,6 @@
name: pocketpy name: pocketpy
description: A lightweight Python interpreter for game engines. description: A lightweight Python interpreter for game engines.
version: 0.4.7 version: 0.4.7+1
homepage: https://pocketpy.dev homepage: https://pocketpy.dev
repository: https://github.com/blueloveth/pocketpy repository: https://github.com/blueloveth/pocketpy