Passing an std::map into JavaScript

7 views
Skip to first unread message

Shazni Nazeer

unread,
Mar 16, 2022, 11:46:51 AMMar 16
to
I'm new to SpiderMonkey. So far the learning experience has been great. I have played around it for about a week now.

I'm implementing a program in which I want to give the user a scripting ability. I have had some success in doing this. I was able to expose a C++ class in JavaScript and invoke its member functions.

I'm facing a challenge now. Maybe it's quite easy which I seem to be having trouble with. One of the member functions in my class return an std::map as in below.
std::map<std::string, std::string> &getKeyValues();

Now I want to allow my scripting authors below.

// JavaScript
var myKeyValues = my_instance.getKeyValues();
for(var key in myKeyValues) {
// do something with key and myKeyValues[key]
}

I have implemented a wrapper class for my C++ class. I have access to an instance of the original class inside the wrapper class thus I have the std::map in a variable.

My questions is how can pass this map back to JavaScript to accomplish the above.
Reply all
Reply to author
Forward
0 new messages