47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using Qrakhen.Qamp.Core.Execution;
|
|
using System.Text;
|
|
|
|
namespace Qrakhen.Qamp.Core.Values.Objects;
|
|
|
|
public class Function(string name, Segment segment, int outerCount, int argumentCount) : Obj(ValueType.Function)
|
|
{
|
|
public readonly string Name = name;
|
|
public Segment Segment = segment;
|
|
public readonly int OuterCount = outerCount;
|
|
public readonly int ArgumentCount = argumentCount;
|
|
|
|
public override string ToString() => $"{Name}()";
|
|
|
|
public byte[] Serialize(bool topLevel = false)
|
|
{
|
|
List<byte> result =
|
|
[
|
|
.. string.IsNullOrEmpty(Name) ? [] : Encoding.ASCII.GetBytes(Name),
|
|
0,
|
|
(byte)ArgumentCount,
|
|
.. OuterCount.GetBytes(),
|
|
.. Segment.Serialize()
|
|
];
|
|
if (topLevel) {
|
|
result.AddRange([0xFF, 0xF0]);
|
|
result.AddRange(String.SerializeStrings());
|
|
result.AddRange([0x0F, 0xFF, 0xFF, 0xF1]);
|
|
result.AddRange(Ptr.SerializeFunctions());
|
|
result.AddRange([0x0F, 0xFF]);
|
|
}
|
|
return result.ToArray();
|
|
}
|
|
}
|
|
|
|
// local c# function, so compiled inside the binary already
|
|
// cheap shortcut for global methods for now.
|
|
public class NativeFunction(string name, Func<Value[], Value> target, params string[] arguments) : Obj(ValueType.Native)
|
|
{
|
|
public string Name { get; } = name;
|
|
public Func<Value[], Value> Target { get; } = target;
|
|
|
|
public string[] Arguments { get; } = arguments;
|
|
|
|
public override string ToString() => $"[NativeFunction <{Name}> ({string.Join(", ", Arguments)})]";
|
|
}
|