Skip to content

Commit 9788af4

Browse files
anonriglemire
andcommitted
src: replace idna functions with ada::idna
Co-authored-by: Daniel Lemire <[email protected]>
1 parent e2e3f5c commit 9788af4

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/internal/idna.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'use strict';
22

3-
const { domainToASCII, domainToUnicode } = require('internal/url');
4-
module.exports = { toASCII: domainToASCII, toUnicode: domainToUnicode };
3+
const { toASCII, toUnicode } = internalBinding('encoding_binding');
4+
module.exports = { toASCII, toUnicode };

src/encoding_binding.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "encoding_binding.h"
2+
#include "ada.h"
23
#include "env-inl.h"
34
#include "node_errors.h"
45
#include "node_external_reference.h"
@@ -193,6 +194,31 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
193194
args.GetReturnValue().Set(ret);
194195
}
195196

197+
void BindingData::ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args) {
198+
Environment* env = Environment::GetCurrent(args);
199+
CHECK_GE(args.Length(), 1);
200+
CHECK(args[0]->IsString());
201+
202+
Utf8Value input(env->isolate(), args[0]);
203+
auto out = ada::idna::to_ascii(input.ToStringView());
204+
printf("to_ascii_returns '%s' for '%s'\n",
205+
out.c_str(),
206+
input.ToString().c_str());
207+
args.GetReturnValue().Set(
208+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
209+
}
210+
211+
void BindingData::ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args) {
212+
Environment* env = Environment::GetCurrent(args);
213+
CHECK_GE(args.Length(), 1);
214+
CHECK(args[0]->IsString());
215+
216+
Utf8Value input(env->isolate(), args[0]);
217+
auto out = ada::idna::to_unicode(input.ToStringView());
218+
args.GetReturnValue().Set(
219+
String::NewFromUtf8(env->isolate(), out.c_str()).ToLocalChecked());
220+
}
221+
196222
void BindingData::Initialize(Local<Object> target,
197223
Local<Value> unused,
198224
Local<Context> context,
@@ -205,13 +231,17 @@ void BindingData::Initialize(Local<Object> target,
205231
SetMethod(context, target, "encodeInto", EncodeInto);
206232
SetMethodNoSideEffect(context, target, "encodeUtf8String", EncodeUtf8String);
207233
SetMethodNoSideEffect(context, target, "decodeUTF8", DecodeUTF8);
234+
SetMethodNoSideEffect(context, target, "toASCII", ToASCII);
235+
SetMethodNoSideEffect(context, target, "toUnicode", ToUnicode);
208236
}
209237

210238
void BindingData::RegisterTimerExternalReferences(
211239
ExternalReferenceRegistry* registry) {
212240
registry->Register(EncodeInto);
213241
registry->Register(EncodeUtf8String);
214242
registry->Register(DecodeUTF8);
243+
registry->Register(ToASCII);
244+
registry->Register(ToUnicode);
215245
}
216246

217247
} // namespace encoding_binding

src/encoding_binding.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class BindingData : public SnapshotableObject {
3232
static void EncodeUtf8String(const v8::FunctionCallbackInfo<v8::Value>& args);
3333
static void DecodeUTF8(const v8::FunctionCallbackInfo<v8::Value>& args);
3434

35+
static void ToASCII(const v8::FunctionCallbackInfo<v8::Value>& args);
36+
static void ToUnicode(const v8::FunctionCallbackInfo<v8::Value>& args);
37+
3538
static void Initialize(v8::Local<v8::Object> target,
3639
v8::Local<v8::Value> unused,
3740
v8::Local<v8::Context> context,

0 commit comments

Comments
 (0)