polycomp 0.0.0-stage → 0.2.0

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/js/index.cjs ADDED
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { spawnSync } = require("child_process");
6
+ const frontendApi = require("./frontend.js");
7
+
8
+ const PKG_ROOT = path.resolve(__dirname, "..");
9
+ const FRONTEND_PATH = path.join(__dirname, "frontend.js");
10
+ const LANG_ALIASES = new Set(["c", "py", "python", "js", "javascript", "ts", "rs", "rust"]);
11
+ const KNOWN_FILE_EXTS = /\.(c|h|py|js|mjs|cjs|ts|rs|polyir|polyx)$/i;
12
+
13
+ function findNativeBinary() {
14
+ const exe = process.platform === "win32" ? "polycomp.exe" : "polycomp";
15
+ const candidates = [
16
+ process.env.POLYCOMP_BIN,
17
+ path.join(PKG_ROOT, "target", "release", exe),
18
+ path.join(PKG_ROOT, "target", "debug", exe),
19
+ path.join(PKG_ROOT, "bin", "native", exe),
20
+ ].filter(Boolean);
21
+
22
+ for (const cand of candidates) {
23
+ if (fs.existsSync(cand) && fs.statSync(cand).isFile()) {
24
+ return cand;
25
+ }
26
+ }
27
+ return null;
28
+ }
29
+
30
+ function requireNativeBinary() {
31
+ const bin = findNativeBinary();
32
+ if (!bin) {
33
+ throw new Error(
34
+ `PolyComp native VM binary not found. Build it once from "${PKG_ROOT}" with:\n cargo build --release`
35
+ );
36
+ }
37
+ return bin;
38
+ }
39
+
40
+ function classifyTarget(target) {
41
+ if (typeof target !== "string" || target.length === 0) {
42
+ throw new TypeError("PolyComp target must be a non-empty file path, source code, or IR/.polyx string");
43
+ }
44
+ if (frontendApi.isIrOrPolyx(target)) {
45
+ return { kind: "ir-text", text: target };
46
+ }
47
+ if (!target.includes("\n") && !target.includes("\r")) {
48
+ const resolved = path.resolve(process.cwd(), target);
49
+ if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
50
+ return { kind: "file", path: resolved };
51
+ }
52
+ if (!/\s/.test(target.trim()) && KNOWN_FILE_EXTS.test(target.trim())) {
53
+ throw new Error(`ENOENT: PolyComp input file not found: ${resolved}`);
54
+ }
55
+ }
56
+ return { kind: "source", code: target };
57
+ }
58
+
59
+ function parseExecuteArgs(langOrOptions, maybeOptions) {
60
+ let language;
61
+ let opts = {};
62
+ if (typeof langOrOptions === "string") {
63
+ language = langOrOptions;
64
+ if (maybeOptions && typeof maybeOptions === "object") opts = { ...maybeOptions };
65
+ } else if (langOrOptions && typeof langOrOptions === "object") {
66
+ opts = { ...langOrOptions };
67
+ language = opts.language || opts.lang;
68
+ }
69
+ return { language, ...opts };
70
+ }
71
+
72
+ function parseOutputAndLangArgs(filePathOrLangOrOpts, maybeThird) {
73
+ let output;
74
+ let language;
75
+ let opts = {};
76
+
77
+ if (typeof filePathOrLangOrOpts === "string") {
78
+ if (LANG_ALIASES.has(filePathOrLangOrOpts.toLowerCase())) {
79
+ language = filePathOrLangOrOpts;
80
+ if (typeof maybeThird === "string") output = maybeThird;
81
+ else if (maybeThird && typeof maybeThird === "object") opts = { ...maybeThird };
82
+ } else {
83
+ output = filePathOrLangOrOpts;
84
+ if (typeof maybeThird === "string") language = maybeThird;
85
+ else if (maybeThird && typeof maybeThird === "object") opts = { ...maybeThird };
86
+ }
87
+ } else if (filePathOrLangOrOpts && typeof filePathOrLangOrOpts === "object") {
88
+ opts = { ...filePathOrLangOrOpts };
89
+ }
90
+
91
+ return {
92
+ output: output ?? opts.output ?? opts.filePath ?? opts.out,
93
+ language: language ?? opts.language ?? opts.lang,
94
+ ...opts,
95
+ };
96
+ }
97
+
98
+ function runNative(args, stdinText) {
99
+ const bin = requireNativeBinary();
100
+ const res = spawnSync(bin, args, {
101
+ input: stdinText,
102
+ encoding: "utf8",
103
+ maxBuffer: 64 * 1024 * 1024,
104
+ });
105
+ if (res.error) throw res.error;
106
+ if (res.status !== 0) {
107
+ throw new Error((res.stderr || res.stdout || `polycomp VM exited with status ${res.status}`).trim());
108
+ }
109
+ return res;
110
+ }
111
+
112
+ /**
113
+ * Execute a file path, raw source code, or .polyir/.polyx string using the PolyComp native VM + Cranelift JIT.
114
+ */
115
+ function execute(target, langOrOptions, maybeOptions) {
116
+ const opts = parseExecuteArgs(langOrOptions, maybeOptions);
117
+ const info = classifyTarget(target);
118
+
119
+ const args = ["run", info.kind === "file" ? info.path : "-", "--json"];
120
+ if (info.kind === "source" && opts.language) args.push("--lang", opts.language);
121
+ if (opts.entry) args.push("--entry", opts.entry);
122
+ if (opts.jit === false) args.push("--no-jit");
123
+ if (opts.jitThreshold != null) args.push("--jit-threshold", String(opts.jitThreshold));
124
+ if (opts.memMode) args.push("--mem-mode", opts.memMode);
125
+ if (opts.traceImports) args.push("--trace-imports");
126
+ if (opts.key) args.push("--key", opts.key);
127
+ if (opts.keyfile) args.push("--keyfile", opts.keyfile);
128
+ if (opts.verifyKey) args.push("--verify-key", opts.verifyKey);
129
+ if (opts.allowUnsigned) args.push("--allow-unsigned");
130
+
131
+ const stdinPayload =
132
+ info.kind === "source" ? info.code : info.kind === "ir-text" ? info.text : undefined;
133
+ const res = runNative(args, stdinPayload);
134
+ const parsed = JSON.parse(res.stdout);
135
+ if (opts.streamStdout && parsed.stdout) process.stdout.write(parsed.stdout);
136
+ return parsed;
137
+ }
138
+
139
+ /**
140
+ * Compile and obfuscate source code or a file into a pure UTF-8 POLYX003 payload string via the PolyComp VM.
141
+ * Optionally writes the payload to `filePath` if provided.
142
+ */
143
+ function obfuscate(target, filePathOrOptions, maybeThird) {
144
+ const opts = parseOutputAndLangArgs(filePathOrOptions, maybeThird);
145
+ const info = classifyTarget(target);
146
+ if (info.kind === "ir-text") {
147
+ throw new Error("obfuscate() expects source code or a source file path");
148
+ }
149
+
150
+ const args = ["compile", info.kind === "file" ? info.path : "-", "-o", "-", "--obfuscate"];
151
+ if (info.kind === "source" && opts.language) args.push("--lang", opts.language);
152
+ if (opts.entry) args.push("--entry", opts.entry);
153
+ if (opts.key) args.push("--key", opts.key);
154
+ if (opts.keyfile) args.push("--keyfile", opts.keyfile);
155
+ if (opts.signKey) args.push("--sign-key", opts.signKey);
156
+ if (opts.traceImports) args.push("--trace-imports");
157
+
158
+ const res = runNative(args, info.kind === "source" ? info.code : undefined);
159
+ const payload = res.stdout;
160
+
161
+ if (opts.output) {
162
+ const outPath = path.resolve(process.cwd(), opts.output);
163
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
164
+ fs.writeFileSync(outPath, payload, "utf8");
165
+ }
166
+ return payload;
167
+ }
168
+
169
+ /**
170
+ * Compile source code or a file into plain UTF-8 IR (.polyir) via the PolyComp VM.
171
+ * Optionally writes to `outputPath` if provided.
172
+ */
173
+ function compile(target, filePathOrOptions, maybeThird) {
174
+ const opts = parseOutputAndLangArgs(filePathOrOptions, maybeThird);
175
+ const info = classifyTarget(target);
176
+ if (info.kind === "ir-text") return info.text;
177
+
178
+ const args = ["compile", info.kind === "file" ? info.path : "-", "-o", "-"];
179
+ if (opts.obfuscate) args.push("--obfuscate");
180
+ if (info.kind === "source" && opts.language) args.push("--lang", opts.language);
181
+ if (opts.entry) args.push("--entry", opts.entry);
182
+ if (opts.key) args.push("--key", opts.key);
183
+ if (opts.keyfile) args.push("--keyfile", opts.keyfile);
184
+ if (opts.signKey) args.push("--sign-key", opts.signKey);
185
+ if (opts.traceImports) args.push("--trace-imports");
186
+
187
+ const res = runNative(args, info.kind === "source" ? info.code : undefined);
188
+ const payload = res.stdout;
189
+ if (opts.output) {
190
+ const outPath = path.resolve(process.cwd(), opts.output);
191
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
192
+ fs.writeFileSync(outPath, payload, "utf8");
193
+ }
194
+ return payload;
195
+ }
196
+
197
+ /**
198
+ * Verify a .polyir or .polyx file or payload string using the PolyComp native VM.
199
+ */
200
+ function verify(irOrPolyxTextOrPath, options = {}) {
201
+ const info = classifyTarget(irOrPolyxTextOrPath);
202
+ const args = ["verify", info.kind === "file" ? info.path : "-"];
203
+ if (options.verifyKey) args.push("--verify-key", options.verifyKey);
204
+ return runNative(args, info.kind === "file" ? undefined : irOrPolyxTextOrPath).stdout.trim();
205
+ }
206
+
207
+ function check(filePath, options = {}) {
208
+ const args = ["check", path.resolve(process.cwd(), filePath)];
209
+ if (options.entry) args.push("--entry", options.entry);
210
+ if (options.traceImports) args.push("--trace-imports");
211
+ return runNative(args).stdout.trim();
212
+ }
213
+
214
+ function keygen(outDir = "./polycomp_keys") {
215
+ return runNative(["keygen", "-o", path.resolve(process.cwd(), outDir)]).stdout.trim();
216
+ }
217
+
218
+ /**
219
+ * Returns the standalone, zero-VM browser frontend JS file (`js/frontend.js`) as a string.
220
+ * If `outputPath` is provided, also writes it to that path.
221
+ */
222
+ function getFrontend(outputPath) {
223
+ const code = fs.readFileSync(FRONTEND_PATH, "utf8");
224
+ if (outputPath) {
225
+ const out = path.resolve(process.cwd(), outputPath);
226
+ fs.mkdirSync(path.dirname(out), { recursive: true });
227
+ fs.writeFileSync(out, code, "utf8");
228
+ }
229
+ return code;
230
+ }
231
+
232
+ const api = {
233
+ execute,
234
+ obfuscate,
235
+ compile,
236
+ verify,
237
+ check,
238
+ keygen,
239
+ getFrontend,
240
+ isIrOrPolyx: frontendApi.isIrOrPolyx,
241
+ findNativeBinary,
242
+ };
243
+ api.polyglot = api;
244
+
245
+ module.exports = api;
package/js/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export function getFrontend(outputPath?: string): string;
2
+ export const polyglot: typeof import("./index");
package/js/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ import { createRequire } from "module";
2
+
3
+ const require = createRequire(import.meta.url);
4
+ const cjs = require("./index.cjs");
5
+
6
+ export const {
7
+ execute,
8
+ obfuscate,
9
+ compile,
10
+ verify,
11
+ check,
12
+ keygen,
13
+ getFrontend,
14
+ isIrOrPolyx,
15
+ findNativeBinary,
16
+ polyglot,
17
+ } = cjs;
18
+
19
+ export default cjs;