@shd101wyy/yo 0.1.26 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +34 -34
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +29 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +36 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +225 -64
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +553 -535
- package/out/cjs/yo-cli.cjs +685 -651
- package/out/cjs/yo-lsp.cjs +596 -569
- package/out/esm/index.mjs +396 -378
- package/out/types/src/env.d.ts +1 -0
- package/out/types/src/evaluator/calls/helper.d.ts +4 -2
- package/out/types/src/evaluator/types/synthesizer.d.ts +1 -0
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/test-runner.d.ts +2 -0
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/scripts/probe-parser-parity.ts +61 -0
- package/scripts/probe-yo-self-parser.sh +33 -0
- package/scripts/validate-yo-self-fmt.ts +184 -0
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +7 -15
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6304 -4315
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- package/std/worker.yo +6 -10
package/package.json
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Probe: for each .yo file in tests/ and yo-self/, parse via TS and check
|
|
2
|
+
// if yo-self's parser via `yo-self compile --emit-c --skip-c-compiler` also
|
|
3
|
+
// succeeds (or fails for the same reason).
|
|
4
|
+
//
|
|
5
|
+
// We don't actually compile — we just lex+parse+evaluate enough to detect
|
|
6
|
+
// parser errors. But yo-self's `compile` subcommand runs the full pipeline
|
|
7
|
+
// which includes evaluation. So we use a lighter probe: try compiling
|
|
8
|
+
// from a tiny stub `main.yo` that imports each file.
|
|
9
|
+
//
|
|
10
|
+
// Simpler: just use the TS parser. If any file fails to TS-parse, log it.
|
|
11
|
+
// Then optionally compare with yo-self by running yo-self-bin and seeing
|
|
12
|
+
// if it also fails.
|
|
13
|
+
import Parser from "../src/parser";
|
|
14
|
+
import * as fs from "fs";
|
|
15
|
+
import * as path from "path";
|
|
16
|
+
|
|
17
|
+
function walk(dir: string, files: string[] = []): string[] {
|
|
18
|
+
if (!fs.existsSync(dir)) return files;
|
|
19
|
+
const stat = fs.statSync(dir);
|
|
20
|
+
if (stat.isFile() && dir.endsWith(".yo")) {
|
|
21
|
+
files.push(dir);
|
|
22
|
+
return files;
|
|
23
|
+
}
|
|
24
|
+
if (stat.isDirectory()) {
|
|
25
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
26
|
+
if ([".git", "node_modules", "out", "yo-out"].includes(entry)) continue;
|
|
27
|
+
walk(path.join(dir, entry), files);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return files;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const files = [
|
|
34
|
+
...walk("tests"),
|
|
35
|
+
...walk("yo-self"),
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
console.log(`Found ${files.length} .yo files\n`);
|
|
39
|
+
let tsOk = 0, tsFail = 0;
|
|
40
|
+
const tsFailures: { file: string; err: string }[] = [];
|
|
41
|
+
|
|
42
|
+
for (const f of files) {
|
|
43
|
+
const src = fs.readFileSync(f, "utf-8");
|
|
44
|
+
try {
|
|
45
|
+
const p = new Parser({ inputString: src, modulePath: f });
|
|
46
|
+
p.getProgram();
|
|
47
|
+
tsOk++;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
tsFail++;
|
|
50
|
+
tsFailures.push({ file: f, err: String(e).split("\n")[0].slice(0, 120) });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
console.log(`TS parser: ${tsOk} OK, ${tsFail} fail`);
|
|
55
|
+
if (tsFailures.length > 0 && tsFailures.length < 30) {
|
|
56
|
+
console.log("\nTS failures:");
|
|
57
|
+
for (const f of tsFailures.slice(0, 20)) {
|
|
58
|
+
console.log(` ${f.file}`);
|
|
59
|
+
console.log(` ${f.err}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Run yo-self-bin fmt on every .yo file, count fail/success.
|
|
3
|
+
BIN=/tmp/yo-self-fmt-bin
|
|
4
|
+
OK=0; FAIL=0; FAILS=()
|
|
5
|
+
|
|
6
|
+
while IFS= read -r f; do
|
|
7
|
+
# Copy to a temp so original isn't modified.
|
|
8
|
+
tmp="/tmp/yo_parse_probe_$$.yo"
|
|
9
|
+
cp "$f" "$tmp"
|
|
10
|
+
if "$BIN" fmt --check "$tmp" >/dev/null 2>&1 ; then
|
|
11
|
+
OK=$((OK+1))
|
|
12
|
+
else
|
|
13
|
+
# --check may exit 1 for "would format" — that's OK. Check stderr.
|
|
14
|
+
out=$("$BIN" fmt --check "$tmp" 2>&1)
|
|
15
|
+
rc=$?
|
|
16
|
+
if [ "$rc" = "0" ] || [ "$rc" = "1" ]; then
|
|
17
|
+
OK=$((OK+1))
|
|
18
|
+
else
|
|
19
|
+
FAIL=$((FAIL+1))
|
|
20
|
+
FAILS+=("$f: $out")
|
|
21
|
+
fi
|
|
22
|
+
fi
|
|
23
|
+
rm -f "$tmp"
|
|
24
|
+
done < <(find tests yo-self -name "*.yo" 2>/dev/null)
|
|
25
|
+
|
|
26
|
+
echo "OK: $OK FAIL: $FAIL"
|
|
27
|
+
if [ "$FAIL" -gt 0 ]; then
|
|
28
|
+
echo
|
|
29
|
+
echo "Failures (first 10):"
|
|
30
|
+
for f in "${FAILS[@]:0:10}"; do
|
|
31
|
+
echo " - $f"
|
|
32
|
+
done
|
|
33
|
+
fi
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// One-off validation: extract each test() block from src/tests/formatter.test.ts,
|
|
2
|
+
// run yo-self-bin fmt on the source, compare to the inline expected output.
|
|
3
|
+
//
|
|
4
|
+
// Usage: bun /tmp/validate-yo-self-fmt.ts
|
|
5
|
+
import { spawnSync } from "child_process";
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
|
|
8
|
+
const BIN = "/tmp/yo-self-fmt-bin";
|
|
9
|
+
const TEST_FILE = "src/tests/formatter.test.ts";
|
|
10
|
+
|
|
11
|
+
const source = fs.readFileSync(TEST_FILE, "utf-8");
|
|
12
|
+
|
|
13
|
+
// Match each `test("name", () => { ... });` block at the top level.
|
|
14
|
+
// Within: extract `const source = (template-literal);` and the final
|
|
15
|
+
// `expect(formatYoSource(source)).toBe(template-literal);`.
|
|
16
|
+
//
|
|
17
|
+
// Template literals may contain newlines, embedded ${...}, and escaped backticks.
|
|
18
|
+
// We use a state-machine over the source to find balanced backtick-delimited
|
|
19
|
+
// template literals starting at a given position.
|
|
20
|
+
function findTemplateLiteral(
|
|
21
|
+
s: string,
|
|
22
|
+
start: number
|
|
23
|
+
): { value: string; end: number } | null {
|
|
24
|
+
if (s[start] !== "`") return null;
|
|
25
|
+
let i = start + 1;
|
|
26
|
+
while (i < s.length) {
|
|
27
|
+
const c = s[i];
|
|
28
|
+
if (c === "\\") {
|
|
29
|
+
i += 2;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (c === "`") {
|
|
33
|
+
// Closing backtick.
|
|
34
|
+
return { value: s.slice(start, i + 1), end: i + 1 };
|
|
35
|
+
}
|
|
36
|
+
if (c === "$" && s[i + 1] === "{") {
|
|
37
|
+
// Skip the ${...} interpolation balanced by curlies.
|
|
38
|
+
let depth = 1;
|
|
39
|
+
i += 2;
|
|
40
|
+
while (i < s.length && depth > 0) {
|
|
41
|
+
if (s[i] === "{") depth++;
|
|
42
|
+
else if (s[i] === "}") depth--;
|
|
43
|
+
i++;
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function findStringLiteral(
|
|
53
|
+
s: string,
|
|
54
|
+
start: number
|
|
55
|
+
): { value: string; end: number } | null {
|
|
56
|
+
if (s[start] !== '"' && s[start] !== "'" && s[start] !== "`") return null;
|
|
57
|
+
if (s[start] === "`") return findTemplateLiteral(s, start);
|
|
58
|
+
const quote = s[start];
|
|
59
|
+
let i = start + 1;
|
|
60
|
+
while (i < s.length) {
|
|
61
|
+
if (s[i] === "\\") {
|
|
62
|
+
i += 2;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (s[i] === quote) return { value: s.slice(start, i + 1), end: i + 1 };
|
|
66
|
+
i++;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Parse all test("name", () => { ... }); blocks.
|
|
72
|
+
//
|
|
73
|
+
// Anchor matching on `test("name", () => {` so we don't mistake
|
|
74
|
+
// `test("name", { … })` substrings that appear inside test source/expected
|
|
75
|
+
// template literals (e.g. test 7's source includes a literal
|
|
76
|
+
// `test("format effect", { … })`). The `() =>` arrow distinguishes a real
|
|
77
|
+
// top-level `test()` call from the inner content.
|
|
78
|
+
type TestCase = { name: string; source: string; expected: string };
|
|
79
|
+
const cases: TestCase[] = [];
|
|
80
|
+
const skipped: { name: string; reason: string }[] = [];
|
|
81
|
+
|
|
82
|
+
const testStartRe = /test\("([^"]+)",\s*\(\s*\)\s*=>\s*\{/g;
|
|
83
|
+
let m: RegExpExecArray | null;
|
|
84
|
+
while ((m = testStartRe.exec(source)) !== null) {
|
|
85
|
+
const name = m[1];
|
|
86
|
+
const p = m.index + m[0].length;
|
|
87
|
+
|
|
88
|
+
// Find `const source = `...`;`
|
|
89
|
+
const srcMarker = "const source = ";
|
|
90
|
+
const srcIdx = source.indexOf(srcMarker, p);
|
|
91
|
+
if (srcIdx === -1) {
|
|
92
|
+
skipped.push({ name, reason: "no `const source = `" });
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const srcLitStart = srcIdx + srcMarker.length;
|
|
96
|
+
const srcLit = findStringLiteral(source, srcLitStart);
|
|
97
|
+
if (!srcLit) {
|
|
98
|
+
skipped.push({ name, reason: "source is not a string literal" });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Find the first `.toBe(` after the source declaration. Most tests use
|
|
103
|
+
// `expect(formatYoSource(source)).toBe(`<literal>`)` but some use the
|
|
104
|
+
// intermediate `const once = formatYoSource(source); expect(once).toBe(`<literal>`)`
|
|
105
|
+
// form — both are caught here because the first `.toBe(` carries the
|
|
106
|
+
// literal. Tests that compare two identifiers (e.g. `expect(twice).toBe(once)`
|
|
107
|
+
// for idempotency-only checks) have no literal to compare and are skipped.
|
|
108
|
+
const expMarker = ".toBe(";
|
|
109
|
+
const expIdx = source.indexOf(expMarker, srcLit.end);
|
|
110
|
+
if (expIdx === -1) {
|
|
111
|
+
skipped.push({ name, reason: "no `.toBe(` after source" });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
let expLitStart = expIdx + expMarker.length;
|
|
115
|
+
while (/\s/.test(source[expLitStart])) expLitStart++;
|
|
116
|
+
const expLit = findStringLiteral(source, expLitStart);
|
|
117
|
+
if (!expLit) {
|
|
118
|
+
skipped.push({
|
|
119
|
+
name,
|
|
120
|
+
reason:
|
|
121
|
+
"`.toBe(` argument is not a string literal (idempotency-only test?)",
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// eval the literals (template-strings with embedded vars: those aren't used
|
|
127
|
+
// in our test cases except for backslash-escaped backticks)
|
|
128
|
+
let srcValue: string, expectedValue: string;
|
|
129
|
+
try {
|
|
130
|
+
srcValue = eval(srcLit.value);
|
|
131
|
+
expectedValue = eval(expLit.value);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
skipped.push({ name, reason: `eval failed: ${e}` });
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
cases.push({ name, source: srcValue, expected: expectedValue });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
console.log(
|
|
140
|
+
`Extracted ${cases.length} test cases; skipped ${skipped.length}.\n`
|
|
141
|
+
);
|
|
142
|
+
if (skipped.length > 0) {
|
|
143
|
+
console.log("Skipped:");
|
|
144
|
+
for (const s of skipped) {
|
|
145
|
+
console.log(` - ${s.name}: ${s.reason}`);
|
|
146
|
+
}
|
|
147
|
+
console.log();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Run each through yo-self-bin
|
|
151
|
+
let pass = 0,
|
|
152
|
+
fail = 0;
|
|
153
|
+
const failures: { name: string; expected: string; actual: string }[] = [];
|
|
154
|
+
|
|
155
|
+
for (const tc of cases) {
|
|
156
|
+
const tmpPath = `/tmp/yo_fmt_${Date.now()}_${Math.floor(Math.random() * 1e9)}.yo`;
|
|
157
|
+
fs.writeFileSync(tmpPath, tc.source);
|
|
158
|
+
spawnSync(BIN, ["fmt", tmpPath], { encoding: "utf-8" });
|
|
159
|
+
const actual = fs.readFileSync(tmpPath, "utf-8");
|
|
160
|
+
fs.unlinkSync(tmpPath);
|
|
161
|
+
|
|
162
|
+
if (actual === tc.expected) {
|
|
163
|
+
console.log(`✓ ${tc.name}`);
|
|
164
|
+
pass++;
|
|
165
|
+
} else {
|
|
166
|
+
console.log(`✗ ${tc.name}`);
|
|
167
|
+
failures.push({ name: tc.name, expected: tc.expected, actual });
|
|
168
|
+
fail++;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
console.log(`\n${pass} passed, ${fail} failed, ${pass + fail} total\n`);
|
|
173
|
+
|
|
174
|
+
if (failures.length > 0) {
|
|
175
|
+
console.log("=== first 5 failures ===\n");
|
|
176
|
+
for (const f of failures.slice(0, 5)) {
|
|
177
|
+
console.log(`--- ${f.name} ---`);
|
|
178
|
+
console.log("expected:");
|
|
179
|
+
console.log(JSON.stringify(f.expected));
|
|
180
|
+
console.log("actual:");
|
|
181
|
+
console.log(JSON.stringify(f.actual));
|
|
182
|
+
console.log();
|
|
183
|
+
}
|
|
184
|
+
}
|
package/std/alg/hash.yo
CHANGED
|
@@ -2,37 +2,29 @@
|
|
|
2
2
|
//!
|
|
3
3
|
//! A fast, non-cryptographic hash function with good distribution.
|
|
4
4
|
//! Used internally by `HashMap` and `HashSet`.
|
|
5
|
-
|
|
6
5
|
/// FNV offset basis constant (0xcbf29ce484222325).
|
|
7
6
|
FNV_OFFSET_BASIS :: u64(14695981039346656037);
|
|
8
7
|
/// FNV prime constant (0x100000001b3).
|
|
9
8
|
FNV_PRIME :: u64(1099511628211);
|
|
10
|
-
|
|
11
9
|
/// Hash a byte sequence using FNV-1a.
|
|
12
|
-
fnv1a_hash_bytes :: (fn(bytes: *(u8), length: usize) -> u64)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
;
|
|
24
|
-
|
|
10
|
+
fnv1a_hash_bytes :: (fn(bytes : *(u8), length : usize) -> u64)({
|
|
11
|
+
hash := FNV_OFFSET_BASIS;
|
|
12
|
+
i := usize(0);
|
|
13
|
+
while(i < length, i = (i + usize(1)), {
|
|
14
|
+
byte := (bytes &+ i).*;
|
|
15
|
+
xor := (hash ^ u64(byte));
|
|
16
|
+
hash = (xor * FNV_PRIME);
|
|
17
|
+
});
|
|
18
|
+
return(hash);
|
|
19
|
+
});
|
|
25
20
|
/// Combine two hash values using `hash * 31 + other`.
|
|
26
21
|
///
|
|
27
22
|
/// The constant 31 is chosen because it is prime, close to a power of 2
|
|
28
23
|
/// (fast multiplication), and proven effective in practice.
|
|
29
|
-
hash_combine :: (fn(hash: u64, other: u64) -> u64)
|
|
30
|
-
|
|
31
|
-
;
|
|
32
|
-
|
|
33
|
-
export
|
|
24
|
+
hash_combine :: (fn(hash : u64, other : u64) -> u64)((hash * u64(31)) + other);
|
|
25
|
+
export(
|
|
34
26
|
fnv1a_hash_bytes,
|
|
35
27
|
hash_combine,
|
|
36
28
|
FNV_OFFSET_BASIS,
|
|
37
29
|
FNV_PRIME
|
|
38
|
-
;
|
|
30
|
+
);
|
package/std/allocator.yo
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
//! Memory allocation abstractions and global allocator interface.
|
|
2
|
-
|
|
3
|
-
{ SIZE_MAX } :: import "./libc/stdint.yo";
|
|
4
|
-
|
|
2
|
+
{ SIZE_MAX } :: import("./libc/stdint.yo");
|
|
5
3
|
/// Memory allocation error variants.
|
|
6
4
|
AllocError :: enum(
|
|
7
5
|
/// Allocator ran out of memory.
|
|
@@ -13,93 +11,80 @@ AllocError :: enum(
|
|
|
13
11
|
/// Pointer passed to dealloc/realloc is invalid.
|
|
14
12
|
InvalidPointer
|
|
15
13
|
);
|
|
16
|
-
|
|
17
14
|
/// Describes the size and alignment requirements for a memory allocation.
|
|
18
15
|
Layout :: struct(
|
|
19
16
|
/// Size in bytes.
|
|
20
|
-
size: usize,
|
|
17
|
+
size : usize,
|
|
21
18
|
/// Alignment in bytes (must be a power of two).
|
|
22
|
-
alignment: usize
|
|
19
|
+
alignment : usize
|
|
23
20
|
);
|
|
24
|
-
|
|
25
21
|
/// Get the memory layout (size and alignment) of a type at compile time.
|
|
26
22
|
layout_of ::
|
|
27
|
-
(fn(comptime(T): Type) -> comptime(Layout))
|
|
23
|
+
(fn(comptime(T) : Type) -> comptime(Layout))(
|
|
28
24
|
Layout(
|
|
29
|
-
size: sizeof(T),
|
|
30
|
-
alignment: alignof(T)
|
|
25
|
+
size : sizeof(T),
|
|
26
|
+
alignment : alignof(T)
|
|
31
27
|
)
|
|
32
|
-
;
|
|
33
|
-
|
|
28
|
+
);
|
|
34
29
|
/// Check if allocating `count` elements of type T would overflow `usize`.
|
|
35
|
-
size_would_overflow ::
|
|
36
|
-
(fn(comptime(T): Type, count: usize) -> bool)
|
|
30
|
+
size_would_overflow ::
|
|
31
|
+
(fn(comptime(T) : Type, count : usize) -> bool)({
|
|
37
32
|
type_size :: sizeof(T);
|
|
38
33
|
cond(
|
|
39
34
|
(type_size == usize(0)) => false,
|
|
40
35
|
true => (count > (SIZE_MAX / type_size))
|
|
41
36
|
)
|
|
42
|
-
};
|
|
43
|
-
|
|
37
|
+
});
|
|
44
38
|
/// User-implementable memory allocator interface.
|
|
45
39
|
/// Implement this trait to provide custom allocation strategies.
|
|
46
40
|
CustomAllocator :: trait(
|
|
47
41
|
/// Allocate memory for `count` items of type T.
|
|
48
|
-
alloc : (fn(self : *(Self), comptime(T): Type, count: usize) -> Result(?*(T), AllocError)),
|
|
49
|
-
|
|
42
|
+
alloc : (fn(self : *(Self), comptime(T) : Type, count : usize) -> Result(?*(T), AllocError)),
|
|
50
43
|
/// Allocate zeroed memory for `count` items of type T.
|
|
51
|
-
alloc_zeroed : (fn(self : *(Self), comptime(T): Type, count: usize) -> Result(?*(T), AllocError)),
|
|
52
|
-
|
|
44
|
+
alloc_zeroed : (fn(self : *(Self), comptime(T) : Type, count : usize) -> Result(?*(T), AllocError)),
|
|
53
45
|
/// Allocate aligned memory for `count` items of type T with the given alignment.
|
|
54
|
-
alloc_aligned : (fn(self : *(Self), comptime(T): Type, count: usize, alignment: usize) -> Result(?*(T), AllocError)),
|
|
55
|
-
|
|
46
|
+
alloc_aligned : (fn(self : *(Self), comptime(T) : Type, count : usize, alignment : usize) -> Result(?*(T), AllocError)),
|
|
56
47
|
/// Reallocate memory to `new_count` items, preserving existing data.
|
|
57
48
|
realloc : (fn(forall(T : Type), self : *(Self), old_ptr : ?*(T), new_count : usize) -> Result(?*(T), AllocError)),
|
|
58
|
-
|
|
59
49
|
/// Deallocate memory pointed to by `ptr`.
|
|
60
50
|
dealloc : (fn(forall(T : Type), self : *(Self), ptr : *(T)) -> Result(unit, AllocError))
|
|
61
51
|
);
|
|
62
|
-
|
|
63
52
|
/// Type-erased dynamic allocator wrapping any `CustomAllocator` implementation.
|
|
64
53
|
Allocator :: Dyn(CustomAllocator);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
__yo_malloc :
|
|
68
|
-
fn(size: usize) -> ?*(void),
|
|
54
|
+
extern(
|
|
55
|
+
"Yo",
|
|
56
|
+
__yo_malloc :
|
|
57
|
+
fn(size : usize) -> ?*(void),
|
|
69
58
|
__yo_calloc :
|
|
70
|
-
fn(nmemb: usize, size: usize) -> ?*(void),
|
|
59
|
+
fn(nmemb : usize, size : usize) -> ?*(void),
|
|
71
60
|
__yo_realloc :
|
|
72
61
|
fn(ptr : ?*(void), size : usize) -> ?*(void),
|
|
73
62
|
__yo_free :
|
|
74
63
|
fn(ptr : ?*(void)) -> unit,
|
|
75
64
|
__yo_aligned_alloc :
|
|
76
65
|
fn(alignment : usize, size : usize) -> ?*(void)
|
|
77
|
-
;
|
|
78
|
-
|
|
66
|
+
);
|
|
79
67
|
/// The process-wide global allocator (mimalloc by default, falls back to libc).
|
|
80
|
-
GlobalAllocator :: impl
|
|
68
|
+
GlobalAllocator :: impl({
|
|
81
69
|
malloc :: __yo_malloc;
|
|
82
70
|
calloc :: __yo_calloc;
|
|
83
71
|
realloc :: __yo_realloc;
|
|
84
72
|
free :: __yo_free;
|
|
85
73
|
aligned_alloc :: __yo_aligned_alloc;
|
|
86
|
-
|
|
87
|
-
export
|
|
74
|
+
export(
|
|
88
75
|
malloc,
|
|
89
76
|
calloc,
|
|
90
77
|
realloc,
|
|
91
78
|
free,
|
|
92
79
|
aligned_alloc
|
|
93
|
-
;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
export(
|
|
97
83
|
size_would_overflow,
|
|
98
84
|
CustomAllocator,
|
|
99
85
|
Allocator,
|
|
100
86
|
Layout,
|
|
101
87
|
layout_of,
|
|
102
88
|
AllocError,
|
|
103
|
-
|
|
104
89
|
GlobalAllocator
|
|
105
|
-
;
|
|
90
|
+
);
|
package/std/async.yo
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
//! Async utilities for cooperative task scheduling.
|
|
2
|
-
|
|
3
2
|
/// Suspend the current async task, allowing other tasks to run on the event loop.
|
|
4
3
|
/// Implemented as an immediately-completing async block that forces a suspension point.
|
|
5
4
|
yield :: (fn(using(io : IO)) -> Impl(Future(unit)))(
|
|
6
|
-
io.async(()=> {
|
|
7
|
-
return
|
|
5
|
+
io.async(() => {
|
|
6
|
+
return(());
|
|
8
7
|
})
|
|
9
8
|
);
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
yield
|
|
13
|
-
;
|
|
9
|
+
export(yield);
|