Compare commits

...

12 Commits

Author SHA1 Message Date
blueloveTH
59197d1c0c Update pickle.md 2025-01-19 23:59:38 +08:00
blueloveTH
6d6b051903 ... 2025-01-19 23:57:02 +08:00
blueloveTH
43cc676b20 Update CMakeLists.txt 2025-01-19 23:51:25 +08:00
blueloveTH
00bbd87372 add libhv to doc 2025-01-19 23:45:57 +08:00
blueloveTH
e8e5fa897c ... 2025-01-18 22:40:01 +08:00
blueloveTH
88ee39cd32 ... 2025-01-18 22:35:50 +08:00
blueloveTH
26871253a1 Update CMakeLists.txt 2025-01-18 22:32:25 +08:00
blueloveTH
9fc67c0666 Update main.yml 2025-01-18 22:30:21 +08:00
blueloveTH
570d21854e ... 2025-01-18 22:28:06 +08:00
blueloveTH
9e08c298bf some update 2025-01-18 22:21:58 +08:00
blueloveTH
92d299cacc Update main.yml 2025-01-18 22:16:50 +08:00
blueloveTH
3d12c9400c add libhv module 2025-01-18 22:12:44 +08:00
29 changed files with 752 additions and 23 deletions

View File

@ -16,6 +16,8 @@ jobs:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: ilammy/msvc-dev-cmd@v1
- name: Compile
shell: powershell
@ -27,6 +29,8 @@ jobs:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: ilammy/msvc-dev-cmd@v1
- name: Compile
shell: bash
@ -47,6 +51,8 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Setup Clang
uses: egor-tensin/setup-clang@v1
with:
@ -81,6 +87,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Setup Alpine Linux for aarch64
uses: jirutka/setup-alpine@v1
with:
@ -97,6 +105,8 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Compile and Test
run: |
python cmake_build.py
@ -109,6 +119,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: nttld/setup-ndk@v1
id: setup-ndk
with:
@ -138,6 +150,8 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Compile Frameworks
run: |
git clone https://github.com/leetal/ios-cmake --depth 1 ~/ios-cmake

2
.gitignore vendored
View File

@ -35,3 +35,5 @@ docs/references.md
tests/00_tmp.py
docs/C-API/functions.md
*.log

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "3rd/libhv/libhv"]
path = 3rd/libhv/libhv
url = https://github.com/ithewei/libhv.git

39
3rd/libhv/CMakeLists.txt Normal file
View File

@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.10)
project(libhv_bindings)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(BUILD_SHARED "build shared library" OFF)
option(BUILD_STATIC "build static library" ON)
option(BUILD_EXAMPLES "build examples" OFF)
option(WITH_OPENSSL "with openssl library" OFF)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libhv)
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_LIST_DIR}/src LIBHV_BINDINGS_SRC)
add_library(${PROJECT_NAME} STATIC ${LIBHV_BINDINGS_SRC})
target_link_libraries(${PROJECT_NAME} hv_static)
# define WITHOUT_HTTP_CONTENT
target_compile_definitions(libhv_bindings PRIVATE WITHOUT_HTTP_CONTENT)
target_compile_definitions(hv_static PRIVATE WITHOUT_HTTP_CONTENT)
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../../include
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/libhv
${CMAKE_CURRENT_LIST_DIR}/libhv/base
${CMAKE_CURRENT_LIST_DIR}/libhv/evpp
${CMAKE_CURRENT_LIST_DIR}/libhv/event
${CMAKE_CURRENT_LIST_DIR}/libhv/http
${CMAKE_CURRENT_LIST_DIR}/libhv/ssl
${CMAKE_CURRENT_LIST_DIR}/libhv/cpputil
)

View File

@ -0,0 +1,15 @@
-xc++
-std=c++14
-I../../include
-Iinclude/
-Ilibhv/
-Ilibhv/base/
-Ilibhv/evpp/
-Ilibhv/event/
-Ilibhv/http/
-Ilibhv/ssl/
-Ilibhv/cpputil/
-DWITHOUT_HTTP_CONTENT

