Skip to content

Commit e46a37e

Browse files
authored
Merge pull request #45 from golemcloud/golem-imports-fixes
Fixes to make all Golem and Golem AI imports work
2 parents 8fa7ce0 + 81c4d0d commit e46a37e

60 files changed

Lines changed: 4790 additions & 378 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- name: Install cargo-component
5959
run: cargo binstall --force --locked cargo-component@0.21.1
6060
- name: Tests
61-
run: cargo test -- --nocapture --report-time --format junit --logfile target/report.xml
61+
run: cargo test -- --nocapture --test-threads=1 --report-time
6262
- name: Publish Test Report
6363
uses: mikepenz/action-junit-report@v5
6464
if: success() || failure() # always run even if the previous step fails

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasm-rquickjs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ camino = { workspace = true }
1313
fs_extra = { workspace = true }
1414
heck = { workspace = true }
1515
include_dir = { workspace = true }
16+
indexmap = "2.11.0"
1617
prettier-please = { workspace = true }
1718
proc-macro2 = { workspace = true }
1819
quote = { workspace = true }

crates/wasm-rquickjs/src/conversions.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::GeneratorContext;
22
use crate::javascript::escape_js_ident;
3-
use crate::rust_bindgen::escape_rust_ident;
3+
use crate::rust_bindgen::{RustType, TypeOwnershipStyle, escape_rust_ident, type_mode_for};
44
use crate::types::{get_wrapped_type, type_id_to_type_ref};
55
use anyhow::{Context, anyhow};
66
use heck::{ToLowerCamelCase, ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase};
@@ -80,13 +80,18 @@ fn generate_conversion_instances_for_type(
8080
);
8181
let field_name_lit = Lit::Str(LitStr::new(&js_field_name, Span::call_site()));
8282

83-
let field_type = get_wrapped_type(context, &field.ty)?;
83+
let rust_type = RustType::from_type(
84+
context,
85+
&field.ty,
86+
type_mode_for(context, &field.ty, TypeOwnershipStyle::Owned, "'_"),
87+
);
88+
let field_type = get_wrapped_type(context, &rust_type, &rust_type, &field.ty)?;
8489

8590
let original_field_type = &field_type.original_type_ref;
8691
let wrapped_field_type = &field_type.wrapped_type_ref;
8792

