gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Precisions on the forth SWIG architecture: return by value structs


From: Francois Gallois
Subject: Precisions on the forth SWIG architecture: return by value structs
Date: Mon, 12 Jun 2023 10:50:46 +0200

Hello there,
I've been taking a look at the gforth bindings for Raylib at
https://github.com/ArnautDaniel/gforth-raylib in order to update them to the
latest version. One of the biggest problems the original authors has been facing
was converting the structures passed by value by raylib into pointers to allow
their use, such as :

```
\c Vector2 * iGetWindowScaleDPI(Vector2 * p){
\c Vector2 v = GetWindowScaleDPI();
\c *p = v; return p; }
c-function iGetWindowScaleDPI iGetWindowScaleDPI a -- a ( emptyVector2 -- 
Vector2 )
```

It seems that the "default behavior" for SWIG is to convert structures returned
by value into pointers 
(https://www.swig.org/Doc3.0/SWIGDocumentation.pdf#__WKANCHOR_58)
(this is the link to the docs of the relevant version of SWIG for
https://github.com/GeraldWodni/swig, which I've been using according to the
manual).

Has anyone played around enough with forth-SWIG to explain in more detail how
the current implementation behaves? I'd like to make that kind of fixes
automatic.

Separately, would there be a way to "typedef" some structures as stack objects?
In the specific case of raylib but also as a common example, I would like to
convert a Vector of 2 objects:

```
struct Vector2 {
  int x;
  int y;
};
```

into 2 stack objects, meaning that a function `foo` that takes a Vector2
(`int foo(Vector2 vec);`) can be called with `x y foo` in Forth,
and that calling a `Vector2 bar();` puts `x y` on the stack.
Could someone light my lantern on the things that could make that kind of
template possible?

I'll be continuing to search on my side of things, but comments would be much
appreciated.

Thanks for your hard work, and have fun.

---
François Gallois
"All told, a category is just a monad in the bicategory of spans, what's the
problem?"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]