Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing vector of floating points value do not work with emscripten #96

Open
Luthaf opened this issue Dec 5, 2017 · 0 comments
Open

Comments

@Luthaf
Copy link

Luthaf commented Dec 5, 2017

Hi there !

I have another problem with emscripten compilation, which I extracted in a separated repository: https://github.com/Luthaf/nbind-vector-double.

The issue occurs when trying to pass std::vector<double> or std::vector<float> from C++ to JS. (The same happens with std::array<{float}, N>)

The code looks like this:

#include <vector>

struct Vector {
    static std::vector<float> floats() {
        return {{0.0, 1.0, 2.0}};
    }

    static std::vector<double> doubles() {
        return {{0.0, 1.0, 2.0}};
    }
};

#include "nbind/nbind.h"

NBIND_CLASS(Vector) {
    method(floats);
    method(doubles);
}

And on the JS side:

var assert = require('assert');
var nbind = require('nbind');
var lib = nbind.init().lib;

var floats = lib.Vector.floats();
console.log(floats);

var doubles = lib.Vector.doubles();
console.log(doubles);

assert(floats[0] == 0.0);
assert(floats[1] == 1.0);
assert(floats[2] == 2.0);

assert(doubles[0] == 0.0);
assert(doubles[1] == 1.0);
assert(doubles[2] == 2.0);

This prints

[ 0, 0, 0 ]
[ undefined, undefined, undefined ]

before failing the assertions.


I think the problem might be related to the code around here:

HEAPU32[result / 4] = length;
const heap = type.memberType.heap;
let ptr = (result + 4) / ptrSize;

because ptrSize will be 8 for double, which means that ptr will not be an integer but a real number with a decimal value of 0.5. But at the same time the issue might come from elsewhere, as the test also fails with floats (but differently ...)

Anyway, do you have an idea on how I could fix this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant