Examples · interop · 51 of 339
main
Calling JavaScript from Tin.
tin run examples/interop/main.tin// Calling JavaScript from Tin. `import js` names an ES module (a file, a Node
// built-in like "node:os", or an installed package); each `extern fn` is answered
// by an export of the same name. Values cross as plain data, checked against the
// Tin types, the same boundary tools use. Async JS functions just work.
//
// tin run examples/interop/main.tin
import js "./geometry.mjs"
// A block says which module answers which extern. Use it when two imports export the same
// name (a flat extern that several modules export is an error, not a guess).
import js "node:path" {
extern fn basename(path: string): string
}
type Point = { x: float, y: float }
extern fn hypot(a: float, b: float): float
extern fn polygon(sides: int, radius: float): Point[]
extern fn slowUpper(s: string): string
fn main() {
print(hypot(3.0, 4.0))
let square = polygon(4, 1.0)
print("{square.len} corners, first at ({square[0].x}, {square[0].y})")
print(slowUpper("tin"))
print(basename("/robots/arm/pose.tin"))
}