88-
let wrapped_field = (field_type.wrap)(quote! { self.#rust_field_ident });
89-
let unwrapped_field = (field_type.unwrap)(quote! { #rust_field_ident });
93+
let wrapped_field = field_type.wrap.run(quote! { self.#rust_field_ident });
94+
let unwrapped_field = field_type.unwrap.run(quote! { #rust_field_ident });
9095

9196
set_fields.push(quote! {
9297
let #rust_field_ident: #wrapped_field_type = #wrapped_field;
@@ -175,9 +180,14 @@ fn generate_conversion_instances_for_type(
175180
let case_name_lit = Lit::Str(LitStr::new(&case.name, Span::call_site()));
176181

177182
if let Some(ty) = &case.ty {
178-
let wrapped_type = get_wrapped_type(context, ty)?;
179-
let wrapped_inner = (wrapped_type.wrap)(quote! { inner });
180-
let unwrapped_inner = (wrapped_type.unwrap)(quote! { inner });
183+
let rust_type = RustType::from_type(
184+
context,
185+
ty,
186+
type_mode_for(context, ty, TypeOwnershipStyle::Owned, "'_"),
187+
);
188+
let wrapped_type = get_wrapped_type(context, &rust_type, &rust_type, ty)?;
189+
let wrapped_inner = wrapped_type.wrap.run(quote! { inner });
190+
let unwrapped_inner = wrapped_type.unwrap.run(quote! { inner });
181191
let wrapped_type = &wrapped_type.wrapped_type_ref;
182192

183193
into_cases.push(quote! {

crates/wasm-rquickjs/src/exports.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::javascript::escape_js_ident;
2-
use crate::rust_bindgen::escape_rust_ident;
2+
use crate::rust_bindgen::RustWitFunction;
33
use crate::types::{
44
ProcessedParameter, WrappedType, get_function_name, get_wrapped_type,
5-
ident_in_exported_interface, ident_in_exported_interface_or_global, identity_wrapper,
6-
param_refs_as_tuple, process_parameter, to_original_func_arg_list, to_wrapped_param_refs,
7-
type_borrows_resource,
5+
ident_in_exported_interface, ident_in_exported_interface_or_global, param_refs_as_tuple,
6+
process_parameter, to_original_func_arg_list, to_wrapped_param_refs, type_borrows_resource,
87
};
98
use crate::{EmbeddingMode, GeneratorContext, JsModuleSpec};
109
use anyhow::{Context, anyhow};
11-
use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
10+
use heck::{ToLowerCamelCase, ToUpperCamelCase};
1211
use proc_macro2::{Ident, Span, TokenStream};
1312
use quote::quote;
1413
use std::collections::BTreeMap;
@@ -294,18 +293,32 @@ fn generate_exported_function_impl(
294293
name: &str,
295294
function: &Function,
296295
) -> anyhow::Result<TokenStream> {
297-
let func_name = Ident::new(&escape_rust_ident(&name.to_snake_case()), Span::call_site());
296+
let rust_fn = RustWitFunction::new(context, name, function);
297+
let func_name = rust_fn.function_name_ident();
298+
298299
let param_ident_type: Vec<_> = function
299300
.params
300301
.iter()
301-
.map(|(param_name, param_type)| process_parameter(context, param_name, param_type))
302+
.zip(rust_fn.export_parameters)
303+
.zip(rust_fn.import_parameters)
304+
.map(
305+
|(((param_name, param_type), export_parameter), import_parameter)| {
306+
process_parameter(
307+
context,
308+
param_name,
309+
param_type,
310+
&export_parameter,
311+
&import_parameter,
312+
)
313+
},
314+
)
302315
.collect::<anyhow::Result<Vec<_>>>()?;
303316

304317
let func_arg_list = to_original_func_arg_list(&param_ident_type);
305318
let func_ret = match &function.result {
306-
Some(typ) => get_wrapped_type(context, typ)
319+
Some(typ) => get_wrapped_type(context, &rust_fn.return_type, &rust_fn.return_type, typ)
307320
.context(format!("Failed to encode result type for {name}"))?,
308-
None => WrappedType::unit(false),
321+
None => WrappedType::unit(),
309322
};
310323

311324
let param_refs = to_wrapped_param_refs(&param_ident_type);
@@ -346,7 +359,7 @@ fn generate_exported_function_impl(
346359
let original_result = &func_ret.original_type_ref;
347360
let wrapped_result = &func_ret.wrapped_type_ref;
348361
let unwrap = &func_ret.unwrap;
349-
let unwrap_result = (unwrap)(quote! { result });
362+
let unwrap_result = unwrap.run(quote! { result });
350363
let func_impl = quote! {
351364
fn #func_name(#(#func_arg_list),*) -> #original_result {
352365
crate::internal::async_exported_function(async move {
@@ -371,15 +384,16 @@ fn generate_exported_resource_function_impl(
371384
function: &Function,
372385
) -> anyhow::Result<TokenStream> {
373386
let func_name = get_function_name(name, &function)?;
374-
let func_name_ident = Ident::new(
375-
&escape_rust_ident(&func_name.to_snake_case()),
376-
Span::call_site(),
377-
);
387+
388+
let rust_fn = RustWitFunction::new(context, &func_name, function);
389+
let func_name_ident = rust_fn.function_name_ident();
378390

379391
let param_ident_type: Vec<_> = function
380392
.params
381393
.iter()
382-
.map(|(param_name, param_type)| {
394+
.zip(rust_fn.export_parameters)
395+
.zip(rust_fn.import_parameters)
396+
.map(|(((param_name, param_type), export_param), import_param)| {
383397
if matches!(
384398
function.kind,
385399
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_)
@@ -388,21 +402,29 @@ fn generate_exported_resource_function_impl(
388402
Ok(ProcessedParameter {
389403
ident: Ident::new(param_name, Span::call_site()),
390404
wrapped_type: None,
405+
export_parameter: export_param,
406+
import_parameter: import_param,
391407
})
392408
} else {
393-
process_parameter(context, param_name, param_type)
409+
process_parameter(
410+
context,
411+
param_name,
412+
param_type,
413+
&export_param,
414+
&import_param,
415+
)
394416
}
395417
})
396418
.collect::<anyhow::Result<Vec<_>>>()?;
397419

398420
let func_arg_list = to_original_func_arg_list(&param_ident_type);
399421
let func_ret = if matches!(function.kind, FunctionKind::Constructor(_)) {
400-
WrappedType::no_wrapping(quote! { Self }, identity_wrapper())
422+
WrappedType::no_wrapping(quote! { Self })
401423
} else {
402424
match &function.result {
403-
Some(typ) => get_wrapped_type(context, typ)
425+
Some(typ) => get_wrapped_type(context, &rust_fn.return_type, &rust_fn.return_type, typ)
404426
.context(format!("Failed to encode result type for {name}"))?,
405-
None => WrappedType::unit(false),
427+
None => WrappedType::unit(),
406428
}
407429
};
408430

@@ -489,7 +511,7 @@ fn generate_exported_resource_function_impl(
489511
let original_result = &func_ret.original_type_ref;
490512
let wrapped_result = &func_ret.wrapped_type_ref;
491513
let unwrap = &func_ret.unwrap;
492-
let unwrap_result = (unwrap)(quote! { result });
514+
let unwrap_result = unwrap.run(quote! { result });
493515
quote! {
494516
fn #func_name_ident(#(#func_arg_list),*) -> #original_result {
495517
crate::internal::async_exported_function(async move {
@@ -510,7 +532,7 @@ fn generate_exported_resource_function_impl(
510532
let original_result = &func_ret.original_type_ref;
511533
let wrapped_result = &func_ret.wrapped_type_ref;
512534
let unwrap = &func_ret.unwrap;
513-
let unwrap_result = (unwrap)(quote! { result });
535+
let unwrap_result = unwrap.run(quote! { result });
514536
quote! {
515537
fn #func_name_ident(#(#func_arg_list),*) -> #original_result {
516538
crate::internal::async_exported_function(async move {

crates/wasm-rquickjs/src/imports.rs

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::javascript::escape_js_ident;
2-
use crate::rust_bindgen::escape_rust_ident;
2+
use crate::rust_bindgen::RustWitFunction;
33
use crate::types::{
44
WrappedType, get_function_name, get_wrapped_type, ident_in_imported_interface_or_global,
55
process_parameter, to_unwrapped_param_refs, to_wrapped_func_arg_list,
@@ -163,10 +163,13 @@ fn generate_import_module(
163163
for (name, function) in &import.functions {
164164
match &function.kind {
165165
FunctionKind::Freestanding => {
166+
let rust_fn = RustWitFunction::new(context, name, function);
167+
168+
let rust_function_name = &rust_fn.function_name;
169+
let rust_function_ident = rust_fn.function_name_ident();
170+
166171
let js_function_name = escape_js_ident(name.to_lower_camel_case());
167172
let js_function_lit = LitStr::new(&js_function_name, Span::call_site());
168-
let rust_function_name = escape_rust_ident(&name.to_snake_case());
169-
let rust_function_ident = Ident::new(&rust_function_name, Span::call_site());
170173
let js_bridge_name = format!("js_{rust_function_name}");
171174
let js_bridge_ident = Ident::new(&js_bridge_name, Span::call_site());
172175

@@ -183,22 +186,35 @@ fn generate_import_module(
183186
let parameters = function
184187
.params
185188
.iter()
186-
.map(|(param_name, param_type)| {
187-
process_parameter(context, param_name, param_type)
188-
})
189+
.zip(rust_fn.export_parameters)
190+
.zip(rust_fn.import_parameters)
191+
.map(
192+
|(((param_name, param_type), export_parameter), import_parameter)| {
193+
process_parameter(
194+
context,
195+
param_name,
196+
param_type,
197+
&export_parameter,
198+
&import_parameter,
199+
)
200+
},
201+
)
189202
.collect::<anyhow::Result<Vec<_>>>()?;
190203

191204
let param_list: Vec<TokenStream> = to_wrapped_func_arg_list(&parameters);
205+
192206
let param_refs: Vec<TokenStream> = to_unwrapped_param_refs(&parameters);
193207
let func_ret = match &function.result {
194-
Some(typ) => get_wrapped_type(context, typ)
195-
.context(format!("Failed to encode result type for {name}"))?,
196-
None => WrappedType::unit(false),
208+
Some(typ) => {
209+
get_wrapped_type(context, &rust_fn.return_type, &rust_fn.return_type, typ)
210+
.context(format!("Failed to encode result type for {name}"))?
211+
}
212+
None => WrappedType::unit(),
197213
};
198214
let original_result = &func_ret.original_type_ref;
199215
let wrapped_result = &func_ret.wrapped_type_ref;
200216
let wrap = &func_ret.wrap;
201-
let wrap_result = (wrap)(quote! { result });
217+
let wrap_result = wrap.run(quote! { result });
202218

203219
bridge_functions.push(quote! {
204220
#[rquickjs::function]
@@ -256,10 +272,24 @@ fn generate_import_module(
256272
.iter()
257273
.find(|(_, f)| matches!(f.kind, FunctionKind::Constructor(_)));
258274
let constructor = if let Some((_, constructor_function)) = constructor_function {
275+
let rust_fn = RustWitFunction::new(context, "new", constructor_function);
276+
259277
let parameters = constructor_function
260278
.params
261279
.iter()
262-
.map(|(param_name, param_type)| process_parameter(context, param_name, param_type))
280+
.zip(rust_fn.export_parameters)
281+
.zip(rust_fn.import_parameters)
282+
.map(
283+
|(((param_name, param_type), export_parameter), import_parameter)| {
284+
process_parameter(
285+
context,
286+
param_name,
287+
param_type,
288+
&export_parameter,
289+
&import_parameter,
290+
)
291+
},
292+
)
263293
.collect::<anyhow::Result<Vec<_>>>()?;
264294

265295
let param_list: Vec<TokenStream> = to_wrapped_func_arg_list(&parameters);
@@ -279,27 +309,44 @@ fn generate_import_module(
279309
let mut methods: Vec<TokenStream> = Vec::new();
280310
for (name, function) in resource_funcs {
281311
let name = get_function_name(name, function)?;
282-
let rust_method_name = escape_rust_ident(&name.to_snake_case());
283-
let rust_method_name_ident = Ident::new(&rust_method_name, Span::call_site());
312+
313+
let rust_fn = RustWitFunction::new(context, &name, function);
314+
315+
let rust_method_name_ident = rust_fn.function_name_ident();
284316

285317
let parameters = function
286318
.params
287319
.iter()
288-
.map(|(param_name, param_type)| process_parameter(context, param_name, param_type))
320+
.zip(rust_fn.export_parameters)
321+
.zip(rust_fn.import_parameters)
322+
.map(
323+
|(((param_name, param_type), export_parameter), import_parameter)| {
324+
process_parameter(
325+
context,
326+
param_name,
327+
param_type,
328+
&export_parameter,
329+
&import_parameter,
330+
)
331+
},
332+
)
289333
.collect::<anyhow::Result<Vec<_>>>()?;
290334

291335
let param_list: Vec<TokenStream> = to_wrapped_func_arg_list(&parameters);
336+
292337
let param_refs: Vec<TokenStream> = to_unwrapped_param_refs(&parameters);
293338

294339
let func_ret = match &function.result {
295-
Some(typ) => get_wrapped_type(context, typ)
296-
.context(format!("Failed to encode result type for {name}"))?,
297-
None => WrappedType::unit(false),
340+
Some(typ) => {
341+
get_wrapped_type(context, &rust_fn.return_type, &rust_fn.return_type, typ)
342+
.context(format!("Failed to encode result type for {name}"))?
343+
}
344+
None => WrappedType::unit(),
298345
};
299346
let original_result = &func_ret.original_type_ref;
300347
let wrapped_result = &func_ret.wrapped_type_ref;
301348
let wrap = &func_ret.wrap;
302-
let wrap_result = (wrap)(quote! { result });
349+
let wrap_result = wrap.run(quote! { result });
303350

304351
match &function.kind {
305352
FunctionKind::Method(_) => {

0 commit comments

Comments
 (0)