diff --git a/src/script/types.c b/src/script/types.c index c72f2cf0a..297a2b3b8 100644 --- a/src/script/types.c +++ b/src/script/types.c @@ -21,6 +21,7 @@ static void _deinitTableValue(void*); static void _allocString(struct mScriptValue*); static void _freeString(struct mScriptValue*); static uint32_t _hashString(const struct mScriptValue*); +static bool _stringCast(const struct mScriptValue*, const struct mScriptType*, struct mScriptValue*); static bool _castScalar(const struct mScriptValue*, const struct mScriptType*, struct mScriptValue*); static uint32_t _hashScalar(const struct mScriptValue*); @@ -169,6 +170,7 @@ const struct mScriptType mSTString = { .free = _freeString, .hash = _hashString, .equal = _stringEqual, + .cast = _stringCast, }; const struct mScriptType mSTCharPtr = { @@ -179,6 +181,7 @@ const struct mScriptType mSTCharPtr = { .free = NULL, .hash = _hashString, .equal = _charpEqual, + .cast = _stringCast, }; const struct mScriptType mSTList = { @@ -272,6 +275,24 @@ static void _freeString(struct mScriptValue* val) { free(string); } +static bool _stringCast(const struct mScriptValue* in, const struct mScriptType* type, struct mScriptValue* out) { + if (in->type == type) { + out->type = type; + out->refs = mSCRIPT_VALUE_UNREF; + out->flags = 0; + out->value.opaque = in->value.opaque; + return true; + } + if (in->type == mSCRIPT_TYPE_MS_STR && type == mSCRIPT_TYPE_MS_CHARP) { + out->type = type; + out->refs = mSCRIPT_VALUE_UNREF; + out->flags = 0; + out->value.opaque = ((struct mScriptString*) in->value.opaque)->buffer; + return true; + } + return false; +} + static uint32_t _hashString(const struct mScriptValue* val) { const char* buffer = 0; size_t size = 0;