slice.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/slice.h>
  19. #include <grpc/support/alloc.h>
  20. #include <nan.h>
  21. #include <node.h>
  22. #include "slice.h"
  23. namespace grpc {
  24. namespace node {
  25. using Nan::Persistent;
  26. using v8::Local;
  27. using v8::String;
  28. using v8::Value;
  29. namespace {
  30. void SliceFreeCallback(char *data, void *hint) {
  31. grpc_slice *slice = reinterpret_cast<grpc_slice *>(hint);
  32. grpc_slice_unref(*slice);
  33. delete slice;
  34. }
  35. void string_destroy_func(void *user_data) {
  36. delete reinterpret_cast<Nan::Utf8String *>(user_data);
  37. }
  38. void buffer_destroy_func(void *user_data) {
  39. delete reinterpret_cast<PersistentValue *>(user_data);
  40. }
  41. } // namespace
  42. grpc_slice CreateSliceFromString(const Local<String> source) {
  43. Nan::HandleScope scope;
  44. Nan::Utf8String *utf8_value = new Nan::Utf8String(source);
  45. return grpc_slice_new_with_user_data(**utf8_value, source->Length(),
  46. string_destroy_func, utf8_value);
  47. }
  48. grpc_slice CreateSliceFromBuffer(const Local<Value> source) {
  49. // Prerequisite: ::node::Buffer::HasInstance(source)
  50. Nan::HandleScope scope;
  51. return grpc_slice_new_with_user_data(
  52. ::node::Buffer::Data(source), ::node::Buffer::Length(source),
  53. buffer_destroy_func, new PersistentValue(source));
  54. }
  55. Local<String> CopyStringFromSlice(const grpc_slice slice) {
  56. Nan::EscapableHandleScope scope;
  57. if (GRPC_SLICE_LENGTH(slice) == 0) {
  58. return scope.Escape(Nan::EmptyString());
  59. }
  60. return scope.Escape(
  61. Nan::New<String>(const_cast<char *>(reinterpret_cast<const char *>(
  62. GRPC_SLICE_START_PTR(slice))),
  63. GRPC_SLICE_LENGTH(slice))
  64. .ToLocalChecked());
  65. }
  66. Local<Value> CreateBufferFromSlice(const grpc_slice slice) {
  67. Nan::EscapableHandleScope scope;
  68. grpc_slice *slice_ptr = new grpc_slice;
  69. *slice_ptr = grpc_slice_ref(slice);
  70. return scope.Escape(
  71. Nan::NewBuffer(
  72. const_cast<char *>(
  73. reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(*slice_ptr))),
  74. GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr)
  75. .ToLocalChecked());
  76. }
  77. } // namespace node
  78. } // namespace grpc