Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickJS generator improvements. #1865

Merged
merged 33 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
de42b4c
Fixes to QuickJS marshaling.
tritao Feb 4, 2023
6712a6a
Handle more primitive types in `GetInfo`.
tritao Sep 3, 2024
343c0aa
Fix generator kind CLI option matching.
tritao Sep 3, 2024
e962ea4
Alias QuickJS generator kind to `quickjs`.
tritao Sep 3, 2024
aaaffff
General CLI code cleanups.
tritao Sep 3, 2024
42d2b8a
Default to x64 platform over x86 for the CLI.
tritao Sep 3, 2024
57ed101
Implement new Lua bindings file and commands for CLI tool.
tritao Sep 3, 2024
642379a
Fix QuickJS primitive type marshaling to take target sizes into account.
tritao Sep 3, 2024
3b78214
Minor code cleanup.
tritao Sep 3, 2024
8f41d96
Avoid generating includes to units when generating the QuickJS module.
tritao Sep 3, 2024
a106f8b
Update file generation naming pattern for QuickJS files.
tritao Sep 3, 2024
e649400
Update QuickJS JS_GetOpaque and JS_GetAnyOpaque references to work wi…
tritao Sep 3, 2024
5e405d9
Update QuickJS runtime and support code to work with latest upstream.
tritao Sep 3, 2024
2bef566
Avoid generating properties when generating QuickJS register code.
tritao Sep 3, 2024
94f1cb5
Update QuickJS test suite to bootstrap its own QuickJS runtime.
tritao Sep 3, 2024
92b1858
Update QuickJS test suite to use a Lua bindings definition file.
tritao Sep 3, 2024
2871b12
Minor fixes to test header files.
tritao Sep 3, 2024
359df54
Fix C++ warning about return values for event invokes.
tritao Sep 3, 2024
c894d1c
Disable some non working tests for QuickJS.
tritao Sep 3, 2024
b20b175
Enable QuickJS testing on CI.
tritao Sep 3, 2024
c27d0ef
Fix shell color attributes for test scripts when under CI.
tritao Sep 3, 2024
6f71182
WIP CI fixes.
tritao Sep 3, 2024
4aa0279
Fix warnings in QuickJS support runtime code.
tritao Sep 3, 2024
4a74806
Use C99 designated initializers for all QuickJS class def members.
tritao Sep 3, 2024
e5a1942
Disable QuickJS CI steps on Windows.
tritao Sep 3, 2024
f8ff3a5
Clean up error handling for `JS_ToBool`.
tritao Sep 3, 2024
260f120
More QuickJS support code fixes.
tritao Sep 3, 2024
54e809e
Rename Signal.cpp to QuickJS nomenclature.
tritao Sep 3, 2024
5581351
Fix QuickJS JS_ToBigUint call.
tritao Sep 3, 2024
9d91616
Remove QuickJS test script verbosity.
tritao Sep 3, 2024
7d1af87
More CI fixes.
tritao Sep 3, 2024
cad11b6
Verbose build.
tritao Sep 3, 2024
7a77d8f
Extension fix.
tritao Sep 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading