本文整理汇总了Java中org.mozilla.javascript.CompilerEnvirons.setLanguageVersion方法的典型用法代码示例。如果您正苦于以下问题:Java CompilerEnvirons.setLanguageVersion方法的具体用法?Java CompilerEnvirons.setLanguageVersion怎么用?Java CompilerEnvirons.setLanguageVersion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mozilla.javascript.CompilerEnvirons
的用法示例。
在下文中一共展示了CompilerEnvirons.setLanguageVersion方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCompilerEnvironment
import org.mozilla.javascript.CompilerEnvirons; //导入方法依赖的package包/类
/**
* Creates options for Rhino based off of the user's preferences.
*
* @param errorHandler The container for errors found while parsing.
* @return The properties for the JS compiler to use.
*/
public static CompilerEnvirons createCompilerEnvironment(ErrorReporter
errorHandler, JavaScriptLanguageSupport langSupport) {
CompilerEnvirons env = new CompilerEnvirons();
env.setErrorReporter(errorHandler);
env.setIdeMode(true);
env.setRecordingComments(true);
env.setRecordingLocalJsDocComments(true);
env.setRecoverFromErrors(true);
if(langSupport != null) {
env.setXmlAvailable(langSupport.isXmlAvailable());
env.setStrictMode(langSupport.isStrictMode());
int version = langSupport.getLanguageVersion();
if (version > 0) {
Logger.log("[JavaScriptParser]: JS language version set to: " + version);
env.setLanguageVersion(version);
}
}
return env;
}
示例2: assertSource
import org.mozilla.javascript.CompilerEnvirons; //导入方法依赖的package包/类
/**
* Asserts that the value returned by {@link AstRoot#toSource()} after
* the given input source was parsed equals the specified expected output source.
*
* @param source the JavaScript source to be parsed
* @param expectedOutput the JavaScript source that is expected to be
* returned by {@link AstRoot#toSource()}
*/
private void assertSource(String source, String expectedOutput)
{
CompilerEnvirons env = new CompilerEnvirons();
env.setLanguageVersion(Context.VERSION_1_7);
Parser parser = new Parser(env);
AstRoot root = parser.parse(source, null, 0);
Assert.assertEquals(expectedOutput, root.toSource());
}
示例3: assertSource
import org.mozilla.javascript.CompilerEnvirons; //导入方法依赖的package包/类
private void assertSource(String source, String expectedOutput) {
CompilerEnvirons env = new CompilerEnvirons();
env.setLanguageVersion(Context.VERSION_ES6);
Parser parser = new Parser(env);
AstRoot root = parser.parse(source, null, 0);
Assert.assertEquals(expectedOutput, root.toSource());
}
示例4: transform
import org.mozilla.javascript.CompilerEnvirons; //导入方法依赖的package包/类
private String transform(String text) throws IOException {
sb.setLength(0);
CompilerEnvirons env = new CompilerEnvirons();
env.setRecoverFromErrors(true);
env.setLanguageVersion(Context.VERSION_1_8);
JSParser factory = new JSParser(env);
factory.enterFunction();
AstRoot rootNode = factory.parse(new StringReader(text), null, 0);
factory.exitFunction();
writer.hoist(rootNode);
writer.print(rootNode);
return sb.toString();
}
示例5: processJSBody
import org.mozilla.javascript.CompilerEnvirons; //导入方法依赖的package包/类
private void processJSBody(Diagnostics diagnostics, MethodReader methodToProcess) {
CallLocation location = new CallLocation(methodToProcess.getReference());
boolean isStatic = methodToProcess.hasModifier(ElementModifier.STATIC);
// validate parameter names
AnnotationReader bodyAnnot = methodToProcess.getAnnotations().get(JSBody.class.getName());
AnnotationValue paramsValue = bodyAnnot.getValue("params");
int jsParamCount = paramsValue != null ? paramsValue.getList().size() : 0;
if (methodToProcess.parameterCount() != jsParamCount) {
diagnostics.error(location, "JSBody method {{m0}} declares " + methodToProcess.parameterCount()
+ " parameters, but annotation specifies " + jsParamCount, methodToProcess.getReference());
return;
}
// generate parameter types for original method and validate
int paramCount = methodToProcess.parameterCount();
if (!isStatic) {
++paramCount;
}
if (!isStatic) {
ValueType paramType = ValueType.object(methodToProcess.getOwnerName());
if (!typeHelper.isSupportedType(paramType)) {
diagnostics.error(location, "Non-static JSBody method {{m0}} is owned by non-JS class {{c1}}",
methodToProcess.getReference(), methodToProcess.getOwnerName());
}
}
if (methodToProcess.getResultType() != ValueType.VOID
&& !typeHelper.isSupportedType(methodToProcess.getResultType())) {
diagnostics.error(location, "JSBody method {{m0}} returns unsupported type {{t1}}",
methodToProcess.getReference(), methodToProcess.getResultType());
}
// generate parameter types for proxy method
ValueType[] proxyParamTypes = new ValueType[paramCount + 1];
for (int i = 0; i < paramCount; ++i) {
proxyParamTypes[i] = ValueType.parse(JSObject.class);
}
proxyParamTypes[paramCount] = methodToProcess.getResultType() == ValueType.VOID
? ValueType.VOID
: ValueType.parse(JSObject.class);
// create proxy method
MethodReference proxyMethod = new MethodReference(methodToProcess.getOwnerName(),
methodToProcess.getName() + "$js_body$_" + methodIndexGenerator++, proxyParamTypes);
String script = bodyAnnot.getValue("script").getString();
String[] parameterNames = paramsValue != null ? paramsValue.getList().stream()
.map(AnnotationValue::getString)
.toArray(String[]::new) : new String[0];
// Parse JS script
TeaVMErrorReporter errorReporter = new TeaVMErrorReporter(diagnostics,
new CallLocation(methodToProcess.getReference()));
CompilerEnvirons env = new CompilerEnvirons();
env.setRecoverFromErrors(true);
env.setLanguageVersion(Context.VERSION_1_8);
env.setIdeMode(true);
JSParser parser = new JSParser(env, errorReporter);
AstRoot rootNode;
try {
rootNode = parser.parse(new StringReader("function(){" + script + "}"), null, 0);
} catch (IOException e) {
throw new RuntimeException("IO Error occurred", e);
}
AstNode body = ((FunctionNode) rootNode.getFirstChild()).getBody();
repository.methodMap.put(methodToProcess.getReference(), proxyMethod);
if (errorReporter.hasErrors()) {
repository.emitters.put(proxyMethod, new JSBodyBloatedEmitter(isStatic, proxyMethod,
script, parameterNames));
} else {
AstNode expr = JSBodyInlineUtil.isSuitableForInlining(methodToProcess.getReference(),
parameterNames, body);
if (expr != null) {
repository.inlineMethods.add(methodToProcess.getReference());
} else {
expr = body;
}
javaInvocationProcessor.process(location, expr);
repository.emitters.put(proxyMethod, new JSBodyAstEmitter(isStatic, expr, parameterNames));
}
}