1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![no_std]
use js::*;
#[macro_use]
extern crate alloc;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use spin::Mutex;

pub type HTMLElement = f64;

#[derive(Copy, Clone)]
struct Destructable {
    function: Option<JSFunction>,
}

pub trait CustomElement {
    fn new(element: HTMLElement) -> Self
    where
        Self: core::marker::Sized + core::marker::Sync + core::marker::Send + 'static;
    fn register(name: &str)
    where
        Self: core::marker::Sized + core::marker::Sync + core::marker::Send + 'static,
    {
        let construct = create_callback_1(|element| {
            let el = Arc::new(Mutex::new(Self::new(element.into())));
            let el1 = el.clone();
            let el2 = el.clone();
            let el3 = el.clone();

            let destruct_connect = Arc::new(Mutex::new(Destructable { function: None }));
            let connect = create_callback_0(move || {
                el1.lock().connected();
            });
            destruct_connect.lock().function = Some(connect.into());

            let destruct_attribute_change = Arc::new(Mutex::new(Destructable { function: None }));
            let attribute_change = create_callback_3(move |name_obj, old_obj, new_obj| {
                let name = cstr_to_string(name_obj as i32);
                let old = if old_obj == -1.0 {
                    None
                } else {
                    Some(cstr_to_string(old_obj as i32))
                };
                let new = if new_obj == -1.0 {
                    None
                } else {
                    Some(cstr_to_string(new_obj as i32))
                };
                el3.lock().attribute_changed(name, old, new);
            });

            destruct_attribute_change.lock().function = Some(connect.into());

            let destruct_disconnect = Arc::new(Mutex::new(Destructable { function: None }));
            let destruct_disconnect2 = destruct_disconnect.clone();
            let disconnect = create_callback_0(move || {
                el2.lock().disconnected();
                remove_callback(destruct_connect.lock().function.as_ref().unwrap().into());
                remove_callback(destruct_disconnect.lock().function.as_ref().unwrap().into());
                remove_callback(
                    destruct_attribute_change
                        .lock()
                        .function
                        .as_ref()
                        .unwrap()
                        .into(),
                );
            });
            destruct_disconnect2.lock().function = Some(disconnect.into());

            lazy_static::lazy_static! {
                static ref FN: JSFunction= {
                register_function(
                    r#"function(e,a,b,c){
                        e.addHooks(a,b,c);
                    }"#,
                )
            };};
            FN.invoke_4(element, connect, disconnect, attribute_change);
        });
        let attrs = Self::observed_attributes().join(",");
        lazy_static::lazy_static! {
            static ref FN: JSFunction= {
            register_function(
                r#"function(construct,elementNamePtr, elementNameLen,attrNamesPtr,attrNamesLen){
                    const elementName = this.readUtf8FromMemory(elementNamePtr,elementNameLen);
                    const attrNames = this.readUtf8FromMemory(attrNamesPtr,attrNamesLen);
                    let attrs = attrNames.split(",");
                    class GeneratedCustomElement extends HTMLElement {
                      constructor() {
                          super();
                          construct(this);
                      }
    
                      static get observedAttributes() {
                        return attrs;
                      }
    
                      connectedCallback() {
                        self.connect();
                      }
    
                      disconnectedCallback() {
                        self.disconnect();
                      }
    
                      attributeChangedCallback(attributeName, oldValue, newValue) {
                        self.attributeChange(attributeName,oldValue,newValue)
                      }
    
                      addHooks(connect,disconnect,attributeChange){
                        self.connect = connect;
                        self.disconnect = disconnect;
                        self.attributeChange = attributeChange;
                      }
                    }
    
                    // tell the dom to associate it with an html tag name
                    customElements.define(elementName, GeneratedCustomElement);
                  }"#,
            )
        };};
        FN.invoke_5(
            construct,
            name.as_ptr() as u32,
            name.len() as u32,
            attrs.as_ptr() as u32,
            attrs.len() as u32,
        );
    }

    fn observed_attributes() -> Vec<&'static str> {
        vec![]
    }

    fn created(&mut self) {}
    fn connected(&mut self) {}
    fn disconnected(&mut self) {}
    fn attribute_changed(
        &mut self,
        _name: String,
        _old_value: Option<String>,
        _new_value: Option<String>,
    ) {
    }
}