Skip to content

Commit 2bca87a

Browse files
committed
Introduce pointer value type
1 parent 3bfd0dd commit 2bca87a

File tree

7 files changed

+350
-4
lines changed

7 files changed

+350
-4
lines changed

include/scratchcpp/value.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class LIBSCRATCHCPP_EXPORT Value
5757
value_assign_cstring(&m_data, stringValue);
5858
}
5959

60+
/*! Constructs a pointer Value. */
61+
Value(const void *pointerValue)
62+
{
63+
value_init(&m_data);
64+
value_assign_pointer(&m_data, pointerValue);
65+
}
66+
6067
/*! Constructs value from ValueData. */
6168
Value(const ValueData &v)
6269
{
@@ -111,6 +118,9 @@ class LIBSCRATCHCPP_EXPORT Value
111118
/*! Returns true if the value is a string. */
112119
bool isString() const { return value_isString(&m_data); }
113120

121+
/*! Returns true if the value is a pointer. */
122+
bool isPointer() const { return value_isPointer(&m_data); }
123+
114124
/*! Returns the int representation of the value. */
115125
int toInt() const { return value_toInt(&m_data); }
116126

@@ -142,6 +152,9 @@ class LIBSCRATCHCPP_EXPORT Value
142152
/*! Converts the value to an RGBA quadruplet. */
143153
Rgb toRgba() const { return value_toRgba(&m_data); }
144154

155+
/*! Returns the pointer stored in the value or nullptr if it isn't a pointer. */
156+
const void *toPointer() const { return value_toPointer(&m_data); };
157+
145158
/*! Adds the given value to the value. */
146159
void add(const Value &v) { value_add(&m_data, &v.m_data, &m_data); }
147160

@@ -188,6 +201,12 @@ class LIBSCRATCHCPP_EXPORT Value
188201
return *this;
189202
}
190203

204+
const Value &operator=(const void *v)
205+
{
206+
value_assign_pointer(&m_data, v);
207+
return *this;
208+
}
209+
191210
const Value &operator=(const ValueData &v)
192211
{
193212
value_assign_copy(&m_data, &v);

include/scratchcpp/value_functions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern "C"
5757
LIBSCRATCHCPP_EXPORT void value_assign_string(ValueData *v, const std::string &stringValue);
5858
LIBSCRATCHCPP_EXPORT void value_assign_cstring(ValueData *v, const char *stringValue);
5959
LIBSCRATCHCPP_EXPORT void value_assign_stringPtr(ValueData *v, const StringPtr *stringValue);
60+
LIBSCRATCHCPP_EXPORT void value_assign_pointer(ValueData *v, const void *pointerValue);
6061
LIBSCRATCHCPP_EXPORT void value_assign_copy(ValueData *v, const ValueData *another);
6162

6263
LIBSCRATCHCPP_EXPORT bool value_isInfinity(const ValueData *v);
@@ -67,6 +68,7 @@ extern "C"
6768
LIBSCRATCHCPP_EXPORT bool value_isInt(const ValueData *v);
6869
LIBSCRATCHCPP_EXPORT bool value_isBool(const ValueData *v);
6970
LIBSCRATCHCPP_EXPORT bool value_isString(const ValueData *v);
71+
LIBSCRATCHCPP_EXPORT bool value_isPointer(const ValueData *v);
7072

7173
LIBSCRATCHCPP_EXPORT long value_toLong(const ValueData *v);
7274
LIBSCRATCHCPP_EXPORT int value_toInt(const ValueData *v);
@@ -76,6 +78,7 @@ extern "C"
7678
LIBSCRATCHCPP_EXPORT StringPtr *value_toStringPtr(const ValueData *v);
7779
LIBSCRATCHCPP_EXPORT void value_toUtf16(const ValueData *v, std::u16string *dst);
7880
LIBSCRATCHCPP_EXPORT Rgb value_toRgba(const ValueData *v);
81+
LIBSCRATCHCPP_EXPORT const void *value_toPointer(const ValueData *v);
7982

8083
LIBSCRATCHCPP_EXPORT bool value_doubleIsInt(double v);
8184

include/scratchcpp/valuedata.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ enum class LIBSCRATCHCPP_EXPORT ValueType
1515
{
1616
Number = 0,
1717
Bool = 1,
18-
String = 2
18+
String = 2,
19+
Pointer = 3
1920
};
2021

2122
extern "C"
@@ -29,6 +30,7 @@ extern "C"
2930
double numberValue;
3031
bool boolValue;
3132
StringPtr *stringValue;
33+
const void *pointerValue;
3234
};
3335

