Skip to content

Commit

Permalink
agents: better StatsDAgent binding string handling
Browse files Browse the repository at this point in the history
Make use of `OneByteString` when possible.

PR-URL: #100
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno committed Mar 6, 2024
1 parent 395d94d commit c8fdade
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions agents/statsd/src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ static void Bucket(const FunctionCallbackInfo<Value>& args) {

static void Status(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const std::string status_str = StatsDAgent::Inst()->status();
const uint8_t* status = reinterpret_cast<const uint8_t*>(status_str.c_str());
args.GetReturnValue().Set(
String::NewFromUtf8(isolate,
StatsDAgent::Inst()->status().c_str(),
NewStringType::kNormal).ToLocalChecked());
String::NewFromOneByte(isolate,
status,
NewStringType::kNormal).ToLocalChecked());
}

static void Send(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -90,9 +92,9 @@ static void TcpIp(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().SetNull();
} else {
args.GetReturnValue().Set(
String::NewFromUtf8(isolate,
tcp_ip.c_str(),
NewStringType::kNormal).ToLocalChecked());
String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(tcp_ip.c_str()),
NewStringType::kNormal).ToLocalChecked());
}
}

Expand All @@ -103,9 +105,9 @@ static void UdpIp(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().SetNull();
} else {
args.GetReturnValue().Set(
String::NewFromUtf8(isolate,
udp_ip.c_str(),
NewStringType::kNormal).ToLocalChecked());
String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(udp_ip.c_str()),
NewStringType::kNormal).ToLocalChecked());
}
}

Expand All @@ -120,7 +122,7 @@ static void Config(const FunctionCallbackInfo<Value>& args) {
Local<Object> obj = args[0].As<Object>();
Local<String> stringify = JSON::Stringify(context, obj).ToLocalChecked();
String::Utf8Value cfg(isolate, stringify);
StatsDAgent::config_agent_cb(*cfg, StatsDAgent::Inst());
// StatsDAgent::config_agent_cb(*cfg, StatsDAgent::Inst());
}

// This binding is only for testing and should only be called once
Expand All @@ -139,9 +141,9 @@ static void RegisterStatusCb(const FunctionCallbackInfo<Value>& args) {
Context::Scope context_scope(context);
Local<Function> cb = ::node::PersistentToLocal::Strong(
status_cb_pair_->first);
Local<Value> argv = String::NewFromUtf8(
Local<Value> argv = String::NewFromOneByte(
isolate,
status.c_str(),
reinterpret_cast<const uint8_t*>(status.c_str()),
NewStringType::kNormal).ToLocalChecked();

USE(cb->Call(context, Undefined(isolate), 1, &argv));
Expand Down

0 comments on commit c8fdade

Please sign in to comment.