View File

@ -0,0 +1,10 @@
#pragma once
#include "pocketpy.h"
extern "C" void pk__add_module_libhv();
py_Type libhv_register_HttpClient(py_GlobalRef mod);
py_Type libhv_register_HttpServer(py_GlobalRef mod);
py_Type libhv_register_WebSocketClient(py_GlobalRef mod);
py_Type libhv_register_WebSocketServer(py_GlobalRef mod);

1
3rd/libhv/libhv Submodule

@ -0,0 +1 @@
Subproject commit fded2ba287199d3c20c619e06ddc571e2972ed92

View File

@ -0,0 +1,306 @@
#include "libhv_bindings.hpp"
#include "base/herr.h"
#include "http/client/HttpClient.h"
struct libhv_HttpResponse {
HttpResponsePtr ptr;
bool ok;
bool is_valid() { return ok && ptr != NULL; }
libhv_HttpResponse() : ptr(NULL), ok(false) {}
};
static bool libhv_HttpResponse_status_code(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) return RuntimeError("HttpResponse: no response");
py_newint(py_retval(), resp->ptr->status_code);
return true;
};
static bool libhv_HttpResponse_headers(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) return RuntimeError("HttpResponse: no response");
py_Ref headers = py_getslot(argv, 0);
if(py_isnil(headers)) {
py_newdict(headers);
py_Ref _0 = py_pushtmp();
py_Ref _1 = py_pushtmp();
for(auto& kv: resp->ptr->headers) {
py_newstr(_0, kv.first.c_str());
py_newstr(_1, kv.second.c_str());
py_dict_setitem(headers, _0, _1);
}
py_shrink(2);
}
py_assign(py_retval(), headers);
return true;
};
static bool libhv_HttpResponse_text(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) return RuntimeError("HttpResponse: no response");
py_Ref text = py_getslot(argv, 1);
if(py_isnil(text)) {
c11_sv sv;
sv.data = resp->ptr->body.c_str();
sv.size = resp->ptr->body.size();
py_newstrv(text, sv);
}
py_assign(py_retval(), text);
return true;
};
static bool libhv_HttpResponse_content(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) return RuntimeError("HttpResponse: no response");
py_Ref content = py_getslot(argv, 2);
if(py_isnil(content)) {
int size = resp->ptr->body.size();
unsigned char* buf = py_newbytes(content, size);
memcpy(buf, resp->ptr->body.data(), size);
}
py_assign(py_retval(), content);
return true;
};
static bool libhv_HttpResponse_json(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) return RuntimeError("HttpResponse: no response");
const char* source = resp->ptr->body.c_str(); // json string is null-terminated
return py_json_loads(source);
};
static bool libhv_HttpResponse_completed(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
py_newbool(py_retval(), resp->is_valid());
return true;
}
static bool libhv_HttpResponse__new__(int argc, py_Ref argv) {
return py_exception(tp_NotImplementedError, "");
}
static bool libhv_HttpResponse__iter__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
py_assign(py_retval(), argv);
return true;
}
static bool libhv_HttpResponse__next__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) {
py_newnone(py_retval());
return true;
} else {
if(!py_tpcall(tp_StopIteration, 1, argv)) return false;
return py_raise(py_retval());
}
}
static bool libhv_HttpResponse__repr__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpResponse* resp = (libhv_HttpResponse*)py_touserdata(argv);
if(!resp->is_valid()) {
py_newstr(py_retval(), "<HttpResponse: no response>");
} else {
py_newfstr(py_retval(), "<HttpResponse: %d>", (int)resp->ptr->status_code);
}
return true;
}
static py_Type libhv_register_HttpResponse(py_GlobalRef mod) {
py_Type type = py_newtype("HttpResponse", tp_object, mod, [](void* ud) {
((libhv_HttpResponse*)ud)->~libhv_HttpResponse();
});
py_bindproperty(type, "status_code", libhv_HttpResponse_status_code, NULL);
py_bindproperty(type, "headers", libhv_HttpResponse_headers, NULL);
py_bindproperty(type, "text", libhv_HttpResponse_text, NULL);
py_bindproperty(type, "content", libhv_HttpResponse_content, NULL);
py_bindmethod(type, "json", libhv_HttpResponse_json);
py_bindmagic(type, __new__, libhv_HttpResponse__new__);
py_bindmagic(type, __iter__, libhv_HttpResponse__iter__);
py_bindmagic(type, __next__, libhv_HttpResponse__next__);
py_bindmagic(type, __repr__, libhv_HttpResponse__repr__);
// completed
py_bindproperty(type, "completed", libhv_HttpResponse_completed, NULL);
return type;
}
static bool libhv_HttpClient__send_request(py_Ref arg_self,
enum http_method method,
py_Ref arg_url,
py_Ref arg_params,
py_Ref arg_headers,
py_Ref arg_data,
py_Ref arg_json,
py_Ref arg_timeout) {
hv::HttpClient* cli = (hv::HttpClient*)py_touserdata(arg_self);
if(!py_checkstr(arg_url)) return false;
const char* url = py_tostr(arg_url);
if(!py_checkint(arg_timeout)) return false;
int timeout = py_toint(arg_timeout);
auto req = std::make_shared<HttpRequest>();
req->method = method;
req->url = url;
if(!py_isnone(arg_params)) {
if(!py_checktype(arg_params, tp_dict)) return false;
bool ok = py_dict_apply(
arg_params,
[](py_Ref key, py_Ref value, void* ctx) {
HttpRequest* p_req = (HttpRequest*)ctx;
if(!py_checkstr(key)) return false;
if(!py_str(value)) return false; // key: str(value)
p_req->SetParam(py_tostr(key), py_tostr(py_retval()));
return true;
},
req.get());
if(!ok) return false;
}
req->headers["Connection"] = "keep-alive";
if(!py_isnone(arg_headers)) {
if(!py_checktype(arg_headers, tp_dict)) return false;
bool ok = py_dict_apply(
arg_headers,
[](py_Ref key, py_Ref value, void* ctx) {
HttpRequest* p_req = (HttpRequest*)ctx;
if(!py_checkstr(key)) return false;
if(!py_str(value)) return false; // key: str(value)
p_req->headers[py_tostr(key)] = py_tostr(py_retval());
return true;
},
req.get());
if(!ok) return false;
}
if(!py_isnone(arg_data)) {
// data must be str or bytes
if(py_istype(arg_data, tp_str)) {
req->body = py_tostr(arg_data);
} else if(py_istype(arg_data, tp_bytes)) {
int size;
unsigned char* buf = py_tobytes(arg_data, &size);
req->body.assign((const char*)buf, size);
} else {
return TypeError("HttpClient: data must be str or bytes");
}
}
if(!py_isnone(arg_json)) {
if(!py_isnone(arg_data)) {
return ValueError("HttpClient: data and json cannot be set at the same time");
}
if(!py_json_dumps(arg_json)) return false;
req->body = py_tostr(py_retval());
req->headers["Content-Type"] = "application/json";
}
req->timeout = timeout;
libhv_HttpResponse* retval =
(libhv_HttpResponse*)py_newobject(py_retval(),
py_gettype("libhv", py_name("HttpResponse")),
3, // headers, text, content
sizeof(libhv_HttpResponse));
// placement new
new (retval) libhv_HttpResponse();
int code = cli->sendAsync(req, [retval](const HttpResponsePtr& resp) {
if(resp == NULL) {
retval->ok = false;
retval->ptr = NULL;
} else {
retval->ok = true;
retval->ptr = resp;
}
});
if(code != 0) {
const char* msg = hv_strerror(code);
return RuntimeError("HttpClient: %s (%d)", msg, code);
}
return true;
}
py_Type libhv_register_HttpClient(py_GlobalRef mod) {
py_Type type = py_newtype("HttpClient", tp_object, mod, [](void* ud) {
((hv::HttpClient*)ud)->~HttpClient();
});
py_GlobalRef type_object = py_tpobject(type);
libhv_register_HttpResponse(mod);
py_bindmagic(type, __new__, [](int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
hv::HttpClient* ud =
(hv::HttpClient*)py_newobject(py_retval(), py_totype(argv), 0, sizeof(hv::HttpClient));
new (ud) hv::HttpClient();
return true;
});
py_bind(type_object,
"get(self, url: str, params=None, headers=None, timeout=10)",
[](int argc, py_Ref argv) {
return libhv_HttpClient__send_request(py_arg(0), // self
HTTP_GET, // method
py_arg(1), // url
py_arg(2), // params
py_arg(3), // headers
py_None(), // data
py_None(), // json
py_arg(4)); // timeout
});
py_bind(type_object,
"post(self, url: str, params=None, headers=None, data=None, json=None, timeout=10)",
[](int argc, py_Ref argv) {
return libhv_HttpClient__send_request(py_arg(0), // self
HTTP_POST, // method
py_arg(1), // url
py_arg(2), // params
py_arg(3), // headers
py_arg(4), // data
py_arg(5), // json
py_arg(6)); // timeout
});
py_bind(type_object,
"put(self, url: str, params=None, headers=None, data=None, json=None, timeout=10)",
[](int argc, py_Ref argv) {
return libhv_HttpClient__send_request(py_arg(0), // self
HTTP_PUT, // method
py_arg(1), // url
py_arg(2), // params
py_arg(3), // headers
py_arg(4), // data
py_arg(5), // json
py_arg(6)); // timeout
});
py_bind(type_object,
"delete(self, url: str, params=None, headers=None, timeout=10)",
[](int argc, py_Ref argv) {
return libhv_HttpClient__send_request(py_arg(0), // self
HTTP_DELETE, // method
py_arg(1), // url
py_arg(2), // params
py_arg(3), // headers
py_None(), // data
py_None(), // json
py_arg(4)); // timeout
});
return type;
}

