Accessing externed type instance, stored in a member variable causes casting error

Hello all!
If you didn’t already know, @darmie created FB Yoga extern – GitHub - darmie/linc_yoga: http://snowkit.github.io/linc/ Haxe @:native bindings for Yoga. Yoga is a cross-platform layout engine which implements Flexbox. This is a Haxe extern library for Yoga.
It’s fully functional, testes are passing.

Problem:
I want to hold an instance of Node and operate over it. Just like in the following example:

package;

import facebook.Yoga;
import facebook.yoga.Node;
import facebook.yoga.Layout;
import facebook.yoga.Enums.Direction;

class Main {
    private final node: Node = Yoga.newNode();

    public function new() {
        node.setLayout(Layout.init());

        Yoga.nodeCalculateLayout(node, 200.0, 200.0, Direction.LTR);
    }

    static function main() {
        new Main();
    }
}

It doesn’t compile with error:

C:/HaxeToolkit/haxe/lib/hxcpp/git/include/cpp/Variant.h: In instantiation of 'RETURN_ cpp::Variant::Cast() const [with RETURN_ = YGNode*]':
./src/Main.cpp:102:70:   required from here
C:/HaxeToolkit/haxe/lib/hxcpp/git/include/cpp/Variant.h:150:37: error: invalid cast from type 'const cpp::Variant' to type 'YGNode*'
       RETURN_ Cast() const { return RETURN_(*this); }

If I make node a local variable like this: var node: Node = Yoga.newNode(); it’s working…


Haxe:

C++:

Hi @Dimous I am glad you find this extern library useful.

This is how I overcame this problem:

	@:unreflective
	private final var node:facebook.yoga.Node;

It need to be marked as @unreflective , because if you don’t , hxcpp attempts to convert it to a cpp::Variant

1 Like

Now it throws another error:

In file included from ./src/__main__.cpp:11:0:
include/Main.h:11:10: fatal error: linc_yoga.h: No such file or directory
 #include "linc_yoga.h"
          ^~~~~~~~~~~~~
compilation terminated.
Error: Build failed

This is awkward. Can I see your build configurations?

-cp src
-main Main
-cpp bin/
-lib linc_yoga
-dce full
-D clean
-D verbose
#-D no-traces
-D toolchain=mingw
-D analyzer-optimize
-D NO_PRECOMPILED_HEADERS
#-D windows
-D mingw
-v

Haxe Compiler 4.0.0-preview.4+eec79c1e5
linc_yoga git ced3dc344eff5ab369cf9de65c20bf763c25f104
hxcpp git 668c004481d7fb7ea20220e781b916d73233804a

Try and pull the latest linc_yoga and try it with hxcpp 4.0.4

I installed haxe 4.0.0-preview.4, hxcpp 4.0.4, pulled latest linc_yoga but still no luck. Both msvc and mingw throw the same error.

It works on my Macbook at the moment. Not quite sure why it can’t find linc_yoga.h when the include directory is already defined for the compiler.

Anyway, original issiue is resolved. I will somehow make compiler find this header :wink:
Thanks!

1 Like

Cool stuff!

1 Like