-
Notifications
You must be signed in to change notification settings - Fork 59
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
9 changed files
with
175 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "livesplit-core" | ||
version = "0.1.3" | ||
version = "0.1.4" | ||
authors = ["Christopher Serr <[email protected]>"] | ||
documentation = "https://docs.rs/livesplit-core/" | ||
repository = "https://github.com/CryZe/livesplit-core" | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "livesplit-core-capi" | ||
version = "0.1.3" | ||
version = "0.1.4" | ||
authors = ["Christopher Serr <[email protected]>"] | ||
|
||
[dependencies] | ||
|
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
[package] | ||
authors = ["Christopher Serr <[email protected]>"] | ||
name = "bindings" | ||
version = "0.1.0" | ||
authors = ["Christopher Serr <[email protected]>"] | ||
|
||
[dependencies] | ||
heck = "0.1.0" | ||
syntex_syntax = "0.58.0" |
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
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,128 @@ | ||
use std::io::{Write, Result}; | ||
use {Function, Type, TypeKind}; | ||
use heck::MixedCase; | ||
|
||
fn get_type(ty: &Type) -> &str { | ||
match (ty.kind, ty.name.as_str()) { | ||
(TypeKind::Ref, "c_char") => "'CString'", | ||
(TypeKind::Ref, _) | | ||
(TypeKind::RefMut, _) => "'pointer'", | ||
(_, t) if !ty.is_custom => { | ||
match t { | ||
"i8" => "'int8'", | ||
"i16" => "'int16'", | ||
"i32" => "'int32'", | ||
"i64" => "'int64'", | ||
"u8" => "'uint8'", | ||
"u16" => "'uint16'", | ||
"u32" => "'uint32'", | ||
"u64" => "'uint64'", | ||
"usize" => "'size_t'", | ||
"f32" => "'float'", | ||
"f64" => "'double'", | ||
"bool" => "'bool'", | ||
"()" => "'void'", | ||
"c_char" => "'char'", | ||
x => x, | ||
} | ||
} | ||
_ => "'pointer'", | ||
} | ||
} | ||
|
||
pub fn write<W: Write>(mut writer: W, functions: &[Function]) -> Result<()> { | ||
let mut prefix = String::from(""); | ||
|
||
write!(writer, | ||
"{}", | ||
r#"var ffi = require('ffi'); | ||
var ls = ffi.Library('livesplit_core', {"#)?; | ||
|
||
for function in functions { | ||
let name = function.name.to_string(); | ||
let mut splits = name.splitn(2, '_'); | ||
let new_prefix = splits.next().unwrap(); | ||
if !prefix.is_empty() && new_prefix != prefix { | ||
writeln!(writer, "")?; | ||
} | ||
prefix = new_prefix.to_string(); | ||
|
||
write!(writer, | ||
r#" | ||
'{}': [{}, ["#, | ||
name, | ||
get_type(&function.output))?; | ||
|
||
for (i, &(_, ref typ)) in function.inputs.iter().enumerate() { | ||
if i != 0 { | ||
write!(writer, ", ")?; | ||
} | ||
write!(writer, "{}", get_type(typ))?; | ||
} | ||
|
||
write!(writer, "]],")?; | ||
} | ||
|
||
writeln!(writer, | ||
"{}", | ||
r#" | ||
});"#)?; | ||
|
||
for function in functions { | ||
let name = function.name.to_string(); | ||
let mut splits = name.splitn(2, '_'); | ||
let new_prefix = splits.next().unwrap(); | ||
let postfix = splits.next().unwrap(); | ||
|
||
write!(writer, | ||
r#" | ||
exports.{}_{} = function("#, | ||
new_prefix, | ||
postfix.to_mixed_case())?; | ||
|
||
for (i, &(ref name, _)) in function.inputs.iter().enumerate() { | ||
if i != 0 { | ||
write!(writer, ", ")?; | ||
} | ||
write!(writer, | ||
"{}", | ||
if name == "this" { | ||
String::from("self") | ||
} else { | ||
name.to_mixed_case() | ||
})?; | ||
} | ||
|
||
write!(writer, | ||
"{}", | ||
r#") { | ||
"#)?; | ||
|
||
if get_type(&function.output) != "'void'" { | ||
write!(writer, "return ")?; | ||
} | ||
|
||
write!(writer, r#"ls.{}("#, name)?; | ||
|
||
for (i, &(ref name, _)) in function.inputs.iter().enumerate() { | ||
if i != 0 { | ||
write!(writer, ", ")?; | ||
} | ||
write!(writer, | ||
"{}", | ||
if name == "this" { | ||
String::from("self") | ||
} else { | ||
name.to_mixed_case() | ||
})?; | ||
} | ||
|
||
writeln!(writer, | ||
"{}", | ||
r#"); | ||
};"#)?; | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.