mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
path.posix, path.win32 and the bare path module are one module object
each, the handle process.getBuiltinModule answers, so require('path/posix'),
path.posix and the target platform's bare path compare as Node's do, a
namespace beside other values in one slot is untyped, and path[family]
reads that object. Functions read through them take checked arguments:
each string parameter passes Node's validateString under its own name
(basename's suffix first), resolve validates paths[i] only as far as its
last-first walk reads them, parse answers an untyped { root, dir, base,
ext, name }, format follows _format over any object, and
toNamespacedPath answers a non-string as it is. A JavaScript call whose
arguments are not strings, or omit one, goes through the same checks.
Typed slots keep the typed function values.
path.win32.relative lowercases both resolved paths with
String.prototype.toLowerCase and walks them by UTF-16 unit, comparing
segments instead when lowercasing changed a length, as Node does. It
folded ASCII only, so a path with İ or Ⱥ in a different case gave a
longer relative path. The function moves into its own runtime unit,
scr_path_win32_relative.c, which links with the regex unit whose case
mapping it uses. scr_path.c stays free of it, so library and
cross-target builds that link the path functions without the regex unit
still link.
A JavaScript binding whose inferred type holds a union of functions and
arrays (a test table of [fn, cases] rows, or one destructured row) takes
the checked-dynamic kind that dynFallbackType gives the unmappable
spellings of such tables. Its rows keep their functions' names, and
for-of, forEach and destructuring read the items as untyped values.
TypeScript bindings keep their typed layout.
25 lines
881 B
JavaScript
25 lines
881 B
JavaScript
'use strict';
|
|
// Test tables that mix functions with arrays of cases, iterated through
|
|
// for-of and forEach with destructuring (JavaScript infers a union of
|
|
// functions and arrays for each row's items).
|
|
function add(a, b) { return a + b; }
|
|
function mul(a, b) { return a * b; }
|
|
|
|
const table = [
|
|
[add, [[1, 2], [3, 4]]],
|
|
[mul, [[5, 6], [0, 9]]],
|
|
];
|
|
for (const [fn, cases] of table) {
|
|
for (const [a, b] of cases) console.log(fn.name, a, b, fn(a, b));
|
|
}
|
|
table.forEach(([fn, cases]) => {
|
|
cases.forEach(([a, b]) => console.log('forEach', fn.name, fn.apply(null, [a, b])));
|
|
});
|
|
for (const row of table) {
|
|
const [fn, cases] = row;
|
|
console.log(typeof fn, Array.isArray(cases), cases.length, fn === add || fn === mul);
|
|
}
|
|
|
|
const nested = [[Math.max, [[1, 7, 3]]], [Math.min, [[4, -2]]]];
|
|
for (const [pick, lists] of nested) for (const list of lists) console.log(pick(...list));
|