Răsfoiți Sursa

Merge branch 'main' of https://github.com/blueloveTH/pocketpy into c_binding_api

Kolten Pearson 2 ani în urmă
părinte
comite
75de7b0b63
6 a modificat fișierele cu 77 adăugiri și 39 ștergeri
  1. 13 0
      docs/modules/base64.md
  2. 4 0
      docs/modules/math.md
  3. 26 0
      docs/modules/requests.md
  4. 10 38
      src/frame.h
  5. 22 1
      src/pocketpy.h
  6. 2 0
      tests/70_base64.py

+ 13 - 0
docs/modules/base64.md

@@ -0,0 +1,13 @@
+---
+icon: package
+label: base64
+---
+
+### `base64.b64encode(b: bytes) -> bytes`
+
+Encode bytes-like object `b` using the standard Base64 alphabet.
+
+### `base64.b64decode(b: bytes) -> bytes`
+
+Decode Base64 encoded bytes-like object `b`.
+

+ 4 - 0
docs/modules/math.md

@@ -58,3 +58,7 @@ Return the smallest integer value greater than or equal to `x`.
 ### `math.sqrt(x)`
 
 Return the square root of `x`.
+
+### `math.gcd(a, b)`
+
+Return the greatest common divisor of `a` and `b`.

+ 26 - 0
docs/modules/requests.md

@@ -0,0 +1,26 @@
+---
+icon: package
+label: requests
+---
+
+!!!
+This module is experimental. To enable it, download `httplib.h` from [here](https://github.com/yhirose/cpp-httplib) and place it in the same directory as `pocketpy.h`.
+
+SSL is not supported.
+!!!
+
+### `requests.get(url, headers=None) -> Response`
+
+Send a GET request to `url` and return a `Response` object.
+
+### `requests.post(url, data=None, headers=None) -> Response`
+
+Send a POST request to `url` and return a `Response` object.
+
+### `requests.put(url, data=None, headers=None) -> Response`
+
+Send a PUT request to `url` and return a `Response` object.
+
+### `requests.delete(url, headers=None) -> Response`
+
+Send a DELETE request to `url` and return a `Response` object.

+ 10 - 38
src/frame.h

@@ -58,13 +58,13 @@ struct FastLocals{
     }
 };
 
-struct ValueStack {
-    static const size_t MAX_SIZE = 32768;
-    // We allocate 512 more bytes to keep `_sp` valid when `is_overflow() == true`.
-    PyObject* _begin[MAX_SIZE + 512];
+template<size_t MAX_SIZE>
+struct ValueStackImpl {
+    // We allocate extra MAX_SIZE/128 places to keep `_sp` valid when `is_overflow() == true`.
+    PyObject* _begin[MAX_SIZE + MAX_SIZE/128];
     PyObject** _sp;
 
-    ValueStack(): _sp(_begin) {}
+    ValueStackImpl(): _sp(_begin) {}
 
     PyObject*& top(){ return _sp[-1]; }
     PyObject* top() const { return _sp[-1]; }
@@ -92,41 +92,13 @@ struct ValueStack {
     void clear() { _sp = _begin; }
     bool is_overflow() const { return _sp >= _begin + MAX_SIZE; }
     
-    ValueStack(const ValueStack&) = delete;
-    ValueStack(ValueStack&&) = delete;
-    ValueStack& operator=(const ValueStack&) = delete;
-    ValueStack& operator=(ValueStack&&) = delete;
+    ValueStackImpl(const ValueStackImpl&) = delete;
+    ValueStackImpl(ValueStackImpl&&) = delete;
+    ValueStackImpl& operator=(const ValueStackImpl&) = delete;
+    ValueStackImpl& operator=(ValueStackImpl&&) = delete;
 };
 
-//stack for working with c bindings
-struct CVirtualStack {
-    static const size_t MAX_SIZE = 256;
-    PyObject* _begin[MAX_SIZE];
-    PyObject** _sp;
-    size_t offset;
-
-    CVirtualStack(): _sp(_begin), offset(0) {}
-
-    PyObject* top() const { return _sp[-1]; }
-    PyObject* get(int index) const { return _begin[offset + index]; }
-    void push(PyObject* v){ *_sp++ = v; }
-    void pop(){ --_sp; }
-    void shrink(int n){ _sp -= n; }
-    int size() const { return (_sp - _begin) - offset; }
-    bool empty() const { return size() == 0; }
-    PyObject** begin() { return _begin + offset; }
-    PyObject** end() { return _sp; }
-    void clear() { _sp = _begin + offset;}
-    int remaining() { return MAX_SIZE - (_sp - _begin); }
-    
-    size_t store() { size_t ret = offset; offset = _sp - _begin; return ret; }
-    void restore(size_t stored) { offset = stored; }
-
-    CVirtualStack(const CVirtualStack&) = delete;
-    CVirtualStack(CVirtualStack&&) = delete;
-    CVirtualStack& operator=(const CVirtualStack&) = delete;
-    CVirtualStack& operator=(CVirtualStack&&) = delete;
-};
+using ValueStack = ValueStackImpl<32768>;
 
 struct Frame {
     int _ip = -1;

+ 22 - 1
src/pocketpy.h

@@ -595,7 +595,16 @@ inline void init_builtins(VM* _vm) {
     _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(VAR("Ellipsis")));
 
     /************ bytes ************/
-    _vm->bind_static_method<1>("bytes", "__new__", CPP_NOT_IMPLEMENTED());
+    _vm->bind_static_method<1>("bytes", "__new__", [](VM* vm, ArgsView args){
+        List& list = CAST(List&, args[0]);
+        std::vector<char> buffer(list.size());
+        for(int i=0; i<list.size(); i++){
+            i64 b = CAST(i64, list[i]);
+            if(b<0 || b>255) vm->ValueError("byte must be in range[0, 256)");
+            buffer[i] = (char)b;
+        }
+        return VAR(Bytes(std::move(buffer)));
+    });
 
     _vm->bind_method<1>("bytes", "__getitem__", [](VM* vm, ArgsView args) {
         const Bytes& self = CAST(Bytes&, args[0]);
@@ -766,6 +775,18 @@ inline void add_module_math(VM* vm){
     vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(vm->num_to_float(args[0])))));
     vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(vm->num_to_float(args[0])))));
     vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(vm->num_to_float(args[0])))));
+    vm->bind_func<2>(mod, "gcd", [](VM* vm, ArgsView args) {
+        i64 a = CAST(i64, args[0]);
+        i64 b = CAST(i64, args[1]);
+        if(a < 0) a = -a;
+        if(b < 0) b = -b;
+        while(b != 0){
+            i64 t = b;
+            b = a % b;
+            a = t;
+        }
+        return VAR(a);
+    });
 }
 
 inline void add_module_dis(VM* vm){

Fișier diff suprimat deoarece este prea mare
+ 2 - 0
tests/70_base64.py


Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff