quickjs-oxide 0.4.1

A safe Rust JavaScript engine targeting semantic parity with QuickJS
Documentation
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
//! The engine's side of the Test262 runner: one
//! worker runtime with `$262`, `print` captured instead of written, the
//! harness and the test as separate scripts, an exact module resolver
//! supplied by the runner, and diagnostics read without running
//! JavaScript. The Test262 runner is its only user.

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use oxide_core::heap::gc::Handle;
use oxide_core::vm::compile::{CompileError, Compiler};
use oxide_core::vm::module::{ModuleHost, ModulePhase, ModuleSource};
use oxide_core::{Runtime, RuntimeOptions};

use super::compile::{
    ModuleCompileError, compile_for_core, compile_module, core_error_kind, run_agent_script,
};
use crate::engine::api::error::{Error, ErrorKind};
use crate::engine::code::debug::DebugInfoMode;
use crate::engine::compiler::compile_unlinked_script_bytes_with_filename;
use crate::engine::compiler::register::LowerError;
use crate::engine::compiler::register::{lower_script, tree_has_import_call};
use crate::source::coordinates::QuickJsSourceLocator;

/// A thrown value as the runner records it.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Diagnostic {
    /// The error's name (`SyntaxError`), `ThrownObject` for an object
    /// without one, `Thrown<type>` for a primitive.
    pub error_type: String,
    pub message: String,
    /// The one-based position of a syntax error.
    pub line: Option<u32>,
    pub column: Option<u32>,
    /// The thrown object's own `stack`, QuickJS's backtrace form.
    pub stack: Option<String>,
}

/// Why a step failed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Failure {
    /// The front end or the lowering declined a construct.
    Unsupported(String),
    /// JavaScript threw.
    Thrown(Diagnostic),
    /// The engine failed internally.
    Internal(String),
}

/// Where a module run failed.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModuleStep {
    /// Compiling the root module.
    Parse,
    /// Resolving, compiling or linking the rest of the graph.
    Resolution,
    /// Evaluating it.
    Runtime,
}

/// The runner's module loader: names resolve and load only inside the
/// admitted graph of the test.
pub trait ModuleResolver {
    /// The normalized name of `specifier` imported by `referrer`.
    fn normalize(&self, referrer: &str, specifier: &str) -> Result<String, String>;
    /// The source text of the module named `name`.
    fn load(&self, name: &str) -> Result<String, String>;
}

/// The settled state of a module evaluation after the job queue drained.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Settled {
    Fulfilled,
    Pending,
    Rejected(Diagnostic),
}

/// A compiled script, held until run.
pub struct Script {
    function: Handle,
    /// Whether its code has an `import()` call.
    pub contains_dynamic_import: bool,
}

struct ResolverState {
    resolver: Box<dyn ModuleResolver>,
    /// The root module's name and source, loaded without counting as
    /// resolution.
    root: RefCell<Option<(String, String)>>,
    /// Set once a name other than the root's is normalized or loaded.
    resolution_started: Cell<bool>,
    /// Whether a module of the graph has an `import()` call.
    dynamic_import: Cell<bool>,
    /// The located diagnostic of the last module that failed to compile.
    load_error: RefCell<Option<Diagnostic>>,
}

thread_local! {
    static RESOLVER: RefCell<Option<Rc<ResolverState>>> = const { RefCell::new(None) };
}

fn resolver() -> Option<Rc<ResolverState>> {
    RESOLVER.with(|state| state.borrow().clone())
}

/// One Test262 worker's runtime.
pub struct Worker {
    runtime: Runtime,
}

impl Worker {
    /// A runtime with `$262` and the agent host, `print` captured, the
    /// clock and time zone of `host`.
    pub fn new(can_block: bool, host: Box<dyn oxide_core::host::Host>) -> Result<Worker, String> {
        let failed = |error: oxide_core::HeapError| error.to_string();
        let mut runtime = Runtime::new(RuntimeOptions::default()).map_err(failed)?;
        runtime.set_host(host);
        runtime.set_can_block(can_block);
        runtime.set_compiler(compile_for_core);
        runtime.install_print().map_err(failed)?;
        runtime.install_test262_host().map_err(failed)?;
        runtime.set_script_runner(run_agent_script);
        runtime.capture_output();
        runtime.set_module_host(ModuleHost {
            normalize: normalize_module,
            load: load_module,
        });
        Ok(Worker { runtime })
    }