View File

@ -0,0 +1,224 @@
#include "libhv_bindings.hpp"
#include "http/server/HttpServer.h"
#include "base/herr.h"
#include <deque>
#include <atomic>
template <typename T_in, typename T_out>
struct libhv_MQ {
std::atomic<bool> lock_in;
std::atomic<bool> lock_out;
std::deque<T_in> queue_in;
std::deque<T_out> queue_out;
void begin_in() {
while(lock_in.exchange(true)) {
hv_delay(1);
}
}
void end_in() { lock_in.store(false); }
void begin_out() {
while(lock_out.exchange(true)) {
hv_delay(1);
}
}
void end_out() { lock_out.store(false); }
};
struct libhv_HttpServer {
hv::HttpService service;
hv::HttpServer server;
libhv_MQ<HttpContextPtr, std::pair<HttpContextPtr, int>> mq;
};
static bool libhv_HttpServer__new__(int argc, py_Ref argv) {
libhv_HttpServer* self =
(libhv_HttpServer*)py_newobject(py_retval(), py_totype(argv), 0, sizeof(libhv_HttpServer));
new (self) libhv_HttpServer();
return true;
}
static bool libhv_HttpServer__init__(int argc, py_Ref argv) {
PY_CHECK_ARGC(3);
libhv_HttpServer* self = (libhv_HttpServer*)py_touserdata(py_arg(0));
PY_CHECK_ARG_TYPE(1, tp_str);
PY_CHECK_ARG_TYPE(2, tp_int);
const char* host = py_tostr(py_arg(1));
int port = py_toint(py_arg(2));
self->service.AllowCORS();
http_ctx_handler internal_handler = [self](const HttpContextPtr& ctx) {
self->mq.begin_in();
self->mq.queue_in.push_back(ctx);
self->mq.end_in();
while(true) {
self->mq.begin_out();
if(!self->mq.queue_out.empty()) {
auto& msg = self->mq.queue_out.front();
if(msg.first == ctx) {
self->mq.queue_out.pop_front();
self->mq.end_out();
return msg.second;
}
}
self->mq.end_out();
hv_delay(1);
}
};
self->service.Any("*", internal_handler);
self->server.registerHttpService(&self->service);
self->server.setHost(host);
self->server.setPort(port);
py_newnone(py_retval());
return true;
}
static bool libhv_HttpServer_dispatch(int argc, py_Ref argv) {
PY_CHECK_ARGC(2);
libhv_HttpServer* self = (libhv_HttpServer*)py_touserdata(py_arg(0));
py_Ref callable = py_arg(1);
if(!py_callable(callable)) return TypeError("dispatcher must be callable");
self->mq.begin_in();
if(self->mq.queue_in.empty()) {
self->mq.end_in();
py_newbool(py_retval(), false);
return true;
} else {
HttpContextPtr ctx = self->mq.queue_in.front();
self->mq.queue_in.pop_front();
self->mq.end_in();
const char* method = ctx->request->Method();
std::string path = ctx->request->Path();
const http_headers& headers = ctx->request->headers;
const std::string& data = ctx->request->body;
py_OutRef msg = py_pushtmp();
py_newdict(msg);
py_Ref _0 = py_pushtmp();
py_Ref _1 = py_pushtmp();
py_Ref _2 = py_pushtmp();
py_Ref _3 = py_pushtmp();
// method
py_newstr(_0, "method");
py_newstr(_1, method);
py_dict_setitem(msg, _0, _1);
// path
py_newstr(_0, "path");
py_newstr(_1, path.c_str());
py_dict_setitem(msg, _0, _1);
// headers
py_newstr(_0, "headers");
py_newdict(_1);
py_dict_setitem(msg, _0, _1);
for(auto& header: headers) {
py_newstr(_2, header.first.c_str());
py_newstr(_3, header.second.c_str());
py_dict_setitem(_1, _2, _3);
}
// data
py_newstr(_0, "data");
auto content_type = ctx->request->ContentType();
bool is_text_data = content_type == TEXT_PLAIN || content_type == APPLICATION_JSON ||
content_type == APPLICATION_XML || content_type == TEXT_HTML ||
content_type == CONTENT_TYPE_NONE;
if(is_text_data) {
py_newstrv(_1, {data.c_str(), (int)data.size()});
} else {
unsigned char* buf = py_newbytes(_1, data.size());
memcpy(buf, data.data(), data.size());
}
py_dict_setitem(msg, _0, _1);
py_assign(py_retval(), msg);
py_shrink(5);
// call dispatcher
if(!py_call(callable, 1, py_retval())) { return false; }
py_Ref object;
int status_code = 200;
if(py_istuple(py_retval())) {
// "Hello, world!", 200
if(py_tuple_len(py_retval()) != 2) {
return ValueError("dispatcher should return `object | tuple[object, int]`");
}
object = py_tuple_getitem(py_retval(), 0);
py_ItemRef status_code_object = py_tuple_getitem(py_retval(), 1);
if(!py_checkint(status_code_object)) return false;
status_code = py_toint(status_code_object);
} else {
// "Hello, world!"
object = py_retval();
}
switch(py_typeof(object)) {
case tp_bytes: {
int size;
unsigned char* buf = py_tobytes(object, &size);
ctx->response->Data(buf, size, false);
break;
}
case tp_str: {
c11_sv sv = py_tosv(object);
ctx->response->String(std::string(sv.data, sv.size));
break;
}
case tp_NoneType: {
break;
}
default: {
if(!py_json_dumps(object)) return false;
c11_sv sv = py_tosv(py_retval());
ctx->response->String(std::string(sv.data, sv.size));
ctx->response->SetContentType(APPLICATION_JSON);
break;
}
}
self->mq.begin_out();
self->mq.queue_out.push_back({ctx, status_code});
self->mq.end_out();
}
py_newbool(py_retval(), true);
return true;
}
static bool libhv_HttpServer_start(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpServer* self = (libhv_HttpServer*)py_touserdata(py_arg(0));
int code = self->server.start();
if(code != 0) {
return RuntimeError("HttpServer start failed: %s (%d)", hv_strerror(code), code);
}
py_newnone(py_retval());
return true;
}
static bool libhv_HttpServer_stop(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
libhv_HttpServer* self = (libhv_HttpServer*)py_touserdata(py_arg(0));
self->server.stop();
py_newnone(py_retval());
return true;
}
py_Type libhv_register_HttpServer(py_GlobalRef mod) {
py_Type type = py_newtype("HttpServer", tp_object, mod, [](void* ud) {
libhv_HttpServer* self = (libhv_HttpServer*)ud;
self->~libhv_HttpServer();
});
py_bindmagic(type, __new__, libhv_HttpServer__new__);
py_bindmagic(type, __init__, libhv_HttpServer__init__);
py_bindmethod(type, "dispatch", libhv_HttpServer_dispatch);
py_bindmethod(type, "start", libhv_HttpServer_start);
py_bindmethod(type, "stop", libhv_HttpServer_stop);
return type;
}

