mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-08 20:50:16 +00:00
remove rcptr and use shared_ptr instead
This commit is contained in:
parent
4f4a095ab7
commit
e82517bf88
@ -38,9 +38,6 @@
|
||||
#define PK_DEBUG_NO_AUTO_GC 0
|
||||
#define PK_DEBUG_GC_STATS 0
|
||||
#define PK_DEBUG_COMPILER 0
|
||||
#ifndef PK_DEBUG_DATASTRUCTURE
|
||||
#define PK_DEBUG_DATASTRUCTURE 0
|
||||
#endif
|
||||
|
||||
#ifndef PK_DEBUG_PRECOMPILED_EXEC
|
||||
#define PK_DEBUG_PRECOMPILED_EXEC 0
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "pocketpy/common/config.h"
|
||||
|
||||
typedef struct {
|
||||
#if PK_DEBUG_DATASTRUCTURE
|
||||
unsigned int magic;
|
||||
#endif
|
||||
unsigned int ref_c;
|
||||
void (*dtor)(void *self);
|
||||
} pkpy_Rcptr_header;
|
||||
|
||||
void pkpy_Rcptr__ctor(void *self);
|
||||
void pkpy_Rcptr__ctor_withd(void *self, void *dtor);
|
||||
void pkpy_Rcptr__ref(void *self);
|
||||
void pkpy_Rcptr__unref(void *self);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -97,7 +97,7 @@ enum class StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES };
|
||||
|
||||
struct Lexer {
|
||||
VM* vm;
|
||||
SourceData src;
|
||||
std::shared_ptr<SourceData> src;
|
||||
const char* token_start;
|
||||
const char* curr_char;
|
||||
int current_line = 1;
|
||||
@ -132,7 +132,7 @@ struct Lexer {
|
||||
[[nodiscard]] Error* IndentationError(const char* msg) noexcept { return _error(true, "IndentationError", msg, NULL); }
|
||||
[[nodiscard]] Error* NeedMoreLines() noexcept { return _error(true, "NeedMoreLines", "", NULL, 0); }
|
||||
|
||||
Lexer(VM* vm, SourceData src) noexcept;
|
||||
Lexer(VM* vm, std::shared_ptr<SourceData> src) noexcept;
|
||||
|
||||
[[nodiscard]] Error* run() noexcept;
|
||||
[[nodiscard]] Error* from_precompiled() noexcept;
|
||||
|
||||
@ -79,7 +79,7 @@ struct CodeObject {
|
||||
int iblock; // block index
|
||||
};
|
||||
|
||||
SourceData src;
|
||||
std::shared_ptr<SourceData> src;
|
||||
Str name;
|
||||
|
||||
vector<Bytecode> codes;
|
||||
@ -99,7 +99,7 @@ struct CodeObject {
|
||||
|
||||
const CodeBlock& _get_block_codei(int codei) const { return blocks[lines[codei].iblock]; }
|
||||
|
||||
CodeObject(SourceData src, const Str& name);
|
||||
CodeObject(std::shared_ptr<SourceData> src, const Str& name);
|
||||
void _gc_mark(VM*) const;
|
||||
};
|
||||
|
||||
|
||||
@ -33,14 +33,14 @@ struct Exception {
|
||||
PyObject* _self; // weak reference
|
||||
|
||||
struct Frame {
|
||||
SourceData src; // weak ref
|
||||
std::shared_ptr<SourceData> src; // weak ref
|
||||
int lineno;
|
||||
const char* cursor;
|
||||
std::string name;
|
||||
|
||||
Str snapshot() const { return src.snapshot(lineno, cursor, name); }
|
||||
Str snapshot() const { return src->snapshot(lineno, cursor, name); }
|
||||
|
||||
Frame(SourceData src, int lineno, const char* cursor, std::string_view name) :
|
||||
Frame(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name) :
|
||||
src(src), lineno(lineno), cursor(cursor), name(name) {}
|
||||
};
|
||||
|
||||
@ -79,7 +79,7 @@ struct TopLevelException : std::exception {
|
||||
|
||||
struct Error{
|
||||
const char* type;
|
||||
SourceData src;
|
||||
std::shared_ptr<SourceData> src;
|
||||
int lineno;
|
||||
const char* cursor;
|
||||
char msg[100];
|
||||
|
||||
@ -5,15 +5,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "pocketpy/common/rcptr.h"
|
||||
#include "pocketpy/common/str.h"
|
||||
#include "pocketpy/common/vector.h"
|
||||
|
||||
enum pkpy_CompileMode { PK_EXEC_MODE, PK_EVAL_MODE, PK_REPL_MODE, PK_JSON_MODE, PK_CELL_MODE };
|
||||
|
||||
struct pkpy_SourceData {
|
||||
pkpy_Rcptr_header _rc;
|
||||
|
||||
enum pkpy_CompileMode mode;
|
||||
bool is_precompiled;
|
||||
|
||||
|
||||
@ -16,75 +16,21 @@ enum {
|
||||
CELL_MODE = PK_CELL_MODE,
|
||||
};
|
||||
|
||||
struct SourceData {
|
||||
pkpy_SourceData *self;
|
||||
|
||||
SourceData(): self(nullptr) {
|
||||
}
|
||||
|
||||
struct SourceData : public pkpy_SourceData {
|
||||
SourceData(std::string_view source, const Str& filename, pkpy_CompileMode mode) {
|
||||
self = static_cast<pkpy_SourceData*>(std::malloc(sizeof(pkpy_SourceData)));
|
||||
pkpy_SourceData__ctor(self, source.data(), source.size(), &filename, mode);
|
||||
}
|
||||
|
||||
SourceData(const SourceData& other) {
|
||||
self = other.self;
|
||||
pkpy_Rcptr__ref(self);
|
||||
}
|
||||
|
||||
SourceData& operator=(const SourceData& other) {
|
||||
if (this != &other) {
|
||||
pkpy_Rcptr__unref(self);
|
||||
self = other.self;
|
||||
pkpy_Rcptr__ref(self);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
SourceData(SourceData &&other) {
|
||||
self = other.self;
|
||||
other.self = nullptr;
|
||||
}
|
||||
|
||||
SourceData& operator=(SourceData &&other) {
|
||||
if (this != &other) {
|
||||
pkpy_Rcptr__unref(self);
|
||||
self = other.self;
|
||||
other.self = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
pkpy_SourceData* get() const {
|
||||
return self;
|
||||
}
|
||||
|
||||
pkpy_SourceData* operator->() const {
|
||||
return self;
|
||||
pkpy_SourceData__ctor(this, source.data(), source.size(), &filename, mode);
|
||||
}
|
||||
|
||||
std::string_view get_line(int lineno) const {
|
||||
const char *st, *ed;
|
||||
if (pkpy_SourceData__get_line(self, lineno, &st, &ed)) {
|
||||
if (pkpy_SourceData__get_line(this, lineno, &st, &ed)) {
|
||||
return std::string_view(st, ed - st);
|
||||
}
|
||||
return "<?>";
|
||||
}
|
||||
|
||||
Str& filename() const {
|
||||
return static_cast<Str&>(self->filename);
|
||||
}
|
||||
|
||||
Str& source() const {
|
||||
return static_cast<Str&>(self->source);
|
||||
}
|
||||
|
||||
Str snapshot(int lineno, const char* cursor, std::string_view name) const {
|
||||
return pkpy_SourceData__snapshot(self, lineno, cursor, name.empty() ? nullptr : name.data());
|
||||
}
|
||||
|
||||
~SourceData() {
|
||||
pkpy_Rcptr__unref(self);
|
||||
return pkpy_SourceData__snapshot(this, lineno, cursor, name.empty() ? nullptr : name.data());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -1,45 +0,0 @@
|
||||
#include "pocketpy/common/rcptr.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define RCPTR_MAGIC 0x3c3d3e3f
|
||||
|
||||
#if PK_DEBUG_DATASTRUCTURE
|
||||
#define CHECK_MAGIC() assert(self->magic == RCPTR_MAGIC)
|
||||
#else
|
||||
#define CHECK_MAGIC() while(0)
|
||||
#endif
|
||||
|
||||
void pkpy_Rcptr__ctor(void *self) {
|
||||
pkpy_Rcptr__ctor_withd(self, NULL);
|
||||
}
|
||||
|
||||
void pkpy_Rcptr__ctor_withd(void *self_, void *dtor) {
|
||||
pkpy_Rcptr_header *self = self_;
|
||||
#if PK_DEBUG_DATASTRUCTURE
|
||||
self->magic = RCPTR_MAGIC;
|
||||
#endif
|
||||
self->ref_c = 1;
|
||||
self->dtor = (void (*)(void *))dtor;
|
||||
}
|
||||
|
||||
void pkpy_Rcptr__ref(void *self_) {
|
||||
if (self_ == NULL)
|
||||
return;
|
||||
pkpy_Rcptr_header *self = self_;
|
||||
CHECK_MAGIC();
|
||||
self->ref_c += 1;
|
||||
}
|
||||
|
||||
void pkpy_Rcptr__unref(void *self_) {
|
||||
if (self_ == NULL)
|
||||
return;
|
||||
pkpy_Rcptr_header *self = self_;
|
||||
CHECK_MAGIC();
|
||||
self->ref_c -= 1;
|
||||
if (self->ref_c == 0) {
|
||||
if (self->dtor)
|
||||
self->dtor(self_);
|
||||
free(self_);
|
||||
}
|
||||
}
|
||||
@ -11,8 +11,6 @@ void pkpy_SourceData__ctor(struct pkpy_SourceData* self,
|
||||
int source_size,
|
||||
const pkpy_Str* filename,
|
||||
enum pkpy_CompileMode mode) {
|
||||
pkpy_Rcptr__ctor_withd(self, &pkpy_SourceData__dtor);
|
||||
|
||||
self->filename = pkpy_Str__copy(filename); // OPTIMIZEME?
|
||||
self->mode = mode;
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ NameScope Compiler::name_scope() const noexcept{
|
||||
}
|
||||
|
||||
CodeObject_ Compiler::push_global_context() noexcept{
|
||||
CodeObject_ co = std::make_shared<CodeObject>(lexer.src, lexer.src.filename());
|
||||
CodeObject_ co = std::make_shared<CodeObject>(lexer.src, static_cast<const Str&>(lexer.src->filename));
|
||||
co->start_line = __i == 0 ? 1 : prev().line;
|
||||
contexts.push_back(CodeEmitContext(vm, co, contexts.size()));
|
||||
return co;
|
||||
@ -1281,7 +1281,7 @@ Error* Compiler::read_literal(PyVar* out) noexcept{
|
||||
}
|
||||
|
||||
Compiler::Compiler(VM* vm, std::string_view source, const Str& filename, pkpy_CompileMode mode, bool unknown_global_scope) noexcept:
|
||||
lexer(vm, {source, filename, mode}){
|
||||
lexer(vm, std::make_shared<SourceData>(source, filename, mode)){
|
||||
this->vm = vm;
|
||||
this->unknown_global_scope = unknown_global_scope;
|
||||
init_pratt_rules();
|
||||
|
||||
@ -533,9 +533,9 @@ Error* Lexer::SyntaxError(const char* fmt, ...) noexcept{
|
||||
return err;
|
||||
}
|
||||
|
||||
Lexer::Lexer(VM* vm, SourceData src) noexcept : vm(vm), src(src){
|
||||
this->token_start = src.source().c_str();
|
||||
this->curr_char = src.source().c_str();
|
||||
Lexer::Lexer(VM* vm, std::shared_ptr<SourceData> src) noexcept : vm(vm), src(src){
|
||||
this->token_start = pkpy_Str__data(&src->source);
|
||||
this->curr_char = pkpy_Str__data(&src->source);
|
||||
}
|
||||
|
||||
Error* Lexer::run() noexcept{
|
||||
@ -557,7 +557,7 @@ Error* Lexer::run() noexcept{
|
||||
}
|
||||
|
||||
Error* Lexer::from_precompiled() noexcept{
|
||||
TokenDeserializer deserializer(src.source().c_str());
|
||||
TokenDeserializer deserializer(pkpy_Str__data(&src->source));
|
||||
deserializer.curr += 5; // skip "pkpy:"
|
||||
std::string_view version = deserializer.read_string('\n');
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
CodeObject::CodeObject(SourceData src, const Str& name) :
|
||||
CodeObject::CodeObject(std::shared_ptr<SourceData> src, const Str& name) :
|
||||
src(src), name(name), nlocals(0), start_line(-1), end_line(-1) {
|
||||
blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user