Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions crates/spidermonkey-embedding-splicer/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ pub fn componentize_bindgen(
// consolidate import specifiers and generate wrappers
// we do this separately because function index order matters
let mut import_bindings = Vec::new();
for (_, item) in bindgen.imports.iter() {
for (specifier, item) in bindgen.imports.iter() {
// this import binding order matters
import_bindings.push(binding_name(
import_bindings.push(binding_name_import(
&item.resource.func_name(&item.name),
&item.iface_name,
specifier,
));
}

Expand Down Expand Up @@ -204,7 +205,8 @@ pub fn componentize_bindgen(
let item = items.first().unwrap();
if let Some(resource) = resource {
let export_name = resource.to_upper_camel_case();
let binding_name = binding_name(&export_name, &item.iface_name);
let binding_name =
binding_name_import(&export_name, &item.iface_name, &item.binding_name);
if item.iface {
specifier_list.push(format!("{export_name}: import_{binding_name}"));
} else {
Expand All @@ -213,13 +215,12 @@ pub fn componentize_bindgen(
} else {
for BindingItem {
iface,
iface_name,
name,
binding_name,
..
} in items
{
let export_name = name.to_lower_camel_case();
let binding_name = binding_name(&export_name, iface_name);
if *iface {
specifier_list.push(format!("{export_name}: import_{binding_name}"));
} else {
Expand Down Expand Up @@ -654,11 +655,11 @@ impl JsBindgen<'_> {
let fn_name = func.item_name();
let fn_camel_name = fn_name.to_lower_camel_case();

use binding_name as binding_name_fn;
use binding_name_import as binding_name_fn;

let (binding_name, resource) = match &func.kind {
FunctionKind::Freestanding => {
let binding_name = binding_name(&fn_camel_name, &iface_name);
let binding_name = binding_name_import(&fn_camel_name, &iface_name, &import_name);

uwrite!(self.src, "\nfunction import_{binding_name}");

Expand Down Expand Up @@ -702,7 +703,11 @@ impl JsBindgen<'_> {
func.params.len(),
&format!(
"$import_{}",
binding_name_fn(&resource.func_name(fn_name), &iface_name)
binding_name_fn(
&resource.func_name(fn_name),
&iface_name,
import_name.as_str()
)
),
StringEncoding::UTF8,
func,
Expand Down Expand Up @@ -1294,6 +1299,21 @@ fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
}
}

fn binding_name_import(func_name: &str, iface_name: &Option<String>, import_name: &str) -> String {
let valid_import = import_name
.chars()
.map(|c| if c.is_alphanumeric() { c } else { '_' })
.collect::<String>();

if import_name != "<<INVALID>>" {
format!("{valid_import}${func_name}")
} else if let Some(iface_name) = iface_name {
format!("{iface_name}${func_name}")
} else {
func_name.to_string()
}
}

/// Extract success and error types from a given optional type, if it is a Result
pub fn get_result_types(
resolve: &Resolve,
Expand Down
Loading