mirror of
https://github.com/pocketpy/pocketpy
synced 2026-08-12 17:17:11 +08:00
Compare commits
1 Commits
9c65c7f1b3
...
14d1572328
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14d1572328 |
4
.github/workflows/main.yml
vendored
4
.github/workflows/main.yml
vendored
@ -56,11 +56,11 @@ jobs:
|
|||||||
- name: Setup Clang
|
- name: Setup Clang
|
||||||
uses: egor-tensin/setup-clang@v1
|
uses: egor-tensin/setup-clang@v1
|
||||||
with:
|
with:
|
||||||
version: 19
|
version: 17
|
||||||
platform: x64
|
platform: x64
|
||||||
- name: Run Sanitizers
|
- name: Run Sanitizers
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install -y libclang-rt-19-dev
|
sudo apt-get install -y libclang-rt-17-dev
|
||||||
bash build_g.sh
|
bash build_g.sh
|
||||||
bash run_tests.sh
|
bash run_tests.sh
|
||||||
rm -rf ./main
|
rm -rf ./main
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
typedef struct PyObject PyObject;
|
typedef struct PyObject PyObject;
|
||||||
typedef struct VM VM;
|
typedef struct VM VM;
|
||||||
extern PK_THREAD_LOCAL VM* pk_current_vm;
|
extern _Thread_local VM* pk_current_vm;
|
||||||
|
|
||||||
typedef struct py_TValue {
|
typedef struct py_TValue {
|
||||||
py_Type type;
|
py_Type type;
|
||||||
|
|||||||
@ -17,7 +17,7 @@ rm -rf .coverage
|
|||||||
mkdir .coverage
|
mkdir .coverage
|
||||||
|
|
||||||
UNITS=$(find ./ -name "*.gcno")
|
UNITS=$(find ./ -name "*.gcno")
|
||||||
llvm-cov-19 gcov ${UNITS} -r -s include/ -r -s src/ >> .coverage/coverage.txt
|
llvm-cov-17 gcov ${UNITS} -r -s include/ -r -s src/ >> .coverage/coverage.txt
|
||||||
|
|
||||||
mv *.gcov .coverage
|
mv *.gcov .coverage
|
||||||
rm *.gcda
|
rm *.gcda
|
||||||
|
|||||||
@ -189,8 +189,8 @@ static Error* LexerError(Lexer* self, const char* fmt, ...) {
|
|||||||
err->src = self->src;
|
err->src = self->src;
|
||||||
PK_INCREF(self->src);
|
PK_INCREF(self->src);
|
||||||
err->lineno = self->current_line;
|
err->lineno = self->current_line;
|
||||||
const char* p_end = self->src->source->data + self->src->source->size;
|
const char* end = self->src->source->data + self->src->source->size;
|
||||||
if(self->curr_char <= p_end && *self->curr_char == '\n') { err->lineno--; }
|
if(self->curr_char <= end && *self->curr_char == '\n') { err->lineno--; }
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vsnprintf(err->msg, sizeof(err->msg), fmt, args);
|
vsnprintf(err->msg, sizeof(err->msg), fmt, args);
|
||||||
@ -282,16 +282,9 @@ static Error* _eat_string(Lexer* self, c11_sbuf* buff, char quote, enum StringTy
|
|||||||
case 'b': c11_sbuf__write_char(buff, '\b'); break;
|
case 'b': c11_sbuf__write_char(buff, '\b'); break;
|
||||||
case 'f': c11_sbuf__write_char(buff, '\f'); break;
|
case 'f': c11_sbuf__write_char(buff, '\f'); break;
|
||||||
case 'v': c11_sbuf__write_char(buff, '\v'); break;
|
case 'v': c11_sbuf__write_char(buff, '\v'); break;
|
||||||
// Special case for the often used \0 while we don't have full support for octal
|
// Special case for the often used \0 while we don't have full support for octal literals.
|
||||||
// literals.
|
|
||||||
case '0': c11_sbuf__write_char(buff, '\0'); break;
|
case '0': c11_sbuf__write_char(buff, '\0'); break;
|
||||||
case 'x': {
|
case 'x': {
|
||||||
// check there are at least 2 chars can read
|
|
||||||
const char* p_end = self->src->source->data + self->src->source->size;
|
|
||||||
if(p_end - self->curr_char < 2) {
|
|
||||||
return LexerError(self, "invalid hex escape");
|
|
||||||
}
|
|
||||||
|
|
||||||
char hex[3] = {eatchar(self), eatchar(self), '\0'};
|
char hex[3] = {eatchar(self), eatchar(self), '\0'};
|
||||||
int code;
|
int code;
|
||||||
if(sscanf(hex, "%x", &code) != 1 || code > 0xFF) {
|
if(sscanf(hex, "%x", &code) != 1 || code > 0xFF) {
|
||||||
|
|||||||
@ -580,9 +580,8 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(p0->type == tp_type) {
|
if(p0->type == tp_type) {
|
||||||
py_Type p0_type = py_totype(p0);
|
|
||||||
// [cls, NULL, args..., kwargs...]
|
// [cls, NULL, args..., kwargs...]
|
||||||
py_Ref new_f = py_tpfindmagic(p0_type, __new__);
|
py_Ref new_f = py_tpfindmagic(py_totype(p0), __new__);
|
||||||
assert(new_f && py_isnil(p0 + 1));
|
assert(new_f && py_isnil(p0 + 1));
|
||||||
bool is_default_new = new_f->type == tp_nativefunc && new_f->_cfunc == pk__object_new;
|
bool is_default_new = new_f->type == tp_nativefunc && new_f->_cfunc == pk__object_new;
|
||||||
|
|
||||||
@ -600,16 +599,14 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
|
|||||||
// NOTE: previously we use `get_unbound_method` but here we just use `tpfindmagic`
|
// NOTE: previously we use `get_unbound_method` but here we just use `tpfindmagic`
|
||||||
// >> [cls, NULL, args..., kwargs...]
|
// >> [cls, NULL, args..., kwargs...]
|
||||||
// >> py_retval() is the new instance
|
// >> py_retval() is the new instance
|
||||||
py_Ref init_f = py_tpfindmagic(p0_type, __init__);
|
py_Ref init_f = py_tpfindmagic(py_totype(p0), __init__);
|
||||||
if(init_f) {
|
if(init_f) {
|
||||||
if(py_isinstance(py_retval(), p0_type)) {
|
// do an inplace patch
|
||||||
// do an inplace patch
|
*p0 = *init_f; // __init__
|
||||||
*p0 = *init_f; // __init__
|
p0[1] = self->last_retval; // self
|
||||||
p0[1] = self->last_retval; // self
|
// [__init__, self, args..., kwargs...]
|
||||||
// [__init__, self, args..., kwargs...]
|
if(VM__vectorcall(self, argc, kwargc, false) == RES_ERROR) return RES_ERROR;
|
||||||
if(VM__vectorcall(self, argc, kwargc, false) == RES_ERROR) return RES_ERROR;
|
*py_retval() = p0[1]; // restore the new instance
|
||||||
*py_retval() = p0[1]; // restore the new instance
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if(is_default_new) {
|
if(is_default_new) {
|
||||||
if(argc != 0 || kwargc != 0) {
|
if(argc != 0 || kwargc != 0) {
|
||||||
@ -717,62 +714,53 @@ void ManagedHeap__mark(ManagedHeap* self) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(obj->type > tp_object) {
|
void* ud = PyObject__userdata(obj);
|
||||||
// NOTE: `defaultdict` -> `dict` -> `object`
|
switch(obj->type) {
|
||||||
// NOTE: native types must extend from `object`.
|
case tp_list: {
|
||||||
py_TypeInfo* ti = pk_typeinfo(obj->type);
|
List* self = ud;
|
||||||
while(ti->base != tp_object) {
|
for(int i = 0; i < self->length; i++) {
|
||||||
ti = ti->base_ti;
|
py_TValue* val = c11__at(py_TValue, self, i);
|
||||||
|
pk__mark_value(val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case tp_dict: {
|
||||||
void* ud = PyObject__userdata(obj);
|
Dict* self = ud;
|
||||||
switch(ti->index) {
|
for(int i = 0; i < self->entries.length; i++) {
|
||||||
case tp_list: {
|
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
||||||
List* self = ud;
|
if(py_isnil(&entry->key)) continue;
|
||||||
for(int i = 0; i < self->length; i++) {
|
pk__mark_value(&entry->key);
|
||||||
py_TValue* val = c11__at(py_TValue, self, i);
|
pk__mark_value(&entry->val);
|
||||||
pk__mark_value(val);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case tp_dict: {
|
break;
|
||||||
Dict* self = ud;
|
}
|
||||||
for(int i = 0; i < self->entries.length; i++) {
|
case tp_generator: {
|
||||||
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
Generator* self = ud;
|
||||||
if(py_isnil(&entry->key)) continue;
|
if(self->frame) Frame__gc_mark(self->frame, p_stack);
|
||||||
pk__mark_value(&entry->key);
|
break;
|
||||||
pk__mark_value(&entry->val);
|
}
|
||||||
}
|
case tp_function: {
|
||||||
break;
|
function__gc_mark(ud, p_stack);
|
||||||
}
|
break;
|
||||||
case tp_generator: {
|
}
|
||||||
Generator* self = ud;
|
case tp_BaseException: {
|
||||||
if(self->frame) Frame__gc_mark(self->frame, p_stack);
|
BaseException* self = ud;
|
||||||
break;
|
pk__mark_value(&self->args);
|
||||||
}
|
pk__mark_value(&self->inner_exc);
|
||||||
case tp_function: {
|
c11__foreach(BaseExceptionFrame, &self->stacktrace, frame) {
|
||||||
function__gc_mark(ud, p_stack);
|
pk__mark_value(&frame->locals);
|
||||||
break;
|
pk__mark_value(&frame->globals);
|
||||||
}
|
|
||||||
case tp_BaseException: {
|
|
||||||
BaseException* self = ud;
|
|
||||||
pk__mark_value(&self->args);
|
|
||||||
pk__mark_value(&self->inner_exc);
|
|
||||||
c11__foreach(BaseExceptionFrame, &self->stacktrace, frame) {
|
|
||||||
pk__mark_value(&frame->locals);
|
|
||||||
pk__mark_value(&frame->globals);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case tp_code: {
|
|
||||||
CodeObject* self = ud;
|
|
||||||
CodeObject__gc_mark(self, p_stack);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case tp_chunked_array2d: {
|
|
||||||
c11_chunked_array2d__mark(ud, p_stack);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case tp_code: {
|
||||||
|
CodeObject* self = ud;
|
||||||
|
CodeObject__gc_mark(self, p_stack);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case tp_chunked_array2d: {
|
||||||
|
c11_chunked_array2d__mark(ud, p_stack);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include "pocketpy/common/name.h"
|
#include "pocketpy/common/name.h"
|
||||||
#include "pocketpy/interpreter/vm.h"
|
#include "pocketpy/interpreter/vm.h"
|
||||||
|
|
||||||
PK_THREAD_LOCAL VM* pk_current_vm;
|
_Thread_local VM* pk_current_vm;
|
||||||
|
|
||||||
static bool pk_initialized;
|
static bool pk_initialized;
|
||||||
static bool pk_finalized;
|
static bool pk_finalized;
|
||||||
|
|||||||
@ -167,14 +167,4 @@ class DerivedClass(BaseClass):
|
|||||||
return super().f()
|
return super().f()
|
||||||
|
|
||||||
|
|
||||||
assert DerivedClass.f() == 'BaseClass'
|
assert DerivedClass.f() == 'BaseClass'
|
||||||
|
|
||||||
# bad __init__
|
|
||||||
class A:
|
|
||||||
def __new__(cls, *args, **kwargs):
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
assert False
|
|
||||||
|
|
||||||
A()
|
|
||||||
Loading…
x
Reference in New Issue
Block a user