forked from bytecodealliance/wit-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,939 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "wit-bindgen-csharp" | ||
authors = ["Timmy Silesmo <[email protected]>"] | ||
version = "0.13.0" | ||
edition.workspace = true | ||
repository = 'https://github.com/bytecodealliance/wit-bindgen' | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
homepage = 'https://github.com/bytecodealliance/wit-bindgen' | ||
description = """ | ||
C# bindings generator for WIT and the component model, typically used | ||
through the `wit-bindgen-cli` crate. | ||
""" | ||
|
||
[lib] | ||
doctest = false | ||
test = false | ||
|
||
[dependencies] | ||
wasm-encoder = { workspace = true } | ||
wit-bindgen-core = { workspace = true } | ||
wit-component = { workspace = true } | ||
wasm-metadata = { workspace = true } | ||
heck = { workspace = true } | ||
clap = { workspace = true, optional = true } | ||
anyhow = { workspace = true } | ||
|
||
[dev-dependencies] | ||
test-helpers = { path = '../test-helpers' } |
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,3 @@ | ||
// generate the c# and the component meta module | ||
|
||
cargo run c-sharp --string-encoding utf8 --out-dir testing-csharp tests/codegen/floats.wit |
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,58 @@ | ||
use anyhow::Result; | ||
use heck::ToSnakeCase; | ||
use wasm_encoder::{ | ||
CodeSection, CustomSection, Function, FunctionSection, LinkingSection, Module, SymbolTable, | ||
TypeSection, | ||
}; | ||
use wit_bindgen_core::wit_parser::{Resolve, WorldId}; | ||
use wit_component::StringEncoding; | ||
|
||
pub fn linking_symbol(name: &str) -> String { | ||
let snake = name.to_snake_case(); | ||
format!("__component_type_object_force_link_{snake}") | ||
} | ||
|
||
pub fn object(resolve: &Resolve, world: WorldId, encoding: StringEncoding) -> Result<Vec<u8>> { | ||
let mut module = Module::new(); | ||
|
||
// Build a module with one function that's a "dummy function" | ||
let mut types = TypeSection::new(); | ||
types.function([], []); | ||
module.section(&types); | ||
let mut funcs = FunctionSection::new(); | ||
funcs.function(0); | ||
module.section(&funcs); | ||
let mut code = CodeSection::new(); | ||
code.function(&Function::new([])); | ||
module.section(&code); | ||
|
||
let mut producers = wasm_metadata::Producers::empty(); | ||
producers.add( | ||
"processed-by", | ||
env!("CARGO_PKG_NAME"), | ||
env!("CARGO_PKG_VERSION"), | ||
); | ||
let data = wit_component::metadata::encode(resolve, world, encoding, Some(&producers)).unwrap(); | ||
|
||
// The custom section name here must start with "component-type" but | ||
// otherwise is attempted to be unique here to ensure that this doesn't get | ||
// concatenated to other custom sections by LLD by accident since LLD will | ||
// concatenate custom sections of the same name. | ||
let world_name = &resolve.worlds[world].name; | ||
let section_name = format!("component-type:{world_name}"); | ||
|
||
// Add our custom section | ||
module.section(&CustomSection { | ||
name: std::borrow::Cow::Borrowed(§ion_name), | ||
data: std::borrow::Cow::Borrowed(data.as_slice()), | ||
}); | ||
|
||
// Append the linking section, so that lld knows the custom section's symbol name | ||
let mut linking = LinkingSection::new(); | ||
let mut symbols = SymbolTable::new(); | ||
symbols.function(0, 0, Some(&linking_symbol(&world_name))); | ||
linking.symbol_table(&symbols); | ||
module.section(&linking); | ||
|
||
Ok(module.finish()) | ||
} |
Oops, something went wrong.