Skip to content

Commit

Permalink
Added array of bytes commodity set/get for variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoLizza committed Jan 15, 2015
1 parent c46c31b commit db198df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
34 changes: 34 additions & 0 deletions TinyJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,15 @@ Variable::Variable(int val) {
setInt(val);
}

Variable::Variable(const std::vector<unsigned char> &val) {
refs = 0;
#if DEBUG_MEMORY
mark_allocated(this);
#endif
init();
setArray(val);
}

Variable::~Variable(void) {
#if DEBUG_MEMORY
mark_deallocated(this);
Expand Down Expand Up @@ -935,6 +944,19 @@ int Variable::getChildren() const {
return n;
}

const std::vector<unsigned char> Variable::getArray() const {
std::vector<unsigned char> out;
if (isArray()) {
int arrayLength = getArrayLength();
out.reserve(arrayLength);
for (int i = 0; i < arrayLength; ++i) {
TinyJS::Variable *cell = getArrayIndex(i);
out.push_back(static_cast<unsigned char>(cell->getInt()));
}
}
return out;
}

int Variable::getInt() const {
/* strtol understands about hex and octal */
if (isInt()) return intData;
Expand Down Expand Up @@ -1013,6 +1035,18 @@ void Variable::setArray() {
removeAllChildren();
}

void Variable::setArray(const std::vector<unsigned char> &val) {
// name sure it's not still a number or integer
flags = (flags&~VARIABLE_TYPEMASK) | VARIABLE_ARRAY;
stringData = TINYJS_BLANK_DATA;
intData = 0;
doubleData = 0;
removeAllChildren();
for (std::vector<unsigned char>::size_type i = 0; i < val.size(); ++i) {
setArrayIndex(i, new TinyJS::Variable(static_cast<int>(val[i])));
}
}

bool Variable::equals(const Variable *v) {
Variable *resV = mathsOp(v, LEXER_EQUAL);
bool res = resV->getBool();
Expand Down
3 changes: 3 additions & 0 deletions TinyJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Variable
Variable(const std::string &str); ///< Create a string
Variable(double varData);
Variable(int val);
Variable(const std::vector<unsigned char> &val); ///< Create a array-of-bytes
~Variable(void);

Variable *getReturnVar(); ///< If this is a function, get the result value (for use by native functions)
Expand All @@ -224,6 +225,7 @@ class Variable
int getArrayLength() const; ///< If this is an array, return the number of items in it (else 0)
int getChildren() const; ///< Get the number of children

const std::vector<unsigned char> getArray() const;
int getInt() const;
bool getBool() const { return getInt() != 0; }
double getDouble() const;
Expand All @@ -234,6 +236,7 @@ class Variable
void setString(const std::string &str);
void setUndefined();
void setArray();
void setArray(const std::vector<unsigned char> &val);
bool equals(const Variable *v);

bool isInt() const { return (flags&VARIABLE_INTEGER)!=0; }
Expand Down

0 comments on commit db198df

Please sign in to comment.