Added var2bytes and bytes2var to convet any variable to bytes and back. Closes #2075

This commit is contained in:
Juan Linietsky 2016-01-02 21:07:03 -03:00
parent 939c5f5c9e
commit 23441ec867
4 changed files with 75 additions and 3 deletions

View file

@ -34,6 +34,7 @@
#include "func_ref.h"
#include "os/os.h"
#include "variant_parser.h"
#include "io/marshalls.h"
const char *GDFunctions::get_func_name(Function p_func) {
@ -94,6 +95,8 @@ const char *GDFunctions::get_func_name(Function p_func) {
"printraw",
"var2str",
"str2var",
"var2bytes",
"bytes2var",
"range",
"load",
"inst2dict",
@ -638,6 +641,57 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va
r_ret=Variant();
}
} break;
case VAR_TO_BYTES: {
VALIDATE_ARG_COUNT(1);
ByteArray barr;
int len;
Error err = encode_variant(*p_args[0],NULL,len);
if (err) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::NIL;
r_ret=Variant();
return;
}
barr.resize(len);
{
ByteArray::Write w = barr.write();
encode_variant(*p_args[0],w.ptr(),len);
}
r_ret=barr;
} break;
case BYTES_TO_VAR: {
VALIDATE_ARG_COUNT(1);
if (p_args[0]->get_type()!=Variant::RAW_ARRAY) {
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::RAW_ARRAY;
r_ret=Variant();
return;
}
ByteArray varr=*p_args[0];
Variant ret;
{
ByteArray::Read r=varr.read();
Error err = decode_variant(ret,r.ptr(),varr.size(),NULL);
if (err!=OK) {
ERR_PRINT("Not enough bytes for decoding..");
r_error.error=Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument=0;
r_error.expected=Variant::RAW_ARRAY;
r_ret=Variant();
return;
}
}
r_ret=ret;
} break;
case GEN_RANGE: {
@ -1358,6 +1412,18 @@ MethodInfo GDFunctions::get_info(Function p_func) {
mi.return_val.type=Variant::NIL;
return mi;
} break;
case VAR_TO_BYTES: {
MethodInfo mi("var2bytes",PropertyInfo(Variant::NIL,"var"));
mi.return_val.type=Variant::RAW_ARRAY;
return mi;
} break;
case BYTES_TO_VAR: {
MethodInfo mi("bytes2var:Variant",PropertyInfo(Variant::RAW_ARRAY,"bytes"));
mi.return_val.type=Variant::NIL;
return mi;
} break;
case GEN_RANGE: {
MethodInfo mi("range",PropertyInfo(Variant::NIL,"..."));

View file

@ -89,6 +89,8 @@ public:
TEXT_PRINTRAW,
VAR_TO_STR,
STR_TO_VAR,
VAR_TO_BYTES,
BYTES_TO_VAR,
GEN_RANGE,
RESOURCE_LOAD,
INST2DICT,

View file

@ -1046,7 +1046,7 @@ void GDTokenizerText::advance(int p_amount) {
//////////////////////////////////////////////////////////////////////////////////////////////////////
#define BYTECODE_VERSION 9
#define BYTECODE_VERSION 10
Error GDTokenizerBuffer::set_code_buffer(const Vector<uint8_t> & p_buffer) {

View file

@ -584,7 +584,10 @@ void DocData::generate(bool p_basic_types) {
md.name=mi.name;
if (mi.return_val.name!="")
md.return_type=mi.return_val.name;
else
else if (mi.name.find(":")!=-1) {
md.return_type=mi.name.get_slice(":",1);
md.name=mi.name.get_slice(":",0);
} else
md.return_type=Variant::get_type_name(mi.return_val.type);
for(int i=0;i<mi.arguments.size();i++) {
@ -594,8 +597,9 @@ void DocData::generate(bool p_basic_types) {
ArgumentDoc ad;
ad.name=pi.name;
if (pi.type==Variant::NIL)
ad.type="var";
ad.type="Variant";
else
ad.type=Variant::get_type_name( pi.type );