3

I have a C++ code like this:

extern "C" {

void MyCoolFunction (int** values)
{
    int howManyValuesNeeded = 5;
    *values = new int[howManyValuesNeeded];
    for (int i = 0; i < howManyValuesNeeded; i++) {
        (*values)[i] = i;
    }
}

}

From C++ it can be used like this:

int *values = NULL;
MyCoolFunction (&values);
// do something with the values
delete[] values;

Of course the real code is much more complicated, but the point is that the function allocates an int array inside, and it decides what the array size will be.

I translated this code with Emscripten, but I don't know how could I access the array allocated inside the function from javascript. (I already know how to use exported functions and pointer parameters with Emscripten generated code, but I don't know how to solve this problem.)

Any ideas?

7
  • “I don't know how could I access the array” – But the code you are showing does exactly this. What is your question? Commented Jan 24, 2015 at 21:17
  • My question is how to access the array from javascript after the emscripten conversion. Commented Jan 24, 2015 at 21:18
  • Please add that to your question. It is not clear as currently written, at least to me. Also look again at the “from C it can be used…”, there is no delete[] in C. Commented Jan 24, 2015 at 21:20
  • I updated the question, and replaced C to C++. Commented Jan 24, 2015 at 21:21
  • Alright, I took back my close as “unclear what you are asking” vote. Commented Jan 24, 2015 at 21:23

2 Answers 2

3

In Emscripten, memory is stored as a giant array of integers, and pointers are just indexes into that array. Thus, you can pass pointers back and forth between C++ and Javascript just like you do integers. (It sounds like you know how to pass values around, but if not, go here.)

Okay. Now if you create a pointer on the C++ side (as in your code above) and pass it over to Javascript, Emscripten comes with a handful of helper functions to allow you to access that memory. Specifically setValue and getValue.

Thus, if you passed your values variable into JS and you wanted to access index 5, you would be able to do so with something like:

var value5 = getValue(values+(5*4), 'i32');

Where you have to add the index times the number of bytes (5*4) to the pointer, and indicate the type (in this case 32 bit ints) of the array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is exactly what is missed from my knowledge. If someone interested, here are my all experiments with pointers: github.com/kovacsv/EmscriptenPointers
If changing the function signature is an option, you could use a std::vector instead of the array. Passing around std::vectors is pretty simple using embind
0

You can call the delete from JavaSCript by wrapping it inside another exported function.

extern "C" { ...
    void MyCoolFunction (int** values);
    void finsih_with_result(int*);
}

void finsih_with_result(int *values) {
    delete[] values;
}

Alternatively you may also directly do this on JavaScript side: Module._free(Module.HEAPU32[values_offset/4]) (or something like that; code not tested).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.