MCPcopy Create free account
hub / github.com/BabylonJS/BabylonNative / JSString

Class JSString

Dependencies/napi/source/js_native_api_JavaScriptCore.cc:20–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18
19namespace {
20 class JSString {
21 public:
22 JSString(const JSString&) = delete;
23
24 JSString(JSString&& other) {
25 _string = other._string;
26 other._string = nullptr;
27 }
28
29 JSString(const char* string, size_t length = NAPI_AUTO_LENGTH)
30 : _string{JSStringCreateWithUTF8CString(length == NAPI_AUTO_LENGTH ? string : std::string(string, length).data())} {
31 }
32
33 JSString(const JSChar* string, size_t length = NAPI_AUTO_LENGTH)
34 : _string{JSStringCreateWithCharacters(string, length == NAPI_AUTO_LENGTH ? std::char_traits<JSChar>::length(string) : length)} {
35 }
36
37 ~JSString() {
38 if (_string != nullptr) {
39 JSStringRelease(_string);
40 }
41 }
42
43 static JSString Attach(JSStringRef string) {
44 return {string};
45 }
46
47 operator JSStringRef() const {
48 return _string;
49 }
50
51 size_t Length() const {
52 return JSStringGetLength(_string);
53 }
54
55 size_t LengthUTF8() const {
56 std::vector<char> buffer(JSStringGetMaximumUTF8CStringSize(_string));
57 return JSStringGetUTF8CString(_string, buffer.data(), buffer.size()) - 1;
58 }
59
60 size_t LengthLatin1() const {
61 // Latin1 has the same length as Unicode.
62 return JSStringGetLength(_string);
63 }
64
65 void CopyTo(JSChar* buf, size_t bufsize, size_t* result) const {
66 size_t length{JSStringGetLength(_string)};
67 const JSChar* chars{JSStringGetCharactersPtr(_string)};
68 size_t size{std::min(length, bufsize - 1)};
69 std::memcpy(buf, chars, size);
70 buf[size] = 0;
71 if (result != nullptr) {
72 *result = size;
73 }
74 }
75
76 void CopyToUTF8(char* buf, size_t bufsize, size_t* result) const {
77 size_t size{JSStringGetUTF8CString(_string, buf, bufsize)};

Callers 15

napi_set_error_codeFunction · 0.85
CreateMethod · 0.85
CreateMethod · 0.85
napi_set_named_propertyFunction · 0.85
napi_has_named_propertyFunction · 0.85
napi_get_named_propertyFunction · 0.85
napi_get_array_lengthFunction · 0.85
napi_create_string_utf8Function · 0.85
napi_create_string_utf16Function · 0.85
napi_throw_errorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected