Skip to content

Commit b4cece4

Browse files
committed
Add initial NAPI and JS generator.
1 parent dc185a6 commit b4cece4

File tree

7 files changed

+166
-1
lines changed

7 files changed

+166
-1
lines changed

src/Generator/Driver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Generator CreateGeneratorFromKind(GeneratorKind kind)
4747
return new CSharpGenerator(Context);
4848
case GeneratorKind.QuickJS:
4949
return new QuickJSGenerator(Context);
50+
case GeneratorKind.NAPI:
51+
return new NAPIGenerator(Context);
5052
}
5153

5254
throw new NotImplementedException();

src/Generator/Generator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public enum GeneratorKind
1717
ObjectiveC,
1818
Java,
1919
Swift,
20-
QuickJS
20+
QuickJS,
21+
NAPI
2122
}
2223

2324
/// <summary>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections.Generic;
2+
using CppSharp.AST;
3+
using CppSharp.Generators.Cpp;
4+
5+
namespace CppSharp.Generators.C
6+
{
7+
/// <summary>
8+
/// N-API generator responsible for driving the generation of binding files.
9+
/// N-API documentation: https://nodejs.org/api/n-api.html
10+
/// </summary>
11+
public class NAPIGenerator : CppGenerator
12+
{
13+
public NAPIGenerator(BindingContext context) : base(context)
14+
{
15+
}
16+
17+
public override List<GeneratorOutput> Generate()
18+
{
19+
var outputs = base.Generate();
20+
21+
foreach (var module in Context.Options.Modules)
22+
{
23+
if (module == Context.Options.SystemModule)
24+
continue;
25+
26+
var output = GenerateModule(module);
27+
if (output != null)
28+
{
29+
OnUnitGenerated(output);
30+
outputs.Add(output);
31+
}
32+
}
33+
34+
return outputs;
35+
}
36+
37+
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
38+
{
39+
var outputs = new List<CodeGenerator>();
40+
41+
var header = new NAPIHeaders(Context, units);
42+
outputs.Add(header);
43+
44+
var source = new NAPISources(Context, units);
45+
outputs.Add(source);
46+
47+
return outputs;
48+
}
49+
50+
public override GeneratorOutput GenerateModule(Module module)
51+
{
52+
if (module == Context.Options.SystemModule)
53+
return null;
54+
55+
var moduleGen = new NAPIModule(Context, module);
56+
57+
var output = new GeneratorOutput
58+
{
59+
TranslationUnit = new TranslationUnit
60+
{
61+
FilePath = $"{module.LibraryName}.cpp",
62+
Module = module
63+
},
64+
Outputs = new List<CodeGenerator> { moduleGen }
65+
};
66+
67+
output.Outputs[0].Process();
68+
69+
return output;
70+
}
71+
}
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using CppSharp.AST;
3+
4+
namespace CppSharp.Generators.Cpp
5+
{
6+
/// <summary>
7+
/// Generates Node N-API C/C++ header files.
8+
/// N-API documentation: https://nodejs.org/api/n-api.html
9+
/// </summary>
10+
public class NAPIHeaders : CppHeaders
11+
{
12+
public NAPIHeaders(BindingContext context, IEnumerable<TranslationUnit> units)
13+
: base(context, units)
14+
{
15+
CTypePrinter.PushContext(TypePrinterContextKind.Managed);
16+
}
17+
18+
public override void Process()
19+
{
20+
base.Process();
21+
}
22+
}
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using CppSharp.AST;
3+
using CppSharp.Generators.C;
4+
5+
namespace CppSharp.Generators.Cpp
6+
{
7+
/// <summary>
8+
/// Generates Node N-API C/C++ module init files.
9+
/// N-API documentation: https://nodejs.org/api/n-api.html
10+
/// </summary>
11+
public class NAPIModule : CCodeGenerator
12+
{
13+
public NAPIModule(BindingContext context, Module module)
14+
: base(context, module.Units.GetGenerated())
15+
{
16+
CTypePrinter.PushContext(TypePrinterContextKind.Managed);
17+
}
18+
19+
public override string FileExtension { get; } = "cpp";
20+
21+
public override void Process()
22+
{
23+
var include = new CInclude()
24+
{
25+
File = "node/node_api.h",
26+
Kind = CInclude.IncludeKind.Angled
27+
};
28+
29+
WriteInclude(include);
30+
NewLine();
31+
32+
WriteLine("NAPI_MODULE_INIT()");
33+
WriteOpenBraceAndIndent();
34+
35+
WriteLine("napi_value result;");
36+
WriteLine("NAPI_CALL(env, napi_create_object(env, &result));");
37+
38+
WriteLine("return result;");
39+
40+
UnindentAndWriteCloseBrace();
41+
}
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using CppSharp.AST;
3+
4+
namespace CppSharp.Generators.Cpp
5+
{
6+
/// <summary>
7+
/// Generates Node N-API C/C++ source files.
8+
/// N-API documentation: https://nodejs.org/api/n-api.html
9+
/// </summary>
10+
public class NAPISources : CppSources
11+
{
12+
public NAPISources(BindingContext context, IEnumerable<TranslationUnit> units)
13+
: base(context, units)
14+
{
15+
CTypePrinter.PushContext(TypePrinterContextKind.Managed);
16+
}
17+
18+
public override void Process()
19+
{
20+
base.Process();
21+
}
22+
}
23+
}

src/Generator/Passes/CheckDuplicatedNamesPass.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ private TypePrinter GetTypePrinter(GeneratorKind kind, BindingContext context)
219219
break;
220220
case GeneratorKind.CPlusPlus:
221221
case GeneratorKind.QuickJS:
222+
case GeneratorKind.NAPI:
222223
typePrinter = new CppTypePrinter(Context);
223224
break;
224225
case GeneratorKind.CLI:

0 commit comments

Comments
 (0)