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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

#![cfg(target_arch = "wasm32")]

mod completion;
mod goto;
mod lsp_ext;
mod properties;
mod semantic_tokens;
mod server_loop;
mod util;

use i_slint_compiler::CompilerConfiguration;
use js_sys::Function;
use lsp_types::InitializeParams;
use serde::Serialize;
pub use server_loop::{DocumentCache, Error};
use std::cell::RefCell;
use std::io::ErrorKind;
use std::rc::Rc;
use wasm_bindgen::prelude::*;

pub mod wasm_prelude {
    use std::path::{Path, PathBuf};

    /// lsp_url doesn't have method to convert to and from PathBuf for wasm, so just make some
    pub trait UrlWasm {
        fn to_file_path(&self) -> Result<PathBuf, ()>;
        fn from_file_path<P: AsRef<Path>>(path: P) -> Result<lsp_types::Url, ()>;
    }
    impl UrlWasm for lsp_types::Url {
        fn to_file_path(&self) -> Result<PathBuf, ()> {
            Ok(self.to_string().into())
        }
        fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Self, ()> {
            Self::parse(path.as_ref().to_str().ok_or(())?).map_err(|_| ())
        }
    }
}

#[derive(Clone)]
pub struct ServerNotifier(Function);
impl ServerNotifier {
    pub fn send_notification(&self, method: String, params: impl Serialize) -> Result<(), Error> {
        self.0
            .call2(&JsValue::UNDEFINED, &method.into(), &JsValue::from_serde(&params)?)
            .map_err(|x| format!("Error calling send_notification: {x:?}"))?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct Request {
    method: String,
    params: serde_json::Value,
}
pub struct RequestHolder {
    req: Request,
    /// The result will be assigned there
    reply: Rc<RefCell<Option<JsValue>>>,

    notifier: ServerNotifier,
}
impl RequestHolder {
    pub fn handle_request<
        Kind: lsp_types::request::Request,
        F: FnOnce(Kind::Params) -> Result<Kind::Result, Error>,
    >(
        &self,
        f: F,
    ) -> Result<bool, Error> {
        if self.req.method != Kind::METHOD {
            return Ok(false);
        }
        let result = f(serde_json::from_value(self.req.params.clone())?)?;
        *self.reply.borrow_mut() = Some(JsValue::from_serde(&result)?);
        Ok(true)
    }

    pub fn server_notifier(&self) -> ServerNotifier {
        self.notifier.clone()
    }
}

#[derive(Default)]
struct ReentryGuard {
    locked: bool,
    waker: Vec<std::task::Waker>,
}

impl ReentryGuard {
    pub async fn lock(this: Rc<RefCell<Self>>) -> ReentryGuardLock {
        struct ReentryGuardLocker(Rc<RefCell<ReentryGuard>>);

        impl std::future::Future for ReentryGuardLocker {
            type Output = ReentryGuardLock;
            fn poll(
                self: std::pin::Pin<&mut Self>,
                cx: &mut std::task::Context<'_>,
            ) -> std::task::Poll<Self::Output> {
                let mut s = self.0.borrow_mut();
                if s.locked {
                    s.waker.push(cx.waker().clone());
                    std::task::Poll::Pending
                } else {
                    s.locked = true;
                    std::task::Poll::Ready(ReentryGuardLock(self.0.clone()))
                }
            }
        }
        ReentryGuardLocker(this).await
    }
}

struct ReentryGuardLock(Rc<RefCell<ReentryGuard>>);

impl Drop for ReentryGuardLock {
    fn drop(&mut self) {
        let mut s = self.0.borrow_mut();
        s.locked = false;
        let wakers = std::mem::take(&mut s.waker);
        drop(s);
        for w in wakers {
            w.wake()
        }
    }
}

#[wasm_bindgen]
pub struct SlintServer {
    document_cache: Rc<RefCell<DocumentCache>>,
    init_param: InitializeParams,
    notifier: ServerNotifier,
    reentry_guard: Rc<RefCell<ReentryGuard>>,
}

#[wasm_bindgen]
pub fn create(
    init_param: JsValue,
    send_notification: Function,
    load_file: Function,
) -> Result<SlintServer, JsError> {
    console_error_panic_hook::set_once();

    let init_param = init_param.into_serde()?;

    let mut compiler_config =
        CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Interpreter);
    compiler_config.open_import_fallback = Some(Rc::new(move |path| {
        let load_file = load_file.clone();
        Box::pin(async move { Some(self::load_file(path, &load_file).await) })
    }));

    let document_cache = DocumentCache::new(compiler_config);

    Ok(SlintServer {
        document_cache: Rc::new(RefCell::new(document_cache)),
        init_param,
        notifier: ServerNotifier(send_notification),
        reentry_guard: Default::default(),
    })
}

#[wasm_bindgen]
impl SlintServer {
    #[wasm_bindgen]
    pub fn capabilities(&self) -> Result<JsValue, JsError> {
        Ok(JsValue::from_serde(&server_loop::server_capabilities())?)
    }

    #[wasm_bindgen]
    pub fn reload_document(&self, content: String, uri: JsValue) -> js_sys::Promise {
        let document_cache = self.document_cache.clone();
        let notifier = self.notifier.clone();
        let guard = self.reentry_guard.clone();
        wasm_bindgen_futures::future_to_promise(async move {
            let _lock = ReentryGuard::lock(guard).await;
            let uri: lsp_types::Url = uri.into_serde().map_err(|e| JsError::new(&e.to_string()))?;
            server_loop::reload_document(&notifier, content, uri, &mut document_cache.borrow_mut())
                .await
                .map_err(|e| JsError::new(&e.to_string()))?;
            Ok(JsValue::UNDEFINED)
        })
    }

    /*  #[wasm_bindgen]
    pub fn show_preview(&self, params: JsValue) -> Result<(), JsError> {
        server_loop::show_preview_command(
            &params.into_serde()?,
            &ServerNotifier,
            &mut self.0.borrow_mut(),
        )
        .map_err(|e| JsError::new(&e.to_string()));
    }*/

    #[wasm_bindgen]
    pub fn handle_request(&self, _id: JsValue, method: String, params: JsValue) -> js_sys::Promise {
        let document_cache = self.document_cache.clone();
        let notifier = self.notifier.clone();
        let guard = self.reentry_guard.clone();
        let init_param = self.init_param.clone();
        wasm_bindgen_futures::future_to_promise(async move {
            let _lock = ReentryGuard::lock(guard).await;
            let req = Request {
                method,
                params: params
                    .into_serde()
                    .map_err(|x| format!("invalid param to handle_request: {x:?}"))?,
            };
            let result = Rc::new(RefCell::new(None));
            server_loop::handle_request(
                RequestHolder { req, reply: result.clone(), notifier: notifier.clone() },
                &init_param,
                &mut document_cache.borrow_mut(),
            )
            .map_err(|e| JsError::new(&e.to_string()))?;

            let result = result.borrow_mut().take();
            Ok(result.ok_or(JsError::new("Empty reply".into()))?)
        })
    }
}

async fn load_file(path: String, load_file: &Function) -> std::io::Result<String> {
    let value = load_file
        .call1(&JsValue::UNDEFINED, &path.into())
        .map_err(|x| std::io::Error::new(ErrorKind::Other, format!("{x:?}")))?;
    let array = wasm_bindgen_futures::JsFuture::from(js_sys::Promise::from(value))
        .await
        .map_err(|e| std::io::Error::new(ErrorKind::Other, format!("{e:?}")))?;
    String::from_utf8(js_sys::Uint8Array::from(array).to_vec())
        .map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e.to_string()))
}