Packages
A package is just another Cresset file. You import it with a use line, and that line also says what the package may do.
use textutil from "lib/textutil.crs"
use files from "lib/files.crs" allow read
fn main() needs print, read:
print(textutil.shout("hello"))
print(files.line_count("examples/scores.txt"))
textutilhas noallow, so it can only compute. It is pure.filesis allowedread, and nothing else.- Call a package's functions as
name.function(...). - The path is relative to the file that contains the
useline.
The package gets exactly what you grant
When Cresset loads a package it checks every function in it against the allow list. A package that wants more is refused, before it loads:
error: package 'evil' wants permissions you did not grant it: network --> examples/evil_package.crs:4 | use evil from "lib/evil.crs" help: tidy() needs network. you granted nothing. If you trust it: use evil from "lib/evil.crs" allow network
That is the whole point. A package called "tidy up text" that quietly wants the network can't run until you say yes.
Other guarantees
- Calling a package function costs you its effects. If
files.line_countneedsread, the function that calls it must declareread. - Packages are isolated. A package can't see your variables, and you can't see its private ones. It only sees the built-in functions and itself.
- Grants can't be passed up the chain. A package can only hand a permission to something it imports if it holds that permission itself.
- A runtime cap backs it up. While a package function is running, the effects it can use are limited to its grant, even if you hand it a callback that would do more.
Sharing records and enums
A package can share the records and enums it declares. Reach them as package.Name, including in type annotations and in match:
use shapes from "lib/shapes.crs"
fn describe(s: shapes.Shape) -> text:
return "area {shapes.area(s)}"
fn main() needs print:
p = shapes.Point(x: 1, y: 2)
print(p, p.x)
print(describe(shapes.Shape.Circle(radius: 2)))
Here lib/shapes.crs declares record Point, enum Shape and the function area. Everything is checked as usual: a wrong field type, a mistyped variant, or a match that forgets a case is caught before the program runs. A mistyped type name gets a "did you mean" suggestion.
Private names
A name that starts with an underscore belongs to the package alone. The package can use _helper and _Hidden freely, but code that imports it can't:
fn _clean(text):
return text.trim().lower()
fn normalize(text):
return _clean(text)
Importing this package, pkg.normalize(...) works and pkg._clean(...) is refused, with a clear message, before the program runs. This holds for functions, records and enums, including when a type is named in an annotation. Private functions still count against the package's grant, so a hidden function can't use a permission you didn't give.
Pinning packages with a lock file
You reviewed a package and decided to trust it. What if it changes later? A lock file makes sure you notice:
node cresset.js lock main.crs
This writes cresset.lock next to your program, recording a SHA-256 hash of every package it imports. From then on, run, check and the editor tools verify each package against the lock before it is even parsed. A package that has changed, or a new one that isn't in the lock, is refused with a message that points at the use line:
error: package 'lib/files.crs' has changed since it was locked --> main.crs:2 | use files from "lib/files.crs" allow read help: if you made or trust the change, run cresset lock <file> again; if you did not, someone has altered the package
When you have read the change and trust it, run cresset lock again. --no-lock ignores the lock for one run. Line endings are ignored when hashing, so Windows and Linux checkouts agree. Commit cresset.lock along with your code.
Pinning protects you against a trusted package being silently swapped or edited. To get packages by name and version, from a folder or web address you control, see the package registry.
What packages are today
- They are single files. You can write them yourself or install them from a registry you run, and pin them with a lock file. There is no shared public registry yet.
- Everything not starting with
_is shared. - Types are told apart by the package they come from: a
Pointin one package is not aPointin another, or your ownPoint, and Cresset says so when you mix them up. - Circular imports are refused.
Next
Cresset can also catch mistakes in how you use your data: Types.