    /// Installs the loader the graph of the next module run resolves
    /// through; without one every import fails.
    pub fn set_module_resolver(&mut self, resolver: Box<dyn ModuleResolver>) {
        let state = ResolverState {
            resolver,
            root: RefCell::new(None),
            resolution_started: Cell::new(false),
            dynamic_import: Cell::new(false),
            load_error: RefCell::new(None),
        };
        RESOLVER.with(|slot| *slot.borrow_mut() = Some(Rc::new(state)));
    }

    /// Compiles and lowers a script; a syntax error carries its position.
    pub fn compile_script(&mut self, source: &str, filename: &str) -> Result<Script, Failure> {
        let script = compile_unlinked_script_bytes_with_filename(
            source.as_bytes(),
            filename,
            DebugInfoMode::Full,
        )
        .map_err(|error| compile_failure(&error, source.as_bytes()))?;
        let lowered = lower_script(&mut self.runtime, &script)
            .map_err(|error| lower_failure(error, source.as_bytes()))?;
        let function = self
            .runtime
            .hold(oxide_core::Value::object(lowered.function));
        Ok(Script {
            function,
            contains_dynamic_import: tree_has_import_call(self.runtime.units(), lowered.root),
        })
    }

    /// Runs a compiled script with the global object as `this`.
    pub fn run_script(&mut self, script: Script) -> Result<(), Failure> {
        let function = self.runtime.held(script.function);
        let result = match function.as_object() {
            Some(function) => self.runtime.run_script(function),
            None => unreachable!("a script function"),
        };
        self.runtime.release(script.function);
        result.map(drop).map_err(|thrown| self.thrown(thrown.0))
    }

    /// Runs pending jobs until the queue is empty.
    pub fn run_jobs(&mut self) -> Result<(), Failure> {
        self.runtime
            .run_jobs()
            .map_err(|thrown| self.thrown(thrown.0))
    }

    /// Loads, links and evaluates the module `name` with source `source`
    /// through the installed resolver, then drains the job queue and reads
    /// the evaluation's promise.
    pub fn run_module(
        &mut self,
        name: &str,
        source: &str,
    ) -> Result<Settled, (ModuleStep, Failure)> {
        let Some(state) = resolver() else {
            return Err((
                ModuleStep::Parse,
                Failure::Internal("no module resolver".into()),
            ));
        };
        *state.root.borrow_mut() = Some((name.to_owned(), source.to_owned()));
        state.resolution_started.set(false);
        state.dynamic_import.set(false);
        let promise = match self.runtime.run_module_phased(name) {
            Ok(promise) => promise,
            Err((phase, thrown)) => {
                let step = match phase {
                    ModulePhase::Load if state.resolution_started.get() => ModuleStep::Resolution,
                    ModulePhase::Load => ModuleStep::Parse,
                    ModulePhase::Link => ModuleStep::Resolution,
                    ModulePhase::Evaluate => ModuleStep::Runtime,
                };
                let located = match phase {
                    ModulePhase::Load => state.load_error.borrow_mut().take(),
                    _ => None,
                };
                let failure = match located {
                    Some(diagnostic) => Failure::Thrown(diagnostic),
                    None => self.thrown(thrown.0),
                };
                return Err((step, failure));
            }
        };
        let handle = self.runtime.hold(promise);
        let jobs = self.runtime.run_jobs();
        let settled = self.runtime.promise_state(promise);
        self.runtime.release(handle);
        if let Err(thrown) = jobs {
            return Err((ModuleStep::Runtime, self.thrown(thrown.0)));
        }
        Ok(match settled {
            Some((0, _)) => Settled::Pending,
            Some((2, reason)) => Settled::Rejected(self.diagnostic(reason)),
            _ => Settled::Fulfilled,
        })
    }

    /// Whether a module of the last graph has an `import()` call.
    pub fn module_graph_contains_dynamic_import(&self) -> bool {
        resolver().is_some_and(|state| state.dynamic_import.get())
    }

    /// Whether a name outside the root was normalized or loaded in the
    /// last module run.
    pub fn module_resolution_started(&self) -> bool {
        resolver().is_some_and(|state| state.resolution_started.get())
    }

    /// The `print` output so far, one entry per line.
    pub fn take_print_log(&mut self) -> Vec<String> {
        self.runtime
            .take_output()
            .lines()
            .map(str::to_owned)
            .collect()
    }

    /// Waits for the agents `$262.agent.start` started.
    pub fn join_agents(&mut self) {
        self.runtime.join_agents();
    }

    fn thrown(&mut self, value: oxide_core::Value) -> Failure {
        Failure::Thrown(self.diagnostic(value))
    }

