Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow capabilities to be referenced before they're defined #48

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/crochet/crochet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export class BootedCrochet {

VM.Types.verify_package_types(cpkg);
VM.Types.verify_package_traits(cpkg);
VM.Capability.verify_package_capabilities(cpkg);
}

private reify_capability_grants(pkg: Package.ResolvedPackage) {
Expand Down
16 changes: 15 additions & 1 deletion source/vm/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,26 @@ export function load_declaration(
}

case t.CAPABILITY: {
const capability = new CrochetCapability(
const new_capability = new CrochetCapability(
module,
declaration.name,
declaration.documentation,
declaration.meta
);
let capability;
const missing = Capability.try_get_placeholder_capability(
module,
declaration.name
);
if (missing != null) {
capability = Capability.fulfill_placeholder_capability(
module,
missing,
new_capability
);
} else {
capability = new_capability;
}
Capability.define_capability(module, capability);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions source/vm/intrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ export class CrochetWorld {
export class CrochetPackage {
readonly missing_traits: Namespace<CrochetTrait>;
readonly missing_types: Namespace<CrochetType>;
readonly missing_capabilities: Namespace<CrochetCapability>;
readonly types: PassthroughNamespace<CrochetType>;
readonly traits: PassthroughNamespace<CrochetTrait>;
readonly definitions: PassthroughNamespace<CrochetValue>;
Expand All @@ -382,6 +383,7 @@ export class CrochetPackage {
) {
this.missing_traits = new Namespace(null, null, null);
this.missing_types = new Namespace(null, null, null);
this.missing_capabilities = new Namespace(null, null, null);
this.types = new PassthroughNamespace(world.types, name);
this.traits = new PassthroughNamespace(world.traits, name);
this.definitions = new PassthroughNamespace(world.definitions, name);
Expand Down
66 changes: 62 additions & 4 deletions source/vm/primitives/capability.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CrochetCapability,
CrochetModule,
CrochetPackage,
CrochetTrait,
CrochetType,
CrochetValue,
Expand Down Expand Up @@ -31,13 +32,70 @@ export function define_capability(

export function get_capability(module: CrochetModule, name: string) {
const capability = module.pkg.capabilities.try_lookup(name);
if (capability == null) {
if (capability != null) {
return capability;
}

const missing = module.pkg.missing_capabilities.try_lookup(name);
if (missing != null) {
return missing;
}

const placeholder = make_placeholder_capability(module, name);
module.pkg.missing_capabilities.define(name, placeholder);
return placeholder;
}

export function make_placeholder_capability(
module: CrochetModule,
name: string
) {
return new CrochetCapability(module, name, "(placeholder capability)", null);
}

export function try_get_placeholder_capability(
module: CrochetModule,
name: string
) {
return module.pkg.missing_capabilities.try_lookup(name);
}

export function fulfill_placeholder_capability(
module: CrochetModule,
placeholder: CrochetCapability,
capability: CrochetCapability
) {
const value = module.pkg.missing_capabilities.try_lookup(capability.name);
if (value !== placeholder) {
throw new ErrArbitrary(
"internal",
`Invalid placeholder for ${capability.name}`
);
}

module.pkg.missing_capabilities.remove(capability.name);
const p: { -readonly [k in keyof typeof capability]: typeof capability[k] } =
placeholder;
p.protecting = capability.protecting;
p.module = capability.module;
p.name = capability.name;
p.documentation = capability.documentation;
p.meta = capability.meta;

return p;
}

export function verify_package_capabilities(pkg: CrochetPackage) {
if (pkg.missing_capabilities.own_bindings.size > 0) {
throw new ErrArbitrary(
"undefined-capability",
`The capability ${name} is not defined in package ${module.pkg.name}`
"internal",
`Package ${
pkg.name
} cannot be loaded because it's missing the following capability definitions: ${[
...pkg.missing_capabilities.own_bindings.keys(),
].join(", ")}`
);
}
return capability;
}

export function protect_type(
Expand Down
7 changes: 7 additions & 0 deletions tests/vm-tests/capabilities.crochet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
% crochet

// We can refer to capabilities before they're defined
type capability-protected;
protect type capability-protected with protecc;

capability protecc;
3 changes: 2 additions & 1 deletion tests/vm-tests/crochet.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"actions.crochet",
"effects.crochet",
"dsl.crochet",
"traits.crochet"
"traits.crochet",
"capabilities.crochet"
],
"capabilities": {
"requires": ["crochet.debug/internal"],
Expand Down