mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 05:52:09 +00:00
vectorcraft-js now provides the ECMAScript 5.1 built-ins: the Object
property-descriptor functions, create, keys, seal and freeze (15.2),
Function.prototype.bind (15.3.4.5), Array with every ES5 method (15.4),
the error types (15.11), Math (15.8), JSON with revivers, replacers and
indentation (15.12), and the global functions with Annex B's escape and
unescape (15.1, B.2). Prototype methods are writable and configurable.
The array methods work on array-like objects and visit only the indices
that exist, so a sparse array of length 2^32-1 costs as much as its
elements, and they see indices a callback adds. join writes a cycle back
to an array as an empty string; JSON.stringify throws a TypeError on a
cycle. JSON parsing, writing and the reviver walk use no native
recursion. Every loop takes fuel and every result is checked against
the heap limit, so new Array(4294967295).join("x") ends with the heap
limit and a huge sort or stringify ends with a limit.
cargo xtask test262 also runs the suite's ES5 built-in tests and fetches
json2.js at a pinned commit, checked against its SHA-256, to compare its
output with the built-in JSON once Date, String.prototype.replace and
RegExp exist.
84 lines
3.8 KiB
Rust
84 lines
3.8 KiB
Rust
//! Runs `json2.js`, Douglas Crockford's public-domain JSON polyfill, when `cargo xtask test262` has
|
||
//! fetched it (it sets `VECTORCRAFT_JSON2`; without it the test looks in `corpus/json2/` and skips
|
||
//! when the file is absent).
|
||
//!
|
||
//! The built-in `JSON` is removed first, so the polyfill defines its own. Each sample value is
|
||
//! written by the polyfill and by the built-in `JSON.stringify` (ECMA-262 5.1 section 15.12.3),
|
||
//! which must give the same text, and the text is read back by the polyfill's `parse` and by the
|
||
//! built-in `JSON.parse` (section 15.12.2), which must give values that write the same text again.
|
||
//!
|
||
//! The polyfill uses `Date`, `String.prototype.replace` and regular expression `test`. While the
|
||
//! interpreter lacks any of them, the test prints that it skipped and passes.
|
||
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use vectorcraft_js::{JsValue, Realm};
|
||
|
||
fn json2_file() -> Option<PathBuf> {
|
||
if let Ok(path) = std::env::var("VECTORCRAFT_JSON2") {
|
||
return Some(PathBuf::from(path));
|
||
}
|
||
let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/json2");
|
||
let mut dirs: Vec<PathBuf> =
|
||
std::fs::read_dir(corpus).ok()?.filter_map(|e| e.ok().map(|e| e.path().join("json2.js"))).filter(|p| p.is_file()).collect();
|
||
dirs.sort();
|
||
dirs.pop()
|
||
}
|
||
|
||
/// Built-ins the polyfill needs.
|
||
const NEEDS: &str =
|
||
"typeof Date === 'function' && typeof ''.replace === 'function' && typeof RegExp === 'function' && typeof /a/.test === 'function'";
|
||
|
||
/// Saves the built-in `JSON` and removes it, so the polyfill defines its own.
|
||
const BEFORE: &str = "var NATIVE = JSON; delete this.JSON; this.JSON = undefined;";
|
||
|
||
/// Writes and reads each sample with both implementations; gives the first difference, or "ok".
|
||
const ROUND_TRIP: &str = r#"
|
||
var POLY = JSON;
|
||
var samples = [
|
||
0, -1.5, 1e21, "", "plain", "quote \" backslash \\ newline \n tab \t \u0001
é",
|
||
true, false, null, [], {}, [1, [2, [3, []]]], {a: 1, b: [true, null, "x"], c: {d: {}}},
|
||
[undefined, function () {}], {skip: undefined, f: function () {}, keep: 2}, [NaN, Infinity, -Infinity]
|
||
];
|
||
var problem = "ok";
|
||
for (var i = 0; i < samples.length && problem === "ok"; i++) {
|
||
var poly = POLY.stringify(samples[i]);
|
||
var native = NATIVE.stringify(samples[i]);
|
||
if (poly !== native) {
|
||
problem = "sample " + i + ": json2 wrote " + poly + ", the built-in wrote " + native;
|
||
} else if (poly !== undefined) {
|
||
if (NATIVE.stringify(POLY.parse(native)) !== native) {
|
||
problem = "sample " + i + ": json2 read " + native + " differently";
|
||
} else if (POLY.stringify(NATIVE.parse(poly)) !== poly) {
|
||
problem = "sample " + i + ": the built-in read " + poly + " differently";
|
||
}
|
||
}
|
||
}
|
||
var indented = {a: [1, {b: 2}], c: "d"};
|
||
if (problem === "ok" && POLY.stringify(indented, null, 2) !== NATIVE.stringify(indented, null, 2)) {
|
||
problem = "indented output differs";
|
||
}
|
||
if (problem === "ok" && POLY.stringify(indented, ["c"]) !== NATIVE.stringify(indented, ["c"])) {
|
||
problem = "property list output differs";
|
||
}
|
||
problem;
|
||
"#;
|
||
|
||
#[test]
|
||
fn json2_polyfill_round_trips_with_the_built_in_json() {
|
||
let Some(path) = json2_file() else {
|
||
println!("json2: SKIPPED (no json2.js; run cargo xtask test262)");
|
||
return;
|
||
};
|
||
let source = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
||
let mut realm = Realm::new();
|
||
if realm.run(NEEDS) != Ok(JsValue::Bool(true)) {
|
||
println!("json2: SKIPPED (the interpreter lacks Date, String.prototype.replace or RegExp, which json2.js uses)");
|
||
return;
|
||
}
|
||
match realm.run(&format!("{BEFORE}\n{source}\n{ROUND_TRIP}")) {
|
||
Ok(JsValue::String(s)) if s == "ok" => println!("json2: the polyfill and the built-in JSON agree on every sample"),
|
||
other => panic!("json2: {other:?}"),
|
||
}
|
||
}
|