From 41c6f92ec9a09a41dfe03715e0877772c2a62077 Mon Sep 17 00:00:00 2001 From: nickyc975 Date: Wed, 6 May 2020 13:44:49 +0800 Subject: [PATCH] Added VSBytesObject. --- inc/objects/VSBytesObject.hpp | 30 ++++++++++++++++++++++++++++++ inc/vs.hpp | 1 + src/objects/VSBytesObject.cpp | 23 +++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 inc/objects/VSBytesObject.hpp create mode 100644 src/objects/VSBytesObject.cpp diff --git a/inc/objects/VSBytesObject.hpp b/inc/objects/VSBytesObject.hpp new file mode 100644 index 0000000..3351045 --- /dev/null +++ b/inc/objects/VSBytesObject.hpp @@ -0,0 +1,30 @@ +#ifndef VS_BYTES_H +#define VS_BYTES_H + +#include + +#include "VSObject.hpp" + +extern VSObject *vs_bytes(VSObject *, VSObject *const *args, vs_size_t nargs); + +class VSBytesObject : public VSObject { +private: + static const str_func_map vs_bytes_methods; + +public: + std::vector _value; + + VSBytesObject(vs_size_t len); + ~VSBytesObject(); + + bool hasattr(std::string &attrname) override; + VSObject *getattr(std::string &attrname) override; + void setattr(std::string &attrname, VSObject *attrvalue) override; +}; + +inline void vs_bytes_append(vs_size_t len, ...); + +#define AS_BYTES(obj) ((VSBytesObject *)obj) +#define BYTES_LEN(obj) (AS_BYTES(obj)->_value.size()) + +#endif \ No newline at end of file diff --git a/inc/vs.hpp b/inc/vs.hpp index 677699d..923b624 100644 --- a/inc/vs.hpp +++ b/inc/vs.hpp @@ -6,6 +6,7 @@ typedef uint8_t cbool_t; typedef char cchar_t; +typedef uint8_t cbyte_t; typedef int64_t cint_t; typedef long double cfloat_t; typedef unsigned long long vs_size_t; diff --git a/src/objects/VSBytesObject.cpp b/src/objects/VSBytesObject.cpp new file mode 100644 index 0000000..192126c --- /dev/null +++ b/src/objects/VSBytesObject.cpp @@ -0,0 +1,23 @@ +#include "objects/VSBytesObject.hpp" + +#include "error.hpp" +#include "objects/VSFunctionObject.hpp" +#include "objects/VSStringObject.hpp" +#include "objects/VSTupleObject.hpp" + +NEW_IDENTIFIER(__bytes__); + +VSObject *vs_bytes(VSObject *, VSObject *const *args, vs_size_t nargs) { + if (nargs != 1) { + ERR_NARGS("bytes()", 1, nargs); + terminate(TERM_ERROR); + } + + VSObject *obj = args[0]; + VSObject *res = CALL_ATTR(obj, ID___bytes__, EMPTY_TUPLE()); + if (!IS_TYPE(res, T_BYTES)) { + err("%s.__bytes__() returned \"%s\" object instead of bytes", TYPE_STR[obj->type], TYPE_STR[res->type]); + terminate(TERM_ERROR); + } + INCREF_RET(res); +} \ No newline at end of file