diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index f9aef2ed..73d07595 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -47,6 +47,8 @@ namespace CppSharp return new CSharpGenerator(Context); case GeneratorKind.QuickJS: return new QuickJSGenerator(Context); + case GeneratorKind.NAPI: + return new NAPIGenerator(Context); } throw new NotImplementedException(); diff --git a/src/Generator/Generator.cs b/src/Generator/Generator.cs index 979c3021..9a044e17 100644 --- a/src/Generator/Generator.cs +++ b/src/Generator/Generator.cs @@ -17,7 +17,8 @@ namespace CppSharp.Generators ObjectiveC, Java, Swift, - QuickJS + QuickJS, + NAPI } /// diff --git a/src/Generator/Generators/C/NAPI/NAPIGenerator.cs b/src/Generator/Generators/C/NAPI/NAPIGenerator.cs new file mode 100644 index 00000000..958dbe11 --- /dev/null +++ b/src/Generator/Generators/C/NAPI/NAPIGenerator.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using CppSharp.AST; +using CppSharp.Generators.Cpp; + +namespace CppSharp.Generators.C +{ + /// + /// N-API generator responsible for driving the generation of binding files. + /// N-API documentation: https://nodejs.org/api/n-api.html + /// + public class NAPIGenerator : CppGenerator + { + public NAPIGenerator(BindingContext context) : base(context) + { + } + + public override List Generate() + { + var outputs = base.Generate(); + + foreach (var module in Context.Options.Modules) + { + if (module == Context.Options.SystemModule) + continue; + + var output = GenerateModule(module); + if (output != null) + { + OnUnitGenerated(output); + outputs.Add(output); + } + } + + return outputs; + } + + public override List Generate(IEnumerable units) + { + var outputs = new List(); + + var header = new NAPIHeaders(Context, units); + outputs.Add(header); + + var source = new NAPISources(Context, units); + outputs.Add(source); + + return outputs; + } + + public override GeneratorOutput GenerateModule(Module module) + { + if (module == Context.Options.SystemModule) + return null; + + var moduleGen = new NAPIModule(Context, module); + + var output = new GeneratorOutput + { + TranslationUnit = new TranslationUnit + { + FilePath = $"{module.LibraryName}.cpp", + Module = module + }, + Outputs = new List { moduleGen } + }; + + output.Outputs[0].Process(); + + return output; + } + } +} \ No newline at end of file diff --git a/src/Generator/Generators/C/NAPI/NAPIHeaders.cs b/src/Generator/Generators/C/NAPI/NAPIHeaders.cs new file mode 100644 index 00000000..8c602c73 --- /dev/null +++ b/src/Generator/Generators/C/NAPI/NAPIHeaders.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using CppSharp.AST; + +namespace CppSharp.Generators.Cpp +{ + /// + /// Generates Node N-API C/C++ header files. + /// N-API documentation: https://nodejs.org/api/n-api.html + /// + public class NAPIHeaders : CppHeaders + { + public NAPIHeaders(BindingContext context, IEnumerable units) + : base(context, units) + { + CTypePrinter.PushContext(TypePrinterContextKind.Managed); + } + + public override void Process() + { + base.Process(); + } + } +} diff --git a/src/Generator/Generators/C/NAPI/NAPIModule.cs b/src/Generator/Generators/C/NAPI/NAPIModule.cs new file mode 100644 index 00000000..ef78e591 --- /dev/null +++ b/src/Generator/Generators/C/NAPI/NAPIModule.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using CppSharp.AST; +using CppSharp.Generators.C; + +namespace CppSharp.Generators.Cpp +{ + /// + /// Generates Node N-API C/C++ module init files. + /// N-API documentation: https://nodejs.org/api/n-api.html + /// + public class NAPIModule : CCodeGenerator + { + public NAPIModule(BindingContext context, Module module) + : base(context, module.Units.GetGenerated()) + { + CTypePrinter.PushContext(TypePrinterContextKind.Managed); + } + + public override string FileExtension { get; } = "cpp"; + + public override void Process() + { + var include = new CInclude() + { + File = "node/node_api.h", + Kind = CInclude.IncludeKind.Angled + }; + + WriteInclude(include); + NewLine(); + + WriteLine("NAPI_MODULE_INIT()"); + WriteOpenBraceAndIndent(); + + WriteLine("napi_value result;"); + WriteLine("NAPI_CALL(env, napi_create_object(env, &result));"); + + WriteLine("return result;"); + + UnindentAndWriteCloseBrace(); + } + } +} \ No newline at end of file diff --git a/src/Generator/Generators/C/NAPI/NAPISources.cs b/src/Generator/Generators/C/NAPI/NAPISources.cs new file mode 100644 index 00000000..ae70b169 --- /dev/null +++ b/src/Generator/Generators/C/NAPI/NAPISources.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using CppSharp.AST; + +namespace CppSharp.Generators.Cpp +{ + /// + /// Generates Node N-API C/C++ source files. + /// N-API documentation: https://nodejs.org/api/n-api.html + /// + public class NAPISources : CppSources + { + public NAPISources(BindingContext context, IEnumerable units) + : base(context, units) + { + CTypePrinter.PushContext(TypePrinterContextKind.Managed); + } + + public override void Process() + { + base.Process(); + } + } +} \ No newline at end of file diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index 7654d71e..a2832527 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -219,6 +219,7 @@ namespace CppSharp.Passes break; case GeneratorKind.CPlusPlus: case GeneratorKind.QuickJS: + case GeneratorKind.NAPI: typePrinter = new CppTypePrinter(Context); break; case GeneratorKind.CLI: