KT-3008 Minor refactoring and clean up
This commit is contained in:
@@ -372,9 +372,6 @@
|
|||||||
<sources dir="${js.stdlib.output.dir}">
|
<sources dir="${js.stdlib.output.dir}">
|
||||||
<file name="${compiled.stdlib.js}"/>
|
<file name="${compiled.stdlib.js}"/>
|
||||||
</sources>
|
</sources>
|
||||||
<sources dir="${stdlib.js.dir}">
|
|
||||||
<file name="save-stdlib.js"/>
|
|
||||||
</sources>
|
|
||||||
|
|
||||||
<sources dir="${stdlib.js.dir}">
|
<sources dir="${stdlib.js.dir}">
|
||||||
<file name="merge.js"/>
|
<file name="merge.js"/>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
|||||||
import com.intellij.util.Function;
|
import com.intellij.util.Function;
|
||||||
import com.intellij.util.SmartList;
|
import com.intellij.util.SmartList;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
|
import com.intellij.util.containers.HashMap;
|
||||||
import kotlin.Unit;
|
import kotlin.Unit;
|
||||||
import kotlin.jvm.functions.Function1;
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -63,12 +64,21 @@ import org.jetbrains.kotlin.utils.PathUtil;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR;
|
import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR;
|
||||||
import static org.jetbrains.kotlin.cli.common.ExitCode.OK;
|
import static org.jetbrains.kotlin.cli.common.ExitCode.OK;
|
||||||
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
|
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
|
||||||
|
|
||||||
public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
||||||
|
private static final Map<String, ModuleKind> moduleKindMap = new HashMap<String, ModuleKind>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
moduleKindMap.put(K2JSCompilerArguments.MODULE_PLAIN, ModuleKind.PLAIN);
|
||||||
|
moduleKindMap.put(K2JSCompilerArguments.MODULE_COMMONJS, ModuleKind.COMMON_JS);
|
||||||
|
moduleKindMap.put(K2JSCompilerArguments.MODULE_AMD, ModuleKind.AMD);
|
||||||
|
moduleKindMap.put(K2JSCompilerArguments.MODULE_UMD, ModuleKind.UMD);
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String... args) {
|
public static void main(String... args) {
|
||||||
doMain(new K2JSCompiler(), args);
|
doMain(new K2JSCompiler(), args);
|
||||||
@@ -274,29 +284,14 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
configuration.put(JSConfigurationKeys.LIBRARY_FILES, libraryFiles);
|
configuration.put(JSConfigurationKeys.LIBRARY_FILES, libraryFiles);
|
||||||
String moduleKindName = arguments.moduleKind;
|
|
||||||
ModuleKind moduleKind = ModuleKind.PLAIN;
|
|
||||||
if (moduleKindName != null) {
|
|
||||||
moduleKindName = moduleKindName.toLowerCase();
|
|
||||||
if (moduleKindName.equals(K2JSCompilerArguments.MODULE_PLAIN)) {
|
|
||||||
moduleKind = ModuleKind.PLAIN;
|
|
||||||
}
|
|
||||||
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_AMD)) {
|
|
||||||
moduleKind = ModuleKind.AMD;
|
|
||||||
}
|
|
||||||
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_COMMONJS)) {
|
|
||||||
moduleKind = ModuleKind.COMMON_JS;
|
|
||||||
}
|
|
||||||
else if (moduleKindName.equals(K2JSCompilerArguments.MODULE_UMD)) {
|
|
||||||
moduleKind = ModuleKind.UMD;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
messageCollector.report(CompilerMessageSeverity.ERROR, "Unknown module kind: " + moduleKindName + ". " +
|
|
||||||
"valid values are: plain, amd, commonjs",
|
|
||||||
CompilerMessageLocation.NO_LOCATION);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String moduleKindName = arguments.moduleKind;
|
||||||
|
ModuleKind moduleKind = moduleKindName != null ? moduleKindMap.get(moduleKindName) : ModuleKind.PLAIN;
|
||||||
|
if (moduleKind == null) {
|
||||||
|
messageCollector.report(CompilerMessageSeverity.ERROR, "Unknown module kind: " + moduleKindName + ". " +
|
||||||
|
"valid values are: plain, amd, commonjs, umd",
|
||||||
|
CompilerMessageLocation.NO_LOCATION);
|
||||||
|
}
|
||||||
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind);
|
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -236,7 +236,7 @@ object KotlinJavascriptSerializationUtil {
|
|||||||
private fun JsModuleDescriptor<ModuleDescriptor>.toBinaryMetadata() = contentMapToByteArray(toContentMap(data), kind, imported)
|
private fun JsModuleDescriptor<ModuleDescriptor>.toBinaryMetadata() = contentMapToByteArray(toContentMap(data), kind, imported)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ByteArray.toContentMap(): JsModuleProto {
|
private fun ByteArray.readAsContentMap(name: String): JsModuleDescriptor<Map<String, ByteArray>> {
|
||||||
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(this))
|
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(this))
|
||||||
val content = JsProtoBuf.Library.parseFrom(gzipInputStream)
|
val content = JsProtoBuf.Library.parseFrom(gzipInputStream)
|
||||||
gzipInputStream.close()
|
gzipInputStream.close()
|
||||||
@@ -244,8 +244,9 @@ private fun ByteArray.toContentMap(): JsModuleProto {
|
|||||||
val contentMap: MutableMap<String, ByteArray> = hashMapOf()
|
val contentMap: MutableMap<String, ByteArray> = hashMapOf()
|
||||||
content.entryList.forEach { entry -> contentMap[entry.path] = entry.content.toByteArray() }
|
content.entryList.forEach { entry -> contentMap[entry.path] = entry.content.toByteArray() }
|
||||||
|
|
||||||
return JsModuleProto(
|
return JsModuleDescriptor(
|
||||||
contentMap = contentMap,
|
name = name,
|
||||||
|
data = contentMap,
|
||||||
kind = when (content.kind) {
|
kind = when (content.kind) {
|
||||||
null, JsProtoBuf.Library.Kind.PLAIN -> ModuleKind.PLAIN
|
null, JsProtoBuf.Library.Kind.PLAIN -> ModuleKind.PLAIN
|
||||||
JsProtoBuf.Library.Kind.AMD -> ModuleKind.AMD
|
JsProtoBuf.Library.Kind.AMD -> ModuleKind.AMD
|
||||||
@@ -255,5 +256,3 @@ private fun ByteArray.toContentMap(): JsModuleProto {
|
|||||||
imported = content.importedModulesList
|
imported = content.importedModulesList
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private class JsModuleProto(val contentMap: Map<String, ByteArray>, val kind: ModuleKind, val imported: List<String>)
|
|
||||||
+194
@@ -0,0 +1,194 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.js.translate.general;
|
||||||
|
|
||||||
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.js.translate.context.Namer;
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
||||||
|
import org.jetbrains.kotlin.serialization.js.ModuleKind;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public final class ModuleWrapperTranslation {
|
||||||
|
private ModuleWrapperTranslation() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<JsStatement> wrapIfNecessary(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program,
|
||||||
|
@NotNull ModuleKind kind
|
||||||
|
) {
|
||||||
|
switch (kind) {
|
||||||
|
case AMD:
|
||||||
|
return wrapAmd(moduleId, function, importedModules, program);
|
||||||
|
case COMMON_JS:
|
||||||
|
return wrapCommonJs(function, importedModules, program);
|
||||||
|
case UMD:
|
||||||
|
return wrapUmd(moduleId, function, importedModules, program);
|
||||||
|
case PLAIN:
|
||||||
|
default:
|
||||||
|
return wrapPlain(moduleId, function, importedModules, program);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapUmd(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName rootName = scope.declareName("root");
|
||||||
|
JsName factoryName = scope.declareName("factory");
|
||||||
|
JsName defineName = scope.declareName("define");
|
||||||
|
JsName exportsName = scope.declareName("exports");
|
||||||
|
|
||||||
|
JsExpression amdTest = JsAstUtils.and(JsAstUtils.typeOfIs(defineName.makeRef(), program.getStringLiteral("function")),
|
||||||
|
new JsNameRef("amd", defineName.makeRef()));
|
||||||
|
JsExpression commonJsTest = JsAstUtils.typeOfIs(exportsName.makeRef(), program.getStringLiteral("object"));
|
||||||
|
|
||||||
|
JsBlock amdBody = new JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program));
|
||||||
|
JsBlock commonJsBody = new JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program));
|
||||||
|
JsInvocation plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules, program);
|
||||||
|
|
||||||
|
JsExpression plainExpr;
|
||||||
|
if (moduleId != null) {
|
||||||
|
JsExpression lhs = Namer.requiresEscaping(moduleId) ?
|
||||||
|
new JsArrayAccess(rootName.makeRef(), program.getStringLiteral(moduleId)) :
|
||||||
|
new JsNameRef(scope.declareName(moduleId), rootName.makeRef());
|
||||||
|
plainExpr = JsAstUtils.assignment(lhs, plainInvocation);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
plainExpr = plainInvocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsStatement selector = JsAstUtils.newJsIf(amdTest, amdBody, JsAstUtils.newJsIf(commonJsTest, commonJsBody, plainExpr.makeStmt()));
|
||||||
|
JsFunction adapter = new JsFunction(program.getScope(), new JsBlock(selector), "UMD adapter");
|
||||||
|
adapter.getParameters().add(new JsParameter(rootName));
|
||||||
|
adapter.getParameters().add(new JsParameter(factoryName));
|
||||||
|
|
||||||
|
return Collections.singletonList(new JsInvocation(adapter, JsLiteral.THIS, function).makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapAmd(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName defineName = scope.declareName("define");
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
||||||
|
|
||||||
|
if (moduleId != null) {
|
||||||
|
invocationArgs.add(program.getStringLiteral(moduleId));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<JsExpression> moduleNameList = new ArrayList<JsExpression>(importedModules.size());
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
moduleNameList.add(program.getStringLiteral(importedModule.id));
|
||||||
|
}
|
||||||
|
invocationArgs.add(new JsArrayLiteral(moduleNameList));
|
||||||
|
|
||||||
|
invocationArgs.add(function);
|
||||||
|
|
||||||
|
JsInvocation invocation = new JsInvocation(defineName.makeRef(), invocationArgs);
|
||||||
|
return Collections.singletonList(invocation.makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapCommonJs(
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsScope scope = program.getScope();
|
||||||
|
JsName moduleName = scope.declareName("module");
|
||||||
|
JsName requireName = scope.declareName("require");
|
||||||
|
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
invocationArgs.add(new JsInvocation(requireName.makeRef(), program.getStringLiteral(importedModule.id)));
|
||||||
|
}
|
||||||
|
|
||||||
|
JsInvocation invocation = new JsInvocation(function, invocationArgs);
|
||||||
|
JsExpression assignment = JsAstUtils.assignment(new JsNameRef("exports", moduleName.makeRef()), invocation);
|
||||||
|
return Collections.singletonList(assignment.makeStmt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static List<JsStatement> wrapPlain(
|
||||||
|
@Nullable String moduleId,
|
||||||
|
@NotNull JsExpression function,
|
||||||
|
@NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program
|
||||||
|
) {
|
||||||
|
JsInvocation invocation = makePlainInvocation(function, importedModules, program);
|
||||||
|
|
||||||
|
JsStatement statement;
|
||||||
|
if (moduleId == null) {
|
||||||
|
statement = invocation.makeStmt();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statement = Namer.requiresEscaping(moduleId) ?
|
||||||
|
JsAstUtils.assignment(makePlainModuleRef(moduleId, program), invocation).makeStmt() :
|
||||||
|
JsAstUtils.newVar(program.getRootScope().declareName(moduleId), invocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.singletonList(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static JsInvocation makePlainInvocation(@NotNull JsExpression function, @NotNull List<ImportedModule> importedModules,
|
||||||
|
@NotNull JsProgram program) {
|
||||||
|
List<JsExpression> invocationArgs = new ArrayList<JsExpression>(importedModules.size());
|
||||||
|
|
||||||
|
for (ImportedModule importedModule : importedModules) {
|
||||||
|
invocationArgs.add(makePlainModuleRef(importedModule.id, program));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JsInvocation(function, invocationArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static JsExpression makePlainModuleRef(@NotNull String moduleId, @NotNull JsProgram program) {
|
||||||
|
// TODO: we could use `this.moduleName` syntax. However, this does not work for `kotlin` module in Rhino, since
|
||||||
|
// we run kotlin.js in a parent scope. Consider better solution
|
||||||
|
return Namer.requiresEscaping(moduleId) ?
|
||||||
|
new JsArrayAccess(JsLiteral.THIS, program.getStringLiteral(moduleId)) :
|
||||||
|
program.getScope().declareName(moduleId).makeRef();
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class ImportedModule {
|
||||||
|
@NotNull public final String id;
|
||||||
|
@NotNull public final JsName name;
|
||||||
|
|
||||||
|
public ImportedModule(@NotNull String id, @NotNull JsName name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.js.translate.declaration.PackageDeclarationTranslato
|
|||||||
import org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor;
|
import org.jetbrains.kotlin.js.translate.expression.ExpressionVisitor;
|
||||||
import org.jetbrains.kotlin.js.translate.expression.FunctionTranslator;
|
import org.jetbrains.kotlin.js.translate.expression.FunctionTranslator;
|
||||||
import org.jetbrains.kotlin.js.translate.expression.PatternTranslator;
|
import org.jetbrains.kotlin.js.translate.expression.PatternTranslator;
|
||||||
|
import org.jetbrains.kotlin.js.translate.general.ModuleWrapperTranslation.ImportedModule;
|
||||||
import org.jetbrains.kotlin.js.translate.test.JSRhinoUnitTester;
|
import org.jetbrains.kotlin.js.translate.test.JSRhinoUnitTester;
|
||||||
import org.jetbrains.kotlin.js.translate.test.JSTestGenerator;
|
import org.jetbrains.kotlin.js.translate.test.JSTestGenerator;
|
||||||
import org.jetbrains.kotlin.js.translate.test.JSTester;
|
import org.jetbrains.kotlin.js.translate.test.JSTester;
|
||||||
@@ -57,6 +58,7 @@ import java.util.Collection;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.jetbrains.kotlin.js.translate.general.ModuleWrapperTranslation.wrapIfNecessary;
|
||||||
import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getFunctionDescriptor;
|
import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getFunctionDescriptor;
|
||||||
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.convertToStatement;
|
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.convertToStatement;
|
||||||
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.toStringLiteralList;
|
import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.toStringLiteralList;
|
||||||
@@ -188,16 +190,6 @@ public final class Translation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ImportedModule {
|
|
||||||
final String id;
|
|
||||||
final JsName name;
|
|
||||||
|
|
||||||
public ImportedModule(String id, JsName name) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static TranslationContext doGenerateAst(
|
private static TranslationContext doGenerateAst(
|
||||||
@NotNull BindingTrace bindingTrace,
|
@NotNull BindingTrace bindingTrace,
|
||||||
@@ -249,157 +241,6 @@ public final class Translation {
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static List<JsStatement> wrapIfNecessary(
|
|
||||||
@NotNull String moduleId,
|
|
||||||
@NotNull JsExpression function,
|
|
||||||
@NotNull List<ImportedModule> importedModules,
|
|
||||||
@NotNull JsProgram program,
|
|
||||||
@NotNull ModuleKind kind
|
|
||||||
) {
|
|
||||||
switch (kind) {
|
|
||||||
case AMD:
|
|
||||||
return wrapAmd(moduleId, function, importedModules, program);
|
|
||||||
case COMMON_JS:
|
|
||||||
return wrapCommonJs(function, importedModules, program);
|
|
||||||
case UMD:
|
|
||||||
return wrapUmd(moduleId, function, importedModules, program);
|
|
||||||
case PLAIN:
|
|
||||||
default:
|
|
||||||
return wrapPlain(moduleId, function, importedModules, program);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static List<JsStatement> wrapUmd(
|
|
||||||
@Nullable String moduleId,
|
|
||||||
@NotNull JsExpression function,
|
|
||||||
@NotNull List<ImportedModule> importedModules,
|
|
||||||
@NotNull JsProgram program
|
|
||||||
) {
|
|
||||||
JsScope scope = program.getScope();
|
|
||||||
JsName rootName = scope.declareName("root");
|
|
||||||
JsName factoryName = scope.declareName("factory");
|
|
||||||
JsName defineName = scope.declareName("define");
|
|
||||||
JsName exportsName = scope.declareName("exports");
|
|
||||||
|
|
||||||
JsExpression amdTest = JsAstUtils.and(JsAstUtils.typeOfIs(defineName.makeRef(), program.getStringLiteral("function")),
|
|
||||||
new JsNameRef("amd", defineName.makeRef()));
|
|
||||||
JsExpression commonJsTest = JsAstUtils.typeOfIs(exportsName.makeRef(), program.getStringLiteral("object"));
|
|
||||||
|
|
||||||
JsBlock amdBody = new JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program));
|
|
||||||
JsBlock commonJsBody = new JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program));
|
|
||||||
JsInvocation plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules, program);
|
|
||||||
|
|
||||||
JsExpression plainExpr;
|
|
||||||
if (moduleId != null) {
|
|
||||||
JsExpression lhs = Namer.requiresEscaping(moduleId) ?
|
|
||||||
new JsArrayAccess(rootName.makeRef(), program.getStringLiteral(moduleId)) :
|
|
||||||
new JsNameRef(scope.declareName(moduleId), rootName.makeRef());
|
|
||||||
plainExpr = JsAstUtils.assignment(lhs, plainInvocation);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
plainExpr = plainInvocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsStatement selector = JsAstUtils.newJsIf(amdTest, amdBody, JsAstUtils.newJsIf(commonJsTest, commonJsBody, plainExpr.makeStmt()));
|
|
||||||
JsFunction adapter = new JsFunction(program.getScope(), new JsBlock(selector), "UMD adapter");
|
|
||||||
adapter.getParameters().add(new JsParameter(rootName));
|
|
||||||
adapter.getParameters().add(new JsParameter(factoryName));
|
|
||||||
|
|
||||||
return Collections.singletonList(new JsInvocation(adapter, JsLiteral.THIS, function).makeStmt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static List<JsStatement> wrapAmd(
|
|
||||||
@Nullable String moduleId,
|
|
||||||
@NotNull JsExpression function,
|
|
||||||
@NotNull List<ImportedModule> importedModules,
|
|
||||||
@NotNull JsProgram program
|
|
||||||
) {
|
|
||||||
JsScope scope = program.getScope();
|
|
||||||
JsName defineName = scope.declareName("define");
|
|
||||||
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
|
||||||
|
|
||||||
if (moduleId != null) {
|
|
||||||
invocationArgs.add(program.getStringLiteral(moduleId));
|
|
||||||
}
|
|
||||||
|
|
||||||
List<JsExpression> moduleNameList = new ArrayList<JsExpression>(importedModules.size());
|
|
||||||
for (ImportedModule importedModule : importedModules) {
|
|
||||||
moduleNameList.add(program.getStringLiteral(importedModule.id));
|
|
||||||
}
|
|
||||||
invocationArgs.add(new JsArrayLiteral(moduleNameList));
|
|
||||||
|
|
||||||
invocationArgs.add(function);
|
|
||||||
|
|
||||||
JsInvocation invocation = new JsInvocation(defineName.makeRef(), invocationArgs);
|
|
||||||
return Collections.singletonList(invocation.makeStmt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static List<JsStatement> wrapCommonJs(
|
|
||||||
@NotNull JsExpression function,
|
|
||||||
@NotNull List<ImportedModule> importedModules,
|
|
||||||
@NotNull JsProgram program
|
|
||||||
) {
|
|
||||||
JsScope scope = program.getScope();
|
|
||||||
JsName moduleName = scope.declareName("module");
|
|
||||||
JsName requireName = scope.declareName("require");
|
|
||||||
|
|
||||||
List<JsExpression> invocationArgs = new ArrayList<JsExpression>();
|
|
||||||
for (ImportedModule importedModule : importedModules) {
|
|
||||||
invocationArgs.add(new JsInvocation(requireName.makeRef(), program.getStringLiteral(importedModule.id)));
|
|
||||||
}
|
|
||||||
|
|
||||||
JsInvocation invocation = new JsInvocation(function, invocationArgs);
|
|
||||||
JsExpression assignment = JsAstUtils.assignment(new JsNameRef("exports", moduleName.makeRef()), invocation);
|
|
||||||
return Collections.singletonList(assignment.makeStmt());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static List<JsStatement> wrapPlain(
|
|
||||||
@Nullable String moduleId,
|
|
||||||
@NotNull JsExpression function,
|
|
||||||
@NotNull List<ImportedModule> importedModules,
|
|
||||||
@NotNull JsProgram program
|
|
||||||
) {
|
|
||||||
JsInvocation invocation = makePlainInvocation(function, importedModules, program);
|
|
||||||
|
|
||||||
JsStatement statement;
|
|
||||||
if (moduleId == null) {
|
|
||||||
statement = invocation.makeStmt();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
statement = Namer.requiresEscaping(moduleId) ?
|
|
||||||
JsAstUtils.assignment(makePlainModuleRef(moduleId, program), invocation).makeStmt() :
|
|
||||||
JsAstUtils.newVar(program.getRootScope().declareName(moduleId), invocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Collections.singletonList(statement);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static JsInvocation makePlainInvocation(@NotNull JsExpression function, @NotNull List<ImportedModule> importedModules,
|
|
||||||
JsProgram program) {
|
|
||||||
List<JsExpression> invocationArgs = new ArrayList<JsExpression>(importedModules.size());
|
|
||||||
|
|
||||||
for (ImportedModule importedModule : importedModules) {
|
|
||||||
invocationArgs.add(makePlainModuleRef(importedModule.id, program));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new JsInvocation(function, invocationArgs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static JsExpression makePlainModuleRef(@NotNull String moduleId, @NotNull JsProgram program) {
|
|
||||||
// TODO: we could use `this.moduleName` syntax. However, this does not work for `kotlin` module in Rhino, since
|
|
||||||
// we run kotlin.js in a parent scope. Consider better solution
|
|
||||||
return Namer.requiresEscaping(moduleId) ?
|
|
||||||
new JsArrayAccess(JsLiteral.THIS, program.getStringLiteral(moduleId)) :
|
|
||||||
program.getScope().declareName(moduleId).makeRef();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements, @NotNull String moduleId) {
|
private static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements, @NotNull String moduleId) {
|
||||||
JsName rootPackageName = context.scope().findName(Namer.getRootPackageName());
|
JsName rootPackageName = context.scope().findName(Namer.getRootPackageName());
|
||||||
if (rootPackageName != null) {
|
if (rootPackageName != null) {
|
||||||
|
|||||||
Vendored
+2
@@ -1,4 +1,6 @@
|
|||||||
|
var stdlib = module.exports;
|
||||||
var mergedRoot = Kotlin;
|
var mergedRoot = Kotlin;
|
||||||
|
|
||||||
for (var propertyName in stdlib) {
|
for (var propertyName in stdlib) {
|
||||||
Kotlin[propertyName] = stdlib[propertyName];
|
Kotlin[propertyName] = stdlib[propertyName];
|
||||||
}
|
}
|
||||||
|
|||||||
-2
@@ -1,2 +0,0 @@
|
|||||||
var builtins = module.exports;
|
|
||||||
delete Kotlin.modules.kotlin;
|
|
||||||
-2
@@ -1,2 +0,0 @@
|
|||||||
var stdlib = module.exports;
|
|
||||||
delete Kotlin.modules.kotlin;
|
|
||||||
Reference in New Issue
Block a user