1+ #include " env-inl.h"
2+ #include " node.h"
3+ #include " node_errors.h"
4+ #include " node_external_reference.h"
5+ #include " util-inl.h"
6+
7+ #include " simdutf.h"
8+
9+ namespace node {
10+
11+ using v8::ArrayBuffer;
12+ using v8::BackingStore;
13+ using v8::CFunction;
14+ using v8::Context;
15+ using v8::FastApiTypedArray;
16+ using v8::FunctionCallbackInfo;
17+ using v8::Isolate;
18+ using v8::Local;
19+ using v8::MaybeLocal;
20+ using v8::Object;
21+ using v8::String;
22+ using v8::Uint32Array;
23+ using v8::Uint8Array;
24+ using v8::Value;
25+
26+ // TODO(anonrig): Replace this with encoding when encoding enum is renamed.
27+ namespace encoding_methods {
28+
29+ static void IsAscii (const FunctionCallbackInfo<Value>& args) {
30+ CHECK_GE (args.Length (), 1 );
31+ CHECK (args[0 ]->IsArrayBuffer ());
32+ Local<ArrayBuffer> input = args[0 ].As <ArrayBuffer>();
33+ auto external_resource = static_cast <const char *>(input->Data ());
34+ args.GetReturnValue ().Set (
35+ simdutf::validate_ascii (external_resource, input->ByteLength ()));
36+ }
37+
38+ static void IsUtf8 (const FunctionCallbackInfo<Value>& args) {
39+ CHECK_GE (args.Length (), 1 );
40+ CHECK (args[0 ]->IsArrayBuffer ());
41+ Local<ArrayBuffer> input = args[0 ].As <ArrayBuffer>();
42+ auto external_resource = static_cast <const char *>(input->Data ());
43+ args.GetReturnValue ().Set (
44+ simdutf::validate_utf8 (external_resource, input->ByteLength ()));
45+ }
46+
47+ static void CountUtf8 (const FunctionCallbackInfo<Value>& args) {
48+ CHECK_GE (args.Length (), 1 );
49+ CHECK (args[0 ]->IsArrayBuffer ());
50+ Local<ArrayBuffer> input = args[0 ].As <ArrayBuffer>();
51+ auto external_resource = static_cast <const char *>(input->Data ());
52+ int count = simdutf::count_utf8 (external_resource, input->ByteLength ());
53+ args.GetReturnValue ().Set (count);
54+ }
55+
56+ static void Initialize (Local<Object> target,
57+ Local<Value> unused,
58+ Local<Context> context,
59+ void * priv) {
60+ SetMethodNoSideEffect (context, target, " isAscii" , IsAscii);
61+ SetMethodNoSideEffect (context, target, " isUtf8" , IsUtf8);
62+ SetMethodNoSideEffect (context, target, " countUtf8" , CountUtf8);
63+ }
64+
65+ void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
66+ registry->Register (IsAscii);
67+ registry->Register (IsUtf8);
68+ registry->Register (CountUtf8);
69+ }
70+
71+ } // namespace encoding_methods
72+ } // namespace node
73+
74+ NODE_BINDING_CONTEXT_AWARE_INTERNAL (encoding_methods,
75+ node::encoding_methods::Initialize)
76+ NODE_BINDING_EXTERNAL_REFERENCE(
77+ encoding_methods, node::encoding_methods::RegisterExternalReferences)
0 commit comments