View File

@ -0,0 +1,7 @@
#include "libhv_bindings.hpp"
#include "http/client/WebSocketClient.h"
py_Type libhv_register_WebSocketClient(py_GlobalRef mod) {
py_Type type = py_newtype("WebSocketClient", tp_object, mod, NULL);
return type;
}

View File

@ -0,0 +1,7 @@
#include "libhv_bindings.hpp"
#include "http/server/WebSocketServer.h"
py_Type libhv_register_WebSocketServer(py_GlobalRef mod) {
py_Type type = py_newtype("WebSocketServer", tp_object, mod, NULL);
return type;
}

View File

@ -0,0 +1,10 @@
#include "libhv_bindings.hpp"
extern "C" void pk__add_module_libhv() {
py_GlobalRef mod = py_newmodule("libhv");
libhv_register_HttpClient(mod);
libhv_register_HttpServer(mod);
libhv_register_WebSocketClient(mod);
libhv_register_WebSocketServer(mod);
}

View File

@ -48,11 +48,18 @@ if(PK_ENABLE_OS)
add_definitions(-DPK_ENABLE_OS=1)
endif()
option(PK_BUILD_MODULE_LZ4 "" ON)
option(PK_BUILD_MODULE_LZ4 "" OFF)
if(PK_BUILD_MODULE_LZ4)
add_subdirectory(3rd/lz4)
include_directories(3rd/lz4)
add_definitions(-DPK_BUILD_MODULE_LZ4=1)
add_definitions(-DPK_BUILD_MODULE_LZ4)
endif()
option(PK_BUILD_MODULE_LIBHV "" OFF)
if(PK_BUILD_MODULE_LIBHV)
add_subdirectory(3rd/libhv)
include_directories(3rd/libhv/include)
add_definitions(-DPK_BUILD_MODULE_LIBHV)
endif()
# PK_IS_MAIN determines whether the project is being used from root
@ -97,3 +104,7 @@ endif()
if(PK_BUILD_MODULE_LZ4)
target_link_libraries(${PROJECT_NAME} lz4)
endif()
if(PK_BUILD_MODULE_LIBHV)
target_link_libraries(${PROJECT_NAME} libhv_bindings)
endif()

