reflaxe.CPP icon indicating copy to clipboard operation
reflaxe.CPP copied to clipboard

Losing string contents if int is passed torough DynamicToString

Open neimanpinchas opened this issue 1 year ago • 1 comments

In My use casse, I passed a int literal to Std.string, which accepts type dynamic, and later passed as std.String to extern function where the param was used in cout, I ws able to see that nothing get logged, when I added a cout in the original location of the int var it had been logged

neimanpinchas avatar Aug 05 '24 20:08 neimanpinchas

This was in the following project ttps://github.com/neimanpinchas/haxe_civet_web

After testing with a mnimal example that was working I figure out it is because of an issue with abstract I made to convert a string to const char *

THe following line is losing string contents

	cout << "as number" << port << endl;
	cout << "as string" << Std::string(port) << endl;
	cout << "as c_str" << _CxxConvert::StdString_Impl_::_new(Std::string(port)) << endl;
	_StartWeb(_CxxConvert::StdString_Impl_::_new(Std::string(port))); // << here

Haxe implementation of abstract

typedef ConstCharStar=cxx.ConstCharPtr;
typedef Star<T>=cxx.Ptr<T>;
abstract StdString(cxx.ConstCharPtr) to cxx.ConstCharPtr{
    public function new(s:String){
        this=cxx.ConstCharPtr.fromString(s);
    }
    @:from
    public static function ofString(s:String) {
        return new StdString(s);
    }
    @:to
    public function toConstCharStar():ConstCharStar{
        return this;
    }

}

C++ output

const char* _CxxConvert::StdString_Impl_::_new(std::string s) {
	const char* this1 = s.c_str();

	return this1;
}

const char* _CxxConvert::StdString_Impl_::ofString(std::string s) {
	return _CxxConvert::StdString_Impl_::_new(s);
}

ConstCharStar _CxxConvert::StdString_Impl_::toConstCharStar(const char* this1) {
	return this1;
}

neimanpinchas avatar Aug 07 '24 09:08 neimanpinchas