forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm_main.rs
374 lines (333 loc) · 12.6 KB
/
wasm_main.rs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
#![cfg(target_arch = "wasm32")]
pub mod common;
mod fmt;
mod language;
pub mod lsp_ext;
#[cfg(feature = "preview-engine")]
mod preview;
pub mod util;
use common::{LspToPreviewMessage, Result, VersionedUrl};
use i_slint_compiler::CompilerConfiguration;
use js_sys::Function;
pub use language::{Context, DocumentCache, RequestHandler};
use lsp_types::Url;
use serde::Serialize;
use std::cell::RefCell;
use std::future::Future;
use std::io::ErrorKind;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use crate::wasm_prelude::*;
type JsResult<T> = std::result::Result<T, JsError>;
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 {
send_notification: Function,
send_request: Function,
}
impl ServerNotifier {
pub fn send_notification(&self, method: String, params: impl Serialize) -> Result<()> {
self.send_notification
.call2(&JsValue::UNDEFINED, &method.into(), &to_value(¶ms)?)
.map_err(|x| format!("Error calling send_notification: {x:?}"))?;
Ok(())
}
pub fn send_request<T: lsp_types::request::Request>(
&self,
request: T::Params,
) -> Result<impl Future<Output = Result<T::Result>>> {
let promise = self
.send_request
.call2(&JsValue::UNDEFINED, &T::METHOD.into(), &to_value(&request)?)
.map_err(|x| format!("Error calling send_request: {x:?}"))?;
let future = wasm_bindgen_futures::JsFuture::from(js_sys::Promise::from(promise));
Ok(async move {
future.await.map_err(|e| format!("{e:?}").into()).and_then(|v| {
serde_wasm_bindgen::from_value(v).map_err(|e| format!("{e:?}").into())
})
})
}
pub fn send_message_to_preview(&self, message: LspToPreviewMessage) {
let _ = self.send_notification("slint/lsp_to_preview".to_string(), message);
}
}
impl RequestHandler {
async fn handle_request(
&self,
method: String,
params: JsValue,
ctx: Rc<Context>,
) -> Result<JsValue> {
if let Some(f) = self.0.get(&method.as_str()) {
let param = serde_wasm_bindgen::from_value(params)
.map_err(|x| format!("invalid param to handle_request: {x:?}"))?;
let r = f(param, ctx).await?;
to_value(&r).map_err(|e| e.to_string().into())
} else {
Err("Cannot handle request".into())
}
}
}
#[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(typescript_custom_section)]
const IMPORT_CALLBACK_FUNCTION_SECTION: &'static str = r#"
type ImportCallbackFunction = (url: string) => Promise<string>;
type SendRequestFunction = (method: string, r: any) => Promise<any>;
type HighlightInPreviewFunction = (file: string, offset: number) => void;
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "ImportCallbackFunction")]
pub type ImportCallbackFunction;
#[wasm_bindgen(typescript_type = "SendRequestFunction")]
pub type SendRequestFunction;
#[wasm_bindgen(typescript_type = "HighlightInPreviewFunction")]
pub type HighlightInPreviewFunction;
// Make console.log available:
#[allow(unused)]
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub struct SlintServer {
ctx: Rc<Context>,
reentry_guard: Rc<RefCell<ReentryGuard>>,
rh: Rc<RequestHandler>,
}
#[wasm_bindgen]
pub fn create(
init_param: JsValue,
send_notification: Function,
send_request: SendRequestFunction,
load_file: ImportCallbackFunction,
) -> JsResult<SlintServer> {
console_error_panic_hook::set_once();
let send_request = Function::from(send_request.clone());
let server_notifier = ServerNotifier { send_notification, send_request };
let init_param = serde_wasm_bindgen::from_value(init_param)?;
let mut compiler_config =
CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Interpreter);
let server_notifier_ = server_notifier.clone();
compiler_config.open_import_fallback = Some(Rc::new(move |path| {
let load_file = Function::from(load_file.clone());
let server_notifier = server_notifier_.clone();
Box::pin(async move {
let contents = self::load_file(path.clone(), &load_file).await;
let Ok(url) = Url::from_file_path(&path) else {
return Some(contents);
};
if let Ok(contents) = &contents {
server_notifier.send_message_to_preview(LspToPreviewMessage::SetContents {
url: VersionedUrl::new(url, None),
contents: contents.clone(),
})
}
Some(contents)
})
}));
let document_cache = RefCell::new(DocumentCache::new(compiler_config));
let reentry_guard = Rc::new(RefCell::new(ReentryGuard::default()));
let mut rh = RequestHandler::default();
language::register_request_handlers(&mut rh);
Ok(SlintServer {
ctx: Rc::new(Context {
document_cache,
init_param,
server_notifier,
to_show: Default::default(),
}),
reentry_guard,
rh: Rc::new(rh),
})
}
fn send_workspace_edit(
server_notifier: ServerNotifier,
label: Option<String>,
edit: Result<lsp_types::WorkspaceEdit>,
) {
let Ok(edit) = edit else {
return;
};
wasm_bindgen_futures::spawn_local(async move {
let fut = server_notifier.send_request::<lsp_types::request::ApplyWorkspaceEdit>(
lsp_types::ApplyWorkspaceEditParams { label, edit },
);
if let Ok(fut) = fut {
// We ignore errors: If the LSP can not be reached, then all is lost
// anyway. The other thing that might go wrong is that our Workspace Edit
// refers to some outdated text. In that case the update is most likely
// in flight already and will cause the preview to re-render, which also
// invalidates all our state
let _ = fut.await;
}
});
}
#[wasm_bindgen]
impl SlintServer {
#[cfg(all(feature = "preview-engine", feature = "preview-external"))]
#[wasm_bindgen]
pub async fn process_preview_to_lsp_message(
&self,
value: JsValue,
) -> std::result::Result<(), JsValue> {
use crate::common::PreviewToLspMessage as M;
let guard = self.reentry_guard.clone();
let _lock = ReentryGuard::lock(guard).await;
let Ok(message) = serde_wasm_bindgen::from_value::<M>(value) else {
return Err(JsValue::from("Failed to convert value to PreviewToLspMessage"));
};
match message {
M::Status { message, health } => {
crate::common::lsp_to_editor::send_status_notification(
&self.ctx.server_notifier,
&message,
health,
);
}
M::Diagnostics { diagnostics, uri } => {
crate::common::lsp_to_editor::notify_lsp_diagnostics(
&self.ctx.server_notifier,
uri,
diagnostics,
);
}
M::ShowDocument { file, selection } => {
let sn = self.ctx.server_notifier.clone();
wasm_bindgen_futures::spawn_local(async move {
crate::common::lsp_to_editor::send_show_document_to_editor(sn, file, selection)
.await
});
}
M::PreviewTypeChanged { is_external: _ } => {
// Nothing to do!
}
M::RequestState { .. } => {
crate::language::request_state(&self.ctx);
}
M::UpdateElement { label, position, properties } => {
send_workspace_edit(
self.ctx.server_notifier.clone(),
label,
language::properties::update_element_properties(
&self.ctx, position, properties,
),
);
}
M::SendWorkspaceEdit { label, edit } => {
send_workspace_edit(self.ctx.server_notifier.clone(), label, Ok(edit));
}
}
Ok(())
}
#[wasm_bindgen]
pub fn server_initialize_result(&self, cap: JsValue) -> JsResult<JsValue> {
Ok(to_value(&language::server_initialize_result(&serde_wasm_bindgen::from_value(cap)?))?)
}
#[wasm_bindgen]
pub fn reload_document(&self, content: String, uri: JsValue, version: i32) -> js_sys::Promise {
let ctx = self.ctx.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 = serde_wasm_bindgen::from_value(uri)?;
language::reload_document(
&ctx,
content,
uri.clone(),
Some(version),
&mut ctx.document_cache.borrow_mut(),
)
.await
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(JsValue::UNDEFINED)
})
}
#[wasm_bindgen]
pub fn handle_request(&self, _id: JsValue, method: String, params: JsValue) -> js_sys::Promise {
let guard = self.reentry_guard.clone();
let rh = self.rh.clone();
let ctx = self.ctx.clone();
wasm_bindgen_futures::future_to_promise(async move {
let fut = rh.handle_request(method, params, ctx);
let _lock = ReentryGuard::lock(guard).await;
fut.await.map_err(|e| JsError::new(&e.to_string()).into())
})
}
#[wasm_bindgen]
pub async fn reload_config(&self) -> JsResult<()> {
let guard = self.reentry_guard.clone();
let _lock = ReentryGuard::lock(guard).await;
language::load_configuration(&self.ctx).await.map_err(|e| JsError::new(&e.to_string()))
}
}
async fn load_file(path: String, load_file: &Function) -> std::io::Result<String> {
let string_promise = load_file
.call1(&JsValue::UNDEFINED, &path.into())
.map_err(|x| std::io::Error::new(ErrorKind::Other, format!("{x:?}")))?;
let string_future = wasm_bindgen_futures::JsFuture::from(js_sys::Promise::from(string_promise));
let js_value =
string_future.await.map_err(|e| std::io::Error::new(ErrorKind::Other, format!("{e:?}")))?;
return Ok(js_value.as_string().unwrap_or_default());
}
// Use a JSON friendly representation to avoid using ES maps instead of JS objects.
fn to_value<T: serde::Serialize + ?Sized>(
value: &T,
) -> std::result::Result<wasm_bindgen::JsValue, serde_wasm_bindgen::Error> {
value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
}