Open haxscramper opened 2 years ago
Main issue that arises from this difference - I need to figure out a way how to "mend together" nim and C++ object ownership/memory-management semantics. Nim (sadly) does not have precise tools for handling objects, like "function requires rvalue", "cannot copy", "cannot default-construct" (https://github.com/nim-lang/RFCs/issues/252) and the best nim counterpart for some of these cases would be just "API by convention", which would just lead users into codegen bugs.
Running with --expandArc:main
shows that nim code infers c1
to be a cursor into c
block :tmp:
var c
c = CxxContainer[NimObject](value: NimObject(field: 2))
var c1_cursor = c
`=destroy`(c)
Partially related https://github.com/nim-lang/RFCs/issues/432 - if would be nice to have some form of "requires move" etc. annotations
ref T
generation for cxx classes. Was supported in IRv1, but best to be reimplemented.Two possible ways of implementing -
proc newImportAux*() {.importc: "//", header: "<new>".} =
discard
proc newType(arg1, arg2...) ref Type =
new(result)
newImportAux()
{.emit: "new ((void*)result) Type(`arg1`, `arg2`);.}
or
proc cxxPlaceNew*[T](x: ref T) {.
header: "<new>", importcpp: "(new (#) '*0(@))", varargs.}
proc newType(arg1, arg2...) ref Type =
new(result)
cxxPlaceNew(arg1, arg2)
cxxPlaceNew
should be a part of helper library, or automatically generated for the wrappers (#11 can setup everything, or code can be generated anew)
In these two example nim and C++ code produce completely different results - nim calls destructor only once, does not invoke neither copy nor move.