31
31
#include " util-inl.h"
32
32
33
33
#include " v8.h"
34
+ #include " v8-fast-api-calls.h"
35
+ #include " node_debug.h"
34
36
35
37
#include " brotli/decode.h"
36
38
#include " brotli/encode.h"
@@ -49,6 +51,7 @@ namespace node {
49
51
50
52
using v8::ArrayBuffer;
51
53
using v8::Context;
54
+ using v8::CFunction;
52
55
using v8::Function;
53
56
using v8::FunctionCallbackInfo;
54
57
using v8::FunctionTemplate;
@@ -1657,7 +1660,6 @@ T CallOnSequence(v8::Isolate* isolate, Local<Value> value, F callback) {
1657
1660
}
1658
1661
}
1659
1662
1660
- // TODO(joyeecheung): use fast API
1661
1663
static void CRC32 (const FunctionCallbackInfo<Value>& args) {
1662
1664
CHECK (args[0 ]->IsArrayBufferView () || args[0 ]->IsString ());
1663
1665
CHECK (args[1 ]->IsUint32 ());
@@ -1673,6 +1675,31 @@ static void CRC32(const FunctionCallbackInfo<Value>& args) {
1673
1675
args.GetReturnValue ().Set (result);
1674
1676
}
1675
1677
1678
+ static uint32_t FastCRC32 (v8::Local<v8::Value> receiver,
1679
+ v8::Local<v8::Value> data,
1680
+ uint32_t value,
1681
+ // NOLINTNEXTLINE(runtime/references)
1682
+ v8::FastApiCallbackOptions& options) {
1683
+ TRACK_V8_FAST_API_CALL (" zlib.crc32" );
1684
+ v8::HandleScope handle_scope (options.isolate );
1685
+ CHECK (data->IsArrayBufferView () || data->IsString ());
1686
+ if (data->IsArrayBufferView ()) {
1687
+ SPREAD_BUFFER_ARG (data, buf);
1688
+ return static_cast <uint32_t >(
1689
+ crc32 (value,
1690
+ reinterpret_cast <const Bytef*>(buf_data),
1691
+ buf_length));
1692
+ }
1693
+ v8::Local<v8::String> s = data.As <v8::String>();
1694
+ Utf8Value utf8 (options.isolate , s);
1695
+ return static_cast <uint32_t >(
1696
+ crc32 (value,
1697
+ reinterpret_cast <const Bytef*>(utf8.out ()),
1698
+ utf8.length ()));
1699
+ }
1700
+
1701
+ static CFunction fast_crc32_ (CFunction::Make(FastCRC32));
1702
+
1676
1703
void Initialize (Local<Object> target,
1677
1704
Local<Value> unused,
1678
1705
Local<Context> context,
@@ -1685,7 +1712,7 @@ void Initialize(Local<Object> target,
1685
1712
MakeClass<ZstdCompressStream>::Make (env, target, " ZstdCompress" );
1686
1713
MakeClass<ZstdDecompressStream>::Make (env, target, " ZstdDecompress" );
1687
1714
1688
- SetMethod (context, target, " crc32" , CRC32);
1715
+ SetFastMethodNoSideEffect (context, target, " crc32" , CRC32, &fast_crc32_ );
1689
1716
target->Set (env->context (),
1690
1717
FIXED_ONE_BYTE_STRING (env->isolate (), " ZLIB_VERSION" ),
1691
1718
FIXED_ONE_BYTE_STRING (env->isolate (), ZLIB_VERSION)).Check ();
@@ -1698,6 +1725,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1698
1725
MakeClass<ZstdCompressStream>::Make (registry);
1699
1726
MakeClass<ZstdDecompressStream>::Make (registry);
1700
1727
registry->Register (CRC32);
1728
+ registry->Register (fast_crc32_);
1701
1729
}
1702
1730
1703
1731
} // anonymous namespace
0 commit comments