Debian Codegraph @

src/com/google/javascript/jscomp/jsonml/SecureCompiler.java

raw symbols SCIP
1 /*
2 * Copyright 2010 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp.jsonml;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.Lists;
22 import com.google.javascript.jscomp.CheckLevel;
23 import com.google.javascript.jscomp.Compiler;
24 import com.google.javascript.jscomp.CompilerInput;
25 import com.google.javascript.jscomp.CompilerOptions;
26 import com.google.javascript.jscomp.CompilerOptions.Reach;
27 import com.google.javascript.jscomp.JSError;
28 import com.google.javascript.jscomp.JSModule;
29 import com.google.javascript.jscomp.Result;
30 import com.google.javascript.jscomp.SourceFile;
31 import com.google.javascript.jscomp.VariableRenamingPolicy;
32
33 import java.util.ArrayList;
34
35 /**
36 * Compilation of JavaScript code which guarantees that all security
37 * capabilities are preserved after the process. In particular, it can be
38 * safely applied to cajoled source.
39 *
40 * JS Compiler is used for code analysis and optimization. It runs a series
41 * of passes which try to improve the code.
42 *
43 * For safety reasons, only a subset of local passes, which are provided by
44 * JS Compiler, are processed. Currently it includes:
45 * - elimination of temporary variables
46 *
47 * Using SecureCompiler is quite straightforward. A user just needs to create
48 * a new instance and call compile() method. Currently the only input which
49 * is supported is JsonML.
50 *
51 * @author [email protected] (Daniel Hans)
52 */
53 public class SecureCompiler {
54
55 private static final String COMPILATION_UNCOMPLETED_MSG =
56 "No compilation has been completed yet.";
57
58 private static final String COMPILATION_UNSUCCESSFUL_MSG =
59 "The last compilation was not successful.";
60
61 private static final String COMPILATION_ALREADY_COMPLETED_MSG =
62 "This instance has already compiled one source.";
63
64 private Compiler compiler;
65 private CompilerOptions options;
66 private JsonMLAst sourceAst;
67
68 /** Report from the last compilation */
69 private Report report;
70
71 public SecureCompiler() {
72 compiler = new Compiler();
73 options = getSecureCompilerOptions();
74 }
75
76 /**
77 * Returns compiled source in JsonML format.
78 */
79 public JsonML getJsonML() {
80 Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG);
81 Preconditions.checkState(report.success, COMPILATION_UNSUCCESSFUL_MSG);
82 return sourceAst.convertToJsonML();
83 }
84
85 /**
86 * Returns compiled source as a JavaScript.
87 */
88 public String getString() {
89 Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG);
90 Preconditions.checkState(report.success, COMPILATION_UNSUCCESSFUL_MSG);
91 return compiler.toSource();
92 }
93
94 /**
95 * Returns report from the last compilation.
96 */
97 public Report getReport() {
98 Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG);
99 return report;
100 }
101
102 public void compile(JsonML source) {
103 if (report != null) {
104 throw new IllegalStateException(COMPILATION_ALREADY_COMPLETED_MSG);
105 }
106
107 sourceAst = new JsonMLAst(source);
108
109 CompilerInput input = new CompilerInput(
110 sourceAst, "[[jsonmlsource]]", false);
111
112 JSModule module = new JSModule("[[jsonmlmodule]]");
113 module.add(input);
114
115 Result result = compiler.compileModules(
116 ImmutableList.<SourceFile>of(),
117 ImmutableList.of(module),
118 options);
119
120 report = generateReport(result);
121 }
122
123 /**
124 * Returns compiler options which are safe for compilation of a cajoled
125 * module. The set of options is similar to the one which is used by
126 * CompilationLevel in simple mode. The main difference is that variable
127 * renaming and closurePass options are turned off.
128 */
129 private CompilerOptions getSecureCompilerOptions() {
130 CompilerOptions options = new CompilerOptions();
131
132 options.variableRenaming = VariableRenamingPolicy.OFF;
133 options.setInlineVariables(Reach.LOCAL_ONLY);
134 options.inlineLocalFunctions = true;
135 options.checkGlobalThisLevel = CheckLevel.OFF;
136 options.coalesceVariableNames = true;
137 options.deadAssignmentElimination = true;
138 options.collapseVariableDeclarations = true;
139 options.convertToDottedProperties = true;
140 options.labelRenaming = true;
141 options.removeDeadCode = true;
142 options.optimizeArgumentsArray = true;
143 options.removeUnusedVars = false;
144 options.removeUnusedLocalVars = true;
145
146 return options;
147 }
148
149 public void enableFoldConstant() {
150 options.foldConstants = true;
151 }
152
153 Report generateReport(Result result) {
154 // a report may be generated only after actual compilation is complete
155 if (result == null) {
156 return null;
157 }
158
159 ArrayList<JsonMLError> errors = Lists.newArrayList();
160 for (JSError error : result.errors) {
161 errors.add(JsonMLError.make(error, sourceAst));
162 }
163
164 ArrayList<JsonMLError> warnings = Lists.newArrayList();
165 for (JSError warning : result.warnings) {
166 warnings.add(JsonMLError.make(warning, sourceAst));
167 }
168
169 return new Report(
170 errors.toArray(new JsonMLError[0]),
171 warnings.toArray(new JsonMLError[0]));
172 }
173
174 public class Report {
175 private final boolean success;
176 private final JsonMLError[] errors;
177 private final JsonMLError[] warnings;
178
179 private Report(JsonMLError[] errors, JsonMLError[] warnings) {
180 this.success = errors.length == 0;
181 this.errors = errors;
182 this.warnings = warnings;
183 }
184
185 public boolean isSuccessful() {
186 return success;
187 }
188
189 public JsonMLError[] getErrors() {
190 return errors;
191 }
192
193 public JsonMLError[] getWarnings() {
194 return warnings;
195 }
196 }
197
198 }