pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
886 stars 118 forks source link

map which contain a string as key fail to convert if the key is a number #177

Closed Wawha closed 2 years ago

Wawha commented 2 years ago

Take a std::map<std::string, double>. If you create a javascript map with this pair { "0": 3.1415 }, the conversion from v8 type to C++ map with fail, because the key "0" is store as number 0 in javascript. In my case, I have this problem because I create a C++ map with a string key (with a just number), return that map in a function bind in javascript, and then retrieve that value inside another function.

I try locally this fix inside convert<String, ...> inside convert.hpp, and it seems to work great:

    static bool is_valid(v8::Isolate*, v8::Local<v8::Value> value)
    {
        return !value.IsEmpty() && (value->IsString() || value->IsNumber());
    }

In case you prefer/require a Pull requests to fix that bug, I can try to do it with the right unit test.

pmed commented 2 years ago

Hi,

thanks for the issue discovering! I've fixed it as you've proposed.