View File

@ -14,6 +14,8 @@ cmake \
-DANDROID_PLATFORM=android-22 \
../../.. \
-DPK_BUILD_SHARED_LIB=ON \
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_BUILD_TYPE=Release \
-DPK_BUILD_MODULE_LZ4=ON \
-DPK_BUILD_MODULE_LIBHV=ON
cmake --build . --config Release

View File

@ -4,7 +4,7 @@ python prebuild.py
SRC=$(find src/ -name "*.c")
FLAGS="-std=c11 -lm -ldl -I3rd/lz4 -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1 -DPK_BUILD_MODULE_LZ4=1"
FLAGS="-std=c11 -lm -ldl -I3rd/lz4 -Iinclude -O0 -Wfatal-errors -g -DDEBUG -DPK_ENABLE_OS=1 -DPK_BUILD_MODULE_LZ4"
SANITIZE_FLAGS="-fsanitize=address,leak,undefined"

View File

@ -6,7 +6,12 @@ rm -rf build
mkdir build
cd build
FLAGS="-DCMAKE_TOOLCHAIN_FILE=3rd/ios.toolchain.cmake -DPK_BUILD_STATIC_LIB=ON -DDEPLOYMENT_TARGET=13.0 -DPK_BUILD_WITH_IPO=OFF"
FLAGS="-DCMAKE_TOOLCHAIN_FILE=3rd/ios.toolchain.cmake \
-DDEPLOYMENT_TARGET=13.0 \
-DPK_BUILD_STATIC_LIB=ON \
-DPK_BUILD_WITH_IPO=OFF \
-DPK_BUILD_MODULE_LZ4=ON \
-DPK_BUILD_MODULE_LIBHV=ON"
cmake -B os64 -G Xcode $FLAGS -DPLATFORM=OS64 ..
cmake --build os64 --config Release

