mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
Scripts: .js scripts have Promise and run its jobs before the run ends
A script run from a .js file now has Promise as ECMA-262 11th edition section 25.6 defines it: the constructor, then, catch, finally, and Promise.resolve, reject, all, allSettled and race. .jsx scripts and scripts given as source keep the ES5.1 built-ins; a named engine gets Promise at the first .js run that names it and keeps it. - The callbacks run as jobs from one FIFO queue after the script's own code ends and before the run ends, sliced like the script, with no native recursion along a then chain. Each job takes 1 unit of fuel, so an endless chain stops at the fuel limit. - More than 1,048,576 waiting jobs end the run with a Jobs limit error. A run that fails, hits a limit or is stopped drops its waiting jobs and rolls back as before. - A promise still rejected with no handler when the run ends goes to the report's unhandledRejections (first 20) and unhandledRejectionCount, and to the Script Console with its file, line and column. - Until the language has iterators, all, allSettled and race take arrays, objects with a whole-number length and strings, and the statics always build plain Promise objects. Promise.any is not provided. cargo xtask test262 now runs the suite's built-ins/Promise tests that need only the built-ins present: 402 of 538 runs pass. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
1 parent
db9acab4da
commit
c417aff8fb
14 files changed
+1765
-32
No files matched your search
@@ -219,6 +219,13 @@ pub(crate) struct Files {
|
||||
frames: Vec<Frame>,
|
||||
}
|
||||
|
||||
/// A script at `path` runs with [`vectorcraft_js::Options::es2015`] when its file name ends in
|
||||
/// `.js` (in any letter case); a `.jsx` script, any other file and a script given as text run as
|
||||
/// ECMAScript 5.1 with the ExtendScript extensions.
|
||||
pub(crate) fn es2015_script(path: Option<&str>) -> bool {
|
||||
path.and_then(|p| Path::new(p).extension()).is_some_and(|e| e.eq_ignore_ascii_case("js"))
|
||||
}
|
||||
|
||||
/// The output being built: its UTF-16 text, the line it is on, the units since that line began,
|
||||
/// whether its last unit is a carriage return, and what the last `#strict` line so far set.
|
||||
struct Out {
|
||||
|
||||
@@ -1592,24 +1592,31 @@ fn preprocessor_error(f: include::Failure) -> (RunError, Option<Arc<str>>) {
|
||||
/// `e`, the error `realm` raised in the run of `host`, at the line and column of the file it is in,
|
||||
/// with that file's name unless it is the run's own script.
|
||||
fn located(host: &Host, realm: &Realm, e: &RunError) -> (RunError, Option<Arc<str>>) {
|
||||
let map = |m: &include::SourceMap| {
|
||||
let (index, file, line, column) = m.locate(e.line, e.column);
|
||||
let message = if e.kind == RunErrorKind::Syntax { relocated(&e.message, e.line, e.column, line, column) } else { e.message.clone() };
|
||||
(RunError { kind: e.kind.clone(), message, line, column, authored: e.authored }, index, file)
|
||||
};
|
||||
// An error `$.evalFile` raised for a file it could not run, and no script caught, is at that
|
||||
// file's place.
|
||||
if let Some((file, line, column)) = realm.error_mark().and_then(|m| host.eval_failures.borrow().get(m as usize).cloned()) {
|
||||
let message = if e.kind == RunErrorKind::Syntax { relocated(&e.message, e.line, e.column, line, column) } else { e.message.clone() };
|
||||
return (RunError { kind: e.kind.clone(), message, line, column, authored: e.authored }, Some(file));
|
||||
}
|
||||
placed(host, e, realm.error_source(), realm.error_tag())
|
||||
}
|
||||
|
||||
/// `e`, raised at its line and column of the source `source` with tag `tag` ([`Realm::source_of`]
|
||||
/// and [`Realm::error_tag`]), at the line and column of the file it is in, with that file's name
|
||||
/// unless it is the run's own script.
|
||||
fn placed(host: &Host, e: &RunError, source: u32, tag: u32) -> (RunError, Option<Arc<str>>) {
|
||||
let map = |m: &include::SourceMap| {
|
||||
let (index, file, line, column) = m.locate(e.line, e.column);
|
||||
let message = if e.kind == RunErrorKind::Syntax { relocated(&e.message, e.line, e.column, line, column) } else { e.message.clone() };
|
||||
(RunError { kind: e.kind.clone(), message, line, column, authored: e.authored }, index, file)
|
||||
};
|
||||
if e.line == 0 {
|
||||
return (e.clone(), None);
|
||||
}
|
||||
// An error in code from a source whose map is not at hand, such as code that `eval` compiled,
|
||||
// is given without a place.
|
||||
let unplaced = || (RunError { line: 0, column: 0, ..e.clone() }, None);
|
||||
match realm.error_source() {
|
||||
match source {
|
||||
0 => {
|
||||
let (e, index, file) = map(&host.script_map.borrow());
|
||||
(e, (index > 0).then_some(file))
|
||||
@@ -1626,7 +1633,7 @@ fn located(host: &Host, realm: &Realm, e: &RunError) -> (RunError, Option<Arc<st
|
||||
// `$.evalFile` gave. A script given as text is named by that run's name.
|
||||
_ => {
|
||||
let kept = host.kept_maps.as_ref().map(|k| k.borrow());
|
||||
match kept.as_ref().and_then(|k| k.get(realm.error_tag())) {
|
||||
match kept.as_ref().and_then(|k| k.get(tag)) {
|
||||
Some(kept) => {
|
||||
let (e, index, file) = map(&kept.map);
|
||||
let file = match &kept.script {
|
||||
@@ -1967,11 +1974,17 @@ impl Session {
|
||||
// A realm made here gets the script extensions, `$` and the dialogs; a kept one has them
|
||||
// from its first run, and they act for the run that calls them.
|
||||
let fresh = realm.is_none();
|
||||
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), ..Options::default() };
|
||||
// A `.js` script's realm has the later editions' built-ins (`Promise`). A kept realm gets
|
||||
// them at the first `.js` run that names it, and keeps them as it keeps its globals.
|
||||
let es2015 = include::es2015_script(request.file.as_deref());
|
||||
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), es2015, ..Options::default() };
|
||||
let strict = options.strict;
|
||||
let mut realm = match realm {
|
||||
Some(mut realm) => {
|
||||
realm.set_limits(limits);
|
||||
if es2015 {
|
||||
realm.add_es2015();
|
||||
}
|
||||
realm
|
||||
}
|
||||
None => {
|
||||
@@ -2264,6 +2277,11 @@ impl Session {
|
||||
"hostCalls": host.calls.get(),
|
||||
});
|
||||
host.more_written(&mut report);
|
||||
let rejections = rejections_report(host, &job.realm);
|
||||
if let (false, Some(o)) = (rejections.is_empty(), report.as_object_mut()) {
|
||||
o.insert("unhandledRejections".into(), Value::Array(rejections));
|
||||
o.insert("unhandledRejectionCount".into(), json!(job.realm.unhandled_rejection_count()));
|
||||
}
|
||||
close_run(self, host, &mut report, console::Ending::Finished);
|
||||
self.end_job(job, false);
|
||||
self.script_report = Some(report.clone());
|
||||
@@ -2309,6 +2327,29 @@ const NO_RUN: &str = "this function's script run has ended";
|
||||
const DROP_MAIN: &str = "the main script engine is new for each run and is never kept, so it cannot be dropped";
|
||||
const NO_ENGINES_ON_WEB: &str = "named script engines (#targetengine) are not available on the web";
|
||||
const ENGINE_BUSY: &str = "the script engine is in use by another run";
|
||||
const UNHANDLED_REJECTION: &str = "unhandled promise rejection";
|
||||
const UNHANDLED_REJECTIONS: &str = "unhandled promise rejections";
|
||||
|
||||
/// The promises `realm`'s run rejected and left without a handler, each with its reason and, when
|
||||
/// known, the line, column and file it was rejected at.
|
||||
fn rejections_report(host: &Host, realm: &Realm) -> Vec<Value> {
|
||||
let mut out = Vec::new();
|
||||
for r in realm.unhandled_rejections() {
|
||||
let e = RunError { kind: RunErrorKind::Thrown, message: r.message.clone(), line: r.line, column: r.column, authored: r.authored };
|
||||
let (e, file) = placed(host, &e, realm.source_of(r.tag), r.tag);
|
||||
let mut o = json!({
|
||||
"message": e.message,
|
||||
"messageOrigin": if e.authored { "script" } else { "built-in" },
|
||||
"line": e.line,
|
||||
"column": e.column,
|
||||
});
|
||||
if let (Some(file), Some(map)) = (file, o.as_object_mut()) {
|
||||
map.insert("file".into(), json!(&*file));
|
||||
}
|
||||
out.push(o);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The report of a run that failed; `file` is the included file the error is in.
|
||||
fn failure_report(name: &str, e: &RunError, file: Option<&str>, host: &Host, fuel_left: u64, fuel_used: u64) -> Value {
|
||||
@@ -2389,6 +2430,18 @@ fn close_run(s: &mut Session, host: &Host, report: &mut Value, ending: console::
|
||||
if report["ok"] == false {
|
||||
each_line(&error_text(&host.name, report), |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
||||
}
|
||||
if let Some(rejections) = report["unhandledRejections"].as_array() {
|
||||
for r in rejections {
|
||||
let mut placed = r.clone();
|
||||
placed["message"] = json!(format!("{UNHANDLED_REJECTION}: {}", r["message"].as_str().unwrap_or_default()));
|
||||
each_line(&error_text(&host.name, &placed), |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
||||
}
|
||||
let more = report["unhandledRejectionCount"].as_u64().unwrap_or(0).saturating_sub(rejections.len() as u64);
|
||||
if more > 0 {
|
||||
let text = format!("{}: {more} more {UNHANDLED_REJECTIONS}", host.name);
|
||||
each_line(&text, |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
||||
}
|
||||
}
|
||||
let timing = clock().is_some();
|
||||
let ms = millis(host.run_nanos.get());
|
||||
let slow = slowest(&host.commands.borrow());
|
||||
|
||||
Reference in new issue
Block a user