blueloveTH 3 年之前
父節點
當前提交
3d610e4fdc

+ 1 - 1
plugins/flutter/CHANGELOG.md

@@ -1,4 +1,4 @@
-## 0.6.0+2
+## 0.6.0+3
 
 + Break change
 

+ 11 - 4
plugins/flutter/lib/no_web.dart

@@ -26,7 +26,8 @@ class _Bindings
 
   static final pkpy_delete = _lib.lookupFunction<ffi.Void Function(ffi.Pointer p), void Function(ffi.Pointer p)>("pkpy_delete");
   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.Int32 Function(ffi.Pointer r, ffi.Pointer<Utf8> line), int Function(ffi.Pointer r, ffi.Pointer<Utf8> line)>("pkpy_repl_input");
+  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");
+  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");
   static final pkpy_new_tvm = _lib.lookupFunction<ffi.Pointer Function(ffi.Bool use_stdio), ffi.Pointer Function(bool use_stdio)>("pkpy_new_tvm");
   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");
   static final pkpy_tvm_get_state = _lib.lookupFunction<ffi.Int32 Function(ffi.Pointer vm), int Function(ffi.Pointer vm)>("pkpy_tvm_get_state");
@@ -162,10 +163,16 @@ class REPL {
     _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).
-  int input(String line)
+  /// Input a source line to an interactive console.
+  void input(String line)
   {
-    var ret = _Bindings.pkpy_repl_input(pointer, _Str(line).p);
+    _Bindings.pkpy_repl_input(pointer, _Str(line).p);
+  }
+
+  /// Check if the REPL needs more lines.
+  int last_input_result()
+  {
+    var ret = _Bindings.pkpy_repl_last_input_result(pointer);
     return ret;
   }
 

+ 11 - 4
plugins/flutter/lib/web.dart

@@ -10,7 +10,8 @@ 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", "number", ["number", "string"], [r, line]);
+  static final pkpy_repl_input = (dynamic r, String line) => ccall("pkpy_repl_input", null, ["number", "string"], [r, line]);
+  static final pkpy_repl_last_input_result = (dynamic r) => ccall("pkpy_repl_last_input_result", "number", ["number"], [r]);
   static final pkpy_new_tvm = (bool use_stdio) => ccall("pkpy_new_tvm", "number", ["boolean"], [use_stdio]);
   static final pkpy_tvm_exec_async = (dynamic vm, String source) => ccall("pkpy_tvm_exec_async", null, ["number", "string"], [vm, source]);
   static final pkpy_tvm_get_state = (dynamic vm) => ccall("pkpy_tvm_get_state", "number", ["number"], [vm]);
@@ -126,10 +127,16 @@ class REPL {
     _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).
-  int input(String line)
+  /// Input a source line to an interactive console.
+  void input(String line)
   {
-    var ret = _Bindings.pkpy_repl_input(pointer, line);
+    _Bindings.pkpy_repl_input(pointer, line);
+  }
+
+  /// Check if the REPL needs more lines.
+  int last_input_result()
+  {
+    var ret = _Bindings.pkpy_repl_last_input_result(pointer);
     return ret;
   }
 

+ 1 - 1
plugins/flutter/pubspec.yaml

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

+ 26 - 22
plugins/flutter/src/pocketpy.h

@@ -6147,7 +6147,7 @@ __LISTCOMP:
 
 enum InputResult {
     NEED_MORE_LINES = 0,
-    EXEC_DONE = 1,
+    EXEC_STARTED = 1,
     EXEC_SKIPPED = 2,
 };
 
@@ -6156,12 +6156,7 @@ protected:
     int need_more_lines = 0;
     std::string buffer;
     VM* vm;
-    bool exited = false;
-
-    void _exit(){
-        exited = true;
-        exit(0);
-    }
+    InputResult lastResult = EXEC_SKIPPED;
 public:
     REPL(VM* vm) : vm(vm){
         (*vm->_stdout) << ("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ")\n");
@@ -6169,12 +6164,11 @@ public:
         (*vm->_stdout) << ("Type \"exit()\" to exit." "\n");
     }
 
-    bool is_need_more_lines() const {
-        return need_more_lines;
+    InputResult last_input_result() const {
+        return lastResult;
     }
 
-    InputResult input(std::string line){
-        if(exited) return EXEC_SKIPPED;
+    void input(std::string line){
         if(need_more_lines){
             buffer += line;
             buffer += '\n';
@@ -6188,11 +6182,15 @@ public:
                 buffer.clear();
             }else{
 __NOT_ENOUGH_LINES:
-                return NEED_MORE_LINES;
+                lastResult = NEED_MORE_LINES;
+                return;
             }
         }else{
-            if(line == "exit()") _exit();
-            if(line.empty()) return EXEC_SKIPPED;
+            if(line == "exit()") exit(0);
+            if(line.empty()) {
+                lastResult = EXEC_SKIPPED;
+                return;
+            }
         }
 
         try{
@@ -6202,12 +6200,16 @@ __NOT_ENOUGH_LINES:
             buffer += line;
             buffer += '\n';
             need_more_lines = ne.isClassDef ? 3 : 2;
-            return NEED_MORE_LINES;
+            if (need_more_lines) {
+                lastResult = NEED_MORE_LINES;
+            }
+            return;
         }catch(...){
             // do nothing
         }
+
+        lastResult = EXEC_STARTED;
         vm->execAsync(line, "<stdin>", SINGLE_MODE);
-        return EXEC_DONE;
     }
 };
 
