-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtauModule.ts
156 lines (122 loc) Β· 4.88 KB
/
tauModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/// <reference path="./tauProlog.d.ts"/>
import {Project, structUtils} from '@yarnpkg/core';
import {PortablePath} from '@yarnpkg/fslib';
import {get as getPath} from 'es-toolkit/compat';
import pl from 'tau-prolog';
import vm from 'vm';
const {is_atom: isAtom, is_variable: isVariable, is_instantiated_list: isInstantiatedList} = pl.type;
function prependGoals(thread: pl.type.Thread, point: pl.type.State, goals: Array<pl.type.Term<number, string>>): void {
thread.prepend(goals.map(
goal => new pl.type.State(
point.goal.replace(goal),
point.substitution,
point,
),
));
}
const projects = new WeakMap<pl.type.Session, Project>();
function getProject(thread: pl.type.Thread): Project {
const project = projects.get(thread.session);
if (project == null)
throw new Error(`Assertion failed: A project should have been registered for the active session`);
return project;
}
const tauModule = new pl.type.Module(`constraints`, {
[`project_workspaces_by_descriptor/3`]: (thread, point, atom) => {
const [descriptorIdent, descriptorRange, workspaceCwd] = atom.args;
if (!isAtom(descriptorIdent) || !isAtom(descriptorRange)) {
thread.throw_error(pl.error.instantiation(atom.indicator));
return;
}
const ident = structUtils.parseIdent(descriptorIdent.id);
const descriptor = structUtils.makeDescriptor(ident, descriptorRange.id);
const project = getProject(thread);
const workspace = project.tryWorkspaceByDescriptor(descriptor);
if (isVariable(workspaceCwd)) {
if (workspace !== null) {
prependGoals(thread, point, [new pl.type.Term(`=`, [
workspaceCwd,
new pl.type.Term(String(workspace.relativeCwd)),
])]);
}
}
if (isAtom(workspaceCwd)) {
if (workspace !== null && workspace.relativeCwd === workspaceCwd.id) {
thread.success(point);
}
}
},
[`workspace_field/3`]: (thread, point, atom) => {
const [workspaceCwd, fieldName, fieldValue] = atom.args;
if (!isAtom(workspaceCwd) || !isAtom(fieldName)) {
thread.throw_error(pl.error.instantiation(atom.indicator));
return;
}
const project = getProject(thread);
const workspace = project.tryWorkspaceByCwd(workspaceCwd.id as PortablePath);
// Workspace not found => this predicate can never match
// We might want to throw here? We can be pretty sure the user did
// something wrong at this point
if (workspace == null)
return;
const value = getPath(workspace.manifest.raw!, fieldName.id);
// Field is not present => this predicate can never match
if (typeof value === `undefined`)
return;
prependGoals(thread, point, [new pl.type.Term(`=`, [
fieldValue,
// TODO: Investigate whether we should JSON.stringify primitive values too.
// For now we don't because it would be a breaking change.
// https://github.com/yarnpkg/berry/issues/3584
new pl.type.Term(typeof value === `object` ? JSON.stringify(value) : value),
])]);
},
[`workspace_field_test/3`]: (thread, point, atom) => {
const [workspaceCwd, fieldName, checkCode] = atom.args;
thread.prepend([new pl.type.State(
point.goal.replace(new pl.type.Term(`workspace_field_test`, [
workspaceCwd,
fieldName,
checkCode,
new pl.type.Term(`[]`, []),
])),
point.substitution,
point,
)]);
},
[`workspace_field_test/4`]: (thread, point, atom) => {
const [workspaceCwd, fieldName, checkCode, checkArgv] = atom.args;
if (!isAtom(workspaceCwd) || !isAtom(fieldName) || !isAtom(checkCode) || !isInstantiatedList(checkArgv)) {
thread.throw_error(pl.error.instantiation(atom.indicator));
return;
}
const project = getProject(thread);
const workspace = project.tryWorkspaceByCwd(workspaceCwd.id as PortablePath);
// Workspace not found => this predicate can never match
// We might want to throw here? We can be pretty sure the user did
// something wrong at this point
if (workspace == null)
return;
const value = getPath(workspace.manifest.raw!, fieldName.id);
// Field is not present => this predicate can never match
if (typeof value === `undefined`)
return;
// Inject the variables into a sandbox
const vars: {[key: string]: any} = {$$: value};
for (const [index, value] of (checkArgv.toJavaScript() as Array<string>).entries())
vars[`$${index}`] = value;
const result = vm.runInNewContext(checkCode.id, vars);
if (result) {
thread.success(point);
}
},
}, [
`project_workspaces_by_descriptor/3`,
`workspace_field/3`,
`workspace_field_test/3`,
`workspace_field_test/4`,
]);
export function linkProjectToSession(session: pl.type.Session, project: Project) {
projects.set(session, project);
session.consult(`:- use_module(library(${tauModule.id})).`);
}