-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add encoding_methods with fast api
- Loading branch information
Showing
9 changed files
with
119 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "env-inl.h" | ||
#include "node.h" | ||
#include "node_external_reference.h" | ||
#include "node_internals.h" | ||
#include "util-inl.h" | ||
#include "v8-fast-api-calls.h" | ||
#include "v8.h" | ||
|
||
namespace node { | ||
|
||
using v8::ArrayBuffer; | ||
using v8::BackingStore; | ||
using v8::CFunction; | ||
using v8::Context; | ||
using v8::FastApiTypedArray; | ||
using v8::FastOneByteString; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::String; | ||
using v8::Uint8Array; | ||
using v8::Value; | ||
|
||
namespace encoding_methods { | ||
|
||
void EncodeUtf8(const v8::FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
Isolate* isolate = env->isolate(); | ||
CHECK_GE(args.Length(), 2); | ||
CHECK(args[0]->IsString()); | ||
CHECK(args[1]->IsUint8Array()); | ||
|
||
Local<String> str = args[0].As<String>(); | ||
Local<Uint8Array> out = args[1].As<Uint8Array>(); | ||
Local<ArrayBuffer> buf = out->Buffer(); | ||
size_t out_length = out->ByteLength(); | ||
char* write_result = static_cast<char*>(buf->Data()) + out->ByteOffset(); | ||
|
||
str->WriteUtf8(isolate, | ||
write_result, | ||
out_length, | ||
nullptr, | ||
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8); | ||
} | ||
|
||
void latin1_to_utf8(const char* str, char* c) { | ||
for (; *str; ++str) { | ||
if (*str & 0x80) { | ||
*c++ = *str; | ||
} else { | ||
*c++ = (char)(0xc0 | (unsigned)*str >> 6); | ||
*c++ = (char)(0x80 | (*str & 0x3f)); | ||
} | ||
} | ||
*c++ = '\0'; | ||
} | ||
|
||
void FastEncodeUtf8(Local<Value> receiver, | ||
const FastOneByteString& source, | ||
const FastApiTypedArray<uint8_t>& output) { | ||
uint8_t* storage; | ||
auto is_available = output.getStorageIfAligned(&storage); | ||
CHECK(is_available); | ||
latin1_to_utf8(source.data, reinterpret_cast<char*>(storage)); | ||
} | ||
|
||
CFunction fast_encode_utf8_(CFunction::Make(FastEncodeUtf8)); | ||
|
||
static void Initialize(Local<Object> target, | ||
Local<Value> unused, | ||
Local<Context> context, | ||
void* priv) { | ||
SetFastMethod(context, target, "encodeUtf8", EncodeUtf8, &fast_encode_utf8_); | ||
} | ||
|
||
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
registry->Register(EncodeUtf8); | ||
registry->Register(FastEncodeUtf8); | ||
registry->Register(fast_encode_utf8_.GetTypeInfo()); | ||
} | ||
|
||
} // namespace encoding_methods | ||
} // namespace node | ||
|
||
NODE_BINDING_CONTEXT_AWARE_INTERNAL(encoding_methods, | ||
node::encoding_methods::Initialize) | ||
NODE_BINDING_EXTERNAL_REFERENCE( | ||
encoding_methods, node::encoding_methods::RegisterExternalReferences) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters