Skip to content

Commit

Permalink
Generate Documentation Comments into Bindings
Browse files Browse the repository at this point in the history
Closes #94
  • Loading branch information
CryZe committed Jan 21, 2018
1 parent 6433eb3 commit 01bd9e1
Show file tree
Hide file tree
Showing 79 changed files with 2,391 additions and 394 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
dist: trusty
language: rust
services: docker
sudo: required
# sudo: required

env:
global:
Expand Down
40 changes: 36 additions & 4 deletions capi/bind_gen/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn get_type(ty: &Type) -> Cow<str> {
"usize" => "size_t",
"f32" => "float",
"f64" => "double",
"bool" => "uint8_t",
"bool" => "bool",
"()" => "void",
"c_char" => "char",
"Json" => "char const*",
Expand All @@ -39,8 +39,8 @@ pub fn write<W: Write>(mut writer: W, classes: &BTreeMap<String, Class>) -> Resu
write!(
writer,
"{}",
r#"#ifndef _LIVESPLIT_CORE_H_
#define _LIVESPLIT_CORE_H_
r#"#ifndef LIVESPLIT_CORE_H
#define LIVESPLIT_CORE_H
#ifdef __cplusplus
#define restrict __restrict
Expand All @@ -50,6 +50,7 @@ extern "C" {
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
"#
)?;
Expand All @@ -76,9 +77,40 @@ typedef struct {0}_s const* {0}Ref;
.chain(class.shared_fns.iter())
.chain(class.mut_fns.iter())
{
if function.method == "drop" {
writeln!(
writer,
r#"/**
Frees the object, allowing it to clean up all of its memory. You need
to call this for every object that you don't use anymore and hasn't
already been freed.
*/"#
)?;
} else if !function.comments.is_empty() {
write!(writer, r#"/**"#)?;

for comment in &function.comments {
write!(
writer,
r#"
{}"#,
comment
.replace("<NULL>", "NULL")
.replace("<TRUE>", "true")
.replace("<FALSE>", "false")
)?;
}

writeln!(
writer,
r#"
*/"#
)?;
}

write!(
writer,
r#"extern {} {}("#,
r#"{} {}("#,
get_type(&function.output),
function.name
)?;
Expand Down
68 changes: 66 additions & 2 deletions capi/bind_gen/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,65 @@ fn get_ll_type(ty: &Type, output: bool) -> &str {
}
}

fn write_class_comments<W: Write>(mut writer: W, comments: &[String]) -> Result<()> {
write!(
writer,
r#"
/// <summary>"#
)?;

for comment in comments {
write!(
writer,
r#"
/// {}"#,
comment
.replace("<NULL>", "null")
.replace("<TRUE>", "true")
.replace("<FALSE>", "false")
)?;
}

write!(
writer,
r#"
/// </summary>"#
)
}

fn write_fn<W: Write>(mut writer: W, function: &Function, class_name: &str) -> Result<()> {
let is_static = function.is_static();
let has_return_type = function.has_return_type();
let return_type = get_hl_type(&function.output);
let return_type_ll = get_ll_type(&function.output, true);
let is_constructor = function.method == "new" && !function.output.is_nullable;

if !function.comments.is_empty() {
write!(
writer,
r#"
/// <summary>"#
)?;

for comment in &function.comments {
write!(
writer,
r#"
/// {}"#,
comment
.replace("<NULL>", "null")
.replace("<TRUE>", "true")
.replace("<FALSE>", "false")
)?;
}

write!(
writer,
r#"
/// </summary>"#
)?;
}

if is_constructor {
write!(
writer,
Expand Down Expand Up @@ -232,6 +284,8 @@ namespace LiveSplitCore
let class_name_ref = format!("{}Ref", class_name);
let class_name_ref_mut = format!("{}RefMut", class_name);

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
Expand Down Expand Up @@ -275,7 +329,15 @@ namespace LiveSplitCore
this.ptr = ptr;
}}
}}
"#,
base_class = class_name_ref
)?;

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
public class {class} : {base_class}
{{"#,
class = class_name_ref_mut,
Expand All @@ -290,14 +352,16 @@ namespace LiveSplitCore
writer,
r#"
internal {class}(IntPtr ptr) : base(ptr) {{ }}
}}"#,
}}
"#,
class = class_name_ref_mut
)?;

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
public class {class} : {base_class}, IDisposable
{{
private void Drop()
Expand Down
129 changes: 106 additions & 23 deletions capi/bind_gen/src/emscripten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ fn get_ll_type(ty: &Type) -> &str {
}
}

fn write_class_comments<W: Write>(mut writer: W, comments: &[String]) -> Result<()> {
write!(
writer,
r#"
/**"#
)?;

for comment in comments {
write!(
writer,
r#"
* {}"#,
comment
.replace("<NULL>", "null")
.replace("<TRUE>", "true")
.replace("<FALSE>", "false")
)?;
}

write!(
writer,
r#"
*/"#
)
}

fn write_fn<W: Write>(mut writer: W, function: &Function, type_script: bool) -> Result<()> {
let is_static = function.is_static();
let has_return_type = function.has_return_type();
Expand All @@ -60,13 +86,35 @@ fn write_fn<W: Write>(mut writer: W, function: &Function, type_script: bool) ->
let method = function.method.to_mixed_case();
let is_json = has_return_type && function.output.name == "Json";

if !type_script {
if !function.comments.is_empty() || !type_script {
write!(
writer,
r#"
/**"#
)?;

for comment in &function.comments {
write!(
writer,
r#"
* {}"#,
comment
.replace("<NULL>", "null")
.replace("<TRUE>", "true")
.replace("<FALSE>", "false")
)?;
}

if type_script {
write!(
writer,
r#"
*/"#
)?;
}
}

if !type_script {
for &(ref name, ref ty) in function.inputs.iter().skip(if is_static { 0 } else { 1 }) {
write!(
writer,
Expand Down Expand Up @@ -298,6 +346,8 @@ const liveSplitCoreNative = {};"#
let class_name_ref = format!("{}Ref", class_name);
let class_name_ref_mut = format!("{}RefMut", class_name);

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
Expand Down Expand Up @@ -364,13 +414,17 @@ const liveSplitCoreNative = {};"#
write!(
writer,
r#"
/**
* This constructor is an implementation detail. Do not use this.
*/
constructor(ptr: number) {{"#
)?;
} else {
write!(
writer,
r#"
/**
* This constructor is an implementation detail. Do not use this.
* @param {{number}} ptr
*/
constructor(ptr) {{"#
Expand All @@ -383,20 +437,27 @@ const liveSplitCoreNative = {};"#
this.ptr = ptr;
}}
}}
"#
)?;

if !type_script {
write!(
writer,
r#"exports.{base_class} = {base_class};
"#,
base_class = class_name_ref
)?;
}

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
{export}class {class} extends {base_class} {{"#,
class = class_name_ref_mut,
base_class = class_name_ref,
export = if type_script {
r#"
export "#.to_string()
} else {
format!(
r#"exports.{base_class} = {base_class};
"#,
base_class = class_name_ref
)
}
export = if type_script { "export " } else { "" }
)?;

for function in &class.mut_fns {
Expand Down Expand Up @@ -465,26 +526,39 @@ export "#.to_string()
writer,
r#"
}}
"#
)?;

if !type_script {
write!(
writer,
r#"exports.{base_class} = {base_class};
"#,
base_class = class_name_ref_mut
)?;
}

write_class_comments(&mut writer, &class.comments)?;

write!(
writer,
r#"
{export}class {class} extends {base_class} {{"#,
class = class_name,
base_class = class_name_ref_mut,
export = if type_script {
r#"
export "#.to_string()
} else {
format!(
r#"exports.{base_class} = {base_class};
"#,
base_class = class_name_ref_mut
)
}
export = if type_script { "export " } else { "" }
)?;

if type_script {
write!(
writer,
r#"
/**
* Allows for scoped usage of the object. The object is guaranteed to get
* disposed once this function returns. You are free to dispose the object
* early yourself anywhere within the scope. The scope's return value gets
* carried to the outside of this function.
*/
with<T>(closure: (obj: {class}) => T): T {{"#,
class = class_name
)?;
Expand All @@ -493,6 +567,10 @@ export "#.to_string()
writer,
r#"
/**
* Allows for scoped usage of the object. The object is guaranteed to get
* disposed once this function returns. You are free to dispose the object
* early yourself anywhere within the scope. The scope's return value gets
* carried to the outside of this function.
* @param {{function({class})}} closure
*/
with(closure) {{"#,
Expand All @@ -509,6 +587,11 @@ export "#.to_string()
this.dispose();
}}
}}
/**
* Disposes the object, allowing it to clean up all of its memory. You need
* to call this for every object that you don't use anymore and hasn't
* already been disposed.
*/
dispose() {{
if (this.ptr != 0) {{"#
)?;
Expand Down
Loading

0 comments on commit 01bd9e1

Please sign in to comment.