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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! <div align="center">
//!     <img alt="Rune Logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
//! </div>
//!
//! <br>
//!
//! <div align="center">
//! <a href="https://rune-rs.github.io">
//!     <b>Visit the site 🌐</b>
//! </a>
//! -
//! <a href="https://rune-rs.github.io/book/">
//!     <b>Read the book 📖</b>
//! </a>
//! </div>
//!
//! <br>
//!
//! <div align="center">
//! <a href="https://github.com/rune-rs/rune/actions">
//!     <img alt="Build Status" src="https://github.com/rune-rs/rune/workflows/Build/badge.svg">
//! </a>
//!
//! <a href="https://github.com/rune-rs/rune/actions">
//!     <img alt="Site Status" src="https://github.com/rune-rs/rune/workflows/Site/badge.svg">
//! </a>
//!
//! <a href="https://crates.io/crates/rune">
//!     <img alt="crates.io" src="https://img.shields.io/crates/v/rune.svg">
//! </a>
//!
//! <a href="https://docs.rs/rune">
//!     <img alt="docs.rs" src="https://docs.rs/rune/badge.svg">
//! </a>
//!
//! <a href="https://discord.gg/v5AeNkT">
//!     <img alt="Chat on Discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square">
//! </a>
//! </div>
//!
//! Very basic WASM bindings for Rune.
//!
//! This is part of the [Rune Language].
//!
//! [Rune Language]: https://rune-rs.github.io

use wasm_bindgen::prelude::*;

use anyhow::Context as _;
use rune::{DumpInstructions as _, EmitDiagnostics as _, Spanned as _};
use runestick::budget;
use runestick::{ContextError, Value};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;

mod core;
mod http;
mod time;

#[derive(Default, Serialize)]
struct Position {
    line: u32,
    character: u32,
}

impl From<(usize, usize)> for Position {
    fn from((line, character): (usize, usize)) -> Self {
        Self {
            line: line as u32,
            character: character as u32,
        }
    }
}

#[derive(Deserialize)]
struct Config {
    /// Budget.
    #[serde(default)]
    budget: Option<usize>,
    /// Compiler options.
    #[serde(default)]
    options: Vec<String>,
    /// Include the `std::experiments` package.
    #[serde(default)]
    experimental: bool,
    /// Show instructions.
    #[serde(default)]
    instructions: bool,
    /// Suppress text warnings.
    #[serde(default)]
    suppress_text_warnings: bool,
}

#[derive(Serialize)]
enum DiagnosticKind {
    #[serde(rename = "error")]
    Error,
    #[serde(rename = "warning")]
    Warning,
}

#[derive(Serialize)]
struct Diagnostic {
    kind: DiagnosticKind,
    start: Position,
    end: Position,
    message: String,
}

#[derive(Serialize)]
pub struct CompileResult {
    error: Option<String>,
    diagnostics_output: Option<String>,
    diagnostics: Vec<Diagnostic>,
    result: Option<String>,
    output: Option<String>,
    instructions: Option<String>,
}

impl CompileResult {
    /// Construct output from compile result.
    fn output(
        output: Value,
        diagnostics_output: Option<String>,
        diagnostics: Vec<Diagnostic>,
        instructions: Option<String>,
    ) -> Self {
        Self {
            error: None,
            diagnostics_output,
            diagnostics,
            result: Some(format!("{:?}", output)),
            output: core::drain_output(),
            instructions,
        }
    }

    /// Construct a result from an error.
    fn from_error<E>(
        error: E,
        diagnostics_output: Option<String>,
        diagnostics: Vec<Diagnostic>,
        instructions: Option<String>,
    ) -> Self
    where
        E: fmt::Display,
    {
        Self {
            error: Some(error.to_string()),
            diagnostics_output,
            diagnostics,
            result: None,
            output: core::drain_output(),
            instructions,
        }
    }
}

/// Setup a wasm-compatible context.
fn setup_context(experimental: bool) -> Result<runestick::Context, ContextError> {
    let mut context = runestick::Context::with_config(false)?;
    context.install(&core::module()?)?;
    context.install(&time::module()?)?;
    context.install(&http::module()?)?;
    context.install(&rune_modules::json::module(false)?)?;
    context.install(&rune_modules::toml::module(false)?)?;
    context.install(&rune_modules::rand::module(false)?)?;
    context.install(&rune_modules::core::module(false)?)?;
    context.install(&rune_modules::test::module(false)?)?;
    context.install(&rune_modules::io::module(false)?)?;
    context.install(&rune_modules::macros::module(false)?)?;

    if experimental {
        context.install(&rune_modules::experiments::module(false)?)?;
    }

    Ok(context)
}

