andresy/torch — pull request #136 by jucor (2013-05-30 14:33:16+00:00)
Title: Fix bug in testwrap with basic types and returned=true

Problem
==

When using `wrap.lua`, it was so far impossible to have a basic type (e.g. double) as a `returned = true` argument. It could only be `creturned = true`, which was problematic when more than one value needed to be returned (as e.g. in issue andresy#129).

This pull-request fixes this. I realize that with FFI, C functions are much easier to wrap, but for uniformity with the rest of the code-base, I wanted to stick to `wrap.lua` when developping andresy#129.

Implemented solution
==

Pass pointers
---
When an arugment of basic type has `returned = true`, a pointer is passed to the C function, so that it can use it as an output argument.

Push on stack at postcall, not precall
---

I also removed the way such returned basic type arguments were pushed on the stack at precall. Instead, I now push them on stack at postcall, just like `creturned=true` arugment. I did

Force requirement for invisible
---

I had to require that such arguments were also `invisible = true`, because they would otherwise be counted in `narg`, and thus mess the check. I could have modified the check to ignore the returned arguments of this type, and prevent them from being stacked by the caller, but that would make too deep modifications: I'm not comfortable enough with the rest of `wrap.lua` to ensure that it would not clash with the smart things done for non-basic types.

An alternative way would be to automatically turn them invisible.

Added a package `testwrap` for the tests, in `/dev`
----

I have added corresponding unit tests in package `dev/testwrap`. However, this relies on pull-request andresy#133 being merged. Alternatively, I will be happy to move those tests to any folder you suggest.