Browse Source

Implement new Lua bindings file and commands for CLI tool.

pull/1865/head
Joao Matos 11 months ago
parent
commit
57ed101bfe
  1. 1
      Directory.Packages.props
  2. 20
      src/CLI/CLI.cs
  3. 1
      src/CLI/CppSharp.CLI.csproj
  4. 85
      src/CLI/LuaContext.cs
  5. 2
      src/CLI/Options.cs
  6. 4
      src/Generator/Generators/QuickJS/QuickJSGenerator.cs

1
Directory.Packages.props

@ -4,5 +4,6 @@ @@ -4,5 +4,6 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageVersion Include="NUnit" Version="3.13.2" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageVersion Include="MoonSharp" Version="2.0.0" />
</ItemGroup>
</Project>

20
src/CLI/CLI.cs

@ -156,7 +156,12 @@ namespace CppSharp @@ -156,7 +156,12 @@ namespace CppSharp
GetFilesFromPath(args, errorMessages);
}
else if (File.Exists(args))
options.HeaderFiles.Add(args);
{
if (Path.GetExtension(args) == ".lua")
options.LuaBindingsFiles.Add(args);
else
options.HeaderFiles.Add(args);
}
else
{
errorMessages.Add($"File '{args}' could not be found.");
@ -207,7 +212,7 @@ namespace CppSharp @@ -207,7 +212,7 @@ namespace CppSharp
}
}
static void GetGeneratorKind(string generator, List<string> errorMessages)
public static void GetGeneratorKind(string generator, List<string> errorMessages)
{
foreach (GeneratorKind generatorKind in GeneratorKind.Registered)
{
@ -221,7 +226,7 @@ namespace CppSharp @@ -221,7 +226,7 @@ namespace CppSharp
errorMessages.Add($"Unknown generator kind: {generator}.");
}
static void GetDestinationPlatform(string platform, List<string> errorMessages)
public static void GetDestinationPlatform(string platform, List<string> errorMessages)
{
switch (platform.ToLower())
{
@ -242,7 +247,7 @@ namespace CppSharp @@ -242,7 +247,7 @@ namespace CppSharp
errorMessages.Add($"Unknown target platform: {platform}. Defaulting to {options.Platform}");
}
static void GetDestinationArchitecture(string architecture, List<string> errorMessages)
public static void GetDestinationArchitecture(string architecture, List<string> errorMessages)
{
switch (architecture.ToLower())
{
@ -284,6 +289,13 @@ namespace CppSharp @@ -284,6 +289,13 @@ namespace CppSharp
return;
}
var luaContext = new LuaContext(options, errorMessages);
foreach (var luaFile in options.LuaBindingsFiles)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(luaFile));
luaContext.LoadFile(luaFile);
}
var gen = new Generator(options);
var validOptions = gen.ValidateOptions(errorMessages);

1
src/CLI/CppSharp.CLI.csproj

@ -5,5 +5,6 @@ @@ -5,5 +5,6 @@
<ItemGroup>
<ProjectReference Include="..\Generator\CppSharp.Generator.csproj" />
<PackageReference Include="MoonSharp" />
</ItemGroup>
</Project>

85
src/CLI/LuaContext.cs

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using CppSharp;
using MoonSharp.Interpreter;
class LuaContext
{
public Script script;
public Options options;
public List<string> errorMessages;
public string CurrentModule;
public LuaContext(Options options, List<string> errorMessages)
{
this.options = options;
this.errorMessages = errorMessages;
script = new Script(CoreModules.Basic | CoreModules.String |
CoreModules.Table | CoreModules.TableIterators);
script.Globals["generator"] = (string kind) =>
{
CLI.GetGeneratorKind(kind, errorMessages);
};
script.Globals["platform"] = (string platform) =>
{
CLI.GetDestinationPlatform(platform, errorMessages);
};
script.Globals["architecture"] = (string arch) =>
{
CLI.GetDestinationArchitecture(arch, errorMessages);
};
script.Globals["output"] = script.Globals["location"] = (string dir) =>
{
options.OutputDir = dir;
};
script.Globals["includedirs"] = (List<string> dirs) =>
{
foreach (var dir in dirs)
{
options.IncludeDirs.Add(dir);
}
};
script.Globals["module"] = (string name) =>
{
CurrentModule = name;
options.OutputFileName = name;
};
script.Globals["namespace"] = (string name) =>
{
options.OutputNamespace = name;
};
script.Globals["headers"] = (List<string> files) =>
{
foreach (var file in files)
{
options.HeaderFiles.Add(file);
}
};
}
public DynValue LoadFile(string luaFile)
{
var code = script.LoadFile(luaFile);
try
{
return code.Function.Call();
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error running {Path.GetFileName(luaFile)}:\n{ex.Message}");
return null;
}
}
}

2
src/CLI/Options.cs

@ -13,6 +13,8 @@ namespace CppSharp @@ -13,6 +13,8 @@ namespace CppSharp
class Options
{
public List<string> LuaBindingsFiles { get; } = new List<string>();
public List<string> HeaderFiles { get; } = new List<string>();
public List<string> IncludeDirs { get; } = new List<string>();

4
src/Generator/Generators/QuickJS/QuickJSGenerator.cs

@ -45,8 +45,8 @@ namespace CppSharp.Generators.C @@ -45,8 +45,8 @@ namespace CppSharp.Generators.C
{
var outputs = new List<CodeGenerator>();
var header = new QuickJSHeaders(Context, units);
outputs.Add(header);
// var header = new QuickJSHeaders(Context, units);
// outputs.Add(header);
var source = new QuickJSSources(Context, units);
outputs.Add(source);

Loading…
Cancel
Save