mirror of https://github.com/mono/CppSharp.git
7 changed files with 166 additions and 1 deletions
@ -0,0 +1,72 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using CppSharp.AST; |
||||||
|
using CppSharp.Generators.Cpp; |
||||||
|
|
||||||
|
namespace CppSharp.Generators.C |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// N-API generator responsible for driving the generation of binding files.
|
||||||
|
/// N-API documentation: https://nodejs.org/api/n-api.html
|
||||||
|
/// </summary>
|
||||||
|
public class NAPIGenerator : CppGenerator |
||||||
|
{ |
||||||
|
public NAPIGenerator(BindingContext context) : base(context) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public override List<GeneratorOutput> 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<CodeGenerator> Generate(IEnumerable<TranslationUnit> units) |
||||||
|
{ |
||||||
|
var outputs = new List<CodeGenerator>(); |
||||||
|
|
||||||
|
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<CodeGenerator> { moduleGen } |
||||||
|
}; |
||||||
|
|
||||||
|
output.Outputs[0].Process(); |
||||||
|
|
||||||
|
return output; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using CppSharp.AST; |
||||||
|
|
||||||
|
namespace CppSharp.Generators.Cpp |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Generates Node N-API C/C++ header files.
|
||||||
|
/// N-API documentation: https://nodejs.org/api/n-api.html
|
||||||
|
/// </summary>
|
||||||
|
public class NAPIHeaders : CppHeaders |
||||||
|
{ |
||||||
|
public NAPIHeaders(BindingContext context, IEnumerable<TranslationUnit> units) |
||||||
|
: base(context, units) |
||||||
|
{ |
||||||
|
CTypePrinter.PushContext(TypePrinterContextKind.Managed); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Process() |
||||||
|
{ |
||||||
|
base.Process(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using CppSharp.AST; |
||||||
|
using CppSharp.Generators.C; |
||||||
|
|
||||||
|
namespace CppSharp.Generators.Cpp |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Generates Node N-API C/C++ module init files.
|
||||||
|
/// N-API documentation: https://nodejs.org/api/n-api.html
|
||||||
|
/// </summary>
|
||||||
|
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(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using CppSharp.AST; |
||||||
|
|
||||||
|
namespace CppSharp.Generators.Cpp |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Generates Node N-API C/C++ source files.
|
||||||
|
/// N-API documentation: https://nodejs.org/api/n-api.html
|
||||||
|
/// </summary>
|
||||||
|
public class NAPISources : CppSources |
||||||
|
{ |
||||||
|
public NAPISources(BindingContext context, IEnumerable<TranslationUnit> units) |
||||||
|
: base(context, units) |
||||||
|
{ |
||||||
|
CTypePrinter.PushContext(TypePrinterContextKind.Managed); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Process() |
||||||
|
{ |
||||||
|
base.Process(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue