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?

Upvotes: 3

Views: 2987

Answers (4)

The following code did the trick

wchar_t path[1024] = L"gokulestás";

v8::String::New((uint16_t*)path, wcslen(path))

Upvotes: 0

Viswanadh
Viswanadh

Reputation: 19

No it doesn't have unicode support, the above solution is fine.

Upvotes: 0

sheltond
sheltond

Reputation: 1937

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)

Upvotes: 3

nob
nob

Reputation: 1414

Unicode just maps Characters to Number. What you need is proper encoding, like UTF8 or UTF-16.

V8 seems to support UTF-8 (v8::String::WriteUtf8) and a not further described 16bit type (Write). I would give it a try and write some UTF-16 into it.

In unicode applications, windows stores UTF-16 in std::wstring. Maybe you try something like

std::wstring yourString;
v8::String::New (yourString.c_str());

Upvotes: 0

Related Questions