Search in sources :

Example 1 with StaticContext

use of org.jetbrains.kotlin.js.translate.context.StaticContext in project kotlin by JetBrains.

the class Translation method doGenerateAst.

@NotNull
private static TranslationContext doGenerateAst(@NotNull BindingTrace bindingTrace, @NotNull Collection<KtFile> files, @NotNull MainCallParameters mainCallParameters, @NotNull ModuleDescriptor moduleDescriptor, @NotNull JsConfig config) {
    StaticContext staticContext = StaticContext.generateStaticContext(bindingTrace, config, moduleDescriptor);
    JsProgram program = staticContext.getProgram();
    JsName rootPackageName = program.getRootScope().declareName(Namer.getRootPackageName());
    JsFunction rootFunction = staticContext.getRootFunction();
    JsBlock rootBlock = rootFunction.getBody();
    List<JsStatement> statements = rootBlock.getStatements();
    program.getScope().declareName("_");
    TranslationContext context = TranslationContext.rootContext(staticContext, rootFunction);
    PackageDeclarationTranslator.translateFiles(files, context);
    staticContext.postProcess();
    statements.add(0, program.getStringLiteral("use strict").makeStmt());
    if (!staticContext.isBuiltinModule()) {
        defineModule(context, statements, config.getModuleId());
    }
    mayBeGenerateTests(files, config, rootBlock, context);
    rootFunction.getParameters().add(new JsParameter((rootPackageName)));
    // Invoke function passing modules as arguments
    // This should help minifier tool to recognize references to these modules as local variables and make them shorter.
    List<StaticContext.ImportedModule> importedModuleList = new ArrayList<StaticContext.ImportedModule>();
    for (StaticContext.ImportedModule importedModule : staticContext.getImportedModules()) {
        rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName()));
        importedModuleList.add(importedModule);
    }
    if (mainCallParameters.shouldBeGenerated()) {
        JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments());
        if (statement != null) {
            statements.add(statement);
        }
    }
    statements.add(new JsReturn(rootPackageName.makeRef()));
    JsBlock block = program.getGlobalBlock();
    block.getStatements().addAll(wrapIfNecessary(config.getModuleId(), rootFunction, importedModuleList, program, config.getModuleKind()));
    return context;
}
Also used : ArrayList(java.util.ArrayList) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) StaticContext(org.jetbrains.kotlin.js.translate.context.StaticContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 StaticContext (org.jetbrains.kotlin.js.translate.context.StaticContext)1 TranslationContext (org.jetbrains.kotlin.js.translate.context.TranslationContext)1