BililiveRecorder/BililiveRecorder.Core/Scripting/Runtime/JintDns.cs

47 lines
1.2 KiB
C#
Raw Normal View History

2022-05-12 17:29:34 +08:00
using System;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
2022-05-11 00:20:57 +08:00
using Jint;
2022-05-12 17:29:34 +08:00
using Jint.Native;
2022-05-11 00:20:57 +08:00
using Jint.Native.Object;
2022-05-12 17:29:34 +08:00
using Jint.Runtime;
2023-01-14 22:43:29 +08:00
using Jint.Runtime.Descriptors;
2022-05-12 17:29:34 +08:00
using Jint.Runtime.Interop;
2022-05-11 00:20:57 +08:00
namespace BililiveRecorder.Core.Scripting.Runtime
{
internal class JintDns : ObjectInstance
2022-05-11 00:20:57 +08:00
{
public JintDns(Engine engine) : base(engine)
{
}
2022-05-12 17:29:34 +08:00
protected override void Initialize()
{
Add("lookup", this.Lookup);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Add(string name, Func<JsValue, JsValue[], JsValue> func)
{
2023-01-14 22:43:29 +08:00
this.FastSetProperty(name, new PropertyDescriptor(new ClrFunctionInstance(this._engine, name, func), false, false, false));
2022-05-12 17:29:34 +08:00
}
}
private JsValue Lookup(JsValue thisObject, JsValue[] arguments)
{
string[] result;
try
{
result = Dns.GetHostAddresses(arguments.At(0).AsString()).Select(x => x.ToString()).ToArray();
}
catch (Exception)
{
result = Array.Empty<string>();
}
return FromObject(this._engine, result);
}
2022-05-11 00:20:57 +08:00
}
}