-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjs-interop.ts
More file actions
21 lines (17 loc) · 1014 Bytes
/
Copy pathjs-interop.ts
File metadata and controls
21 lines (17 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-FileCopyrightText: 2026 MesTTo
//
// SPDX-License-Identifier: MIT
// JavaScript interop: because the engine is TypeScript, MeTTa can call straight into the host runtime,
// no FFI. `js-atom` resolves a global, `js-dot` calls a method, `js-list`/`js-dict` build JS values.
//
// Run it (after `pnpm build`): npx tsx examples/js-interop.ts
import { MeTTa } from "@mettascript/hyperon";
import { registerJsInterop } from "@mettascript/hyperon";
const m = new MeTTa();
registerJsInterop(m);
const run1 = (q: string): string[] => m.run(q)[0]!.map((a) => a.toString());
console.log("Math.abs(-5):", run1(`!((js-atom "Math.abs") -5)`)); // [ '5' ]
console.log("Math.max:", run1(`!((js-atom "Math.max") 3 7 2)`)); // [ '7' ]
console.log("toUpperCase:", run1(`!((js-dot "hello world" "toUpperCase"))`)); // [ '"HELLO WORLD"' ]
console.log("array join:", run1(`!((js-dot (js-list (5 1 3)) "join") "-")`)); // [ '"5-1-3"' ]
console.log("dict read:", run1(`!(js-dot (js-dict (("a" 1) ("b" 2))) "b")`)); // [ '2' ]