|
@@ -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;
|
|
|
}
|
|
}
|
|
|
|
|
|