no_web.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. import 'common.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.Void Function(ffi.Pointer r, ffi.Pointer<Utf8> line), void Function(ffi.Pointer r, ffi.Pointer<Utf8> line)>("pkpy_repl_input");
  26. static final pkpy_repl_last_input_result = _lib.lookupFunction<ffi.Int32 Function(ffi.Pointer r), int Function(ffi.Pointer r)>("pkpy_repl_last_input_result");
  27. static final pkpy_new_tvm = _lib.lookupFunction<ffi.Pointer Function(ffi.Bool use_stdio), ffi.Pointer Function(bool use_stdio)>("pkpy_new_tvm");
  28. static final pkpy_tvm_exec_async = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm, ffi.Pointer<Utf8> source), void Function(ffi.Pointer vm, ffi.Pointer<Utf8> source)>("pkpy_tvm_exec_async");
  29. static final pkpy_tvm_get_state = _lib.lookupFunction<ffi.Int32 Function(ffi.Pointer vm), int Function(ffi.Pointer vm)>("pkpy_tvm_get_state");
  30. 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");
  31. static final pkpy_tvm_reset_state = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm), void Function(ffi.Pointer vm)>("pkpy_tvm_reset_state");
  32. static final pkpy_tvm_terminate = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm), void Function(ffi.Pointer vm)>("pkpy_tvm_terminate");
  33. 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");
  34. static final pkpy_new_vm = _lib.lookupFunction<ffi.Pointer Function(ffi.Bool use_stdio), ffi.Pointer Function(bool use_stdio)>("pkpy_new_vm");
  35. static final pkpy_vm_add_module = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm, ffi.Pointer<Utf8> name, ffi.Pointer<Utf8> source), void Function(ffi.Pointer vm, ffi.Pointer<Utf8> name, ffi.Pointer<Utf8> source)>("pkpy_vm_add_module");
  36. 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");
  37. static final pkpy_vm_exec = _lib.lookupFunction<ffi.Void Function(ffi.Pointer vm, ffi.Pointer<Utf8> source), void Function(ffi.Pointer vm, ffi.Pointer<Utf8> source)>("pkpy_vm_exec");
  38. 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");
  39. 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");
  40. }
  41. class _Str {
  42. static final Finalizer<ffi.Pointer<Utf8>> finalizer = Finalizer((p) => malloc.free(p));
  43. late final ffi.Pointer<Utf8> _p;
  44. _Str(String s) {
  45. _p = s.toNativeUtf8();
  46. finalizer.attach(this, _p);
  47. }
  48. ffi.Pointer<Utf8> get p => _p;
  49. }
  50. class VM {
  51. late final ffi.Pointer pointer;
  52. VM() {
  53. if (this is ThreadedVM) {
  54. pointer = _Bindings.pkpy_new_tvm(false);
  55. } else {
  56. pointer = _Bindings.pkpy_new_vm(false);
  57. }
  58. }
  59. void dispose() {
  60. _Bindings.pkpy_delete(pointer);
  61. }
  62. PyOutput read_output() {
  63. var _o = _Bindings.pkpy_vm_read_output(pointer);
  64. String _j = _o.toDartString();
  65. var ret = PyOutput.fromJson(cvt.jsonDecode(_j));
  66. _Bindings.pkpy_delete(_o);
  67. return ret;
  68. }
  69. /// Add a source module into a virtual machine.
  70. void add_module(String name, String source)
  71. {
  72. _Bindings.pkpy_vm_add_module(pointer, _Str(name).p, _Str(source).p);
  73. }
  74. /// Evaluate an expression. Return a json representing the result. If there is any error, return `nullptr`.
  75. String? eval(String source)
  76. {
  77. var ret = _Bindings.pkpy_vm_eval(pointer, _Str(source).p);
  78. if (ret == ffi.nullptr) return null;
  79. String s = ret.toDartString();
  80. _Bindings.pkpy_delete(ret);
  81. return s;
  82. }
  83. /// Run a given source on a virtual machine.
  84. void exec(String source)
  85. {
  86. _Bindings.pkpy_vm_exec(pointer, _Str(source).p);
  87. }
  88. /// Get a global variable of a virtual machine. Return a json representing the result. If the variable is not found, return `nullptr`.
  89. String? get_global(String name)
  90. {
  91. var ret = _Bindings.pkpy_vm_get_global(pointer, _Str(name).p);
  92. if (ret == ffi.nullptr) return null;
  93. String s = ret.toDartString();
  94. _Bindings.pkpy_delete(ret);
  95. return s;
  96. }
  97. }
  98. enum ThreadState { ready, running, suspended, finished }
  99. class ThreadedVM extends VM {
  100. ThreadState get state => ThreadState.values[_Bindings.pkpy_tvm_get_state(pointer)];
  101. /// Run a given source on a threaded virtual machine. The excution will be started in a new thread.
  102. void exec_async(String source)
  103. {
  104. _Bindings.pkpy_tvm_exec_async(pointer, _Str(source).p);
  105. }
  106. /// Read the current JSONRPC request from shared string buffer.
  107. String? read_jsonrpc_request()
  108. {
  109. var ret = _Bindings.pkpy_tvm_read_jsonrpc_request(pointer);
  110. if (ret == ffi.nullptr) return null;
  111. String s = ret.toDartString();
  112. _Bindings.pkpy_delete(ret);
  113. return s;
  114. }
  115. /// Set the state of a threaded virtual machine to `THREAD_READY`. The current state should be `THREAD_FINISHED`.
  116. void reset_state()
  117. {
  118. _Bindings.pkpy_tvm_reset_state(pointer);
  119. }
  120. /// Emit a KeyboardInterrupt signal to stop a running threaded virtual machine.
  121. void terminate()
  122. {
  123. _Bindings.pkpy_tvm_terminate(pointer);
  124. }
  125. /// Write a JSONRPC response to shared string buffer.
  126. void write_jsonrpc_response(String value)
  127. {
  128. _Bindings.pkpy_tvm_write_jsonrpc_response(pointer, _Str(value).p);
  129. }
  130. }
  131. class REPL {
  132. late final ffi.Pointer pointer;
  133. REPL(VM vm) {
  134. pointer = _Bindings.pkpy_new_repl(vm.pointer);
  135. }
  136. void dispose() {
  137. _Bindings.pkpy_delete(pointer);
  138. }
  139. /// Input a source line to an interactive console.
  140. void input(String line)
  141. {
  142. _Bindings.pkpy_repl_input(pointer, _Str(line).p);
  143. }
  144. /// Check if the REPL needs more lines.
  145. int last_input_result()
  146. {
  147. var ret = _Bindings.pkpy_repl_last_input_result(pointer);
  148. return ret;
  149. }
  150. }