diff --git a/include/pocketpy/common/config.h b/include/pocketpy/common/config.h index be288768..547c4a50 100644 --- a/include/pocketpy/common/config.h +++ b/include/pocketpy/common/config.h @@ -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 diff --git a/include/pocketpy/common/rcptr.h b/include/pocketpy/common/rcptr.h deleted file mode 100644 index 4008e6f4..00000000 --- a/include/pocketpy/common/rcptr.h +++ /dev/null @@ -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 diff --git a/include/pocketpy/compiler/lexer.hpp b/include/pocketpy/compiler/lexer.hpp index ddc25e89..41b4cbaf 100644 --- a/include/pocketpy/compiler/lexer.hpp +++ b/include/pocketpy/compiler/lexer.hpp @@ -97,7 +97,7 @@ enum class StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES }; struct Lexer { VM* vm; - SourceData src; + std::shared_ptr 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 src) noexcept; [[nodiscard]] Error* run() noexcept; [[nodiscard]] Error* from_precompiled() noexcept; diff --git a/include/pocketpy/objects/codeobject.hpp b/include/pocketpy/objects/codeobject.hpp index f8685092..baefd679 100644 --- a/include/pocketpy/objects/codeobject.hpp +++ b/include/pocketpy/objects/codeobject.hpp @@ -79,7 +79,7 @@ struct CodeObject { int iblock; // block index }; - SourceData src; + std::shared_ptr src; Str name; vector 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 src, const Str& name); void _gc_mark(VM*) const; }; diff --git a/include/pocketpy/objects/error.hpp b/include/pocketpy/objects/error.hpp index 099507bf..a125329d 100644 --- a/include/pocketpy/objects/error.hpp +++ b/include/pocketpy/objects/error.hpp @@ -33,14 +33,14 @@ struct Exception { PyObject* _self; // weak reference struct Frame { - SourceData src; // weak ref + std::shared_ptr 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 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 src; int lineno; const char* cursor; char msg[100]; diff --git a/include/pocketpy/objects/sourcedata.h b/include/pocketpy/objects/sourcedata.h index 82d34481..67c35f8c 100644 --- a/include/pocketpy/objects/sourcedata.h +++ b/include/pocketpy/objects/sourcedata.h @@ -5,15 +5,12 @@ extern "C" { #endif #include -#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; diff --git a/include/pocketpy/objects/sourcedata.hpp b/include/pocketpy/objects/sourcedata.hpp index 27ec6978..dff77f60 100644 --- a/include/pocketpy/objects/sourcedata.hpp +++ b/include/pocketpy/objects/sourcedata.hpp @@ -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(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(self->filename); - } - - Str& source() const { - return static_cast(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()); } }; diff --git a/src/common/rcptr.c b/src/common/rcptr.c deleted file mode 100644 index 2f3f95d6..00000000 --- a/src/common/rcptr.c +++ /dev/null @@ -1,45 +0,0 @@ -#include "pocketpy/common/rcptr.h" -#include -#include - -#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_); - } -} diff --git a/src/common/sourcedata.c b/src/common/sourcedata.c index 51840a92..bb204e66 100644 --- a/src/common/sourcedata.c +++ b/src/common/sourcedata.c @@ -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; diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 3758d922..1e425a63 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -20,7 +20,7 @@ NameScope Compiler::name_scope() const noexcept{ } CodeObject_ Compiler::push_global_context() noexcept{ - CodeObject_ co = std::make_shared(lexer.src, lexer.src.filename()); + CodeObject_ co = std::make_shared(lexer.src, static_cast(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(source, filename, mode)){ this->vm = vm; this->unknown_global_scope = unknown_global_scope; init_pratt_rules(); diff --git a/src/compiler/lexer.cpp b/src/compiler/lexer.cpp index 42c54632..f6505d87 100644 --- a/src/compiler/lexer.cpp +++ b/src/compiler/lexer.cpp @@ -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 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'); diff --git a/src/objects/codeobject.cpp b/src/objects/codeobject.cpp index 949810d6..28087330 100644 --- a/src/objects/codeobject.cpp +++ b/src/objects/codeobject.cpp @@ -2,7 +2,7 @@ namespace pkpy { -CodeObject::CodeObject(SourceData src, const Str& name) : +CodeObject::CodeObject(std::shared_ptr 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)); } diff --git a/xmake.lua b/xmake.lua index aa07c37d..78c0907e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -26,5 +26,5 @@ target("main") if is_mode("debug") then set_optimize("none") set_symbols("debug") - add_defines("DEBUG", "PK_DEBUG_DATASTRUCTURE") + add_defines("DEBUG") end