UNPKG

4.35 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const FunctionDeclaration = require('./functiondeclaration');
18const JavaScriptParser = require('./javascriptparser');
19const debug = require('debug')('ergo-compiler:Script');
20
21/**
22 * <p>
23 * An executable script.
24 * </p>
25 * @private
26 * @class
27 * @memberof module:ergo-compiler
28 */
29class Script {
30
31 /**
32 * Create the Script.
33 * <p>
34 * @param {ModelManager} modelManager - The ModelManager associated with this Script
35 * @param {string} identifier - The identifier of the script
36 * @param {string} language - The language type of the script
37 * @param {string} contents - The contents of the script
38 * @param {string} contractName - The name of the contract if known or null
39 */
40 constructor(modelManager, identifier, language, contents, contractName) {
41 this.modelManager = modelManager;
42 this.identifier = identifier;
43 this.contractName = contractName;
44 this.language = language;
45 this.contents = contents;
46 this.functions = [];
47 this.tokens = [];
48
49 if(!contents) {
50 throw new Error('Empty script contents');
51 }
52 if (this.language === '.js') {
53 let data = {errorStatement:''};
54 let parser;
55
56 try {
57 parser = new JavaScriptParser(this.contents, false, 8);
58 } catch (cause) {
59 // consider adding a toHex method in the exception to put out the pure hex values of the file.
60 const error = new SyntaxError('Failed to parse ' + this.identifier + ': ' + cause.message+'\n'+data.errorStatement);
61 error.cause = cause;
62 debug('constructor', error.message, contents);
63 throw error;
64 }
65
66 const functions = parser.getFunctions();
67
68 for(let n=0; n < functions.length; n++) {
69 const func = functions[n];
70 const functionDeclaration =
71 new FunctionDeclaration(this.modelManager, this.language,
72 func.name, func.visibility,
73 func.returnType, func.throws, func.parameterNames,
74 func.parameterTypes, func.decorators, func.functionText );
75 this.functions.push( functionDeclaration );
76 }
77
78 this.tokens = parser.getTokens();
79
80 if (!this.getContractName()) {
81 let classNames = parser.getClasses().map(x => x.name);
82 if (classNames.length !== 0) {
83 this.contractName = classNames[0];
84 }
85 }
86 }
87 }
88
89 /**
90 * Returns the identifier of the script
91 * @return {string} the identifier of the script
92 */
93 getIdentifier() {
94 return this.identifier;
95 }
96
97 /**
98 * Returns the name of the contract for this script
99 * @return {string} the name of the contract, if known
100 */
101 getContractName() {
102 return this.contractName;
103 }
104
105 /**
106 * Returns the language of the script
107 * @return {string} the language of the script
108 */
109 getLanguage() {
110 return this.language;
111 }
112
113 /**
114 * Returns the contents of the script
115 * @return {string} the content of the script
116 */
117 getContents() {
118 return this.contents;
119 }
120
121 /**
122 * Returns the FunctionDeclaration for all functions that have been defined in this
123 * Script.
124 *
125 * @return {FunctionDeclaration[]} The array of FunctionDeclarations
126 */
127 getFunctionDeclarations() {
128 return this.functions;
129 }
130
131 /**
132 * Returns the tokens of the script
133 * @return {Object[]} the tokens of the script
134 */
135 getTokens() {
136 return this.tokens;
137 }
138
139}
140
141module.exports = Script;