mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
remove unused assets
This commit is contained in:
parent
492c3fda00
commit
b530937393
@ -1,4 +0,0 @@
|
|||||||
include: package:flutter_lints/flutter.yaml
|
|
||||||
|
|
||||||
# Additional information about this file can be found at
|
|
||||||
# https://dart.dev/guides/language/analysis-options
|
|
@ -1,7 +1,5 @@
|
|||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pocketpy/pocketpy.dart' as pkpy;
|
import 'package:pocketpy/pocketpy.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MaterialApp(home: MyApp()));
|
runApp(const MaterialApp(home: MyApp()));
|
||||||
@ -15,102 +13,17 @@ class MyApp extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
late final pkpy.VM vm;
|
|
||||||
late final pkpy.REPL repl;
|
|
||||||
bool needMoreLines = false;
|
|
||||||
|
|
||||||
final TextEditingController _controller = TextEditingController();
|
|
||||||
final StringBuffer buffer = StringBuffer();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
test();
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
// create a pocketpy virtual machine
|
|
||||||
vm = pkpy.VM();
|
|
||||||
|
|
||||||
// create a REPL
|
|
||||||
repl = pkpy.REPL(vm);
|
|
||||||
|
|
||||||
// create a binding
|
|
||||||
vm.bind<int>("builtins", "add", (int x, int y) => x + y);
|
|
||||||
vm.bind<String>("builtins", "test", (String x) => x.split(',').join('|'));
|
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
|
||||||
refresh();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void addMessage(String text) {
|
|
||||||
setState(() {
|
|
||||||
buffer.write(text);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void submitCode() {
|
|
||||||
var text = _controller.text;
|
|
||||||
_controller.clear();
|
|
||||||
setState(() {
|
|
||||||
buffer.write(needMoreLines ? '... $text' : '>>> $text\n');
|
|
||||||
});
|
|
||||||
if (text == "exit()") exit(0);
|
|
||||||
needMoreLines = repl.input(text) == 0;
|
|
||||||
refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
void refresh() {
|
|
||||||
// ignore: no_leading_underscores_for_local_identifiers
|
|
||||||
var _o = vm.read_output();
|
|
||||||
if (_o.stdout.isNotEmpty) buffer.write(_o.stdout);
|
|
||||||
if (_o.stderr.isNotEmpty) buffer.write(_o.stderr);
|
|
||||||
setState(() {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var style = const TextStyle(fontSize: 16);
|
return const Scaffold(
|
||||||
return Scaffold(
|
body: Center(
|
||||||
appBar: AppBar(
|
child: Text("Hello World!"),
|
||||||
title: const Text('Demo'),
|
|
||||||
),
|
|
||||||
body: Padding(
|
|
||||||
padding: const EdgeInsets.all(8.0),
|
|
||||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
||||||
Expanded(
|
|
||||||
child: SingleChildScrollView(
|
|
||||||
reverse: true,
|
|
||||||
child: Text(
|
|
||||||
buffer.toString(),
|
|
||||||
style: style,
|
|
||||||
textAlign: TextAlign.left,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(
|
|
||||||
height: 16,
|
|
||||||
),
|
|
||||||
SizedBox(
|
|
||||||
height: 50,
|
|
||||||
child: TextFormField(
|
|
||||||
controller: _controller,
|
|
||||||
style: style,
|
|
||||||
maxLines: 1,
|
|
||||||
decoration: const InputDecoration(
|
|
||||||
border: OutlineInputBorder(),
|
|
||||||
hintText: 'Enter Python code',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Container(
|
|
||||||
height: 60,
|
|
||||||
alignment: Alignment.centerRight,
|
|
||||||
child: MaterialButton(
|
|
||||||
onPressed: submitCode,
|
|
||||||
color: Colors.blue,
|
|
||||||
textColor: Colors.white,
|
|
||||||
child: const Text('Run')),
|
|
||||||
),
|
|
||||||
]),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,107 +1,51 @@
|
|||||||
# Generated by pub
|
# Generated by pub
|
||||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
packages:
|
packages:
|
||||||
async:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: async
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.9.0"
|
|
||||||
boolean_selector:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: boolean_selector
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.1.0"
|
|
||||||
characters:
|
characters:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: characters
|
name: characters
|
||||||
url: "https://pub.dartlang.org"
|
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.2.1"
|
version: "1.2.1"
|
||||||
clock:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: clock
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.1.1"
|
|
||||||
collection:
|
collection:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: collection
|
name: collection
|
||||||
url: "https://pub.dartlang.org"
|
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.16.0"
|
version: "1.17.0"
|
||||||
cupertino_icons:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: cupertino_icons
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.0.5"
|
|
||||||
fake_async:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: fake_async
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.3.1"
|
|
||||||
ffi:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: ffi
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.0.1"
|
|
||||||
flutter:
|
flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
flutter_test:
|
|
||||||
dependency: "direct dev"
|
|
||||||
description: flutter
|
|
||||||
source: sdk
|
|
||||||
version: "0.0.0"
|
|
||||||
js:
|
js:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: js
|
name: js
|
||||||
url: "https://pub.dartlang.org"
|
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.6.5"
|
version: "0.6.5"
|
||||||
matcher:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: matcher
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "0.12.12"
|
|
||||||
material_color_utilities:
|
material_color_utilities:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: material_color_utilities
|
name: material_color_utilities
|
||||||
url: "https://pub.dartlang.org"
|
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.1.5"
|
version: "0.2.0"
|
||||||
meta:
|
meta:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: meta
|
name: meta
|
||||||
url: "https://pub.dartlang.org"
|
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.8.0"
|
version: "1.8.0"
|
||||||
path:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: path
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.8.2"
|
|
||||||
pocketpy:
|
pocketpy:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -114,55 +58,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.99"
|
version: "0.0.99"
|
||||||
source_span:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: source_span
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.9.0"
|
|
||||||
stack_trace:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: stack_trace
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.10.0"
|
|
||||||
stream_channel:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: stream_channel
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "2.1.0"
|
|
||||||
string_scanner:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: string_scanner
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.1.1"
|
|
||||||
term_glyph:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: term_glyph
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "1.2.1"
|
|
||||||
test_api:
|
|
||||||
dependency: transitive
|
|
||||||
description:
|
|
||||||
name: test_api
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "0.4.12"
|
|
||||||
vector_math:
|
vector_math:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: vector_math
|
name: vector_math
|
||||||
url: "https://pub.dartlang.org"
|
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
|
||||||
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.2"
|
version: "2.1.4"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.17.0 <3.0.0"
|
dart: ">=2.17.0 <3.0.0"
|
||||||
flutter: ">=2.5.0"
|
flutter: ">=2.5.0"
|
||||||
|
@ -40,14 +40,6 @@ dependencies:
|
|||||||
# the parent directory to use the current plugin's version.
|
# the parent directory to use the current plugin's version.
|
||||||
path: ../
|
path: ../
|
||||||
|
|
||||||
# The following adds the Cupertino Icons font to your application.
|
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
|
||||||
cupertino_icons: ^1.0.2
|
|
||||||
|
|
||||||
dev_dependencies:
|
|
||||||
flutter_test:
|
|
||||||
sdk: flutter
|
|
||||||
|
|
||||||
# For information on the generic Dart part of this file, see the
|
# For information on the generic Dart part of this file, see the
|
||||||
# following page: https://dart.dev/tools/pub/pubspec
|
# following page: https://dart.dev/tools/pub/pubspec
|
||||||
|
|
||||||
|
@ -1,76 +0,0 @@
|
|||||||
// ignore_for_file: non_constant_identifier_names
|
|
||||||
|
|
||||||
import 'dart:convert';
|
|
||||||
import 'dart:ffi' as ffi;
|
|
||||||
import 'package:ffi/ffi.dart';
|
|
||||||
|
|
||||||
Map<String, Function> _mappings = {};
|
|
||||||
|
|
||||||
class StrWrapper {
|
|
||||||
static final Finalizer<ffi.Pointer<Utf8>> finalizer =
|
|
||||||
Finalizer((p) => malloc.free(p));
|
|
||||||
|
|
||||||
late final ffi.Pointer<Utf8> _p;
|
|
||||||
StrWrapper(String s) {
|
|
||||||
_p = s.toNativeUtf8();
|
|
||||||
finalizer.attach(this, _p);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffi.Pointer<Utf8> get p => _p;
|
|
||||||
}
|
|
||||||
|
|
||||||
dynamic invoke_f_any(ffi.Pointer<Utf8> p) {
|
|
||||||
String s = p.toDartString();
|
|
||||||
var parts = s.split(' ');
|
|
||||||
List<dynamic> args = [];
|
|
||||||
for (int i = 1; i < parts.length; i++) {
|
|
||||||
args.add(jsonDecode(parts[i]));
|
|
||||||
}
|
|
||||||
var f = _mappings[parts[0]];
|
|
||||||
return Function.apply(f!, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
int invoke_f_int(ffi.Pointer<Utf8> p) => invoke_f_any(p);
|
|
||||||
double invoke_f_float(ffi.Pointer<Utf8> p) => invoke_f_any(p);
|
|
||||||
bool invoke_f_bool(ffi.Pointer<Utf8> p) => invoke_f_any(p);
|
|
||||||
ffi.Pointer<Utf8> invoke_f_str(ffi.Pointer<Utf8> p) =>
|
|
||||||
StrWrapper(invoke_f_any(p)).p;
|
|
||||||
void invoke_f_None(ffi.Pointer<Utf8> p) => invoke_f_any(p);
|
|
||||||
|
|
||||||
ffi.Pointer f_int() {
|
|
||||||
return ffi.Pointer.fromFunction<ffi.Int64 Function(ffi.Pointer<Utf8>)>(
|
|
||||||
invoke_f_int, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffi.Pointer f_float() {
|
|
||||||
return ffi.Pointer.fromFunction<ffi.Double Function(ffi.Pointer<Utf8>)>(
|
|
||||||
invoke_f_float, 0.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffi.Pointer f_bool() {
|
|
||||||
return ffi.Pointer.fromFunction<ffi.Bool Function(ffi.Pointer<Utf8>)>(
|
|
||||||
invoke_f_bool, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffi.Pointer f_str() {
|
|
||||||
return ffi.Pointer.fromFunction<
|
|
||||||
ffi.Pointer<Utf8> Function(ffi.Pointer<Utf8>)>(invoke_f_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
ffi.Pointer f_None() {
|
|
||||||
return ffi.Pointer.fromFunction<ffi.Void Function(ffi.Pointer<Utf8>)>(
|
|
||||||
invoke_f_None);
|
|
||||||
}
|
|
||||||
|
|
||||||
void register(String? key, Function value) {
|
|
||||||
_mappings[key!] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int t_code<T>() {
|
|
||||||
if (T == int) return 'i'.codeUnitAt(0);
|
|
||||||
if (T == double) return 'f'.codeUnitAt(0);
|
|
||||||
if (T == bool) return 'b'.codeUnitAt(0);
|
|
||||||
if (T == String) return 's'.codeUnitAt(0);
|
|
||||||
return 'N'.codeUnitAt(0);
|
|
||||||
// throw Exception("Type must be int/double/bool/String");
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
class PyOutput {
|
|
||||||
final String stdout;
|
|
||||||
final String stderr;
|
|
||||||
PyOutput(this.stdout, this.stderr);
|
|
||||||
|
|
||||||
PyOutput.fromJson(Map<String, dynamic> json)
|
|
||||||
: stdout = json['stdout'],
|
|
||||||
stderr = json['stderr'];
|
|
||||||
}
|
|
@ -1,152 +0,0 @@
|
|||||||
// ignore_for_file: non_constant_identifier_names, prefer_typing_uninitialized_variables, constant_identifier_names, no_leading_underscores_for_local_identifiers
|
|
||||||
|
|
||||||
import 'dart:convert' as cvt;
|
|
||||||
import 'dart:ffi' as ffi;
|
|
||||||
import 'dart:io';
|
|
||||||
import 'package:ffi/ffi.dart';
|
|
||||||
import '_ffi.dart';
|
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
class _Bindings {
|
|
||||||
static ffi.DynamicLibrary _load() {
|
|
||||||
String _libName = "pocketpy";
|
|
||||||
if (Platform.isIOS) {
|
|
||||||
return ffi.DynamicLibrary.process();
|
|
||||||
}
|
|
||||||
if (Platform.isAndroid || Platform.isLinux) {
|
|
||||||
return ffi.DynamicLibrary.open('lib$_libName.so');
|
|
||||||
}
|
|
||||||
if (Platform.isWindows) {
|
|
||||||
return ffi.DynamicLibrary.open('$_libName.dll');
|
|
||||||
}
|
|
||||||
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
|
|
||||||
}
|
|
||||||
|
|
||||||
static final _lib = _load();
|
|
||||||
|
|
||||||
static final pkpy_delete = _lib.lookupFunction<
|
|
||||||
ffi.Void Function(ffi.Pointer p),
|
|
||||||
void Function(ffi.Pointer p)>("pkpy_delete");
|
|
||||||
static final pkpy_setup_callbacks = _lib.lookupFunction<
|
|
||||||
ffi.Void Function(ffi.Pointer _f_int, ffi.Pointer _f_float,
|
|
||||||
ffi.Pointer _f_bool, ffi.Pointer _f_str, ffi.Pointer _f_None),
|
|
||||||
void Function(
|
|
||||||
ffi.Pointer _f_int,
|
|
||||||
ffi.Pointer _f_float,
|
|
||||||
ffi.Pointer _f_bool,
|
|
||||||
ffi.Pointer _f_str,
|
|
||||||
ffi.Pointer _f_None)>("pkpy_setup_callbacks");
|
|
||||||
static final pkpy_new_repl = _lib.lookupFunction<
|
|
||||||
ffi.Pointer Function(ffi.Pointer vm),
|
|
||||||
ffi.Pointer Function(ffi.Pointer vm)>("pkpy_new_repl");
|
|
||||||
static final pkpy_repl_input = _lib.lookupFunction<
|
|
||||||
ffi.Bool Function(ffi.Pointer r, ffi.Pointer<Utf8> line),
|
|
||||||
bool Function(ffi.Pointer r, ffi.Pointer<Utf8> line)>("pkpy_repl_input");
|
|
||||||
static final pkpy_new_vm = _lib.lookupFunction<
|
|
||||||
ffi.Pointer Function(ffi.Bool use_stdio),
|
|
||||||
ffi.Pointer Function(bool use_stdio)>("pkpy_new_vm");
|
|
||||||
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");
|
|
||||||
static final pkpy_vm_bind = _lib.lookupFunction<
|
|
||||||
ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> mod,
|
|
||||||
ffi.Pointer<Utf8> name, ffi.Int32 ret_code),
|
|
||||||
ffi.Pointer<Utf8> Function(ffi.Pointer vm, ffi.Pointer<Utf8> mod,
|
|
||||||
ffi.Pointer<Utf8> name, int ret_code)>("pkpy_vm_bind");
|
|
||||||
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");
|
|
||||||
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");
|
|
||||||
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");
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
class VM {
|
|
||||||
late final pointer = _Bindings.pkpy_new_vm(false);
|
|
||||||
static bool _firstNew = true;
|
|
||||||
|
|
||||||
VM() {
|
|
||||||
if (!_firstNew) return;
|
|
||||||
_firstNew = false;
|
|
||||||
_Bindings.pkpy_setup_callbacks(
|
|
||||||
f_int(), f_float(), f_bool(), f_str(), f_None());
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_Bindings.pkpy_delete(pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
PyOutput read_output() {
|
|
||||||
var _o = _Bindings.pkpy_vm_read_output(pointer);
|
|
||||||
String _j = _o.toDartString();
|
|
||||||
var ret = PyOutput.fromJson(cvt.jsonDecode(_j));
|
|
||||||
_Bindings.pkpy_delete(_o);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a source module into a virtual machine.
|
|
||||||
void add_module(String name, String source) {
|
|
||||||
_Bindings.pkpy_vm_add_module(
|
|
||||||
pointer, StrWrapper(name).p, StrWrapper(source).p);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Evaluate an expression. Return `__repr__` of the result. If there is any error, return `nullptr`.
|
|
||||||
String? eval(String source) {
|
|
||||||
var ret = _Bindings.pkpy_vm_eval(pointer, StrWrapper(source).p);
|
|
||||||
if (ret == ffi.nullptr) return null;
|
|
||||||
String s = ret.toDartString();
|
|
||||||
_Bindings.pkpy_delete(ret);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Run a given source on a virtual machine.
|
|
||||||
void exec(String source) {
|
|
||||||
_Bindings.pkpy_vm_exec(pointer, StrWrapper(source).p);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a global variable of a virtual machine. Return `__repr__` of the result. If the variable is not found, return `nullptr`.
|
|
||||||
String? get_global(String name) {
|
|
||||||
var ret = _Bindings.pkpy_vm_get_global(pointer, StrWrapper(name).p);
|
|
||||||
if (ret == ffi.nullptr) return null;
|
|
||||||
String s = ret.toDartString();
|
|
||||||
_Bindings.pkpy_delete(ret);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind<T>(String mod, String name, Function f) {
|
|
||||||
ffi.Pointer<Utf8> p = _Bindings.pkpy_vm_bind(
|
|
||||||
pointer, StrWrapper(mod).p, StrWrapper(name).p, t_code<T>());
|
|
||||||
if (p == ffi.nullptr) throw Exception("vm.bind() failed");
|
|
||||||
String s = p.toDartString();
|
|
||||||
_Bindings.pkpy_delete(p);
|
|
||||||
register(s, f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class REPL {
|
|
||||||
late final dynamic pointer;
|
|
||||||
|
|
||||||
REPL(VM vm) {
|
|
||||||
pointer = _Bindings.pkpy_new_repl(vm.pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_Bindings.pkpy_delete(pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Input a source line to an interactive console. Return true if need more lines.
|
|
||||||
bool input(String line) {
|
|
||||||
var ret = _Bindings.pkpy_repl_input(pointer, StrWrapper(line).p);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,5 @@
|
|||||||
library pocketpy;
|
library pocketpy;
|
||||||
|
|
||||||
export 'common.dart';
|
void test(){
|
||||||
export 'no_web.dart' if (dart.library.html) 'web.dart';
|
print("Hello World!");
|
||||||
|
}
|
@ -1,92 +0,0 @@
|
|||||||
// ignore_for_file: prefer_function_declarations_over_variables, non_constant_identifier_names, no_leading_underscores_for_local_identifiers
|
|
||||||
import 'dart:convert' as cvt;
|
|
||||||
import 'package:js/js.dart';
|
|
||||||
import 'common.dart';
|
|
||||||
|
|
||||||
@JS("Module.ccall")
|
|
||||||
external dynamic ccall(
|
|
||||||
String name, String? returnType, List<String> argTypes, List<dynamic> args);
|
|
||||||
|
|
||||||
class _Bindings {
|
|
||||||
static final pkpy_delete =
|
|
||||||
(dynamic p) => ccall("pkpy_delete", null, ["number"], [p]);
|
|
||||||
static final pkpy_new_repl =
|
|
||||||
(dynamic vm) => ccall("pkpy_new_repl", "number", ["number"], [vm]);
|
|
||||||
static final pkpy_repl_input = (dynamic r, String line) =>
|
|
||||||
ccall("pkpy_repl_input", "boolean", ["number", "string"], [r, line]);
|
|
||||||
static final pkpy_new_vm = (bool use_stdio) =>
|
|
||||||
ccall("pkpy_new_vm", "number", ["boolean"], [use_stdio]);
|
|
||||||
static final pkpy_vm_add_module = (dynamic vm, String name, String source) =>
|
|
||||||
ccall("pkpy_vm_add_module", null, ["number", "string", "string"],
|
|
||||||
[vm, name, source]);
|
|
||||||
static final pkpy_vm_eval = (dynamic vm, String source) =>
|
|
||||||
ccall("pkpy_vm_eval", "string", ["number", "string"], [vm, source]);
|
|
||||||
static final pkpy_vm_exec = (dynamic vm, String source) =>
|
|
||||||
ccall("pkpy_vm_exec", null, ["number", "string"], [vm, source]);
|
|
||||||
static final pkpy_vm_get_global = (dynamic vm, String name) =>
|
|
||||||
ccall("pkpy_vm_get_global", "string", ["number", "string"], [vm, name]);
|
|
||||||
static final pkpy_vm_read_output =
|
|
||||||
(dynamic vm) => ccall("pkpy_vm_read_output", "string", ["number"], [vm]);
|
|
||||||
}
|
|
||||||
|
|
||||||
class VM {
|
|
||||||
late final pointer = _Bindings.pkpy_new_vm(false);
|
|
||||||
static bool _firstNew = true;
|
|
||||||
|
|
||||||
VM() {
|
|
||||||
if (!_firstNew) return;
|
|
||||||
_firstNew = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_Bindings.pkpy_delete(pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
PyOutput read_output() {
|
|
||||||
var _o = _Bindings.pkpy_vm_read_output(pointer);
|
|
||||||
String _j = _o;
|
|
||||||
var ret = PyOutput.fromJson(cvt.jsonDecode(_j));
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Add a source module into a virtual machine.
|
|
||||||
void add_module(String name, String source) {
|
|
||||||
_Bindings.pkpy_vm_add_module(pointer, name, source);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Evaluate an expression. Return `__repr__` of the result. If there is any error, return `nullptr`.
|
|
||||||
String? eval(String source) {
|
|
||||||
var ret = _Bindings.pkpy_vm_eval(pointer, source);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Run a given source on a virtual machine.
|
|
||||||
void exec(String source) {
|
|
||||||
_Bindings.pkpy_vm_exec(pointer, source);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a global variable of a virtual machine. Return `__repr__` of the result. If the variable is not found, return `nullptr`.
|
|
||||||
String? get_global(String name) {
|
|
||||||
var ret = _Bindings.pkpy_vm_get_global(pointer, name);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class REPL {
|
|
||||||
late final dynamic pointer;
|
|
||||||
|
|
||||||
REPL(VM vm) {
|
|
||||||
pointer = _Bindings.pkpy_new_repl(vm.pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_Bindings.pkpy_delete(pointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Input a source line to an interactive console. Return true if need more lines.
|
|
||||||
bool input(String line) {
|
|
||||||
var ret = _Bindings.pkpy_repl_input(pointer, line);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,13 +11,6 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
ffi: ^2.0.1
|
|
||||||
js: ^0.6.4
|
|
||||||
|
|
||||||
dev_dependencies:
|
|
||||||
flutter_test:
|
|
||||||
sdk: flutter
|
|
||||||
flutter_lints: ^2.0.0
|
|
||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
plugin:
|
plugin:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user