async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult, anyhow::Error> {
    let instructions = None;

    let config = config.into_serde::<Config>()?;
    let budget = config.budget.unwrap_or(1_000_000);

    let source = runestick::Source::new("entry", input);
    let mut sources = rune::Sources::new();
    sources.insert(source);

    let context = setup_context(config.experimental)?;

    let mut options = rune::Options::default();

    for option in &config.options {
        options.parse_option(option)?;
    }

    let mut d = rune::Diagnostics::new();
    let mut diagnostics = Vec::new();

    let result = rune::load_sources(&context, &options, &mut sources, &mut d);

    for diagnostic in d.diagnostics() {
        match diagnostic {
            rune::Diagnostic::Error(error) => {
                if let Some(source) = sources.get(error.source_id()) {
                    match error.kind() {
                        rune::ErrorKind::ParseError(error) => {
                            let span = error.span();

                            let start = Position::from(
                                source.position_to_unicode_line_char(span.start.into_usize()),
                            );
                            let end = Position::from(
                                source.position_to_unicode_line_char(span.end.into_usize()),
                            );

                            diagnostics.push(Diagnostic {
                                kind: DiagnosticKind::Error,
                                start,
                                end,
                                message: error.to_string(),
                            });
                        }
                        rune::ErrorKind::CompileError(error) => {
                            let span = error.span();

                            let start = Position::from(
                                source.position_to_unicode_line_char(span.start.into_usize()),
                            );
                            let end = Position::from(
                                source.position_to_unicode_line_char(span.end.into_usize()),
                            );

                            diagnostics.push(Diagnostic {
                                kind: DiagnosticKind::Error,
                                start,
                                end,
                                message: error.to_string(),
                            });
                        }
                        rune::ErrorKind::QueryError(error) => {
                            let span = error.span();

                            let start = Position::from(
                                source.position_to_unicode_line_char(span.start.into_usize()),
                            );
                            let end = Position::from(
                                source.position_to_unicode_line_char(span.end.into_usize()),
                            );

                            diagnostics.push(Diagnostic {
                                kind: DiagnosticKind::Error,
                                start,
                                end,
                                message: error.to_string(),
                            });
                        }
                        rune::ErrorKind::LinkError(error) => match error {
                            rune::LinkerError::MissingFunction { hash, spans } => {
                                for (span, _) in spans {
                                    let start = Position::from(
                                        source
                                            .position_to_unicode_line_char(span.start.into_usize()),
                                    );
                                    let end = Position::from(
                                        source.position_to_unicode_line_char(span.end.into_usize()),
                                    );

                                    diagnostics.push(Diagnostic {
                                        kind: DiagnosticKind::Error,
                                        start,
                                        end,
                                        message: format!("missing function (hash: {})", hash),
                                    });
                                }
                            }
                        },
                        rune::ErrorKind::Internal(_) => {}
                        rune::ErrorKind::BuildError(_) => {}
                    }
                }
            }
            rune::Diagnostic::Warning(warning) => {
                let span = warning.span();

                if let Some(source) = sources.get(warning.source_id()) {
                    let start = Position::from(
                        source.position_to_unicode_line_char(span.start.into_usize()),
                    );
                    let end =
                        Position::from(source.position_to_unicode_line_char(span.end.into_usize()));

                    diagnostics.push(Diagnostic {
                        kind: DiagnosticKind::Warning,
                        start,
                        end,
                        message: warning.to_string(),
                    });
                }
            }
        }
    }

    let mut writer = rune::termcolor::Buffer::no_color();

    if !config.suppress_text_warnings {
        d.emit_diagnostics(&mut writer, &sources)
            .context("emitting to buffer should never fail")?;
    }

    let unit = match result {
        Ok(unit) => Arc::new(unit),
        Err(error) => {
            return Ok(CompileResult::from_error(
                error,
                diagnostics_output(writer),
                diagnostics,
                instructions,
            ));
        }
    };

    let instructions = if config.instructions {
        let mut out = rune::termcolor::Buffer::no_color();
        unit.dump_instructions(&mut out, &sources, false)
            .expect("dumping to string shouldn't fail");
        Some(diagnostics_output(out).context("converting instructions to UTF-8")?)
    } else {
        None
    };

    let vm = runestick::Vm::new(Arc::new(context.runtime()), unit);

    let mut execution = match vm.execute(&["main"], ()) {
        Ok(execution) => execution,
        Err(error) => {
            error
                .emit_diagnostics(&mut writer, &sources)
                .context("emitting to buffer should never fail")?;

            return Ok(CompileResult::from_error(
                error,
                diagnostics_output(writer),
                diagnostics,
                instructions,
            ));
        }
    };

    let future = budget::with(budget, execution.async_complete());

    let output = match future.await {
        Ok(output) => output,
        Err(error) => {
            if let Ok(vm) = execution.vm() {
                let (kind, unwound) = error.as_unwound();

                let (unit, ip, _frames) = match unwound {
                    Some((unit, ip, frames)) => (unit, ip, frames),
                    None => (vm.unit(), vm.ip(), vm.call_frames().to_owned()),
                };

                // NB: emit diagnostics if debug info is available.
                if let Some(debug) = unit.debug_info() {
                    if let Some(inst) = debug.instruction_at(ip) {
                        if let Some(source) = sources.get(inst.source_id) {
                            let start = Position::from(
                                source.position_to_unicode_line_char(inst.span.start.into_usize()),
                            );
                            let end = Position::from(
                                source.position_to_unicode_line_char(inst.span.end.into_usize()),
                            );

                            diagnostics.push(Diagnostic {
                                kind: DiagnosticKind::Error,
                                start,
                                end,
                                message: kind.to_string(),
                            });
                        }
                    }
                }
            }

            error
                .emit_diagnostics(&mut writer, &sources)
                .context("emitting to buffer should never fail")?;

            return Ok(CompileResult::from_error(
                error,
                diagnostics_output(writer),
                diagnostics,
                instructions,
            ));
        }
    };

    Ok(CompileResult::output(
        output,
        diagnostics_output(writer),
        diagnostics,
        instructions,
    ))
}

fn diagnostics_output(writer: rune::termcolor::Buffer) -> Option<String> {
    let mut string = String::from_utf8(writer.into_inner()).ok()?;
    let new_len = string.trim_end().len();
    string.truncate(new_len);
    Some(string)
}

#[wasm_bindgen]
pub async fn compile(input: String, config: JsValue) -> JsValue {
    let result = match inner_compile(input, config).await {
        Ok(result) => result,
        Err(error) => CompileResult::from_error(error, None, Vec::new(), None),
    };

    JsValue::from_serde(&result).unwrap()
}