    /// The runner's view of a thrown value, read without running
    /// JavaScript.
    fn diagnostic(&mut self, value: oxide_core::Value) -> Diagnostic {
        if let Some(summary) = self.runtime.thrown_summary(value) {
            return Diagnostic {
                error_type: summary.name.unwrap_or_else(|| "ThrownObject".into()),
                message: summary.message.unwrap_or_default(),
                line: None,
                column: None,
                stack: summary.stack,
            };
        }
        use oxide_core::value::Kind;
        let kind = match value.kind() {
            Kind::Undefined | Kind::Empty => "undefined",
            Kind::Null => "null",
            Kind::Bool => "boolean",
            Kind::Number | Kind::Int32 => "number",
            Kind::BigInt => "bigint",
            Kind::String => "string",
            Kind::Symbol => "symbol",
            Kind::Object => "object",
        };
        let message = if kind == "symbol" {
            "Symbol()".to_owned()
        } else {
            self.runtime.display(value).unwrap_or_default()
        };
        Diagnostic {
            error_type: format!("Thrown{kind}"),
            message,
            line: None,
            column: None,
            stack: None,
        }
    }
}

impl Drop for Worker {
    fn drop(&mut self) {
        RESOLVER.with(|slot| slot.borrow_mut().take());
    }
}

fn compile_failure(error: &Error, source: &[u8]) -> Failure {
    let name = match error.kind() {
        ErrorKind::Syntax => "SyntaxError",
        ErrorKind::Type => "TypeError",
        ErrorKind::Reference => "ReferenceError",
        ErrorKind::Range => "RangeError",
        ErrorKind::JsInternal => "InternalError",
        ErrorKind::Unsupported => return Failure::Unsupported(error.message().to_owned()),
        ErrorKind::Internal | ErrorKind::Io => return Failure::Internal(error.to_string()),
    };
    let position = (error.kind() == ErrorKind::Syntax)
        .then(|| error.span())
        .flatten()
        .and_then(|span| {
            QuickJsSourceLocator::from_bytes(source)
                .locate_byte_offset(span.start.byte_offset)
                .ok()
        })
        .and_then(|position| position.one_based());
    Failure::Thrown(Diagnostic {
        error_type: name.to_owned(),
        message: error.message().to_owned(),
        line: position.map(|(line, _)| line),
        column: position.map(|(_, column)| column),
        stack: None,
    })
}

fn lower_failure(error: LowerError, source: &[u8]) -> Failure {
    match error {
        LowerError::Unsupported(construct) => Failure::Unsupported(construct),
        LowerError::Syntax(message) => {
            compile_failure(&Error::new(ErrorKind::Syntax, message), source)
        }
        other => Failure::Internal(other.to_string()),
    }
}

fn normalize_module(
    _: &mut Compiler<'_>,
    referrer: &str,
    specifier: &str,
    _attributes: &[(String, String)],
) -> Result<String, CompileError> {
    let state = resolver().ok_or_else(|| reference_error("no module resolver".into()))?;
    state.resolution_started.set(true);
    state
        .resolver
        .normalize(referrer, specifier)
        .map_err(reference_error)
}

fn load_module(
    compiler: &mut Compiler<'_>,
    name: &str,
    attributes: &[(String, String)],
) -> Result<ModuleSource, CompileError> {
    let state = resolver().ok_or_else(|| reference_error("no module resolver".into()))?;
    let root = state.root.borrow().clone();
    let source = match root {
        Some((root, source)) if root == name => source,
        _ => {
            state.resolution_started.set(true);
            state.resolver.load(name).map_err(reference_error)?
        }
    };
    let json = attributes
        .iter()
        .any(|(key, value)| key == "type" && value == "json");
    if json {
        return Ok(ModuleSource::Json(
            source.encode_utf16().collect::<Vec<u16>>().into(),
        ));
    }
    let failed = |error: CompileError, diagnostic: Option<Diagnostic>| {
        *state.load_error.borrow_mut() = diagnostic;
        error
    };
    match compile_module(compiler, name, source.as_bytes(), DebugInfoMode::Full) {
        Ok((module, root)) => {
            if tree_has_import_call(compiler.units(), root) {
                state.dynamic_import.set(true);
            }
            Ok(module)
        }
        Err(ModuleCompileError::Front(error)) => {
            let diagnostic = match compile_failure(&error, source.as_bytes()) {
                Failure::Thrown(diagnostic) => Some(diagnostic),
                _ => None,
            };
            Err(failed(
                CompileError::new(core_error_kind(&error), error.message().to_owned()),
                diagnostic,
            ))
        }
        Err(ModuleCompileError::Core(error)) => Err(failed(error, None)),
    }
}

fn reference_error(message: String) -> CompileError {
    CompileError::new(oxide_core::ErrorKind::Reference, message)
}