|
| 1 | +#include "env-inl.h" |
| 2 | +#include "node.h" |
| 3 | +#include "node_external_reference.h" |
| 4 | +#include "node_internals.h" |
| 5 | +#include "util-inl.h" |
| 6 | +#include "v8-fast-api-calls.h" |
| 7 | +#include "v8.h" |
| 8 | + |
| 9 | +#if defined(NODE_HAVE_I18N_SUPPORT) |
| 10 | +#include <unicode/utf8.h> |
| 11 | +#endif // NODE_HAVE_I18N_SUPPORT |
| 12 | + |
| 13 | +namespace node { |
| 14 | + |
| 15 | +using v8::ArrayBuffer; |
| 16 | +using v8::BackingStore; |
| 17 | +using v8::CFunction; |
| 18 | +using v8::Context; |
| 19 | +using v8::FastApiTypedArray; |
| 20 | +using v8::FastOneByteString; |
| 21 | +using v8::FunctionCallbackInfo; |
| 22 | +using v8::Isolate; |
| 23 | +using v8::Local; |
| 24 | +using v8::Object; |
| 25 | +using v8::String; |
| 26 | +using v8::Uint32Array; |
| 27 | +using v8::Uint8Array; |
| 28 | +using v8::Value; |
| 29 | + |
| 30 | +namespace encoding_methods { |
| 31 | + |
| 32 | +static void EncodeUtf8(const FunctionCallbackInfo<Value>& args) { |
| 33 | + Environment* env = Environment::GetCurrent(args); |
| 34 | + Isolate* isolate = env->isolate(); |
| 35 | + CHECK_GE(args.Length(), 1); |
| 36 | + CHECK(args[0]->IsString()); |
| 37 | + |
| 38 | + Local<String> str = args[0].As<String>(); |
| 39 | + size_t length = str->Utf8Length(isolate); |
| 40 | + |
| 41 | + Local<ArrayBuffer> ab; |
| 42 | + { |
| 43 | + NoArrayBufferZeroFillScope no_zero_fill_scope(env->isolate_data()); |
| 44 | + std::unique_ptr<BackingStore> bs = |
| 45 | + ArrayBuffer::NewBackingStore(isolate, length); |
| 46 | + |
| 47 | + CHECK(bs); |
| 48 | + |
| 49 | + str->WriteUtf8(isolate, |
| 50 | + static_cast<char*>(bs->Data()), |
| 51 | + -1, // We are certain that `data` is sufficiently large |
| 52 | + nullptr, |
| 53 | + String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8); |
| 54 | + |
| 55 | + ab = ArrayBuffer::New(isolate, std::move(bs)); |
| 56 | + } |
| 57 | + |
| 58 | + args.GetReturnValue().Set(Uint8Array::New(ab, 0, length)); |
| 59 | +} |
| 60 | + |
| 61 | +static void EncodeIntoUtf8(const FunctionCallbackInfo<Value>& args) { |
| 62 | + Environment* env = Environment::GetCurrent(args); |
| 63 | + Isolate* isolate = env->isolate(); |
| 64 | + CHECK_GE(args.Length(), 3); |
| 65 | + CHECK(args[0]->IsString()); |
| 66 | + CHECK(args[1]->IsUint8Array()); |
| 67 | + CHECK(args[2]->IsUint32Array()); |
| 68 | + |
| 69 | + Local<String> source = args[0].As<String>(); |
| 70 | + |
| 71 | + Local<Uint8Array> dest = args[1].As<Uint8Array>(); |
| 72 | + Local<ArrayBuffer> buf = dest->Buffer(); |
| 73 | + char* write_result = static_cast<char*>(buf->Data()) + dest->ByteOffset(); |
| 74 | + size_t dest_length = dest->ByteLength(); |
| 75 | + |
| 76 | + // results = [ read, written ] |
| 77 | + Local<Uint32Array> result_arr = args[2].As<Uint32Array>(); |
| 78 | + uint32_t* results = reinterpret_cast<uint32_t*>( |
| 79 | + static_cast<char*>(result_arr->Buffer()->Data()) + |
| 80 | + result_arr->ByteOffset()); |
| 81 | + |
| 82 | + int nchars; |
| 83 | + int written = source->WriteUtf8( |
| 84 | + isolate, |
| 85 | + write_result, |
| 86 | + dest_length, |
| 87 | + &nchars, |
| 88 | + String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8); |
| 89 | + results[0] = nchars; |
| 90 | + results[1] = written; |
| 91 | +} |
| 92 | + |
| 93 | +#if defined(NODE_HAVE_I18N_SUPPORT) |
| 94 | +static void FastEncodeIntoUtf8(Local<Value> receiver, |
| 95 | + const FastOneByteString& source, |
| 96 | + const FastApiTypedArray<uint8_t>& destination, |
| 97 | + const FastApiTypedArray<uint32_t>& result) { |
| 98 | + uint8_t* destination_data; |
| 99 | + CHECK(destination.getStorageIfAligned(&destination_data)); |
| 100 | + |
| 101 | + uint32_t* results; |
| 102 | + CHECK(result.getStorageIfAligned(&results)); |
| 103 | + |
| 104 | + size_t source_length = source.length; |
| 105 | + size_t destination_length = destination.length(); |
| 106 | + |
| 107 | + size_t read = 0; |
| 108 | + size_t offset = 0; |
| 109 | + UBool has_error = false; |
| 110 | + for (; read < source_length && offset < destination_length && !has_error; |
| 111 | + read++) { |
| 112 | + U8_APPEND(destination_data, |
| 113 | + offset, |
| 114 | + destination_length, |
| 115 | + source.data[read], |
| 116 | + has_error); |
| 117 | + } |
| 118 | + |
| 119 | + results[0] = has_error ? read - 1 : read; |
| 120 | + results[1] = offset; |
| 121 | +} |
| 122 | + |
| 123 | +CFunction fast_encode_into_utf8_(CFunction::Make(FastEncodeIntoUtf8)); |
| 124 | +#endif // NODE_HAVE_I18N_SUPPORT |
| 125 | + |
| 126 | +static void Initialize(Local<Object> target, |
| 127 | + Local<Value> unused, |
| 128 | + Local<Context> context, |
| 129 | + void* priv) { |
| 130 | + SetMethodNoSideEffect(context, target, "encodeUtf8", EncodeUtf8); |
| 131 | +#if defined(NODE_HAVE_I18N_SUPPORT) |
| 132 | + SetFastMethod(context, |
| 133 | + target, |
| 134 | + "encodeIntoUtf8", |
| 135 | + EncodeIntoUtf8, |
| 136 | + &fast_encode_into_utf8_); |
| 137 | +#else |
| 138 | + SetMethodNoSideEffect(context, target, "encodeIntoUtf8", EncodeIntoUtf8); |
| 139 | +#endif // NODE_HAVE_I18N_SUPPORT |
| 140 | +} |
| 141 | + |
| 142 | +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
| 143 | + registry->Register(EncodeUtf8); |
| 144 | + |
| 145 | + registry->Register(EncodeIntoUtf8); |
| 146 | + |
| 147 | +#if defined(NODE_HAVE_I18N_SUPPORT) |
| 148 | + registry->Register(FastEncodeIntoUtf8); |
| 149 | + registry->Register(fast_encode_into_utf8_.GetTypeInfo()); |
| 150 | +#endif // NODE_HAVE_I18N_SUPPORT |
| 151 | +} |
| 152 | + |
| 153 | +} // namespace encoding_methods |
| 154 | +} // namespace node |
| 155 | + |
| 156 | +NODE_BINDING_CONTEXT_AWARE_INTERNAL(encoding_methods, |
| 157 | + node::encoding_methods::Initialize) |
| 158 | +NODE_BINDING_EXTERNAL_REFERENCE( |
| 159 | + encoding_methods, node::encoding_methods::RegisterExternalReferences) |
0 commit comments