mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
Generate accessors for public struct fields
Automatically infer public struct fields as "JS wants to access this" and generate appropriate getters/setters for the field. At this time the field is required to implement `Copy`, but we will probably want to relax that in the future to at least encompass `JsValue` and maybe other `Clone` values as well. Closes #121
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
pub const SCHEMA_VERSION: &str = "2";
|
||||
pub const SCHEMA_VERSION: &str = "3";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ProgramOnlySchema {
|
||||
@ -14,7 +14,7 @@ pub struct Program {
|
||||
pub exports: Vec<Export>,
|
||||
pub enums: Vec<Enum>,
|
||||
pub imports: Vec<Import>,
|
||||
pub structs: Vec<String>,
|
||||
pub structs: Vec<Struct>,
|
||||
pub version: String,
|
||||
pub schema_version: String,
|
||||
}
|
||||
@ -82,6 +82,17 @@ pub struct Function {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Struct {
|
||||
pub name: String,
|
||||
pub fields: Vec<StructField>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct StructField {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub fn new_function(struct_name: &str) -> String {
|
||||
let mut name = format!("__wbg_");
|
||||
name.extend(struct_name
|
||||
@ -114,6 +125,26 @@ pub fn struct_function_export_name(struct_: &str, f: &str) -> String {
|
||||
return name
|
||||
}
|
||||
|
||||
pub fn struct_field_get(struct_: &str, f: &str) -> String {
|
||||
let mut name = String::from("__wbg_get_");
|
||||
name.extend(struct_
|
||||
.chars()
|
||||
.flat_map(|s| s.to_lowercase()));
|
||||
name.push_str("_");
|
||||
name.push_str(f);
|
||||
return name
|
||||
}
|
||||
|
||||
pub fn struct_field_set(struct_: &str, f: &str) -> String {
|
||||
let mut name = String::from("__wbg_set_");
|
||||
name.extend(struct_
|
||||
.chars()
|
||||
.flat_map(|s| s.to_lowercase()));
|
||||
name.push_str("_");
|
||||
name.push_str(f);
|
||||
return name
|
||||
}
|
||||
|
||||
pub fn version() -> String {
|
||||
let mut v = env!("CARGO_PKG_VERSION").to_string();
|
||||
if let Some(s) = option_env!("WBG_VERSION") {
|
||||
|
Reference in New Issue
Block a user