From 9dedb7252a602ee9f6b80660cc4540589d61de5a Mon Sep 17 00:00:00 2001 From: Shark Date: Tue, 31 Dec 2024 17:07:19 +0100 Subject: [PATCH] rename Web Object with web_interop macro --- .../src/impl_interop_struct.rs | 4 +-- crates/gosub_webinterop/src/lib.rs | 27 +++++++++++++------ examples/vello-renderer/application.rs | 1 - 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/crates/gosub_webinterop/src/impl_interop_struct.rs b/crates/gosub_webinterop/src/impl_interop_struct.rs index cc428db27..a2604496b 100644 --- a/crates/gosub_webinterop/src/impl_interop_struct.rs +++ b/crates/gosub_webinterop/src/impl_interop_struct.rs @@ -3,7 +3,7 @@ use quote::{format_ident, quote}; use crate::types::Field; -pub fn impl_interop_struct(name: Ident, fields: &[Field]) -> TokenStream { +pub fn impl_interop_struct(name: Ident, fields: &[Field], js_name: TokenStream) -> TokenStream { let marker_struct = format_ident!("{}JSMethodsMarker", name); let marker_trait = format_ident!("{}JSMethods", name); @@ -12,7 +12,7 @@ pub fn impl_interop_struct(name: Ident, fields: &[Field]) -> TokenStream { quote! { impl JSInterop for #name { fn implement(s: Rc>, mut ctx: RT::Context) -> Result<()> { - let mut obj = ctx.new_global_object(stringify!(#name))?; + let mut obj = ctx.new_global_object(stringify!(#js_name))?; #getters_setters diff --git a/crates/gosub_webinterop/src/lib.rs b/crates/gosub_webinterop/src/lib.rs index d597c0433..aeca09c06 100644 --- a/crates/gosub_webinterop/src/lib.rs +++ b/crates/gosub_webinterop/src/lib.rs @@ -4,18 +4,18 @@ use proc_macro::TokenStream; use std::collections::HashMap; use std::sync::RwLock; -use lazy_static::lazy_static; -use proc_macro2::{Ident, TokenTree}; -use quote::ToTokens; -use syn::spanned::Spanned; -use syn::{FnArg, ItemImpl, ItemStruct}; - use crate::function::Function; use crate::impl_function::impl_js_functions; use crate::impl_interop_struct::impl_interop_struct; use crate::property::{FieldProperty, FunctionProperty}; use crate::types::{Arg, ArgVariant, Field, GenericsMatcher, ReturnType, SelfType}; use crate::utils::crate_name; +use lazy_static::lazy_static; +use proc_macro2::{Ident, TokenTree}; +use quote::ToTokens; +use syn::punctuated::Punctuated; +use syn::spanned::Spanned; +use syn::{parse_macro_input, FnArg, ItemImpl, ItemStruct, MetaNameValue, Token}; mod function; mod impl_function; @@ -29,7 +29,7 @@ lazy_static! { } #[proc_macro_attribute] -pub fn web_interop(_: TokenStream, item: TokenStream) -> TokenStream { +pub fn web_interop(args: TokenStream, item: TokenStream) -> TokenStream { let mut fields: Vec = Vec::new(); let mut input: ItemStruct = syn::parse_macro_input!(item); @@ -47,7 +47,18 @@ pub fn web_interop(_: TokenStream, item: TokenStream) -> TokenStream { } } - let extend = impl_interop_struct(input.ident.clone(), &fields); + let items: Punctuated = + parse_macro_input!(args with Punctuated::::parse_terminated); + + let mut js_name = input.ident.to_token_stream(); + + for item in items { + if item.path.is_ident("js_name") { + js_name = item.value.to_token_stream(); + } + } + + let extend = impl_interop_struct(input.ident.clone(), &fields, js_name); let name = input.ident.clone().into_token_stream().to_string(); STATE.write().unwrap().insert((crate_name(), name), 0); diff --git a/examples/vello-renderer/application.rs b/examples/vello-renderer/application.rs index 3a686afed..4075e746f 100644 --- a/examples/vello-renderer/application.rs +++ b/examples/vello-renderer/application.rs @@ -6,7 +6,6 @@ use crate::window::Window; use crate::WinitEventLoopHandle; use anyhow::anyhow; use gosub_instance::{DebugEvent, InstanceMessage}; -use gosub_interface::chrome::ChromeHandle; use gosub_interface::config::{HasRenderBackend, ModuleConfiguration}; use gosub_interface::instance::{Handles, InstanceId}; use gosub_interface::render_backend::{NodeDesc, RenderBackend, SizeU32};