pocketpy.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ignore_for_file: non_constant_identifier_names, prefer_typing_uninitialized_variables, constant_identifier_names, no_leading_underscores_for_local_identifiers
  2. import 'dart:convert' as cvt;
  3. import 'dart:ffi' as ffi;
  4. import 'dart:io';
  5. import 'package:ffi/ffi.dart';
  6. export 'jsonrpc.dart';
  7. class _Bindings
  8. {
  9. static ffi.DynamicLibrary _load() {
  10. String _libName = "pocketpy";
  11. if (Platform.isIOS) {
  12. return ffi.DynamicLibrary.process();
  13. }
  14. if (Platform.isAndroid || Platform.isLinux) {
  15. return ffi.DynamicLibrary.open('lib$_libName.so');
  16. }
  17. if (Platform.isWindows) {
  18. return ffi.DynamicLibrary.open('$_libName.dll');
  19. }
  20. throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
  21. }
  22. static final _lib = _load();
  23. static final pkpy_delete = _lib.lookupFunction<ffi.Void Function(ffi.Pointer p), void Function(ffi.Pointer p)>("pkpy_delete");
  24. static final pkpy_new_repl = _lib.lookupFunction<ffi.Pointer Function(ffi.Pointer vm), ffi.Pointer Function(ffi.Pointer vm)>("pkpy_new_repl");
  25. static final pkpy_repl_input = _lib.lookupFunction<ffi.Int32 Function(ffi.Pointer r, ffi.Pointer<Utf8> line), int Function(ffi.Pointer r, ffi.Pointer<Utf8> line)>("pkpy_repl_input");
  26. static final pkpy_new_tvm = _lib.lookupFunction<ffi.Pointer Function(ffi.Bool use_stdio), ffi.Pointer Function(bool use_stdio)>("pkpy_new_tvm");
  27. static final pkpy_tvm_exec_async = _lib.lookupFunction<ffi.Bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> source), bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> source)>("pkpy_tvm_exec_async");
  28. static final pkpy_tvm_get_state = _lib.lookupFunction<ffi.Int32 Function(ffi.Pointer vm), int Function(ffi.Pointer vm)>("pkpy_tvm_get_state");
  29. static final pkpy_tvm_read_jsonrpc_request = _lib.lookupFunction<ffi.Pointer<Utf8> Function(ffi.Pointer vm), ffi.Pointer<Utf8> Function(ffi.Pointer vm)>("pkpy_tvm_read_jsonrpc_request");
  30. static final pkpy_tvm_reset_state = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm), void Function(ffi.Pointer vm)>("pkpy_tvm_reset_state");
  31. static final pkpy_tvm_terminate = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm), void Function(ffi.Pointer vm)>("pkpy_tvm_terminate");
  32. static final pkpy_tvm_write_jsonrpc_response = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm, ffi.Pointer<Utf8> value), void Function(ffi.Pointer vm, ffi.Pointer<Utf8> value)>("pkpy_tvm_write_jsonrpc_response");
  33. static final pkpy_new_vm = _lib.lookupFunction<ffi.Pointer Function(ffi.Bool use_stdio), ffi.Pointer Function(bool use_stdio)>("pkpy_new_vm");
  34. static final pkpy_vm_add_module = _lib.lookupFunction<ffi.Bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> name, ffi.Pointer<Utf8> source), bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> name, ffi.Pointer<Utf8> source)>("pkpy_vm_add_module");
  35. static final pkpy_vm_eval = _lib.lookupFunction<ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> source), ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> source)>("pkpy_vm_eval");
  36. static final pkpy_vm_exec = _lib.lookupFunction<ffi.Bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> source), bool Function(ffi.Pointer vm, ffi.Pointer<Utf8> source)>("pkpy_vm_exec");
  37. static final pkpy_vm_get_global = _lib.lookupFunction<ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> name), ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> name)>("pkpy_vm_get_global");
  38. static final pkpy_vm_read_output = _lib.lookupFunction<ffi.Pointer<Utf8> Function(ffi.Pointer vm), ffi.Pointer<Utf8> Function(ffi.Pointer vm)>("pkpy_vm_read_output");
  39. }
  40. class PyOutput {
  41. final String stdout;
  42. final String stderr;
  43. PyOutput(this.stdout, this.stderr);
  44. PyOutput.fromJson(Map<String, dynamic> json)
  45. : stdout = json['stdout'], stderr = json['stderr'];
  46. }
  47. class _Str {
  48. static final Finalizer<ffi.Pointer<Utf8>> finalizer = Finalizer((p) => calloc.free(p));
  49. late final ffi.Pointer<Utf8> _p;
  50. _Str(String s) {
  51. _p = s.toNativeUtf8();
  52. finalizer.attach(this, _p);
  53. }
  54. ffi.Pointer<Utf8> get p => _p;
  55. }
  56. class VM {
  57. late final ffi.Pointer pointer;
  58. VM() {
  59. if (this is ThreadedVM) {
  60. pointer = _Bindings.pkpy_new_tvm(false);
  61. } else {
  62. pointer = _Bindings.pkpy_new_vm(false);
  63. }
  64. }
  65. void dispose() {
  66. _Bindings.pkpy_delete(pointer);
  67. }
  68. PyOutput read_output() {
  69. var _o = _Bindings.pkpy_vm_read_output(pointer);
  70. String _j = _o.toDartString();
  71. var ret = PyOutput.fromJson(cvt.jsonDecode(_j));
  72. _Bindings.pkpy_delete(_o);
  73. return ret;
  74. }
  75. /// Add a source module into a virtual machine. Return `true` if there is no complie error.
  76. bool add_module(String name, String source)
  77. {
  78. var ret = _Bindings.pkpy_vm_add_module(pointer, _Str(name).p, _Str(source).p);
  79. return ret;
  80. }
  81. /// Evaluate an expression. Return a json representing the result. If there is any error, return `nullptr`.
  82. String? eval(String source)
  83. {
  84. var ret = _Bindings.pkpy_vm_eval(pointer, _Str(source).p);
  85. if (ret == ffi.nullptr) return null;
  86. String s = ret.toDartString();
  87. calloc.free(ret);
  88. return s;
  89. }
  90. /// Run a given source on a virtual machine. Return `true` if there is no compile error.
  91. bool exec(String source)
  92. {
  93. var ret = _Bindings.pkpy_vm_exec(pointer, _Str(source).p);
  94. return ret;
  95. }
  96. /// Get a global variable of a virtual machine. Return a json representing the result. If the variable is not found, return `nullptr`.
  97. String? get_global(String name)
  98. {
  99. var ret = _Bindings.pkpy_vm_get_global(pointer, _Str(name).p);
  100. if (ret == ffi.nullptr) return null;
  101. String s = ret.toDartString();
  102. calloc.free(ret);
  103. return s;
  104. }
  105. }
  106. enum ThreadState { ready, running, suspended, finished }
  107. class ThreadedVM extends VM {
  108. ThreadState get state => ThreadState.values[_Bindings.pkpy_tvm_get_state(pointer)];
  109. /// 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.
  110. bool exec_async(String source)
  111. {
  112. var ret = _Bindings.pkpy_tvm_exec_async(pointer, _Str(source).p);
  113. return ret;
  114. }
  115. /// Read the current JSONRPC request from shared string buffer.
  116. String? read_jsonrpc_request()
  117. {
  118. var ret = _Bindings.pkpy_tvm_read_jsonrpc_request(pointer);
  119. if (ret == ffi.nullptr) return null;
  120. String s = ret.toDartString();
  121. calloc.free(ret);
  122. return s;
  123. }
  124. /// Set the state of a threaded virtual machine to `THREAD_READY`. The current state should be `THREAD_FINISHED`.
  125. void reset_state()
  126. {
  127. _Bindings.pkpy_tvm_reset_state(pointer);
  128. }
  129. /// Emit a KeyboardInterrupt signal to stop a running threaded virtual machine.
  130. void terminate()
  131. {
  132. _Bindings.pkpy_tvm_terminate(pointer);
  133. }
  134. /// Write a JSONRPC response to shared string buffer.
  135. void write_jsonrpc_response(String value)
  136. {
  137. _Bindings.pkpy_tvm_write_jsonrpc_response(pointer, _Str(value).p);
  138. }
  139. }
  140. class REPL {
  141. late final ffi.Pointer pointer;
  142. REPL(VM vm) {
  143. pointer = _Bindings.pkpy_new_repl(vm.pointer);
  144. }
  145. void dispose() {
  146. _Bindings.pkpy_delete(pointer);
  147. }
  148. /// 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).
  149. int input(String line)
  150. {
  151. var ret = _Bindings.pkpy_repl_input(pointer, _Str(line).p);
  152. return ret;
  153. }
  154. }