Search code examples
v8unicode-string

Does V8 have Unicode support?


I'm using v8 to use JavaScript in native(c++) code. To call a Javascript function I need to convert all the parameters to v8 data types. For eg: Code to convert char* to v8 data type

char* value;
...
v8::String::New(value);

Now, I need to pass unicode chars(wchar_t) to JavaScript.

First of all does v8 supports Unicode chars? If yes, how to convert wchar_t/std::wstring to v8 data type?


Solution

  • I'm not sure if this was the case at the time this question was asked, but at the moment the V8 API has a number of functions which support UTF-8, UTF-16 and Latin-1 encoded text:

    https://github.com/v8/v8/blob/master/include/v8.h

    The relevant functions to create new string objects are:

    • String::NewFromUtf8 (UTF-8 encoded, obviously)
    • String::NewFromOneByte (Latin-1 encoded)
    • String::NewFromTwoByte (UTF-16 encoded)

    Alternatively, you can avoid copying the string data and construct a V8 string object that refers to existing data (whose lifecycle you control):

    • String::NewExternalOneByte (Latin-1 encoded)
    • String::NewExternalTwoByte (UTF-16 encoded)