Skip to content

Commit

Permalink
Added VSBytesObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickyc975 committed May 6, 2020
1 parent 63f48c3 commit 41c6f92
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions inc/objects/VSBytesObject.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef VS_BYTES_H
#define VS_BYTES_H

#include <vector>

#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<cbyte_t> _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
1 change: 1 addition & 0 deletions inc/vs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/objects/VSBytesObject.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 41c6f92

Please sign in to comment.