On Fri, Apr 8, 2016 at 2:13 PM, Ziyuan Lin <[email protected]> wrote:
> I want to create an array of structs in JavaScript via Emscripten's
> interfaces:
> [snip]
>
> // minimal.html
> <script type="text/javascript" src="minimal.em.js" charset="utf-8"></script>
> <script type="text/javascript">
> var pointVec = new Module.PointVec();
> </script>
>
>
> The browser will complain Uncaught TypeError: Module.PointVec is not a
> constructor.
>
Your code is probably being run before the module is fully initialized.
If I'm reading the docs/code correctly, you'll want to put a callback on
Module.onRuntimeInitialized, something like this:
<script type="text/javascript">
Module = {
onRuntimeInitialized: function() {
var pointVec = new Module.PointVec();
}
};
</script>
<script type="text/javascript" src="minimal.em.js" charset="utf-8"></script>
Alternately there's a build option to disable the memory init file, which
will force the memory initialization to live in the JS code and load
synchronously (at the cost of larger file sizes).
To try that, add "--memory-init-file 0" into your em++ invocation.
But interestingly I can actually run var pointVec = new Module.PointVec(); in
> the browser's console (Chrome's, for example), but I cannot set the
> entries. Log:
>
>
> x Uncaught TypeError: Module.PointVec is not a constructor
> > var pointVec = new Module.PointVec()
> < undefined
> > pointVec
> < PointVec {$$: Object}
> > pointVec.set(0, {'x': 0, 'y': 0})
> < true
> > pointVec.get(0)
> < undefined
>
>
C++'s std::vector doesn't seem to allow you to use the [] = operators to
set an element value that's out of range; set() and get() seem to map to []
= and [] so I think the same limitation applies. Try using push_back()
instead, which will add an element to the end:
pointVec.push_back({x: 0, y: 0});
To change an existing element in the vector you can use set() and it should
work; it just won't work to add new elements.
-- brion
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.