Skip to content

Commit

Permalink
Upgrade Library to support ABI.
Browse files Browse the repository at this point in the history
  • Loading branch information
TeknoPT committed Sep 26, 2023
1 parent f958b09 commit 46d3810
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
40 changes: 38 additions & 2 deletions Library/src/CodeGen/Libraries.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Phantasma.Business.Blockchain.Contracts.Native;
using Phantasma.Core.Domain.Contract;
using Phantasma.Core.Domain.Contract.Enums;
using Phantasma.Core.Domain.Serializer;
using Phantasma.Core.Domain.VM;
using Phantasma.Core.Domain.VM.Enums;
using Phantasma.Core.Numerics;
using Phantasma.Tomb.AST;
Expand Down Expand Up @@ -32,7 +34,7 @@ public void ImportLibrary(string name)
public static string[] AvailableLibraries = new[] {
"Call", "Runtime", "Math","Token", "NFT", "Organization", "Oracle", "Storage", "Contract", "Array",
"Leaderboard", "Market", "Account", "Crowdsale", "Stake", "Governance", "Relay", "Mail",
"Time", "Task", "UID", "Map", "List", "String", "Bytes", "Decimal", "Enum", "Address", "Module", FormatLibraryName };
"Time", "Task", "UID", "Map", "List", "String", "Bytes", "Decimal", "Enum", "Address", "Module", "ABI", FormatLibraryName };

public const string FormatLibraryName = "Format";

Expand Down Expand Up @@ -102,6 +104,39 @@ public static LibraryDeclaration LoadLibrary(string name, Scope scope, ModuleKin

switch (name)
{
case "ABI":
{
libDecl.AddMethod("getMethod", MethodImplementationType.Custom, VarKind.Bytes, new[] { new MethodParameter("target", VarKind.Module), new MethodParameter("method", VarKind.String) }).
SetPreCallback((output, scope, expr) =>
{
var reg = Compiler.Instance.AllocRegister(output, expr);
var module = expr.arguments[0].AsLiteral<Module>();
var methodName = expr.arguments[1].AsLiteral<string>();
var method = module.abi.FindMethod(methodName);
if (method == null)
{
throw new CompilerException($"Cannot find method '{methodName}' in module '{module.Name}'");
}
var vmObj = VMObject.FromStruct(method);
var hexString = vmObj.Serialize();
output.AppendLine(expr, $"LOAD {reg} 0x{hexString}");
output.AppendLine(expr, $"UNPACK {reg} {reg}");
return reg;
});

libDecl.AddMethod("hasMethod", MethodImplementationType.Custom, VarKind.Bool, new[] { new MethodParameter("target", VarKind.Module), new MethodParameter("method", VarKind.String) }).
SetPreCallback((output, scope, expr) =>
{
var reg = Compiler.Instance.AllocRegister(output, expr);
var module = expr.arguments[0].AsLiteral<Module>();
var methodName = expr.arguments[1].AsLiteral<string>();

var hasMethod = module.abi.HasMethod(methodName);
output.AppendLine(expr, $"LOAD {reg} {hasMethod}");
return reg;
});
break;
}
case "Module":
libDecl.AddMethod("getScript", MethodImplementationType.Custom, VarKind.Bytes, new[] { new MethodParameter("target", VarKind.Module) }).
SetPreCallback((output, scope, expr) =>
Expand Down Expand Up @@ -466,7 +501,7 @@ public static LibraryDeclaration LoadLibrary(string name, Scope scope, ModuleKin
break;

case "Token":
libDecl.AddMethod("create", MethodImplementationType.ExtCall, VarKind.None, new[] { new MethodParameter("from", VarKind.Address), new MethodParameter("symbol", VarKind.String), new MethodParameter("name", VarKind.String), new MethodParameter("maxSupply", VarKind.Number), new MethodParameter("decimals", VarKind.Number), new MethodParameter("flags", VarType.Find(VarKind.Enum, "TokenFlags")), new MethodParameter("script", VarKind.Bytes), new MethodParameter("abi", VarKind.Bytes) }).SetAlias("Nexus.CreateToken");
libDecl.AddMethod("create", MethodImplementationType.ExtCall, VarKind.None, new[] { new MethodParameter("from", VarKind.Address), new MethodParameter("script", VarKind.Bytes), new MethodParameter("abi bytes", VarKind.Bytes) }).SetAlias("Nexus.CreateToken");
libDecl.AddMethod("exists", MethodImplementationType.ExtCall, VarKind.Bool, new[] { new MethodParameter("symbol", VarKind.String) }).SetAlias("Runtime.TokenExists");
libDecl.AddMethod("getDecimals", MethodImplementationType.ExtCall, VarKind.Number, new[] { new MethodParameter("symbol", VarKind.String) }).SetAlias("Runtime.GetTokenDecimals");
libDecl.AddMethod("getFlags", MethodImplementationType.ExtCall, VarType.Find(VarKind.Enum, "TokenFlag"), new[] { new MethodParameter("symbol", VarKind.String) }).SetAlias("Runtime.GetTokenFlags");
Expand Down Expand Up @@ -766,6 +801,7 @@ public static LibraryDeclaration LoadLibrary(string name, Scope scope, ModuleKin
libDecl.AddMethod("getUserDomain", MethodImplementationType.ContractCall, VarKind.String, new[] { new MethodParameter("target", VarKind.Address) }).SetContract(contract).SetAlias(nameof(MailContract.GetUserDomain));
break;
}

default:
return FindExternalLibrary(name, scope, moduleKind);
}
Expand Down
8 changes: 8 additions & 0 deletions Library/src/Compilers/TombLangCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ private void InitStructs()
new StructField("Symbol", VarKind.String),
new StructField("Value", VarKind.Number)
});

CreateStruct("ContractMethod", new []
{
new StructField("name", VarKind.String),
new StructField("returnType", VarKind.Enum),
//new StructField("parameters", VarKind.Array),
new StructField("offset", VarKind.Number),
});
}


Expand Down

0 comments on commit 46d3810

Please sign in to comment.