/
scriptmanager.js
244 lines (220 loc) · 7.06 KB
/
scriptmanager.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Script = require('./script');
const ErgoCompiler = require('@accordproject/ergo-compiler').Compiler;
/**
* <p>
* Manages a set of scripts.
* </p>
* @private
* @class
* @memberof module:cicero-core
*/
class ScriptManager {
/**
* Create the ScriptManager.
* <p>
* <strong>Note: Only to be called by framework code. Applications should
* retrieve instances from {@link BusinessNetworkDefinition}</strong>
* </p>
* @param {ModelManager} modelManager - The ModelManager to use for this ScriptManager
*/
constructor(modelManager) {
this.modelManager = modelManager;
this.scripts = {};
this.compiledScript = null;
}
/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
accept(visitor,parameters) {
return visitor.visit(this, parameters);
}
/**
* Creates a new Script from a string.
*
* @param {string} identifier - the identifier of the script
* @param {string} language - the language identifier of the script
* @param {string} contents - the contents of the script
* @returns {Script} - the instantiated script
*/
createScript(identifier, language, contents) {
return new Script(this.modelManager, identifier, language, contents);
}
/**
* Modify an existing Script from a string.
*
* @param {string} identifier - the identifier of the script
* @param {string} language - the language identifier of the script
* @param {string} contents - the contents of the script
*/
modifyScript(identifier, language, contents) {
this.updateScript(new Script(this.modelManager, identifier, language, contents));
}
/**
* Adds a Script to the ScriptManager
* @param {Script} script - The script to add to the ScriptManager
*/
addScript(script) {
this.scripts[script.getIdentifier()] = script;
}
/**
* Update an existing Script in the ScriptManager
* @param {Script} script - The script to add to the ScriptManager
*/
updateScript(script) {
if (!this.scripts[script.getIdentifier()]) {
throw new Error('Script file does not exist');
}
this.addScript(script);
// Re-compile Ergo
this.compileLogic();
}
/**
* Remove the Script
* @param {string} identifier - The identifier of the script to remove
* delete.
*/
deleteScript(identifier) {
if (!this.scripts[identifier]) {
throw new Error('Script file does not exist');
}
delete this.scripts[identifier];
}
/**
* Get the array of Script instances
* @return {Script[]} The Scripts registered
* @private
*/
getScripts() {
let keys = Object.keys(this.scripts);
let result = [];
for(let n=0; n < keys.length;n++) {
result.push(this.scripts[keys[n]]);
}
return result;
}
/**
* Get the array of all Script instances, including compiled ones
* @return {Script[]} The Scripts registered, including compiled ones
* @private
*/
getAllScripts() {
let result = this.getScripts();
if (this.compiledScript !== null) {
result.push(this.compiledScript);
}
return result;
}
/**
* Get the array of Script instances for the given language
* @param {string} language - The scripts' language
* @return {Script[]} The Scripts registered
* @private
*/
getScriptsForLanguage(language) {
let keys = Object.keys(this.scripts);
let result = [];
for(let n=0; n < keys.length;n++) {
if (this.scripts[keys[n]].getLanguage() === language) {
result.push(this.scripts[keys[n]]);
}
}
return result;
}
/**
* Gets all the Ergo logic
* @return {Array<{name:string, content:string}>} the name and content of each Ergo file
*/
getLogic() {
let logic = [];
const scripts = this.getScriptsForLanguage('.ergo');
scripts.forEach(function (script) {
logic.push({ 'name' : script.getName(), 'content' : script.getContents() });
});
return logic;
}
/**
* Remove all registered Composer files
*/
clearScripts() {
this.scripts = {};
this.compiledScript = null;
}
/**
* Get the Script associated with an identifier
* @param {string} identifier - the identifier of the Script
* @return {Script} the Script
* @private
*/
getScript(identifier) {
return this.scripts[identifier];
}
/**
* Get the Script associated with an identifier
* @param {string} identifier - the identifier of the Script
* @return {Script} the Script
* @private
*/
getCompiledScript() {
if (!this.compiledScript) {
this.compileLogic();
}
return this.compiledScript;
}
/**
* Get the identifiers of all registered scripts
* @return {string[]} The identifiers of all registered scripts
*/
getScriptIdentifiers() {
return Object.keys(this.scripts);
}
/**
* Compile the Ergo logic
* @return {object} The script compiled to JavaScript
*/
compileLogic() {
let sourceErgo = this.getLogic();
if (sourceErgo === undefined || sourceErgo.length === 0) {
return null;
}
const compiledErgo = ErgoCompiler.compileToJavaScript(sourceErgo,this.modelManager.getModels(),'cicero',true);
//console.log('compiling' + this.contents);
if (compiledErgo.hasOwnProperty('error')) {
throw new Error(ErgoCompiler.ergoVerboseErrorToString(compiledErgo.error));
}
this.compiledScript = new Script(this.modelManager, 'main.js', '.js', compiledErgo.success);
return this.compiledScript;
}
/**
* Helper method to retrieve all function declarations
* @returns {Array} a list of function declarations
*/
allFunctionDeclarations() {
let allScripts = this.getAllScripts();
const functionDeclarations = allScripts
.map((ele) => {
return ele.getFunctionDeclarations();
}).reduce((flat, next) => {
return flat.concat(next);
},[]);
return functionDeclarations;
}
}
module.exports = ScriptManager;