mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-11 12:40:18 +00:00
Compare commits
5 Commits
24a7e6f060
...
0109829ad4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0109829ad4 | ||
|
|
2d4cf81356 | ||
|
|
389adc64d2 | ||
|
|
972f86f781 | ||
|
|
000fd1f087 |
@ -7,14 +7,14 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
/* string */
|
||||
typedef struct c11_string{
|
||||
typedef struct c11_string {
|
||||
// int size | char[] | '\0'
|
||||
int size;
|
||||
const char data[]; // flexible array member
|
||||
char data[]; // flexible array member
|
||||
} c11_string;
|
||||
|
||||
/* bytes */
|
||||
typedef struct c11_bytes{
|
||||
typedef struct c11_bytes {
|
||||
int size;
|
||||
unsigned char data[]; // flexible array member
|
||||
} c11_bytes;
|
||||
@ -33,6 +33,7 @@ c11_string* c11_string__new2(const char* data, int size);
|
||||
c11_string* c11_string__new3(const char* fmt, ...);
|
||||
void c11_string__ctor(c11_string* self, const char* data);
|
||||
void c11_string__ctor2(c11_string* self, const char* data, int size);
|
||||
void c11_string__ctor3(c11_string* self, int size);
|
||||
c11_string* c11_string__copy(c11_string* self);
|
||||
void c11_string__delete(c11_string* self);
|
||||
c11_sv c11_string__sv(c11_string* self);
|
||||
@ -55,8 +56,8 @@ bool c11_sv__endswith(c11_sv self, c11_sv suffix);
|
||||
c11_string* c11_sv__replace(c11_sv self, char old, char new_);
|
||||
c11_string* c11_sv__replace2(c11_sv self, c11_sv old, c11_sv new_);
|
||||
|
||||
c11_vector/* T=c11_sv */ c11_sv__split(c11_sv self, char sep);
|
||||
c11_vector/* T=c11_sv */ c11_sv__split2(c11_sv self, c11_sv sep);
|
||||
c11_vector /* T=c11_sv */ c11_sv__split(c11_sv self, char sep);
|
||||
c11_vector /* T=c11_sv */ c11_sv__split2(c11_sv self, c11_sv sep);
|
||||
|
||||
// misc
|
||||
int c11__unicode_index_to_byte(const char* data, int i);
|
||||
@ -65,7 +66,7 @@ int c11__byte_index_to_unicode(const char* data, int n);
|
||||
bool c11__is_unicode_Lo_char(int c);
|
||||
int c11__u8_header(unsigned char c, bool suppress);
|
||||
|
||||
typedef enum IntParsingResult{
|
||||
typedef enum IntParsingResult {
|
||||
IntParsing_SUCCESS,
|
||||
IntParsing_FAILURE,
|
||||
IntParsing_OVERFLOW,
|
||||
|
||||
@ -153,8 +153,10 @@ PK_EXPORT void py_newfloat(py_OutRef, py_f64);
|
||||
PK_EXPORT void py_newbool(py_OutRef, bool);
|
||||
/// Create a `str` object from a null-terminated string (utf-8).
|
||||
PK_EXPORT void py_newstr(py_OutRef, const char*);
|
||||
/// Create a `str` object from a char array (utf-8).
|
||||
PK_EXPORT void py_newstrn(py_OutRef, const char*, int);
|
||||
/// Create a `str` object with `n` UNINITIALIZED bytes plus `'\0'`.
|
||||
PK_EXPORT char* py_newstrn(py_OutRef, int);
|
||||
/// Create a `str` object from a `c11_sv`.
|
||||
PK_EXPORT void py_newstrv(py_OutRef, c11_sv);
|
||||
/// Create a `bytes` object with `n` UNINITIALIZED bytes.
|
||||
PK_EXPORT unsigned char* py_newbytes(py_OutRef, int n);
|
||||
/// Create a `None` object.
|
||||
@ -504,6 +506,7 @@ PK_EXPORT void py_clearexc(py_StackRef p0);
|
||||
#define NameError(n) py_exception(tp_NameError, "name '%n' is not defined", (n))
|
||||
#define TypeError(...) py_exception(tp_TypeError, __VA_ARGS__)
|
||||
#define RuntimeError(...) py_exception(tp_RuntimeError, __VA_ARGS__)
|
||||
#define IOError(...) py_exception(tp_IOError, __VA_ARGS__)
|
||||
#define ValueError(...) py_exception(tp_ValueError, __VA_ARGS__)
|
||||
#define IndexError(...) py_exception(tp_IndexError, __VA_ARGS__)
|
||||
#define ImportError(...) py_exception(tp_ImportError, __VA_ARGS__)
|
||||
|
||||
@ -11,11 +11,17 @@ class module : public object {
|
||||
|
||||
static module import(const char* name) {
|
||||
raise_call<py_import>(name);
|
||||
return module(py_retval(), object::realloc_t{});
|
||||
return borrow<module>(py_retval());
|
||||
}
|
||||
|
||||
static module create(const char* name) {
|
||||
auto m = py_newmodule(name);
|
||||
return steal<module>(m);
|
||||
}
|
||||
|
||||
module def_submodule(const char* name, const char* doc = nullptr) {
|
||||
// auto package = (attr("__package__").cast<std::string>() += ".") += attr("__name__").cast<std::string_view>();
|
||||
// auto package = (attr("__package__").cast<std::string>() += ".") +=
|
||||
// attr("__name__").cast<std::string_view>();
|
||||
auto fname = (attr("__name__").cast<std::string>() += ".") += name;
|
||||
auto m = py_newmodule(fname.c_str());
|
||||
setattr(*this, name, m);
|
||||
|
||||
@ -136,7 +136,7 @@ iterator interface<Dervied>::end() const {
|
||||
class str : public object {
|
||||
PKBIND_TYPE_IMPL(object, str, tp_str);
|
||||
|
||||
str(const char* data, int size) : object(alloc_t{}) { py_newstrn(m_ptr, data, size); }
|
||||
str(const char* data, int size) : object(alloc_t{}) { py_newstrv(m_ptr, {data, size}); }
|
||||
|
||||
str(const char* data) : str(data, static_cast<int>(strlen(data))) {}
|
||||
|
||||
|
||||
@ -53,7 +53,7 @@ struct Line {
|
||||
};
|
||||
|
||||
TEST_F(PYBIND11_TEST, class) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
py::class_<Point>(m, "Point")
|
||||
.def(py::init<>())
|
||||
.def(py::init<int, int, int>())
|
||||
@ -121,7 +121,7 @@ TEST_F(PYBIND11_TEST, inheritance) {
|
||||
Point3D(int x, int y, int z) : Point(x, y), z(z) { constructor_calls++; }
|
||||
};
|
||||
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
|
||||
py::class_<Point>(m, "Point")
|
||||
.def(py::init<>())
|
||||
@ -160,7 +160,7 @@ assert p.z == 30
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, dynamic_attr) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
|
||||
struct Point {
|
||||
int x;
|
||||
@ -185,7 +185,7 @@ TEST_F(PYBIND11_TEST, dynamic_attr) {
|
||||
TEST_F(PYBIND11_TEST, enum) {
|
||||
enum class Color { RED, Yellow, GREEN, BLUE };
|
||||
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
|
||||
py::enum_<Color> color(m, "Color");
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ TEST_F(PYBIND11_TEST, exception_python_to_cpp) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, exception_cpp_to_python) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
#define TEST_EXCEPTION(cppe, pye) \
|
||||
m.def("test_" #cppe, []() { \
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
namespace {
|
||||
|
||||
TEST_F(PYBIND11_TEST, vectorcall) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
py::exec(R"(
|
||||
def add(a, b):
|
||||
@ -28,7 +28,7 @@ def add2(a, *args):
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, constructor) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
struct Point {
|
||||
int x, y;
|
||||
@ -52,7 +52,7 @@ TEST_F(PYBIND11_TEST, constructor) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, args) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with args
|
||||
m.def("sum", [](py::args args) {
|
||||
@ -67,7 +67,7 @@ TEST_F(PYBIND11_TEST, args) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, kwargs) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with kwargs
|
||||
m.def("cal", [](py::kwargs kwargs) {
|
||||
@ -79,7 +79,7 @@ TEST_F(PYBIND11_TEST, kwargs) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, defaults) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with defaults
|
||||
m.def(
|
||||
@ -99,7 +99,7 @@ TEST_F(PYBIND11_TEST, defaults) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, defaults_with_args) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with defaults
|
||||
m.def(
|
||||
@ -123,7 +123,7 @@ TEST_F(PYBIND11_TEST, defaults_with_args) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with defaults
|
||||
m.def(
|
||||
@ -182,7 +182,7 @@ TEST_F(PYBIND11_TEST, default_with_args_and_kwargs) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, overload) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
// test for binding function with overloads
|
||||
m.def("cal", [](int a, int b) {
|
||||
@ -231,7 +231,7 @@ TEST_F(PYBIND11_TEST, return_value_policy) {
|
||||
move_constructor_calls = 0;
|
||||
destructor_calls = 0;
|
||||
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
py::class_<Point>(m, "Point")
|
||||
.def(py::init<int, int>())
|
||||
@ -292,7 +292,7 @@ TEST_F(PYBIND11_TEST, default_return_value_policy) {
|
||||
bool operator== (const Point& p) const { return x == p.x && y == p.y; }
|
||||
};
|
||||
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
py::class_<Point>(m, "Point")
|
||||
.def(py::init<int, int>())
|
||||
@ -333,7 +333,7 @@ TEST_F(PYBIND11_TEST, default_return_value_policy) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, lambda) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
static int destructor_calls = 0;
|
||||
|
||||
@ -373,7 +373,7 @@ int add(int a, int b) { return a + b; }
|
||||
int add(int a, int b, int c) { return a + b + c; }
|
||||
|
||||
TEST_F(PYBIND11_TEST, overload_cast) {
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
m.def("add", py::overload_cast<int, int>(add));
|
||||
m.def("add", py::overload_cast<int, int, int>(add));
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "test.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
PYBIND11_EMBEDDED_MODULE(example, m) {
|
||||
m.def("add", [](int a, int b) {
|
||||
@ -23,9 +24,32 @@ TEST_F(PYBIND11_TEST, module) {
|
||||
py::exec("from example.math import sub");
|
||||
EXPECT_EVAL_EQ("sub(1, 2)", -1);
|
||||
|
||||
auto math = py::module_::import("example.math");
|
||||
auto math = py::module::import("example.math");
|
||||
EXPECT_EQ(math.attr("sub")(4, 3).cast<int>(), 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
TEST_F(PYBIND11_TEST, raw_module) {
|
||||
auto m = py::module::create("example2");
|
||||
m.def("add", [](int a, int b) {
|
||||
return a + b;
|
||||
});
|
||||
|
||||
auto math = m.def_submodule("math");
|
||||
math.def("sub", [](int a, int b) {
|
||||
return a - b;
|
||||
});
|
||||
|
||||
py::exec("import example2");
|
||||
EXPECT_EVAL_EQ("example2.add(1, 2)", 3);
|
||||
|
||||
py::exec("from example2 import math");
|
||||
EXPECT_EVAL_EQ("math.sub(1, 2)", -1);
|
||||
|
||||
py::exec("from example2.math import sub");
|
||||
EXPECT_EVAL_EQ("sub(1, 2)", -1);
|
||||
|
||||
auto math2 = py::module::import("example2.math");
|
||||
EXPECT_EQ(math2.attr("sub")(4, 3).cast<int>(), 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -58,7 +58,7 @@ class Point:
|
||||
)";
|
||||
|
||||
TEST_F(PYBIND11_TEST, object) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
py::exec(source);
|
||||
py::exec("p = Point(3, 4)");
|
||||
py::object p = py::eval("p");
|
||||
|
||||
@ -54,7 +54,7 @@ struct Int {
|
||||
} // namespace
|
||||
|
||||
TEST_F(PYBIND11_TEST, arithmetic_operators) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
py::class_<Int>(m, "Int")
|
||||
.def(py::init<int>())
|
||||
.def(py::self + py::self)
|
||||
@ -126,7 +126,7 @@ TEST_F(PYBIND11_TEST, arithmetic_operators) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, logic_operators) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
py::class_<Int>(m, "Int")
|
||||
.def(py::init<int>())
|
||||
.def_readwrite("x", &Int::x)
|
||||
@ -149,7 +149,7 @@ TEST_F(PYBIND11_TEST, logic_operators) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, item_operators) {
|
||||
py::module_ m = py::module_::import("__main__");
|
||||
py::module m = py::module::import("__main__");
|
||||
|
||||
py::class_<std::vector<int>>(m, "vector")
|
||||
.def(py::init<>())
|
||||
|
||||
@ -41,7 +41,7 @@ TEST_F(PYBIND11_TEST, vector_bool) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, list_like) {
|
||||
py::class_<Point>(py::module_::__main__(), "Point")
|
||||
py::class_<Point>(py::module::__main__(), "Point")
|
||||
.def(py::init<int, int>())
|
||||
.def_readwrite("x", &Point::x)
|
||||
.def_readwrite("y", &Point::y)
|
||||
@ -89,7 +89,7 @@ TEST_F(PYBIND11_TEST, list_like) {
|
||||
}
|
||||
|
||||
TEST_F(PYBIND11_TEST, dict_like) {
|
||||
py::class_<Point>(py::module_::__main__(), "Point")
|
||||
py::class_<Point>(py::module::__main__(), "Point")
|
||||
.def(py::init<int, int>())
|
||||
.def_readwrite("x", &Point::x)
|
||||
.def_readwrite("y", &Point::y)
|
||||
|
||||
@ -152,7 +152,7 @@ TEST_F(PYBIND11_TEST, capsule) {
|
||||
delete static_cast<NotTrivial*>(ptr);
|
||||
});
|
||||
|
||||
auto m = py::module_::__main__();
|
||||
auto m = py::module::__main__();
|
||||
|
||||
m.def("foo", [](int x) {
|
||||
return py::capsule(new int(x));
|
||||
|
||||
@ -148,7 +148,7 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
|
||||
|
||||
void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out) {
|
||||
c11_string* res = c11_sbuf__submit(self);
|
||||
py_newstrn(out, res->data, res->size);
|
||||
py_newstrv(out, (c11_sv){res->data, res->size});
|
||||
c11_string__delete(res);
|
||||
}
|
||||
|
||||
|
||||
@ -38,6 +38,12 @@ void c11_string__ctor2(c11_string* self, const char* data, int size) {
|
||||
p[size] = '\0';
|
||||
}
|
||||
|
||||
void c11_string__ctor3(c11_string* self, int size) {
|
||||
self->size = size;
|
||||
char* p = (char*)self->data;
|
||||
p[size] = '\0';
|
||||
}
|
||||
|
||||
c11_string* c11_string__copy(c11_string* self) {
|
||||
int total_size = sizeof(c11_string) + self->size + 1;
|
||||
c11_string* retval = malloc(total_size);
|
||||
|
||||
@ -1209,7 +1209,7 @@ static int Ctx__add_const_string(Ctx* self, c11_sv key) {
|
||||
return *val;
|
||||
} else {
|
||||
py_TValue tmp;
|
||||
py_newstrn(&tmp, key.data, key.size);
|
||||
py_newstrv(&tmp, key);
|
||||
c11_vector__push(py_TValue, &self->co->consts, tmp);
|
||||
int index = self->co->consts.length - 1;
|
||||
c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
|
||||
|
||||
@ -242,6 +242,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
||||
DISPATCH();
|
||||
}
|
||||
case OP_LOAD_CLASS_GLOBAL: {
|
||||
assert(self->__curr_class);
|
||||
py_Name name = byte.arg;
|
||||
py_Ref tmp = py_getdict(self->__curr_class, name);
|
||||
if(tmp) {
|
||||
@ -902,6 +903,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
||||
DISPATCH();
|
||||
}
|
||||
case OP_STORE_CLASS_ATTR: {
|
||||
assert(self->__curr_class);
|
||||
py_Name name = byte.arg;
|
||||
if(py_istype(TOP(), tp_function)) {
|
||||
Function* ud = py_touserdata(TOP());
|
||||
@ -912,6 +914,7 @@ FrameResult VM__run_top_frame(VM* self) {
|
||||
DISPATCH();
|
||||
}
|
||||
case OP_ADD_CLASS_ANNOTATION: {
|
||||
assert(self->__curr_class);
|
||||
// [type_hint string]
|
||||
py_Type type = py_totype(self->__curr_class);
|
||||
py_TypeInfo* ti = TypeList__get(&self->types, type);
|
||||
|
||||
@ -149,6 +149,7 @@ static bool vec2__truediv__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
float divisor;
|
||||
if(!py_castfloat32(&argv[1], &divisor)) {
|
||||
py_clearexc(NULL);
|
||||
py_newnotimplemented(py_retval());
|
||||
return true;
|
||||
}
|
||||
@ -163,7 +164,7 @@ static bool vec2__repr__(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
char buf[64];
|
||||
int size = snprintf(buf, 64, "vec2(%.4f, %.4f)", argv[0]._vec2.x, argv[0]._vec2.y);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -355,7 +356,7 @@ static bool mat3x3__repr__(int argc, py_Ref argv) {
|
||||
m->data[6],
|
||||
m->data[7],
|
||||
m->data[8]);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -660,7 +661,7 @@ static bool vec2i__repr__(int argc, py_Ref argv) {
|
||||
c11_vec2i data = py_tovec2i(argv);
|
||||
char buf[64];
|
||||
int size = snprintf(buf, 64, "vec2i(%d, %d)", data.x, data.y);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -693,7 +694,7 @@ static bool vec3i__repr__(int argc, py_Ref argv) {
|
||||
c11_vec3i data = py_tovec3i(argv);
|
||||
char buf[64];
|
||||
int size = snprintf(buf, 64, "vec3i(%d, %d, %d)", data.x, data.y, data.z);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -730,7 +731,7 @@ static bool vec3__repr__(int argc, py_Ref argv) {
|
||||
c11_vec3 data = py_tovec3(argv);
|
||||
char buf[64];
|
||||
int size = snprintf(buf, 64, "vec3(%.4f, %.4f, %.4f)", data.x, data.y, data.z);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -43,7 +43,7 @@ py_Ref py_newmodule(const char* path) {
|
||||
const char* start = path + last_dot + 1;
|
||||
py_newstr(r1, start);
|
||||
py_setdict(r0, __name__, r1);
|
||||
py_newstrn(r1, path, last_dot);
|
||||
py_newstrv(r1, (c11_sv){path, last_dot});
|
||||
py_setdict(r0, __package__, r1);
|
||||
}
|
||||
|
||||
@ -404,7 +404,8 @@ static bool builtins_chr(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARG_TYPE(0, tp_int);
|
||||
py_i64 val = py_toint(py_arg(0));
|
||||
if(val < 0 || val > 128) { return ValueError("chr() arg not in range(128)"); }
|
||||
py_newstrn(py_retval(), (const char*)&val, 1);
|
||||
char* data = py_newstrn(py_retval(), 1);
|
||||
data[0] = (char)val;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -137,7 +137,10 @@ void py_clearexc(py_StackRef p0) {
|
||||
vm->last_retval = *py_NIL;
|
||||
vm->curr_exception = *py_NIL;
|
||||
vm->is_curr_exc_handled = false;
|
||||
vm->__curr_class = NULL;
|
||||
|
||||
/* Don't clear this, because StopIteration() may corrupt the class defination */
|
||||
// vm->__curr_class = NULL;
|
||||
|
||||
vm->__curr_function = NULL;
|
||||
if(p0) vm->stack.sp = p0;
|
||||
}
|
||||
|
||||
@ -210,7 +210,7 @@ static bool int__repr__(int argc, py_Ref argv) {
|
||||
py_i64 val = py_toint(&argv[0]);
|
||||
char buf[32];
|
||||
int size = snprintf(buf, sizeof(buf), "%lld", (long long)val);
|
||||
py_newstrn(py_retval(), buf, size);
|
||||
py_newstrv(py_retval(), (c11_sv){buf, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -6,17 +6,23 @@
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
#include "pocketpy/common/sstream.h"
|
||||
|
||||
void py_newstr(py_Ref out, const char* data) { py_newstrn(out, data, strlen(data)); }
|
||||
void py_newstr(py_Ref out, const char* data) { py_newstrv(out, (c11_sv){data, strlen(data)}); }
|
||||
|
||||
void py_newstrn(py_Ref out, const char* data, int size) {
|
||||
char* py_newstrn(py_Ref out, int size) {
|
||||
ManagedHeap* heap = &pk_current_vm->heap;
|
||||
int total_size = sizeof(c11_string) + size + 1;
|
||||
PyObject* obj = ManagedHeap__gcnew(heap, tp_str, 0, total_size);
|
||||
c11_string* ud = PyObject__userdata(obj);
|
||||
c11_string__ctor2(ud, data, size);
|
||||
c11_string__ctor3(ud, size);
|
||||
out->type = tp_str;
|
||||
out->is_ptr = true;
|
||||
out->_obj = obj;
|
||||
return ud->data;
|
||||
}
|
||||
|
||||
void py_newstrv(py_OutRef out, c11_sv sv) {
|
||||
char* data = py_newstrn(out, sv.size);
|
||||
memcpy(data, sv.data, sv.size);
|
||||
}
|
||||
|
||||
unsigned char* py_newbytes(py_Ref out, int size) {
|
||||
@ -97,7 +103,7 @@ static bool str__add__(int argc, py_Ref argv) {
|
||||
int total_size = sizeof(c11_string) + self->size + other->size + 1;
|
||||
c11_string* res = py_newobject(py_retval(), tp_str, 0, total_size);
|
||||
res->size = self->size + other->size;
|
||||
char* p = (char*)res->data;
|
||||
char* p = res->data;
|
||||
memcpy(p, self->data, self->size);
|
||||
memcpy(p + self->size, other->data, other->size);
|
||||
p[res->size] = '\0';
|
||||
@ -118,7 +124,7 @@ static bool str__mul__(int argc, py_Ref argv) {
|
||||
int total_size = sizeof(c11_string) + self->size * n + 1;
|
||||
c11_string* res = py_newobject(py_retval(), tp_str, 0, total_size);
|
||||
res->size = self->size * n;
|
||||
char* p = (char*)res->data;
|
||||
char* p = res->data;
|
||||
for(int i = 0; i < n; i++) {
|
||||
memcpy(p + i * self->size, self->data, self->size);
|
||||
}
|
||||
@ -174,14 +180,14 @@ static bool str__getitem__(int argc, py_Ref argv) {
|
||||
int index = py_toint(py_arg(1));
|
||||
if(!pk__normalize_index(&index, self.size)) return false;
|
||||
c11_sv res = c11_sv__u8_getitem(self, index);
|
||||
py_newstrn(py_retval(), res.data, res.size);
|
||||
py_newstrv(py_retval(), res);
|
||||
return true;
|
||||
} else if(_1->type == tp_slice) {
|
||||
int start, stop, step;
|
||||
bool ok = pk__parse_int_slice(_1, c11_sv__u8_length(self), &start, &stop, &step);
|
||||
if(!ok) return false;
|
||||
c11_string* res = c11_sv__u8_slice(self, start, stop, step);
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
py_newstrv(py_retval(), (c11_sv){res->data, res->size});
|
||||
c11_string__delete(res);
|
||||
return true;
|
||||
} else {
|
||||
@ -218,7 +224,7 @@ static bool str_lower(int argc, py_Ref argv) {
|
||||
int total_size = sizeof(c11_string) + self->size + 1;
|
||||
c11_string* res = py_newobject(py_retval(), tp_str, 0, total_size);
|
||||
res->size = self->size;
|
||||
char* p = (char*)res->data;
|
||||
char* p = res->data;
|
||||
for(int i = 0; i < self->size; i++) {
|
||||
char c = self->data[i];
|
||||
p[i] = c >= 'A' && c <= 'Z' ? c + 32 : c;
|
||||
@ -233,7 +239,7 @@ static bool str_upper(int argc, py_Ref argv) {
|
||||
int total_size = sizeof(c11_string) + self->size + 1;
|
||||
c11_string* res = py_newobject(py_retval(), tp_str, 0, total_size);
|
||||
res->size = self->size;
|
||||
char* p = (char*)res->data;
|
||||
char* p = res->data;
|
||||
for(int i = 0; i < self->size; i++) {
|
||||
char c = self->data[i];
|
||||
p[i] = c >= 'a' && c <= 'z' ? c - 32 : c;
|
||||
@ -303,7 +309,7 @@ static bool str_replace(int argc, py_Ref argv) {
|
||||
c11_string* new_ = py_touserdata(&argv[2]);
|
||||
c11_string* res =
|
||||
c11_sv__replace2(c11_string__sv(self), c11_string__sv(old), c11_string__sv(new_));
|
||||
py_newstrn(py_retval(), res->data, res->size);
|
||||
py_newstrv(py_retval(), (c11_sv){res->data, res->size});
|
||||
c11_string__delete(res);
|
||||
return true;
|
||||
}
|
||||
@ -325,7 +331,7 @@ static bool str_split(int argc, py_Ref argv) {
|
||||
py_newlistn(py_retval(), res.length);
|
||||
for(int i = 0; i < res.length; i++) {
|
||||
c11_sv item = c11__getitem(c11_sv, &res, i);
|
||||
py_newstrn(py_list_getitem(py_retval(), i), item.data, item.size);
|
||||
py_newstrv(py_list_getitem(py_retval(), i), item);
|
||||
}
|
||||
c11_vector__dtor(&res);
|
||||
return true;
|
||||
@ -353,7 +359,7 @@ static bool str__strip_impl(bool left, bool right, int argc, py_Ref argv) {
|
||||
return TypeError("strip() takes at most 2 arguments");
|
||||
}
|
||||
c11_sv res = c11_sv__strip(self, chars, left, right);
|
||||
py_newstrn(py_retval(), res.data, res.size);
|
||||
py_newstrv(py_retval(), res);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -506,7 +512,7 @@ static bool str_iterator__next__(int argc, py_Ref argv) {
|
||||
int start = *ud;
|
||||
int len = c11__u8_header(data[*ud], false);
|
||||
*ud += len;
|
||||
py_newstrn(py_retval(), data + start, len);
|
||||
py_newstrv(py_retval(), (c11_sv){data + start, len});
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -629,7 +635,7 @@ static bool bytes_decode(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
int size;
|
||||
unsigned char* data = py_tobytes(&argv[0], &size);
|
||||
py_newstrn(py_retval(), (const char*)data, size);
|
||||
py_newstrv(py_retval(), (c11_sv){(const char*)data, size});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -137,3 +137,9 @@ assert (g == 1), g
|
||||
|
||||
assert list.__new__(list) == []
|
||||
assert a.__new__ == list.__new__
|
||||
|
||||
|
||||
class A:
|
||||
x: list[int] = [i for i in range(1, 4)]
|
||||
|
||||
assert A.x == [1, 2, 3]
|
||||
Loading…
x
Reference in New Issue
Block a user