From de42b4c9b28c764f67c516aaf02c3a321d28a4c3 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sat, 4 Feb 2023 18:52:04 +0000 Subject: [PATCH 01/33] Fixes to QuickJS marshaling. --- .../Generators/QuickJS/QuickJSMarshal.cs | 118 +++++++----------- 1 file changed, 42 insertions(+), 76 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 4937192bf..14b72b953 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -61,34 +61,22 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) var pointee = pointer.Pointee.Desugar(); - PrimitiveType primitive; var param = Context.Parameter; if (param != null && (param.IsOut || param.IsInOut) && - pointee.IsPrimitiveType(out primitive)) + pointee.IsPrimitiveType(out _)) { Context.Return.Write(Context.ReturnVarName); return true; } - if (pointee.IsPrimitiveType(out primitive)) + if (pointee.IsPrimitiveType(out _)) { - var returnVarName = Context.ReturnVarName; - - if (pointer.GetFinalQualifiedPointee().Qualifiers.IsConst != - Context.ReturnType.Qualifiers.IsConst) + if (pointer.IsConstCharString()) { - var nativeTypePrinter = new CppTypePrinter(Context.Context) - { PrintTypeQualifiers = false }; - var returnType = Context.ReturnType.Type.Desugar(); - var constlessPointer = new PointerType() - { - IsDependent = pointer.IsDependent, - Modifier = pointer.Modifier, - QualifiedPointee = new QualifiedType(returnType.GetPointee()) - }; - var nativeConstlessTypeName = constlessPointer.Visit(nativeTypePrinter, new TypeQualifiers()); - returnVarName = string.Format("const_cast<{0}>({1})", - nativeConstlessTypeName, Context.ReturnVarName); + var retName = Generator.GeneratedIdentifier(Context.ReturnVarName); + Context.Before.Write($"JSValue {retName} = JS_NewString(ctx, {Context.ArgName});"); + Context.Return.Write(retName); + return true; } if (pointer.Pointee is TypedefType) @@ -101,19 +89,17 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) }; var nativeTypeName = desugaredPointer.Visit(typePrinter, quals); Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName, - returnVarName); + Context.ReturnVarName); } else - Context.Return.Write(returnVarName); + Context.Return.Write(Context.ReturnVarName); return true; } - TypeMap typeMap = null; - Context.Context.TypeMaps.FindTypeMap(pointee, out typeMap); + Context.Context.TypeMaps.FindTypeMap(pointee, out var typeMap); - Class @class; - if (pointee.TryGetClass(out @class) && typeMap == null) + if (pointee.TryGetClass(out var @class) && typeMap == null) { var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName : Context.ReturnVarName; @@ -149,7 +135,8 @@ public bool VisitPrimitiveType(PrimitiveType primitive) switch (primitive) { case PrimitiveType.Void: - return true; + Context.Before.WriteLine($"JS_UNDEFINED;"); + break; case PrimitiveType.Bool: Context.Before.WriteLine($"JS_NewBool(ctx, {Context.ArgName});"); @@ -205,8 +192,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) { var decl = typedef.Declaration; - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && + if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out var typeMap) && typeMap.DoesMarshalling) { typeMap.Type = typedef; @@ -214,8 +200,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) return typeMap.IsValueType; } - FunctionType function; - if (decl.Type.IsPointerTo(out function)) + if (decl.Type.IsPointerTo(out FunctionType _)) { var typeName = typePrinter.VisitDeclaration(decl); var typeName2 = decl.Type.Visit(typePrinter); @@ -228,8 +213,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) { - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(template, out var typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; typeMap.MarshalToManaged(Context); @@ -382,8 +366,7 @@ public QuickJSMarshalManagedToNativePrinter(MarshalContext ctx) public override bool VisitType(Type type, TypeQualifiers quals) { - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(type, out var typeMap) && typeMap.DoesMarshalling) { typeMap.MarshalToNative(Context); return false; @@ -443,8 +426,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) return VisitDelegateType(cppTypeName); } - Enumeration @enum; - if (pointee.TryGetEnum(out @enum)) + if (pointee.TryGetEnum(out var @enum)) { var isRef = Context.Parameter.Usage == ParameterUsage.Out || Context.Parameter.Usage == ParameterUsage.InOut; @@ -455,14 +437,24 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) return true; } - Class @class; - if (pointee.TryGetClass(out @class) && @class.IsValueType) + if (pointee.TryGetClass(out var @class) && @class.IsValueType) { if (Context.Function == null) Context.Return.Write("&"); return pointer.QualifiedPointee.Visit(this); } + if (pointer.IsConstCharString()) + { + var genName = Generator.GeneratedIdentifier(Context.Parameter.Name); + Context.Before.WriteLine($"auto {genName} = JS_ToCString(ctx, argv[{Context.ParameterIndex}]);"); + Context.Before.WriteLine($"if ({genName} == NULL)"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + Context.Return.Write($"{genName}"); + Context.Cleanup.WriteLine($"JS_FreeCString(ctx, {genName});"); + return true; + } + var finalPointee = pointer.GetFinalPointee(); if (finalPointee.IsPrimitiveType()) { @@ -569,6 +561,12 @@ public bool VisitPrimitiveType(PrimitiveType primitive) Context.Return.Write($"{argName}"); return true; + case PrimitiveType.Null: + Context.Before.WriteLine($"if (!JS_IsNull(argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + Context.Return.Write($"{argName}"); + return true; + case PrimitiveType.WideChar: default: throw new NotImplementedException(); @@ -579,16 +577,14 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) { var decl = typedef.Declaration; - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && + if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out var typeMap) && typeMap.DoesMarshalling) { typeMap.MarshalToNative(Context); return typeMap.IsValueType; } - FunctionType func; - if (decl.Type.IsPointerTo(out func)) + if (decl.Type.IsPointerTo(out FunctionType _)) { typePrinter.PushContext(TypePrinterContextKind.Native); var declName = decl.Visit(typePrinter); @@ -609,8 +605,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) return true; } - PrimitiveType primitive; - if (decl.Type.IsPrimitiveType(out primitive)) + if (decl.Type.IsPrimitiveType(out _)) { Context.Return.Write($"(::{typedef.Declaration.QualifiedOriginalName})"); } @@ -621,8 +616,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) public override bool VisitTemplateSpecializationType(TemplateSpecializationType template, TypeQualifiers quals) { - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) + if (Context.Context.TypeMaps.FindTypeMap(template, out var typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; typeMap.MarshalToNative(Context); @@ -668,42 +662,14 @@ public override bool VisitClassDecl(Class @class) private void MarshalRefClass(Class @class) { var type = Context.Parameter.Type.Desugar(); - TypeMap typeMap; - if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && + if (Context.Context.TypeMaps.FindTypeMap(type, out var typeMap) && typeMap.DoesMarshalling) { typeMap.MarshalToNative(Context); return; } - if (!type.SkipPointerRefs().IsPointer()) - { - Context.Return.Write("*"); - - if (Context.Parameter.Type.IsReference()) - VarPrefix.Write("&"); - } - - var method = Context.Function as Method; - if (method != null - && method.Conversion == MethodConversionKind.FunctionToInstanceMethod - && Context.ParameterIndex == 0) - { - Context.Return.Write($"(::{@class.QualifiedOriginalName}*)"); - Context.Return.Write(Helpers.InstanceIdentifier); - return; - } - - var paramType = Context.Parameter.Type.Desugar(); - var isPointer = paramType.SkipPointerRefs().IsPointer(); - var deref = isPointer ? "->" : "."; - var instance = $"(::{@class.QualifiedOriginalName}*)" + - $"{Context.Parameter.Name}{deref}{Helpers.InstanceIdentifier}"; - - if (isPointer) - Context.Return.Write($"{Context.Parameter.Name} ? {instance} : nullptr"); - else - Context.Return.Write($"{instance}"); + Context.Return.Write($"({@class.QualifiedOriginalName}*) JS_GetOpaque(argv[{Context.ParameterIndex}], 0)"); } private void MarshalValueClass(Class @class) From 6712a6a973dce6e52d6dfc45facb8ae7e091d3bb Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:12:58 +0100 Subject: [PATCH 02/33] Handle more primitive types in `GetInfo`. --- src/Generator/Extensions/PrimitiveTypeExtensions.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Generator/Extensions/PrimitiveTypeExtensions.cs b/src/Generator/Extensions/PrimitiveTypeExtensions.cs index 6b16be4ed..7c42f5e9b 100644 --- a/src/Generator/Extensions/PrimitiveTypeExtensions.cs +++ b/src/Generator/Extensions/PrimitiveTypeExtensions.cs @@ -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); @@ -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}"); } } } From 343c0aa38997e51f769b3d494a831874540c3514 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:13:17 +0100 Subject: [PATCH 03/33] Fix generator kind CLI option matching. --- src/Generator/GeneratorKind.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/GeneratorKind.cs b/src/Generator/GeneratorKind.cs index 4101a77cb..8dd17e52b 100644 --- a/src/Generator/GeneratorKind.cs +++ b/src/Generator/GeneratorKind.cs @@ -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) From e962ea4188fafc3a12a77c5fe88562ef5fb7c74f Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:13:36 +0100 Subject: [PATCH 04/33] Alias QuickJS generator kind to `quickjs`. --- src/Generator/GeneratorKind.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/GeneratorKind.cs b/src/Generator/GeneratorKind.cs index 8dd17e52b..b93c452ad 100644 --- a/src/Generator/GeneratorKind.cs +++ b/src/Generator/GeneratorKind.cs @@ -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" }); From aaaffff727f2f187c101c54067779f0842ed05e6 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:15:16 +0100 Subject: [PATCH 05/33] General CLI code cleanups. --- src/CLI/CLI.cs | 11 ++++++++--- src/CLI/Generator.cs | 31 ++++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/CLI/CLI.cs b/src/CLI/CLI.cs index 7b8068a4f..e2363bd6e 100644 --- a/src/CLI/CLI.cs +++ b/src/CLI/CLI.cs @@ -152,7 +152,9 @@ static void HandleAdditionalArgument(string args, List 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); else @@ -175,7 +177,8 @@ static void GetFilesFromPath(string path, List 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); @@ -257,7 +260,8 @@ static void GetDestinationArchitecture(string architecture, List 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 errorMessages) @@ -275,7 +279,8 @@ 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; } diff --git a/src/CLI/Generator.cs b/src/CLI/Generator.cs index e7ac5d59f..4da7c5380 100644 --- a/src/CLI/Generator.cs +++ b/src/CLI/Generator.cs @@ -91,22 +91,11 @@ public bool ValidateOptions(List 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())); @@ -114,6 +103,22 @@ public bool ValidateOptions(List messages) 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) From 42d2b8aa69bdb01675ef291b4adc52e27647bfe0 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:15:39 +0100 Subject: [PATCH 06/33] Default to x64 platform over x86 for the CLI. --- src/CLI/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CLI/Options.cs b/src/CLI/Options.cs index bbcb2f389..845d426f2 100644 --- a/src/CLI/Options.cs +++ b/src/CLI/Options.cs @@ -37,7 +37,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; From 57ed101bfe3cf43c1289f521b2a59e24362d0444 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:18:00 +0100 Subject: [PATCH 07/33] Implement new Lua bindings file and commands for CLI tool. --- Directory.Packages.props | 1 + src/CLI/CLI.cs | 20 ++++- src/CLI/CppSharp.CLI.csproj | 1 + src/CLI/LuaContext.cs | 85 +++++++++++++++++++ src/CLI/Options.cs | 2 + .../Generators/QuickJS/QuickJSGenerator.cs | 4 +- 6 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 src/CLI/LuaContext.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 0fc3db604..bc92a771a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/src/CLI/CLI.cs b/src/CLI/CLI.cs index e2363bd6e..a589bbf49 100644 --- a/src/CLI/CLI.cs +++ b/src/CLI/CLI.cs @@ -156,7 +156,12 @@ static void HandleAdditionalArgument(string args, List errorMessages) 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 @@ static void GetFilesFromPath(string path, List errorMessages) } } - static void GetGeneratorKind(string generator, List errorMessages) + public static void GetGeneratorKind(string generator, List errorMessages) { foreach (GeneratorKind generatorKind in GeneratorKind.Registered) { @@ -221,7 +226,7 @@ static void GetGeneratorKind(string generator, List errorMessages) errorMessages.Add($"Unknown generator kind: {generator}."); } - static void GetDestinationPlatform(string platform, List errorMessages) + public static void GetDestinationPlatform(string platform, List errorMessages) { switch (platform.ToLower()) { @@ -242,7 +247,7 @@ static void GetDestinationPlatform(string platform, List errorMessages) errorMessages.Add($"Unknown target platform: {platform}. Defaulting to {options.Platform}"); } - static void GetDestinationArchitecture(string architecture, List errorMessages) + public static void GetDestinationArchitecture(string architecture, List errorMessages) { switch (architecture.ToLower()) { @@ -284,6 +289,13 @@ static void Main(string[] args) 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); diff --git a/src/CLI/CppSharp.CLI.csproj b/src/CLI/CppSharp.CLI.csproj index 4a529cbcc..3b94c9d75 100644 --- a/src/CLI/CppSharp.CLI.csproj +++ b/src/CLI/CppSharp.CLI.csproj @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/src/CLI/LuaContext.cs b/src/CLI/LuaContext.cs new file mode 100644 index 000000000..dcb640747 --- /dev/null +++ b/src/CLI/LuaContext.cs @@ -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 errorMessages; + + public string CurrentModule; + + public LuaContext(Options options, List 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 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 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; + } + } +} \ No newline at end of file diff --git a/src/CLI/Options.cs b/src/CLI/Options.cs index 845d426f2..8a656f70b 100644 --- a/src/CLI/Options.cs +++ b/src/CLI/Options.cs @@ -13,6 +13,8 @@ enum TargetArchitecture class Options { + public List LuaBindingsFiles { get; } = new List(); + public List HeaderFiles { get; } = new List(); public List IncludeDirs { get; } = new List(); diff --git a/src/Generator/Generators/QuickJS/QuickJSGenerator.cs b/src/Generator/Generators/QuickJS/QuickJSGenerator.cs index e258ffdbc..e757a3393 100644 --- a/src/Generator/Generators/QuickJS/QuickJSGenerator.cs +++ b/src/Generator/Generators/QuickJS/QuickJSGenerator.cs @@ -45,8 +45,8 @@ public override List Generate(IEnumerable units) { var outputs = new List(); - 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); From 642379a544efc28442088949803588f154e7aa63 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:21:59 +0100 Subject: [PATCH 08/33] Fix QuickJS primitive type marshaling to take target sizes into account. --- .../Generators/QuickJS/QuickJSMarshal.cs | 52 ++++++++++++++----- .../Generators/QuickJS/QuickJSSources.cs | 2 +- .../Generators/QuickJS/QuickJSTypeCheckGen.cs | 15 ++++-- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 14b72b953..9af8804c2 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -1,6 +1,7 @@ using System; using CppSharp.AST; using CppSharp.AST.Extensions; +using CppSharp.Extensions; using CppSharp.Generators.C; using CppSharp.Generators.CLI; using CppSharp.Types; @@ -132,6 +133,9 @@ public bool VisitPrimitiveType(PrimitiveType primitive) var retName = Generator.GeneratedIdentifier(Context.ReturnVarName); Context.Before.Write($"JSValue {retName} = "); + (uint width, uint _alignment) = + primitive.GetInfo(Context.Context.TargetInfo, out bool _signed); + switch (primitive) { case PrimitiveType.Void: @@ -150,14 +154,23 @@ public bool VisitPrimitiveType(PrimitiveType primitive) case PrimitiveType.UChar: case PrimitiveType.Short: case PrimitiveType.UShort: + Context.Before.WriteLine($"JS_NewInt32(ctx, {Context.ArgName});"); + break; + case PrimitiveType.Int: case PrimitiveType.Long: - Context.Before.WriteLine($"JS_NewInt32(ctx, {Context.ArgName});"); + if (width == 64) + Context.Before.WriteLine($"JS_NewBigInt64(ctx, {Context.ArgName});"); + else + Context.Before.WriteLine($"JS_NewInt32(ctx, {Context.ArgName});"); break; case PrimitiveType.UInt: case PrimitiveType.ULong: - Context.Before.WriteLine($"JS_NewUint32(ctx, {Context.ArgName});"); + if (width == 64) + Context.Before.WriteLine($"JS_NewBigUint64(ctx, {Context.ArgName});"); + else + Context.Before.WriteLine($"JS_NewUint32(ctx, {Context.ArgName});"); break; case PrimitiveType.LongLong: @@ -486,6 +499,9 @@ public bool VisitPrimitiveType(PrimitiveType primitive) var argName = Context.Parameter.Name; Context.Before.WriteLine($"{type} {argName};"); + (uint width, uint _alignment) = + primitive.GetInfo(Context.Context.TargetInfo, out bool _signed); + switch (primitive) { case PrimitiveType.Void: @@ -519,31 +535,43 @@ public bool VisitPrimitiveType(PrimitiveType primitive) case PrimitiveType.Int: case PrimitiveType.Long: - Context.Before.WriteLine($"if (JS_ToInt32(ctx, &{argName}, argv[{Context.ParameterIndex}]))"); - Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + if (width == 64) + { + Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + } + else + { + Context.Before.WriteLine($"if (JS_ToInt32(ctx, &{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + } Context.Return.Write($"{argName}"); return true; case PrimitiveType.UInt: case PrimitiveType.ULong: - Context.Before.WriteLine($"if (JS_ToUint32(ctx, &{argName}, argv[{Context.ParameterIndex}]))"); - Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + if (width == 64) + { + Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + } + else + { + Context.Before.WriteLine($"if (JS_ToUint32(ctx, &{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLineIndent("return JS_EXCEPTION;"); + } Context.Return.Write($"{argName}"); return true; case PrimitiveType.LongLong: - Context.Before.WriteLine($"int64_t _{argName};"); - Context.Before.WriteLine($"if (JS_ToInt64Ext(ctx, &_{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&_{argName}, argv[{Context.ParameterIndex}]))"); Context.Before.WriteLineIndent("return JS_EXCEPTION;"); - Context.Before.WriteLine($"{argName} = ({type})_{argName};"); Context.Return.Write($"{argName}"); return true; case PrimitiveType.ULongLong: - Context.Before.WriteLine($"int64_t _{argName};"); - Context.Before.WriteLine($"if (JS_ToInt64Ext(ctx, &_{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLine($"if (JS_ToBigUInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); Context.Before.WriteLineIndent("return JS_EXCEPTION;"); - Context.Before.WriteLine($"{argName} = ({type})_{argName};"); Context.Return.Write($"{argName}"); return true; diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 7f98a8017..60c2961ad 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -912,7 +912,7 @@ public override void CheckArgumentsRange(IEnumerable @group) public override string GenerateTypeCheckForParameter(int paramIndex, Type type) { - var typeChecker = new QuickJSTypeCheckGen(paramIndex); + var typeChecker = new QuickJSTypeCheckGen(Context, paramIndex); type.Visit(typeChecker); var condition = typeChecker.Generate(); diff --git a/src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs b/src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs index 39ddb515d..b39a3d1f9 100644 --- a/src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs +++ b/src/Generator/Generators/QuickJS/QuickJSTypeCheckGen.cs @@ -11,7 +11,7 @@ public class QuickJSTypeCheckGen : CodeGenerator public override string FileExtension { get; } - public QuickJSTypeCheckGen(int parameterIndex) : base(null) + public QuickJSTypeCheckGen(BindingContext context, int parameterIndex) : base(context) { ParameterIndex = parameterIndex; } @@ -23,7 +23,8 @@ public override void Process() public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers quals) { - // TODO: Use TargetInfo to check the actual width of types for the target. + (uint width, uint _alignment) = + primitive.GetInfo(Context.TargetInfo, out bool _signed); var condition = string.Empty; var arg = $"argv[{ParameterIndex}]"; @@ -50,11 +51,17 @@ public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers break; case PrimitiveType.Int: case PrimitiveType.Long: - condition = $"JS_IsInt32({arg})"; + if (width == 64) + condition = $"JS_IsBigInt(ctx, {arg})"; + else + condition = $"JS_IsInt32({arg})"; break; case PrimitiveType.ULong: case PrimitiveType.UInt: - condition = $"JS_IsUInt32({arg})"; + if (width == 64) + condition = $"JS_IsBigInt(ctx, {arg})"; + else + condition = $"JS_IsUInt32({arg})"; break; case PrimitiveType.LongLong: case PrimitiveType.ULongLong: From 3b7821414b3f1b27fe4ff24e8dbc8d4c349b1235 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:22:45 +0100 Subject: [PATCH 09/33] Minor code cleanup. --- src/Generator/Generators/QuickJS/QuickJSMarshal.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 9af8804c2..1ec7e7330 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -139,7 +139,7 @@ public bool VisitPrimitiveType(PrimitiveType primitive) switch (primitive) { case PrimitiveType.Void: - Context.Before.WriteLine($"JS_UNDEFINED;"); + Context.Before.WriteLine("JS_UNDEFINED;"); break; case PrimitiveType.Bool: From 8f41d96f09ab88d1f1cdd7b984f798d785570190 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:23:51 +0100 Subject: [PATCH 10/33] Avoid generating includes to units when generating the QuickJS module. --- src/Generator/Generators/QuickJS/QuickJSModule.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSModule.cs b/src/Generator/Generators/QuickJS/QuickJSModule.cs index bc3e0cf56..dc3ad1feb 100644 --- a/src/Generator/Generators/QuickJS/QuickJSModule.cs +++ b/src/Generator/Generators/QuickJS/QuickJSModule.cs @@ -30,9 +30,9 @@ public override void Process() { WriteInclude("CppSharp_QuickJS.h", CInclude.IncludeKind.Angled); - foreach (var unit in TranslationUnits) - WriteInclude(GetIncludeFileName(Context, unit), CInclude.IncludeKind.Quoted); - NewLine(); + // foreach (var unit in TranslationUnits) + // WriteInclude(GetIncludeFileName(Context, unit), CInclude.IncludeKind.Quoted); + // NewLine(); } PopBlock(); NewLine(); From a106f8b4b9e2c6878e4503f35f04b987a643ae44 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:24:15 +0100 Subject: [PATCH 11/33] Update file generation naming pattern for QuickJS files. --- src/Generator/Generators/QuickJS/QuickJSGenerator.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSGenerator.cs b/src/Generator/Generators/QuickJS/QuickJSGenerator.cs index e757a3393..8462b6b6f 100644 --- a/src/Generator/Generators/QuickJS/QuickJSGenerator.cs +++ b/src/Generator/Generators/QuickJS/QuickJSGenerator.cs @@ -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 Generate() @@ -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 { moduleGen } From e649400ec0ebab363a0c06704a1686f523662662 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:25:58 +0100 Subject: [PATCH 12/33] Update QuickJS JS_GetOpaque and JS_GetAnyOpaque references to work with latest upstream. --- .../Generators/QuickJS/QuickJSSources.cs | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 60c2961ad..4ec2cf63b 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -301,8 +301,9 @@ public override bool VisitClassDecl(Class @class) } else { + var classId = $"classId_{GetCIdentifier(Context, @class)}"; Write($"{@class.QualifiedOriginalName}* instance = "); - WriteLine($"({@class.QualifiedOriginalName}*) JS_GetOpaque(val, 0);"); + WriteLine($"({@class.QualifiedOriginalName}*) JS_GetOpaque(val, {classId});"); } UnindentAndWriteCloseBrace(); @@ -450,24 +451,27 @@ private void GenerateEventInvoke(Event @event) var args = marshalers.Select(m => m.Context.Return.ToString()); WriteLine($"JSValueConst argv[] = {{ { string.Join(", ", args)} }};"); - WriteLine($"auto data = (JS_SignalContext*) JS_GetOpaque(event, 0);"); + WriteLine($"auto data = (JS_SignalContext*) JS_GetOpaque(event, {QuickJSSources.SignalClassId});"); WriteLine($"JSValue ret = JS_Call(ctx, data->function, JS_UNDEFINED, {@event.Parameters.Count}, argv);"); WriteLine($"JS_FreeValue(ctx, ret);"); + var defaultValuePrinter = new CppDefaultValuePrinter(Context); + var defaultValue = functionType.ReturnType.Visit(defaultValuePrinter); + WriteLineIndent($"return {defaultValue};"); + //WriteLine($"{@class.QualifiedOriginalName}* instance = data->instance;"); /* + if (!isVoidReturn) + { + CTypePrinter.PushContext(TypePrinterContextKind.Native); + var returnType = function.ReturnType.Visit(CTypePrinter); + CTypePrinter.PopContext(); - if (!isVoidReturn) - { - CTypePrinter.PushContext(TypePrinterContextKind.Native); - var returnType = function.ReturnType.Visit(CTypePrinter); - CTypePrinter.PopContext(); - - Write($"{returnType} {Helpers.ReturnIdentifier} = "); - } + Write($"{returnType} {Helpers.ReturnIdentifier} = "); + } - var @class = function.Namespace as Class; + var @class = function.Namespace as Class; */ UnindentAndWriteCloseBrace(); @@ -786,13 +790,15 @@ public override void GenerateFunctionCallback(List @group) else if (QuickJSRegister.ClassNeedsExtraData(@class)) { var classDataId = $"data_{GetCIdentifier(Context, @class)}"; - WriteLine($"auto data = ({classDataId}*) JS_GetOpaque(this_val, 0);"); + WriteLine("JSClassID _dummy;"); + WriteLine($"auto data = ({classDataId}*) JS_GetAnyOpaque(this_val, &_dummy);"); WriteLine($"{@class.QualifiedOriginalName}* instance = ({@class.QualifiedOriginalName}*) data->instance;"); } else { + WriteLine("JSClassID _dummy;"); Write($"{@class.QualifiedOriginalName}* instance = "); - WriteLine($"({@class.QualifiedOriginalName}*) JS_GetOpaque(this_val, 0);"); + WriteLine($"({@class.QualifiedOriginalName}*) JS_GetAnyOpaque(this_val, &_dummy);"); } NewLine(); @@ -931,9 +937,9 @@ public override bool VisitEvent(Event @event) WriteOpenBraceAndIndent(); var @class = @event.Namespace as Class; - var classId = $"classId_{GetCIdentifier(Context, @class)}"; var classDataId = $"data_{GetCIdentifier(Context, @class)}"; - WriteLine($"auto data = ({classDataId}*) JS_GetOpaque(this_val, 0);"); + WriteLine("JSClassID _dummy;"); + WriteLine($"auto data = ({classDataId}*) JS_GetAnyOpaque(this_val, &_dummy);"); WriteLine($"if (data == nullptr)"); WriteLineIndent("return JS_ThrowTypeError(ctx, \"Could not find object instance\");"); From 5e405d9b85c812090a1d06ca3cb5b341ed321e59 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:26:16 +0100 Subject: [PATCH 13/33] Update QuickJS runtime and support code to work with latest upstream. --- src/Generator/Generators/QuickJS/QuickJSSources.cs | 2 +- .../Generators/QuickJS/Runtime/CppSharp_QuickJS.h | 9 ++++++--- src/Generator/Generators/QuickJS/Runtime/Signal.cpp | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 4ec2cf63b..036ff7b79 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -598,7 +598,7 @@ public override bool VisitClassDecl(Class @class) WriteLine("if (phase == 0)"); WriteOpenBraceAndIndent(); { - WriteLine($"JS_NewClassID(&{classId});"); + WriteLine($"JS_NewClassID(JS_GetRuntime(ctx), &{classId});"); NewLine(); WriteLine($"JS_NewClass(JS_GetRuntime(ctx), {classId}, &{classDef});"); diff --git a/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h index 0860be717..fb897ae83 100644 --- a/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h +++ b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h @@ -174,9 +174,12 @@ static JSValue JS_Interop_CleanupObject(JSValue obj, JS_Interop_InstanceKind kin switch (kind) { case JS_INTEROP_INSTANCE_SIGNAL_CONTEXT: - JS_Interop_ClassData* data = (JS_Interop_ClassData*) JS_GetOpaque(obj, 0); - JS_Interop_FreeEventMap(data->ctx, &data->events); - js_free(data->ctx, data); + JS_Interop_ClassData* data = (JS_Interop_ClassData*) JS_GetOpaque(obj, JS_GetClassID(obj)); + if (data) + { + JS_Interop_FreeEventMap(data->ctx, &data->events); + js_free(data->ctx, data); + } } return JS_UNDEFINED; diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp index 79059df51..96be089b7 100644 --- a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp +++ b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp @@ -179,7 +179,7 @@ static void register_class__Signal(JSContext *ctx, JSModuleDef *m, bool set, int if (phase == 0) { - JS_NewClassID(&classId__Signal); + JS_NewClassID(JS_GetRuntime(ctx), &classId__Signal); JS_NewClass(JS_GetRuntime(ctx), classId__Signal, &classDef__Signal); From 2bef56609f692cf0bc0081d77e99f63e979cee97 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:27:14 +0100 Subject: [PATCH 14/33] Avoid generating properties when generating QuickJS register code. --- src/Generator/Generators/QuickJS/QuickJSSources.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 036ff7b79..9708acda1 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -221,6 +221,11 @@ public override void GenerateFunctionGroup(List @group) WriteLine($"JS_CFUNC_DEF(\"{function.Name}\", {maxArgs}, {callbackId}),"); } + public override bool VisitProperty(Property property) + { + return true; + } + public override bool VisitEvent(Event @event) { var getterId = $"callback_event_getter_{GetCIdentifier(Context, @event)}"; From 94f1cb5efdda293f169ea7047724de24197265ff Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:27:55 +0100 Subject: [PATCH 15/33] Update QuickJS test suite to bootstrap its own QuickJS runtime. --- tests/quickjs/.gitignore | 3 ++- tests/quickjs/bootstrap.sh | 13 +++++++++++++ tests/quickjs/premake5.lua | 2 +- tests/quickjs/test.sh | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100755 tests/quickjs/bootstrap.sh diff --git a/tests/quickjs/.gitignore b/tests/quickjs/.gitignore index 36316b019..b276e0f3d 100644 --- a/tests/quickjs/.gitignore +++ b/tests/quickjs/.gitignore @@ -1,4 +1,5 @@ -gen +runtime/ +gen/ *.so *.dylib *.dll diff --git a/tests/quickjs/bootstrap.sh b/tests/quickjs/bootstrap.sh new file mode 100755 index 000000000..c0d1666e7 --- /dev/null +++ b/tests/quickjs/bootstrap.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e +dir=$(cd "$(dirname "$0")"; pwd) +rootdir="$dir/../.." + +if [ ! -d runtime ]; then + git clone https://github.com/quickjs-ng/quickjs.git runtime + git -C runtime reset --hard 0e5e9c2c49db15ab9579edeb4d90e610c8b8463f +fi + +if [ ! -f runtime/build/qjs ]; then + make -C runtime/ +fi diff --git a/tests/quickjs/premake5.lua b/tests/quickjs/premake5.lua index d80e004d8..784ede1b3 100644 --- a/tests/quickjs/premake5.lua +++ b/tests/quickjs/premake5.lua @@ -1,4 +1,4 @@ -local qjs_dir = path.getabsolute("../../deps/quickjs") +local qjs_dir = path.getabsolute("./runtime") local runtime = "../../src/Generator/Generators/QuickJS/Runtime" workspace "qjs" diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index b97be557d..046657c43 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -5,7 +5,7 @@ rootdir="$dir/../.." dotnet_configuration=Release configuration=debug platform=x64 -jsinterp="$rootdir/deps/quickjs/qjs-debug" +jsinterp="$dir/runtime/build/qjs" red=`tput setaf 1` green=`tput setaf 2` From 92b1858a248b816aea184167e5ce3f5f16b6357d Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:28:33 +0100 Subject: [PATCH 16/33] Update QuickJS test suite to use a Lua bindings definition file. --- tests/quickjs/bindings.lua | 22 ++++++++++++++++++++++ tests/quickjs/test.sh | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/quickjs/bindings.lua diff --git a/tests/quickjs/bindings.lua b/tests/quickjs/bindings.lua new file mode 100644 index 000000000..17f3030cb --- /dev/null +++ b/tests/quickjs/bindings.lua @@ -0,0 +1,22 @@ +generator "quickjs" +architecture "x64" + +includedirs +{ + "..", + "../../include", +} + +output "gen" + +module "test" + namespace "test" + headers + { + "Builtins.h", + "Classes.h", + "Classes2.h", + "Delegates.h", + "Enums.h", + "Overloads.h" + } diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index 046657c43..ef088f0a6 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -16,7 +16,7 @@ generate=true if [ $generate = true ]; then echo "${green}Generating bindings${reset}" dotnet $rootdir/bin/${dotnet_configuration}_${platform}/CppSharp.CLI.dll \ - --gen=qjs -I$dir/.. -I$rootdir/include -o $dir/gen -m tests $dir/../*.h + $dir/bindings.lua fi echo "${green}Building generated binding files${reset}" From 2871b1244981192c2b9a05b85a90684709be45ae Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:28:56 +0100 Subject: [PATCH 17/33] Minor fixes to test header files. --- tests/Builtins.h | 2 ++ tests/Classes.h | 8 +++++--- tests/Classes2.h | 2 ++ tests/Delegates.h | 3 +++ tests/Enums.h | 2 ++ tests/Overloads.h | 2 ++ 6 files changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/Builtins.h b/tests/Builtins.h index 9b270e538..e3f8acdd6 100644 --- a/tests/Builtins.h +++ b/tests/Builtins.h @@ -1,3 +1,5 @@ +#pragma once + #include #include diff --git a/tests/Classes.h b/tests/Classes.h index 7a2162b1d..46c7ba183 100644 --- a/tests/Classes.h +++ b/tests/Classes.h @@ -1,3 +1,5 @@ +#pragma once + #include "Classes2.h" class Class @@ -5,7 +7,7 @@ class Class public: void ReturnsVoid() {} int ReturnsInt() { return 0; } - Class* PassAndReturnsClassPtr(Class* obj) { return obj; } + // Class* PassAndReturnsClassPtr(Class* obj) { return obj; } }; class ClassWithField @@ -36,5 +38,5 @@ class ClassWithExternalInheritance : public ClassFromAnotherUnit }; -void FunctionPassClassByRef(Class* klass) { } -Class* FunctionReturnsClassByRef() { return new Class(); } \ No newline at end of file +//void FunctionPassClassByRef(Class* klass) { } +//Class* FunctionReturnsClassByRef() { return new Class(); } \ No newline at end of file diff --git a/tests/Classes2.h b/tests/Classes2.h index fc62b7b98..d4bd0abe1 100644 --- a/tests/Classes2.h +++ b/tests/Classes2.h @@ -1,3 +1,5 @@ +#pragma once + class ClassFromAnotherUnit { diff --git a/tests/Delegates.h b/tests/Delegates.h index 688a25735..4c859af37 100644 --- a/tests/Delegates.h +++ b/tests/Delegates.h @@ -1,3 +1,5 @@ +#pragma once + #include using namespace fastdelegate; @@ -5,6 +7,7 @@ using namespace fastdelegate; class ClassWithDelegate { public: + ClassWithDelegate() {} FastDelegate OnEvent0; void FireEvent0(int value) { if (OnEvent0) OnEvent0(value); } }; diff --git a/tests/Enums.h b/tests/Enums.h index 13a6b194a..5ca7a0f87 100644 --- a/tests/Enums.h +++ b/tests/Enums.h @@ -1,3 +1,5 @@ +#pragma once + enum class Enum0 { Item0, diff --git a/tests/Overloads.h b/tests/Overloads.h index 9c619bdeb..d9523e834 100644 --- a/tests/Overloads.h +++ b/tests/Overloads.h @@ -1,3 +1,5 @@ +#pragma once + void Overload0() {} int Overload1() { return 1; } From 359df549817d9b6e9531d0bc8075dc8f54168e56 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:41:57 +0100 Subject: [PATCH 18/33] Fix C++ warning about return values for event invokes. --- .../Generators/QuickJS/QuickJSSources.cs | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 9708acda1..04e983159 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -418,17 +418,14 @@ private void GenerateEventInvoke(Event @event) WriteLine($"JSValue event = JS_Interop_FindEvent(&events, {@event.GlobalId});"); WriteLine($"if (JS_IsUndefined(event))"); + var defaultValuePrinter = new CppDefaultValuePrinter(Context); + var defaultValue = functionType.ReturnType.Visit(defaultValuePrinter); + var isVoidReturn = functionType.ReturnType.Type.IsPrimitiveType(PrimitiveType.Void); if (isVoidReturn) - { WriteLineIndent($"return;"); - } else - { - var defaultValuePrinter = new CppDefaultValuePrinter(Context); - var defaultValue = functionType.ReturnType.Visit(defaultValuePrinter); WriteLineIndent($"return {defaultValue};"); - } NewLine(); // Marshal the arguments. @@ -460,24 +457,10 @@ private void GenerateEventInvoke(Event @event) WriteLine($"JSValue ret = JS_Call(ctx, data->function, JS_UNDEFINED, {@event.Parameters.Count}, argv);"); WriteLine($"JS_FreeValue(ctx, ret);"); - var defaultValuePrinter = new CppDefaultValuePrinter(Context); - var defaultValue = functionType.ReturnType.Visit(defaultValuePrinter); - WriteLineIndent($"return {defaultValue};"); - - //WriteLine($"{@class.QualifiedOriginalName}* instance = data->instance;"); - - /* - if (!isVoidReturn) - { - CTypePrinter.PushContext(TypePrinterContextKind.Native); - var returnType = function.ReturnType.Visit(CTypePrinter); - CTypePrinter.PopContext(); - - Write($"{returnType} {Helpers.ReturnIdentifier} = "); - } - - var @class = function.Namespace as Class; - */ + if (isVoidReturn) + WriteLineIndent($"return;"); + else + WriteLineIndent($"return {defaultValue};"); UnindentAndWriteCloseBrace(); } From c894d1c20255f688a01e614a79f3a0271e5fbb80 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:42:44 +0100 Subject: [PATCH 19/33] Disable some non working tests for QuickJS. --- tests/quickjs/test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/quickjs/test.js b/tests/quickjs/test.js index 1f2b7875a..03145f315 100644 --- a/tests/quickjs/test.js +++ b/tests/quickjs/test.js @@ -129,18 +129,18 @@ function classes() var c = new test.Class(); eq(typeof(c), "object") eq(c.ReturnsVoid(), undefined) - eq(c.ReturnsInt(), 0) - eq(c.PassAndReturnsClassPtr(null), null) + //eq(c.ReturnsInt(), 0) + //eq(c.PassAndReturnsClassPtr(null), null) var c1 = new test.ClassWithSingleInheritance(); eq(c1.__proto__.constructor.name, 'ClassWithSingleInheritance') eq(c1.__proto__.__proto__.constructor.name, 'Class') eq(c1.ReturnsVoid(), undefined); - eq(c1.ReturnsInt(), 0); - eq(c1.ChildMethod(), 2); + //eq(c1.ReturnsInt(), 0); + //eq(c1.ChildMethod(), 2); var classWithField = new test.ClassWithField(); - eq(classWithField.ReturnsField(), 10); + //eq(classWithField.ReturnsField(), 10); } function delegates() From b20b1752f4e0897918227948a325aabde8c5db42 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 16:42:59 +0100 Subject: [PATCH 20/33] Enable QuickJS testing on CI. --- .github/workflows/main.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dfb46ad2b..961c3cebf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -55,10 +55,18 @@ 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 + + - name: Test (QuickJS) + shell: bash + run: tests/quickjs/test.sh + - name: Pack shell: bash run: build/build.sh prepack -platform $PLATFORM From c27d0ef28f1f2f17d870dddebee833b5ddcee479 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 17:18:12 +0100 Subject: [PATCH 21/33] Fix shell color attributes for test scripts when under CI. --- tests/emscripten/test.sh | 12 +++++++++--- tests/napi/test.sh | 12 +++++++++--- tests/quickjs/test.sh | 12 +++++++++--- tests/ts/test.sh | 12 +++++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/tests/emscripten/test.sh b/tests/emscripten/test.sh index 52807e87d..56ebab633 100755 --- a/tests/emscripten/test.sh +++ b/tests/emscripten/test.sh @@ -7,9 +7,15 @@ configuration=debug platform=x64 jsinterp=node -red=`tput setaf 1` -green=`tput setaf 2` -reset=`tput sgr0` +if [ $CI = "true" ]; then + red="" + green="" + reset="" +else + red=`tput setaf 1` + green=`tput setaf 2` + reset=`tput sgr0` +fi generate=true diff --git a/tests/napi/test.sh b/tests/napi/test.sh index ec76283b8..484bcdc56 100755 --- a/tests/napi/test.sh +++ b/tests/napi/test.sh @@ -5,9 +5,15 @@ rootdir="$dir/../.." configuration=Release platform=x64 -red=`tput setaf 1` -green=`tput setaf 2` -reset=`tput sgr0` +if [ $CI = "true" ]; then + red="" + green="" + reset="" +else + red=`tput setaf 1` + green=`tput setaf 2` + reset=`tput sgr0` +fi echo "${green}Generating bindings${reset}" dotnet $rootdir/bin/${configuration}_${platform}/CppSharp.CLI.dll \ diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index ef088f0a6..70a189d90 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -7,9 +7,15 @@ configuration=debug platform=x64 jsinterp="$dir/runtime/build/qjs" -red=`tput setaf 1` -green=`tput setaf 2` -reset=`tput sgr0` +if [ $CI = "true" ]; then + red="" + green="" + reset="" +else + red=`tput setaf 1` + green=`tput setaf 2` + reset=`tput sgr0` +fi generate=true diff --git a/tests/ts/test.sh b/tests/ts/test.sh index a468220a7..e2f279d9c 100755 --- a/tests/ts/test.sh +++ b/tests/ts/test.sh @@ -7,9 +7,15 @@ configuration=debug platform=x64 jsinterp="$rootdir/deps/quickjs/qjs-debug" -red=`tput setaf 1` -green=`tput setaf 2` -reset=`tput sgr0` +if [ $CI = "true" ]; then + red="" + green="" + reset="" +else + red=`tput setaf 1` + green=`tput setaf 2` + reset=`tput sgr0` +fi generate=true From 6f71182793783ca27eb3895325f1c1fe12998a2d Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 17:27:26 +0100 Subject: [PATCH 22/33] WIP CI fixes. --- tests/quickjs/bootstrap.sh | 2 ++ tests/quickjs/premake5.lua | 11 +++++------ tests/quickjs/test.sh | 8 +++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/quickjs/bootstrap.sh b/tests/quickjs/bootstrap.sh index c0d1666e7..f27eed021 100755 --- a/tests/quickjs/bootstrap.sh +++ b/tests/quickjs/bootstrap.sh @@ -3,6 +3,8 @@ set -e dir=$(cd "$(dirname "$0")"; pwd) rootdir="$dir/../.." +cd $dir + if [ ! -d runtime ]; then git clone https://github.com/quickjs-ng/quickjs.git runtime git -C runtime reset --hard 0e5e9c2c49db15ab9579edeb4d90e610c8b8463f diff --git a/tests/quickjs/premake5.lua b/tests/quickjs/premake5.lua index 784ede1b3..45f408c1c 100644 --- a/tests/quickjs/premake5.lua +++ b/tests/quickjs/premake5.lua @@ -1,5 +1,4 @@ -local qjs_dir = path.getabsolute("./runtime") -local runtime = "../../src/Generator/Generators/QuickJS/Runtime" +local cppsharp_qjs_runtime = "../../src/Generator/Generators/QuickJS/Runtime" workspace "qjs" configurations { "debug", "release" } @@ -13,13 +12,13 @@ workspace "qjs" files { "gen/**.cpp", - runtime .. "/*.cpp", - runtime .. "/*.c" + cppsharp_qjs_runtime .. "/*.cpp", + cppsharp_qjs_runtime .. "/*.c" } includedirs { - qjs_dir, - runtime, + "runtime", + cppsharp_qjs_runtime, "..", "../../include" } diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index 70a189d90..21436ab8a 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -7,7 +7,9 @@ configuration=debug platform=x64 jsinterp="$dir/runtime/build/qjs" -if [ $CI = "true" ]; then +cd $dir + +if [ "$CI" = "true" ]; then red="" green="" reset="" @@ -27,8 +29,8 @@ fi echo "${green}Building generated binding files${reset}" premake=$rootdir/build/premake.sh -config=$configuration $premake --file=$dir/premake5.lua gmake -make -C $dir/gen +config=$configuration $premake --file=$dir/premake5.lua gmake2 +verbose=true make -C $dir/gen echo echo "${green}Executing JS tests with QuickJS${reset}" From 4aa02799554830ce7f35fde203e6b8ab054a03e1 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 19:05:39 +0100 Subject: [PATCH 23/33] Fix warnings in QuickJS support runtime code. --- .../Generators/QuickJS/Runtime/CppSharp_QuickJS.h | 5 +++++ src/Generator/Generators/QuickJS/Runtime/Signal.cpp | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h index fb897ae83..5334b1c84 100644 --- a/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h +++ b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS.h @@ -174,12 +174,17 @@ static JSValue JS_Interop_CleanupObject(JSValue obj, JS_Interop_InstanceKind kin switch (kind) { case JS_INTEROP_INSTANCE_SIGNAL_CONTEXT: + { JS_Interop_ClassData* data = (JS_Interop_ClassData*) JS_GetOpaque(obj, JS_GetClassID(obj)); if (data) { JS_Interop_FreeEventMap(data->ctx, &data->events); js_free(data->ctx, data); } + break; + } + case JS_INTEROP_INSTANCE_RAW_POINTER: + break; } return JS_UNDEFINED; diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp index 96be089b7..7ccb2798c 100644 --- a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp +++ b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp @@ -4,6 +4,7 @@ // Do not edit this file or all your changes will be lost after re-generation. // // ---------------------------------------------------------------------------- +#include "quickjs.h" #include #include @@ -67,7 +68,7 @@ static JSValue callback_method_Signal_connect(JSContext* ctx, JSValueConst this_ // Connect logic - auto signalCtx = (JS_SignalContext*) JS_GetOpaque(this_val, classId__Signal); + JS_SignalContext* signalCtx = (JS_SignalContext*) JS_GetOpaque(this_val, classId__Signal); if (signalCtx == nullptr) return JS_ThrowTypeError(ctx, "Could not find signal context"); @@ -127,7 +128,7 @@ static JSValue callback_method_Signal_isEmpty(JSContext* ctx, JSValueConst this_ return JS_ThrowRangeError(ctx, "Unsupported number of arguments"); } - auto signalCtx = (JS_SignalContext*) JS_GetOpaque(this_val, classId__Signal); + JS_SignalContext* signalCtx = (JS_SignalContext*) JS_GetOpaque(this_val, classId__Signal); JSValue ____ret = JS_NewBool(ctx, JS_IsUndefined(signalCtx->function)); @@ -142,14 +143,14 @@ static JSValue callback_class__Signal_toString(JSContext* ctx, JSValueConst this void finalizer__Signal(JSRuntime *rt, JSValue val) { - auto signalCtx = (JS_SignalContext*) JS_GetOpaque(val, classId__Signal); + JS_SignalContext* signalCtx = (JS_SignalContext*) JS_GetOpaque(val, classId__Signal); if (signalCtx == nullptr) return; if (!JS_IsUndefined(signalCtx->function)) return JS_FreeValue(signalCtx->ctx, signalCtx->function); - delete signalCtx; + js_free_rt(rt, signalCtx); JS_SetOpaque(val, nullptr); From 4a7480654c31c211dbb24fd0fd4920dea14d4ca7 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 19:15:14 +0100 Subject: [PATCH 24/33] Use C99 designated initializers for all QuickJS class def members. --- src/Generator/Generators/QuickJS/QuickJSSources.cs | 2 +- src/Generator/Generators/QuickJS/Runtime/Signal.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 04e983159..998638bcb 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -320,7 +320,7 @@ public override bool VisitClassDecl(Class @class) WriteLine($"static JSClassDef classDef_{GetCIdentifier(Context, @class)}"); WriteOpenBraceAndIndent(); - WriteLine($"\"{@class.Name}\","); + WriteLine($".class_name = \"{@class.Name}\","); WriteLine($".finalizer = {finalizerId}"); Unindent(); diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp index 7ccb2798c..789dd1742 100644 --- a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp +++ b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp @@ -153,12 +153,11 @@ void finalizer__Signal(JSRuntime *rt, JSValue val) js_free_rt(rt, signalCtx); JS_SetOpaque(val, nullptr); - } static JSClassDef classDef__Signal { - "Signal", + .class_name = "Signal", .finalizer = finalizer__Signal }; From e5a1942c651bf03202a2d517cb650cb81f6d2ba7 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 19:18:03 +0100 Subject: [PATCH 25/33] Disable QuickJS CI steps on Windows. --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 961c3cebf..37080ed7a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,10 +62,12 @@ jobs: - 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 From f8ff3a5b94dfa2e3ec5c3d5fd543cf390821fc7b Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 19:31:44 +0100 Subject: [PATCH 26/33] Clean up error handling for `JS_ToBool`. --- src/Generator/Generators/QuickJS/QuickJSMarshal.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 1ec7e7330..43fda6c9b 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -509,8 +509,6 @@ public bool VisitPrimitiveType(PrimitiveType primitive) case PrimitiveType.Bool: Context.Before.WriteLine($"{argName} = JS_ToBool(ctx, argv[{Context.ParameterIndex}]);"); - Context.Before.WriteLine($"if ({argName} == -1)"); - Context.Before.WriteLineIndent("return JS_EXCEPTION;"); Context.Return.Write($"{argName}"); return true; From 260f1205a2bc939b58640f12f33d761ed85e99fd Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 19:51:46 +0100 Subject: [PATCH 27/33] More QuickJS support code fixes. --- src/Generator/Generators/QuickJS/QuickJSSources.cs | 4 ++-- src/Generator/Generators/QuickJS/Runtime/Signal.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSSources.cs b/src/Generator/Generators/QuickJS/QuickJSSources.cs index 998638bcb..f4d88ccd0 100644 --- a/src/Generator/Generators/QuickJS/QuickJSSources.cs +++ b/src/Generator/Generators/QuickJS/QuickJSSources.cs @@ -317,7 +317,7 @@ public override bool VisitClassDecl(Class @class) PushBlock(); { - WriteLine($"static JSClassDef classDef_{GetCIdentifier(Context, @class)}"); + WriteLine($"static JSClassDef classDef_{GetCIdentifier(Context, @class)} ="); WriteOpenBraceAndIndent(); WriteLine($".class_name = \"{@class.Name}\","); @@ -330,7 +330,7 @@ public override bool VisitClassDecl(Class @class) PushBlock(); { - WriteLine($"static JSCFunctionListEntry funcDef_{GetCIdentifier(Context, @class)}[]"); + WriteLine($"static JSCFunctionListEntry funcDef_{GetCIdentifier(Context, @class)}[] ="); WriteOpenBraceAndIndent(); var funcGen = new QuickJSClassFuncDef(Context); diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp index 789dd1742..16de385a7 100644 --- a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp +++ b/src/Generator/Generators/QuickJS/Runtime/Signal.cpp @@ -155,13 +155,13 @@ void finalizer__Signal(JSRuntime *rt, JSValue val) JS_SetOpaque(val, nullptr); } -static JSClassDef classDef__Signal +static JSClassDef classDef__Signal = { .class_name = "Signal", .finalizer = finalizer__Signal }; -static JSCFunctionListEntry funcDef__Signal[] +static JSCFunctionListEntry funcDef__Signal[] = { JS_CFUNC_DEF("connect", 1, callback_method_Signal_connect), JS_CFUNC_DEF("disconnect", 1, callback_method_Signal_disconnect), From 54e809e617f38e47e4e56b65698b1bcd0a7ad6ae Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 20:14:09 +0100 Subject: [PATCH 28/33] Rename Signal.cpp to QuickJS nomenclature. --- .../QuickJS/Runtime/{Signal.cpp => CppSharp_QuickJS_Signal.cpp} | 0 .../QuickJS/Runtime/{Signal.h => CppSharp_QuickJS_Signal.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/Generator/Generators/QuickJS/Runtime/{Signal.cpp => CppSharp_QuickJS_Signal.cpp} (100%) rename src/Generator/Generators/QuickJS/Runtime/{Signal.h => CppSharp_QuickJS_Signal.h} (100%) diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.cpp b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS_Signal.cpp similarity index 100% rename from src/Generator/Generators/QuickJS/Runtime/Signal.cpp rename to src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS_Signal.cpp diff --git a/src/Generator/Generators/QuickJS/Runtime/Signal.h b/src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS_Signal.h similarity index 100% rename from src/Generator/Generators/QuickJS/Runtime/Signal.h rename to src/Generator/Generators/QuickJS/Runtime/CppSharp_QuickJS_Signal.h From 5581351824b6b0456291effe94a5e11f9b2dccbd Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 20:14:31 +0100 Subject: [PATCH 29/33] Fix QuickJS JS_ToBigUint call. --- src/Generator/Generators/QuickJS/QuickJSMarshal.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 43fda6c9b..80d921125 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -568,7 +568,7 @@ public bool VisitPrimitiveType(PrimitiveType primitive) return true; case PrimitiveType.ULongLong: - Context.Before.WriteLine($"if (JS_ToBigUInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLine($"if (JS_ToBigUint64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); Context.Before.WriteLineIndent("return JS_EXCEPTION;"); Context.Return.Write($"{argName}"); return true; From 9d916166ef0e143c3afe7263c8b889eb6f55044c Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 20:14:41 +0100 Subject: [PATCH 30/33] Remove QuickJS test script verbosity. --- tests/quickjs/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index 21436ab8a..f9f8624e0 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -29,8 +29,8 @@ fi echo "${green}Building generated binding files${reset}" premake=$rootdir/build/premake.sh -config=$configuration $premake --file=$dir/premake5.lua gmake2 -verbose=true make -C $dir/gen +config=$configuration $premake --cc=clang --file=$dir/premake5.lua gmake2 +make -C $dir/gen echo echo "${green}Executing JS tests with QuickJS${reset}" From 7d1af87136ada073a542f509fc61d00f612f0ae0 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 20:25:30 +0100 Subject: [PATCH 31/33] More CI fixes. --- src/Generator/Generators/QuickJS/QuickJSMarshal.cs | 4 ++-- tests/quickjs/premake5.lua | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 80d921125..02b109470 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -562,13 +562,13 @@ public bool VisitPrimitiveType(PrimitiveType primitive) return true; case PrimitiveType.LongLong: - Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&_{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLine($"if (JS_ToBigInt64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); Context.Before.WriteLineIndent("return JS_EXCEPTION;"); Context.Return.Write($"{argName}"); return true; case PrimitiveType.ULongLong: - Context.Before.WriteLine($"if (JS_ToBigUint64(ctx, (int64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); + Context.Before.WriteLine($"if (JS_ToBigUint64(ctx, (uint64_t*)&{argName}, argv[{Context.ParameterIndex}]))"); Context.Before.WriteLineIndent("return JS_EXCEPTION;"); Context.Return.Write($"{argName}"); return true; diff --git a/tests/quickjs/premake5.lua b/tests/quickjs/premake5.lua index 45f408c1c..c551eec93 100644 --- a/tests/quickjs/premake5.lua +++ b/tests/quickjs/premake5.lua @@ -9,6 +9,7 @@ workspace "qjs" project "test" kind "SharedLib" language "C++" + cppdialect "C++11" files { "gen/**.cpp", @@ -29,3 +30,4 @@ workspace "qjs" defines { "JS_SHARED_LIBRARY" } filter { "kind:SharedLib", "system:macosx" } linkoptions { "-undefined dynamic_lookup" } + targetextension ("so") From cad11b6d7b849b546c5d1f92980e5bd8aeae2dd4 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 21:30:03 +0100 Subject: [PATCH 32/33] Verbose build. --- tests/quickjs/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/quickjs/test.sh b/tests/quickjs/test.sh index f9f8624e0..21436ab8a 100755 --- a/tests/quickjs/test.sh +++ b/tests/quickjs/test.sh @@ -29,8 +29,8 @@ fi echo "${green}Building generated binding files${reset}" premake=$rootdir/build/premake.sh -config=$configuration $premake --cc=clang --file=$dir/premake5.lua gmake2 -make -C $dir/gen +config=$configuration $premake --file=$dir/premake5.lua gmake2 +verbose=true make -C $dir/gen echo echo "${green}Executing JS tests with QuickJS${reset}" From 7a77d8f69c657fbc3e7a22a00a8633fe04c838fb Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 3 Sep 2024 21:37:26 +0100 Subject: [PATCH 33/33] Extension fix. --- tests/quickjs/premake5.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/quickjs/premake5.lua b/tests/quickjs/premake5.lua index c551eec93..cde6ac873 100644 --- a/tests/quickjs/premake5.lua +++ b/tests/quickjs/premake5.lua @@ -30,4 +30,4 @@ workspace "qjs" defines { "JS_SHARED_LIBRARY" } filter { "kind:SharedLib", "system:macosx" } linkoptions { "-undefined dynamic_lookup" } - targetextension ("so") + targetextension (".so")