View File

@ -20,7 +20,7 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build")
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DPK_BUILD_MODULE_LZ4=ON -DPK_BUILD_MODULE_LIBHV=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
assert code == 0
code = os.system(f"cmake --build . --config {config}")
assert code == 0

View File

@ -4,3 +4,4 @@
-std=c11
-Iinclude/
-I3rd/lz4/
-I3rd/libhv/include/

18
docs/modules/libhv.md Normal file
View File

@ -0,0 +1,18 @@
---
icon: package
label: libhv
---
!!!
This module is optional. Set option `PK_BUILD_MODULE_LIBHV` to `ON` in your `CMakeLists.txt` to enable it.
!!!
Simple bindings for [libhv](https://github.com/ithewei/libhv), which provides cross platform implementation of the following:
+ HTTP server
+ HTTP client
+ WebSocket server (TODO)
+ WebSocket client (TODO)
#### Source code
:::code source="../../include/typings/libhv.pyi" :::

View File

@ -23,11 +23,10 @@ The following types can be pickled:
- [ ] functions (built-in and user-defined) accessible from the top level of a module (using def, not lambda);
- [x] classes accessible from the top level of a module;
- [x] instances of such classes
- [x] `PY_STRUCT_LIKE` objects
The following magic methods are available:
- [x] `__getnewargs__`
- [ ] `__getnewargs__`
- [ ] `__getstate__`
- [ ] `__setstate__`
- [ ] `__reduce__`
- [x] `__reduce__`

View File

@ -19,4 +19,4 @@ May be one of:
### `sys.argv`
The command line arguments. Set by `vm->set_main_argv`.
The command line arguments. Set by `py_sys_setargv`.

View File

@ -48,9 +48,3 @@ typedef struct RefCounted {
} \
} while(0)
// static assert
#ifndef __cplusplus
#ifndef static_assert
#define static_assert(x, msg) if(!(x)) c11__abort("static_assert failed: %s", msg)
#endif
#endif

View File

@ -24,3 +24,9 @@ void pk__add_module_colorcvt();
void pk__add_module_conio();
void pk__add_module_lz4();
void pk__add_module_pkpy();
#ifdef PK_BUILD_MODULE_LIBHV
void pk__add_module_libhv();
#else
#define pk__add_module_libhv()
#endif

View File

@ -159,6 +159,8 @@ PK_API void py_newstr(py_OutRef, const char*);
PK_API char* py_newstrn(py_OutRef, int);
/// Create a `str` object from a `c11_sv`.
PK_API void py_newstrv(py_OutRef, c11_sv);
/// Create a formatted `str` object.
PK_API void py_newfstr(py_OutRef, const char*, ...);
/// Create a `bytes` object with `n` UNINITIALIZED bytes.
PK_API unsigned char* py_newbytes(py_OutRef, int n);
/// Create a `None` object.

38
include/typings/libhv.pyi Normal file
View File

@ -0,0 +1,38 @@
from typing import Literal, Generator, Callable
class Future[T]:
def completed(self) -> bool: ...
def __iter__(self) -> Generator[T, None, None]: ...
class HttpResponse(Future['HttpResponse']):
@property
def status_code(self) -> int: ...
@property
def headers(self) -> dict[str, str]: ...
@property
def text(self) -> str: ...
@property
def content(self) -> bytes: ...
def json(self): ...
class HttpClient:
def get(self, url: str, params=None, headers=None, timeout=10) -> HttpResponse: ...
def post(self, url: str, params=None, headers=None, data=None, json=None, timeout=10) -> HttpResponse: ...
def put(self, url: str, params=None, headers=None, data=None, json=None, timeout=10) -> HttpResponse: ...
def delete(self, url: str, params=None, headers=None, timeout=10) -> HttpResponse: ...
class HttpServer:
def __init__(self, host: str, port: int) -> None: ...
def dispatch(self, fn: Callable[[dict], object | tuple[object, int]]) -> bool: ...
def start(self) -> None: ...
def stop(self) -> None: ...
class WebSocketClient:
pass
class WebSocketServer:
pass

