forked from BabylonJS/JsRuntimeHost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEncoder.cpp
More file actions
72 lines (62 loc) · 2.12 KB
/
Copy pathTextEncoder.cpp
File metadata and controls
72 lines (62 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <Babylon/Polyfills/TextEncoder.h>
#include <napi/napi.h>
#include <cstring>
#include <string>
namespace
{
class TextEncoder final : public Napi::ObjectWrap<TextEncoder>
{
public:
static void Initialize(Napi::Env env)
{
Napi::HandleScope scope{env};
static constexpr auto JS_TEXTENCODER_CONSTRUCTOR_NAME = "TextEncoder";
if (env.Global().Get(JS_TEXTENCODER_CONSTRUCTOR_NAME).IsUndefined())
{
Napi::Function func = DefineClass(
env,
JS_TEXTENCODER_CONSTRUCTOR_NAME,
{
InstanceAccessor("encoding", &TextEncoder::Encoding, nullptr),
InstanceMethod("encode", &TextEncoder::Encode),
});
env.Global().Set(JS_TEXTENCODER_CONSTRUCTOR_NAME, func);
}
}
explicit TextEncoder(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<TextEncoder>{info}
{
// The TextEncoder constructor takes no arguments. The encoding is
// always UTF-8 per the WHATWG Encoding Standard.
}
private:
Napi::Value Encoding(const Napi::CallbackInfo& info)
{
return Napi::String::New(info.Env(), "utf-8");
}
// encode(input = "") - returns a Uint8Array containing the UTF-8 bytes.
// Per spec, undefined input defaults to the empty string (not "undefined").
Napi::Value Encode(const Napi::CallbackInfo& info)
{
auto env = info.Env();
std::string utf8;
if (info.Length() > 0 && !info[0].IsUndefined())
{
utf8 = info[0].ToString().Utf8Value();
}
auto bytes = Napi::Uint8Array::New(env, utf8.size());
if (!utf8.empty())
{
std::memcpy(bytes.Data(), utf8.data(), utf8.size());
}
return bytes;
}
};
}
namespace Babylon::Polyfills::TextEncoder
{
void BABYLON_API Initialize(Napi::Env env)
{
::TextEncoder::Initialize(env);
}
}