Skip to content

Commit

Permalink
QuickJS generator improvements. (#1865)
Browse files Browse the repository at this point in the history
* Fixes to QuickJS marshaling.

* Handle more primitive types in `GetInfo`.

* Fix generator kind CLI option matching.

* Alias QuickJS generator kind to `quickjs`.

* General CLI code cleanups.

* Default to x64 platform over x86 for the CLI.

* Implement new Lua bindings file and commands for CLI tool.

* Fix QuickJS primitive type marshaling to take target sizes into account.

* Minor code cleanup.

* Avoid generating includes to units when generating the QuickJS module.

* Update file generation naming pattern for QuickJS files.

* Update QuickJS JS_GetOpaque and JS_GetAnyOpaque references to work with latest upstream.

* Update QuickJS runtime and support code to work with latest upstream.

* Avoid generating properties when generating QuickJS register code.

* Update QuickJS test suite to bootstrap its own QuickJS runtime.

* Update QuickJS test suite to use a Lua bindings definition file.

* Minor fixes to test header files.

* Fix C++ warning about return values for event invokes.

* Disable some non working tests for QuickJS.

* Enable QuickJS testing on CI.

* Fix shell color attributes for test scripts when under CI.

* WIP CI fixes.

* Fix warnings in QuickJS support runtime code.

* Use C99 designated initializers for all QuickJS class def members.

* Disable QuickJS CI steps on Windows.

* Clean up error handling for `JS_ToBool`.

* More QuickJS support code fixes.

* Rename Signal.cpp to QuickJS nomenclature.

* Fix QuickJS JS_ToBigUint call.

* Remove QuickJS test script verbosity.

* More CI fixes.

* Verbose build.

* Extension fix.
  • Loading branch information
tritao authored Sep 3, 2024
1 parent d82f78d commit 3f5fc99
Show file tree
Hide file tree
Showing 32 changed files with 415 additions and 200 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ jobs:
shell: bash
run: build/build.sh -platform $PLATFORM -build_only

- name: Test
- name: Test (.NET)
shell: bash
run: build/test.sh -platform $PLATFORM

- name: Build (QuickJS runtime)
shell: bash
run: tests/quickjs/bootstrap.sh
if: runner.os != 'Windows'

- name: Test (QuickJS)
shell: bash
run: tests/quickjs/test.sh
if: runner.os != 'Windows'

- name: Pack
shell: bash
run: build/build.sh prepack -platform $PLATFORM
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -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>
31 changes: 24 additions & 7 deletions src/CLI/CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,16 @@ static void HandleAdditionalArgument(string args, List<string> errorMessages)
{
bool searchQuery = args.IndexOf('*') >= 0 || args.IndexOf('?') >= 0;
if (searchQuery || Directory.Exists(args))
{
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.");
Expand All @@ -175,7 +182,8 @@ static void GetFilesFromPath(string path, List<string> errorMessages)

if (lastSeparatorPosition >= 0)
{
if (path.IndexOf('*', lastSeparatorPosition) >= lastSeparatorPosition || path.IndexOf('?', lastSeparatorPosition) >= lastSeparatorPosition)
if (path.IndexOf('*', lastSeparatorPosition) >= lastSeparatorPosition ||
path.IndexOf('?', lastSeparatorPosition) >= lastSeparatorPosition)
{
searchPattern = path.Substring(lastSeparatorPosition + 1);
path = path.Substring(0, lastSeparatorPosition);
Expand Down Expand Up @@ -204,7 +212,7 @@ static void GetFilesFromPath(string path, List<string> errorMessages)
}
}

static void GetGeneratorKind(string generator, List<string> errorMessages)
public static void GetGeneratorKind(string generator, List<string> errorMessages)
{
foreach (GeneratorKind generatorKind in GeneratorKind.Registered)
{
Expand All @@ -218,7 +226,7 @@ static void GetGeneratorKind(string generator, List<string> errorMessages)
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())
{
Expand All @@ -239,7 +247,7 @@ static void GetDestinationPlatform(string platform, List<string> errorMessages)
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())
{
Expand All @@ -257,7 +265,8 @@ static void GetDestinationArchitecture(string architecture, List<string> errorMe
return;
}

errorMessages.Add($"Unknown target architecture: {architecture}. Defaulting to {options.Architecture}");
errorMessages.Add($@"Unknown target architecture: {architecture}. \
Defaulting to {options.Architecture}");
}

static void PrintErrorMessages(List<string> errorMessages)
Expand All @@ -275,10 +284,18 @@ static void Main(string[] args)
{
PrintErrorMessages(errorMessages);

// Don't need to show the help since if ParseCommandLineArgs returns false the help has already been shown
// Don't need to show the help since if ParseCommandLineArgs returns false
// since the help has already been shown
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);
Expand Down
1 change: 1 addition & 0 deletions src/CLI/CppSharp.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

<ItemGroup>
<ProjectReference Include="..\Generator\CppSharp.Generator.csproj" />
<PackageReference Include="MoonSharp" />
</ItemGroup>
</Project>
31 changes: 18 additions & 13 deletions src/CLI/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,34 @@ public bool ValidateOptions(List<string> messages)
options.OutputDir = Path.Combine(Directory.GetCurrentDirectory(), "gen");
}

string moduleName;
if (options.HeaderFiles.Count == 1)
{
moduleName = Path.GetFileNameWithoutExtension(options.HeaderFiles.First());
}
else
{
var dir = Path.GetDirectoryName(options.HeaderFiles.First());
moduleName = new DirectoryInfo(dir).Name;
}

if (string.IsNullOrEmpty(options.OutputFileName))
options.OutputFileName = moduleName;
options.OutputFileName = GetModuleNameFromHeaderFiles();

if (string.IsNullOrEmpty(options.OutputNamespace))
options.OutputNamespace = moduleName;
options.OutputNamespace = GetModuleNameFromHeaderFiles();

if (options.IncludeDirs.Count == 0)
options.IncludeDirs.Add(Path.GetDirectoryName(options.HeaderFiles.First()));

SetupTargetTriple();

return true;

string GetModuleNameFromHeaderFiles()
{
string moduleName;
if (options.HeaderFiles.Count == 1)
{
moduleName = Path.GetFileNameWithoutExtension(options.HeaderFiles.First());
}
else
{
var dir = Path.GetDirectoryName(options.HeaderFiles.First());
moduleName = new DirectoryInfo(dir).Name;
}

return moduleName;
}
}

public void Setup(Driver driver)
Expand Down
85 changes: 85 additions & 0 deletions src/CLI/LuaContext.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
4 changes: 3 additions & 1 deletion src/CLI/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ enum TargetArchitecture

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>();
Expand All @@ -37,7 +39,7 @@ class Options

public TargetPlatform? Platform { get; set; }

public TargetArchitecture Architecture { get; set; } = TargetArchitecture.x86;
public TargetArchitecture Architecture { get; set; } = TargetArchitecture.x64;

public GeneratorKind Kind { get; set; } = GeneratorKind.CSharp;

Expand Down
7 changes: 6 additions & 1 deletion src/Generator/Extensions/PrimitiveTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static (uint Width, uint Alignment) GetInfo(this PrimitiveType primitive,

switch (primitive)
{
case PrimitiveType.Void:
case PrimitiveType.Null:
case PrimitiveType.String:
return (0, 0);

case PrimitiveType.Bool:
return (targetInfo.BoolWidth, targetInfo.BoolAlign);

Expand Down Expand Up @@ -97,7 +102,7 @@ public static (uint Width, uint Alignment) GetInfo(this PrimitiveType primitive,
return (targetInfo.PointerWidth, targetInfo.PointerAlign);

default:
throw new NotImplementedException();
throw new Exception($"Not implemented for {primitive}");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/GeneratorKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public bool IsCLIOptionMatch(string cliOption)
{
return false;
}
return CLIOptions.Any(cliOption.Contains);
return CLIOptions.Any(option => option == cliOption);
}

public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2)
Expand Down Expand Up @@ -134,7 +134,7 @@ public override string ToString()
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter));

public const string QuickJS_ID = "QuickJS";
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), typeof(QuickJSTypePrinter), new[] { "qjs" });
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), typeof(QuickJSTypePrinter), new[] { "quickjs", "qjs" });

public const string NAPI_ID = "NAPI";
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), typeof(NAPITypePrinter), new[] { "napi" });
Expand Down
16 changes: 13 additions & 3 deletions src/Generator/Generators/QuickJS/QuickJSGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public class QuickJSGenerator : CppGenerator
{
public QuickJSGenerator(BindingContext context) : base(context)
{
if (context.Options.GenerateName == null)
{
context.Options.GenerateName = (unit) =>
{
if (unit.FileName == "premake5.lua")
return unit.FileNameWithoutExtension;
else
return $"{unit.Module.LibraryName}_JS_{unit.FileNameWithoutExtension}";
};
}
}

public override List<GeneratorOutput> Generate()
Expand Down Expand Up @@ -45,8 +55,8 @@ public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
{
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);
Expand All @@ -65,7 +75,7 @@ public override GeneratorOutput GenerateModule(Module module)
{
TranslationUnit = new TranslationUnit
{
FilePath = $"{module.LibraryName}_qjs_module.cpp",
FilePath = $"QJSModule.cpp",
Module = module
},
Outputs = new List<CodeGenerator> { moduleGen }
Expand Down
Loading

0 comments on commit 3f5fc99

Please sign in to comment.