SomeRanDev / reflaxe.CPP

An alternative C++ target for Haxe that generates dependent-less, GC-less C++17 code.
MIT License
72 stars 5 forks source link

Losing string contents if int is passed torough DynamicToString #54

Open neimanpinchas opened 1 month ago

neimanpinchas commented 1 month ago

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 commented 1 month ago

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;
}