3436
ValueType type;

src/scratch/value_functions.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ extern "C"
8181
string_assign(v->stringValue, stringValue);
8282
}
8383

84+
/*! Assigns pointer to the given value. */
85+
void value_assign_pointer(ValueData *v, const void *pointerValue)
86+
{
87+
value_free(v);
88+
89+
v->type = ValueType::Pointer;
90+
v->pointerValue = pointerValue;
91+
}
92+
8493
/*! Assigns another value to the given value. */
8594
void value_assign_copy(ValueData *v, const libscratchcpp::ValueData *another)
8695
{
@@ -98,6 +107,9 @@ extern "C"
98107
string_assign(v->stringValue, another->stringValue);
99108
v->type = ValueType::String;
100109
}
110+
} else if (another->type == ValueType::Pointer) {
111+
value_free(v);
112+
v->pointerValue = another->pointerValue;
101113
}
102114

103115
v->type = another->type;
@@ -183,11 +195,20 @@ extern "C"
183195

184196
case ValueType::String:
185197
return value_checkString(v->stringValue) == 1;
198+
199+
case ValueType::Pointer:
200+
return false;
186201
}
187202

188203
return false;
189204
}
190205

206+
/*! Returns true if the given value is a pointer. */
207+
bool value_isPointer(const ValueData *v)
208+
{
209+
return v->type == ValueType::Pointer;
210+
}
211+
191212
/*! Returns true if the given value is a boolean. */
192213
bool value_isBool(const libscratchcpp::ValueData *v)
193214
{
@@ -250,6 +271,8 @@ extern "C"
250271
return v->numberValue != 0 && !std::isnan(v->numberValue);
251272
} else if (v->type == ValueType::String) {
252273
return value_stringToBool(v->stringValue);
274+
} else if (v->type == ValueType::Pointer) {
275+
return v->pointerValue;
253276
} else {
254277
return false;
255278
}
@@ -285,8 +308,11 @@ extern "C"
285308
string_assign(ret, &FALSE_STR);
286309
return ret;
287310
}
288-
} else
289-
return string_pool_new();
311+
} else {
312+
StringPtr *ret = string_pool_new();
313+
string_assign(ret, &ZERO_STR);
314+
return ret;
315+
}
290316
}
291317

292318
/*! Writes the UTF-16 representation of the given value to dst. */
@@ -309,6 +335,8 @@ extern "C"
309335
string = v->stringValue;
310336
else if (v->type == ValueType::Bool)
311337
return v->boolValue;
338+
else
339+
return 0;
312340

313341
if (string->size > 0 && string->data[0] == u'#') {
314342
// https://github.com/scratchfoundation/scratch-vm/blob/a4f095db5e03e072ba222fe721eeeb543c9b9c15/src/util/color.js#L60-L69
@@ -347,6 +375,15 @@ extern "C"
347375
return rgb(0, 0, 0);
348376
}
349377

378+
/*! Returns the pointer stored in the given value or nullptr if it isn't a pointer. */
379+
const void *value_toPointer(const ValueData *v)
380+
{
381+
if (v->type == ValueType::Pointer)
382+
return v->pointerValue;
383+
else
384+
return nullptr;
385+
}
386+
350387
/*! Returns true if the given number represents a round integer. */
351388
bool value_doubleIsInt(double v)
352389
{

src/scratch/value_functions_p.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ extern "C"
411411
return v->numberValue;
412412
} else if (v->type == ValueType::Bool)
413413
return v->boolValue;
414+
else if (v->type == ValueType::Pointer)
415+
return 0;
414416
else {
415417
assert(false); // this should never happen
416418
if (ok)
@@ -471,6 +473,8 @@ extern "C"
471473
return v1->numberValue - v2->numberValue;
472474
} else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool)
473475
return v1->boolValue - v2->boolValue;
476+
else if (v1->type == ValueType::Pointer && v2->type == ValueType::Pointer)
477+
return (long long)v1->pointerValue - (long long)v2->pointerValue; // NOTE: We probably don't need GT/LT pointer comparison...
474478

475479
bool ok;
476480
double n1 = value_getNumber(v1, &ok);

test/scratch_classes/monitor_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ TEST(MonitorTest, Sprite)
113113
TEST(MonitorTest, UpdateValue)
114114
{
115115
Monitor monitor("", "");
116-
monitor.updateValue(nullptr);
117116
monitor.updateValue("test");
118117

119118
MonitorHandlerMock handler;

0 commit comments

Comments
 (0)