Giter Site home page Giter Site logo

Comments (7)

sagudev avatar sagudev commented on May 25, 2024

Here is how wasm-bindgen does this: https://rustwasm.github.io/wasm-bindgen/contributing/design/importing-js-struct.html

Although I think it would be better to have struct with specific trait for that. Something like:

#[js_class]
struct Fields {
    s: String,
    num: u64
}

impl CLASS for Fields {
    fn constructor() -> IonResult<Name> {
        Fields {
            s: "",
            num: 0
        }
    }
    fn get(self) -> IonResult<f64> {
        Ok(self.num)
    }
    
    fn set(&mut self, s: String) -> IonResult<()> {
        self.s = s;
        Ok(())
    }
}

impl Fields {
    fn method(self, num: f64) -> IonResult<()> {
        println!("{}", self.s);
        self.num = num;
    }
}

With the goal to keep as much rust syntax as possible.

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

Hmm, this way of doing it would require impl's for FromJSValConvertible and ToJSValConvertible to be automatically created.
Also, the attributes for functions will most likely still be needed.

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

I have a slightly improved idea for this:

#[js_class]
mod class {
    #[class]
    #[extends(OtherClass)]
    struct Class {
        #[convert(EnforceRange)]
        field: u8
    }
    
    impl Class {
        #[constructor]
        fn constructor() -> IonResult<Class> {
            Class { field: 644 }
        }
    
        #[js_fn]
        fn add(&self, #[convert(Clamp)] other: u8) -> IonResult<u8> {
            Ok(self.field + other)
        }
        
        #[js_get]
        fn f(&self) -> IonResult<u8> {
            Ok(self.field)
        }
        
        #[js_set]
        fn f(&mut self, field: u8) -> IonResult<()> {
            self.field = u8;
            Ok(())
        }
    }
}

Putting the attribute macro on the mod instead of one the struct is still required as access to the functions by the macro is required.

from spiderfire.

sagudev avatar sagudev commented on May 25, 2024

Two functions named f?

And also rjs has some interesting ideas: https://github.com/rjs-team/rjs/blob/master/src/main.rs#L323, but I would like to use more rusty syntax.

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

You can have both a getter and a setter with the same name, you just can't have a property and (getter and/or setter) with the same name.

from spiderfire.

sagudev avatar sagudev commented on May 25, 2024

You can have both a getter and a setter with the same name, you just can't have a property and (getter and/or setter) with the same name.

The only problem I have is that it does not look rusty enough. I would do with less attr macros, as the whole mod is already transformed using #[js_class].

I especially like how wasm-bindgen handled this:
https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/constructor.html https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/getter-and-setter.html

If we could make js macro compatible with wasm-bindgen we could ideally just replace all wasm-bindgen to js and it would just work.

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 25, 2024

This is just a draft PR for now but #22 has the syntax and design I've currently settled on, based on both ease of implementation and use.

from spiderfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.