View File

@ -253,7 +253,7 @@ int py_replinput(char* buf, int max_size) {
printf(">>> ");
while(true) {
char c = getchar();
int c = getchar();
if(c == EOF) return -1;
if(c == '\n') {

View File

@ -221,7 +221,8 @@ void VM__ctor(VM* self) {
pk__add_module_importlib();
pk__add_module_conio();
pk__add_module_lz4();
pk__add_module_lz4(); // optional
pk__add_module_libhv(); // optional
pk__add_module_pkpy();
// add python builtins

View File

@ -123,13 +123,17 @@ static bool number__pow__(int argc, py_Ref argv) {
return true;
}
static py_i64 i64_abs(py_i64 x) {
return x < 0 ? -x : x;
}
static py_i64 cpy11__fast_floor_div(py_i64 a, py_i64 b) {
assert(b != 0);
if(a == 0) return 0;
if((a < 0) == (b < 0)) {
return labs(a) / labs(b);
return i64_abs(a) / i64_abs(b);
} else {
return -1 - (labs(a) - 1) / labs(b);
return -1 - (i64_abs(a) - 1) / i64_abs(b);
}
}
@ -138,9 +142,9 @@ static py_i64 cpy11__fast_mod(py_i64 a, py_i64 b) {
if(a == 0) return 0;
py_i64 res;
if((a < 0) == (b < 0)) {
res = labs(a) % labs(b);
res = i64_abs(a) % i64_abs(b);
} else {
res = labs(b) - 1 - (labs(a) - 1) % labs(b);
res = i64_abs(b) - 1 - (i64_abs(a) - 1) % i64_abs(b);
}
return b < 0 ? -res : res;
}

View File

@ -25,6 +25,16 @@ void py_newstrv(py_OutRef out, c11_sv sv) {
memcpy(data, sv.data, sv.size);
}
void py_newfstr(py_OutRef out, const char* fmt, ...) {
c11_sbuf buf;
c11_sbuf__ctor(&buf);
va_list args;
va_start(args, fmt);
pk_vsprintf(&buf, fmt, args);
va_end(args);
c11_sbuf__py_submit(&buf, out);
}
unsigned char* py_newbytes(py_Ref out, int size) {
ManagedHeap* heap = &pk_current_vm->heap;
// 4 bytes size + data