forked from mono/CppSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial Emscripten generator. (mono#1712)
- Loading branch information
Showing
21 changed files
with
719 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ namespace CppSharp | |
enum TargetArchitecture | ||
{ | ||
x86, | ||
x64 | ||
x64, | ||
WASM32, | ||
WASM64 | ||
} | ||
|
||
class Options | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ public enum GeneratorKind | |
CSharp = 2, | ||
C, | ||
CPlusPlus, | ||
Emscripten, | ||
ObjectiveC, | ||
Java, | ||
Swift, | ||
|
73 changes: 73 additions & 0 deletions
73
src/Generator/Generators/Emscripten/EmscriptenGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using CppSharp.AST; | ||
using CppSharp.Generators.Cpp; | ||
|
||
namespace CppSharp.Generators.Emscripten | ||
{ | ||
/// <summary> | ||
/// Emscripten generator responsible for driving the generation of binding files. | ||
/// Embind documentation: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html | ||
/// </summary> | ||
public class EmscriptenGenerator : CppGenerator | ||
{ | ||
public EmscriptenGenerator(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 EmscriptenHeaders(Context, units); | ||
outputs.Add(header); | ||
|
||
var source = new EmscriptenSources(Context, units); | ||
outputs.Add(source); | ||
|
||
return outputs; | ||
} | ||
|
||
public override GeneratorOutput GenerateModule(Module module) | ||
{ | ||
if (module == Context.Options.SystemModule) | ||
return null; | ||
|
||
var moduleGen = new EmscriptenModule(Context, module); | ||
|
||
var output = new GeneratorOutput | ||
{ | ||
TranslationUnit = new TranslationUnit | ||
{ | ||
FilePath = $"{module.LibraryName}_embind_module.cpp", | ||
Module = module | ||
}, | ||
Outputs = new List<CodeGenerator> { moduleGen } | ||
}; | ||
|
||
output.Outputs[0].Process(); | ||
|
||
return output; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using CppSharp.AST; | ||
|
||
namespace CppSharp.Generators.Emscripten | ||
{ | ||
/// <summary> | ||
/// Generates Emscripten Embind C/C++ header files. | ||
/// Embind documentation: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html | ||
/// </summary> | ||
public class EmscriptenHeaders : EmscriptenCodeGenerator | ||
{ | ||
public EmscriptenHeaders(BindingContext context, IEnumerable<TranslationUnit> units) | ||
: base(context, units) | ||
{ | ||
CTypePrinter.PushContext(TypePrinterContextKind.Managed); | ||
} | ||
|
||
//public override bool ShouldGenerateNamespaces => false; | ||
|
||
public override void Process() | ||
{ | ||
GenerateFilePreamble(CommentKind.BCPL); | ||
|
||
PushBlock(BlockKind.Includes); | ||
WriteLine("#pragma once"); | ||
NewLine(); | ||
PopBlock(); | ||
|
||
var name = GetTranslationUnitName(TranslationUnit); | ||
WriteLine($"extern \"C\" void embind_init_{name}();"); | ||
} | ||
|
||
public override bool VisitClassDecl(Class @class) | ||
{ | ||
return true; | ||
} | ||
|
||
public override bool VisitEvent(Event @event) | ||
{ | ||
return true; | ||
} | ||
|
||
public override bool VisitFieldDecl(Field field) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using CppSharp.Generators.C; | ||
|
||
namespace CppSharp.Generators.Emscripten | ||
{ | ||
public class EmscriptenMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter> | ||
{ | ||
public EmscriptenMarshalNativeToManagedPrinter(MarshalContext marshalContext) | ||
: base(marshalContext) | ||
{ | ||
} | ||
} | ||
|
||
public class EmscriptenMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter> | ||
{ | ||
public EmscriptenMarshalManagedToNativePrinter(MarshalContext ctx) | ||
: base(ctx) | ||
{ | ||
Context.MarshalToNative = this; | ||
} | ||
} | ||
} |
Oops, something went wrong.