@@ -7106,12 +7108,14 @@ extern "C" {
 
     __EXPORT
     /// 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 pkpy_repl_input(REPL* r, const char* line){
-        return r->input(line);
+    void pkpy_repl_input(REPL* r, const char* line){
+        r->input(line);
+    }
+
+    __EXPORT
+    /// Check if the REPL needs more lines.
+    int pkpy_repl_last_input_result(REPL* r){
+        return (int)(r->last_input_result());
     }
 
     __EXPORT

+ 1 - 1
plugins/godot/godot-cpp

@@ -1 +1 @@
-Subproject commit 9766382dcb2bdeace0d209b9bf2813cf62c88fb7
+Subproject commit ac067d7f1f84cb757db20ffb3c929d880053c6c1

+ 5 - 3
src/main.cpp

@@ -56,13 +56,15 @@ int main(int argc, char** argv){
         ThreadedVM* vm = pkpy_new_tvm(true);
 #endif
         REPL repl(vm);
+        int result = -1;
         while(true){
-            (*vm->_stdout) << (repl.is_need_more_lines() ? "... " : ">>> ");
+            (*vm->_stdout) << (result==0 ? "... " : ">>> ");
             std::string line;
             std::getline(std::cin, line);
-            int result = pkpy_repl_input(&repl, line.c_str());
+            pkpy_repl_input(&repl, line.c_str());
+            result = pkpy_repl_last_input_result(&repl);
 #ifdef PK_DEBUG_THREADED
-            if(result == (int)EXEC_DONE){
+            if(result == (int)EXEC_STARTED){
                 _tvm_dispatch(vm);
                 pkpy_tvm_reset_state(vm);
             }

+ 8 - 6
src/pocketpy.h

@@ -900,12 +900,14 @@ extern "C" {
 
     __EXPORT
     /// 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 pkpy_repl_input(REPL* r, const char* line){
-        return r->input(line);
+    void pkpy_repl_input(REPL* r, const char* line){
+        r->input(line);
+    }
+
+    __EXPORT
+    /// Check if the REPL needs more lines.
+    int pkpy_repl_last_input_result(REPL* r){
+        return (int)(r->last_input_result());
     }
 
     __EXPORT

+ 18 - 16
src/repl.h

@@ -5,7 +5,7 @@
 
 enum InputResult {
     NEED_MORE_LINES = 0,
-    EXEC_DONE = 1,
+    EXEC_STARTED = 1,
     EXEC_SKIPPED = 2,
 };
 
@@ -14,12 +14,7 @@ protected:
     int need_more_lines = 0;
     std::string buffer;
     VM* vm;
-    bool exited = false;
-
-    void _exit(){
-        exited = true;
-        exit(0);
-    }
+    InputResult lastResult = EXEC_SKIPPED;
 public:
     REPL(VM* vm) : vm(vm){
         (*vm->_stdout) << ("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ")\n");
@@ -27,12 +22,11 @@ public:
         (*vm->_stdout) << ("Type \"exit()\" to exit." "\n");
     }
 
-    bool is_need_more_lines() const {
-        return need_more_lines;
+    InputResult last_input_result() const {
+        return lastResult;
     }
 
-    InputResult input(std::string line){
-        if(exited) return EXEC_SKIPPED;
+    void input(std::string line){
         if(need_more_lines){
             buffer += line;
             buffer += '\n';
@@ -46,11 +40,15 @@ public:
                 buffer.clear();
             }else{
 __NOT_ENOUGH_LINES:
-                return NEED_MORE_LINES;
+                lastResult = NEED_MORE_LINES;
+                return;
             }
         }else{
-            if(line == "exit()") _exit();
-            if(line.empty()) return EXEC_SKIPPED;
+            if(line == "exit()") exit(0);
+            if(line.empty()) {
+                lastResult = EXEC_SKIPPED;
+                return;
+            }
         }
 
         try{
@@ -60,11 +58,15 @@ __NOT_ENOUGH_LINES:
             buffer += line;
             buffer += '\n';
             need_more_lines = ne.isClassDef ? 3 : 2;
-            return NEED_MORE_LINES;
+            if (need_more_lines) {
+                lastResult = NEED_MORE_LINES;
+            }
+            return;
         }catch(...){
             // do nothing
         }
+
+        lastResult = EXEC_STARTED;
         vm->execAsync(line, "<stdin>", SINGLE_MODE);
-        return EXEC_DONE;
     }
 };