JS: change how declarations are exported from modules. Change how parts of kotlin.js merged

This commit is contained in:
Alexey Andreev
2016-10-25 12:50:16 +03:00
parent 3d9beb15da
commit f244bbaae3
10 changed files with 1295 additions and 1336 deletions
+9 -11
View File
@@ -368,20 +368,9 @@
<sources dir="${js.stdlib.output.dir}"> <sources dir="${js.stdlib.output.dir}">
<file name="builtins.js"/> <file name="builtins.js"/>
</sources>
<sources dir="${stdlib.js.dir}">
<file name="merge-builtins.js"/>
</sources>
<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="merge.js"/>
</sources>
<externs dir="${stdlib.js.dir}"> <externs dir="${stdlib.js.dir}">
<file name="externs.js"/> <file name="externs.js"/>
</externs> </externs>
@@ -412,6 +401,15 @@
<move file="${js.stdlib.output.dir}/tmp/kotlin" todir="${js.stdlib.output.dir}" /> <move file="${js.stdlib.output.dir}/tmp/kotlin" todir="${js.stdlib.output.dir}" />
<move file="${js.stdlib.output.dir}/tmp/${compiled.stdlib.meta.js}" tofile="${js.stdlib.output.dir}/${compiled.stdlib.meta.js}" /> <move file="${js.stdlib.output.dir}/tmp/${compiled.stdlib.meta.js}" tofile="${js.stdlib.output.dir}/${compiled.stdlib.meta.js}" />
<replaceregexp file="${js.stdlib.output.dir}/builtins.js"
match="module.exports,\s*require\([^)]+\)"
replace="Kotlin, Kotlin"
byline="true" encoding="UTF-8" />
<replaceregexp file="${js.stdlib.output.dir}/${compiled.stdlib.js}"
match="module.exports,\s*require\([^)]+\)"
replace="Kotlin, Kotlin"
byline="true" encoding="UTF-8" />
<condition property="jdk17" value="${env.JDK_17}" else="${env.JAVA_HOME}"> <condition property="jdk17" value="${env.JDK_17}" else="${env.JAVA_HOME}">
<isset property="env.JDK_17" /> <isset property="env.JDK_17" />
</condition> </condition>
@@ -39,7 +39,6 @@ import org.jetbrains.kotlin.js.translate.intrinsic.Intrinsics;
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils; import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe; import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTrace; import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils;
@@ -145,15 +144,12 @@ public final class StaticContext {
@NotNull @NotNull
private final Set<ClassDescriptor> classes = new HashSet<ClassDescriptor>(); private final Set<ClassDescriptor> classes = new HashSet<ClassDescriptor>();
@NotNull
private final ExportedPackage rootPackage = new ExportedPackage("");
@NotNull
private final JsObjectLiteral exportObject = rootPackage.objectLiteral;
@NotNull @NotNull
private final Set<MemberDescriptor> exportedDeclarations = new HashSet<MemberDescriptor>(); private final Set<MemberDescriptor> exportedDeclarations = new HashSet<MemberDescriptor>();
@NotNull
private final Map<FqName, JsName> localPackageNames = new HashMap<FqName, JsName>();
//TODO: too many parameters in constructor //TODO: too many parameters in constructor
private StaticContext( private StaticContext(
@NotNull JsProgram program, @NotNull JsProgram program,
@@ -665,11 +661,7 @@ public final class StaticContext {
exportedDeclarations.add(descriptor); exportedDeclarations.add(descriptor);
if (container instanceof PackageFragmentDescriptor) { if (container instanceof PackageFragmentDescriptor) {
PackageFragmentDescriptor packageDescriptor = (PackageFragmentDescriptor) container; JsExpression packageRef = getLocalPackageReference(((PackageFragmentDescriptor) container).getFqName());
ExportedPackage exportedPackage = rootPackage;
for (Name packageName : packageDescriptor.getFqName().pathSegments()) {
exportedPackage = exportedPackage.getSubpackage(packageName.asString());
}
JsExpression initializerExpr = DeclarationExporter.exportDeclaration(this, descriptor, exportStatements); JsExpression initializerExpr = DeclarationExporter.exportDeclaration(this, descriptor, exportStatements);
if (initializerExpr != null) { if (initializerExpr != null) {
@@ -679,8 +671,7 @@ public final class StaticContext {
MetadataProperties.setStaticRef(propertyName, initializerExpr); MetadataProperties.setStaticRef(propertyName, initializerExpr);
} }
} }
JsPropertyInitializer initializer = new JsPropertyInitializer(propertyName.makeRef(), initializerExpr); exportStatements.add(JsAstUtils.assignment(new JsNameRef(propertyName, packageRef), initializerExpr).makeStmt());
exportedPackage.objectLiteral.getPropertyInitializers().add(initializer);
} }
} }
else { else {
@@ -689,6 +680,24 @@ public final class StaticContext {
} }
} }
private JsExpression getLocalPackageReference(FqName packageName) {
if (packageName.isRoot()) {
return rootFunction.getScope().declareName(Namer.getRootPackageName()).makeRef();
}
JsName name = localPackageNames.get(packageName);
if (name == null) {
name = rootFunction.getScope().declareFreshName("package$" + packageName.shortName().asString());
localPackageNames.put(packageName, name);
JsExpression parentRef = getLocalPackageReference(packageName.parent());
JsExpression selfRef = new JsNameRef(packageName.shortName().asString(), parentRef);
JsExpression rhs = JsAstUtils.or(selfRef, JsAstUtils.assignment(selfRef.deepCopy(), new JsObjectLiteral(false)));
exportStatements.add(JsAstUtils.newVar(name, rhs));
}
return name.makeRef();
}
@NotNull @NotNull
public NameSuggestion getNameSuggestion() { public NameSuggestion getNameSuggestion() {
return nameSuggestion; return nameSuggestion;
@@ -704,11 +713,7 @@ public final class StaticContext {
rootFunction.getBody().getStatements().addAll(importStatements); rootFunction.getBody().getStatements().addAll(importStatements);
addClassPrototypes(); addClassPrototypes();
rootFunction.getBody().getStatements().addAll(declarationStatements); rootFunction.getBody().getStatements().addAll(declarationStatements);
JsName rootPackageName = rootFunction.getScope().declareName(Namer.getRootPackageName());
rootFunction.getBody().getStatements().add(JsAstUtils.newVar(rootPackageName, exportObject));
rootFunction.getBody().getStatements().addAll(exportStatements); rootFunction.getBody().getStatements().addAll(exportStatements);
rootFunction.getBody().getStatements().addAll(topLevelStatements); rootFunction.getBody().getStatements().addAll(topLevelStatements);
} }
@@ -753,25 +758,4 @@ public final class StaticContext {
} }
return false; return false;
} }
private static class ExportedPackage {
@NotNull final String name;
@NotNull final Map<String, ExportedPackage> subpackages = new HashMap<String, ExportedPackage>();
@NotNull final JsObjectLiteral objectLiteral = new JsObjectLiteral(true);
public ExportedPackage(@NotNull String name) {
this.name = name;
}
@NotNull
public ExportedPackage getSubpackage(@NotNull String name) {
ExportedPackage subpackage = subpackages.get(name);
if (subpackage == null) {
subpackage = new ExportedPackage(name);
subpackages.put(name, subpackage);
objectLiteral.getPropertyInitializers().add(new JsPropertyInitializer(new JsNameRef(name), subpackage.objectLiteral));
}
return subpackage;
}
}
} }
@@ -55,7 +55,7 @@ object ModuleWrapperTranslation {
val amdBody = JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program)) val amdBody = JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program))
val commonJsBody = JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program)) val commonJsBody = JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program))
val plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules, program) val plainInvocation = makePlainInvocation(moduleId, factoryName.makeRef(), importedModules, program)
val lhs: JsExpression = if (Namer.requiresEscaping(moduleId)) { val lhs: JsExpression = if (Namer.requiresEscaping(moduleId)) {
JsArrayAccess(rootName.makeRef(), program.getStringLiteral(moduleId)) JsArrayAccess(rootName.makeRef(), program.getStringLiteral(moduleId))
@@ -79,7 +79,7 @@ object ModuleWrapperTranslation {
val defineName = scope.declareName("define") val defineName = scope.declareName("define")
val invocationArgs = listOf( val invocationArgs = listOf(
program.getStringLiteral(moduleId), program.getStringLiteral(moduleId),
JsArrayLiteral(importedModules.map { program.getStringLiteral(it) }), JsArrayLiteral(listOf(program.getStringLiteral("exports")) + importedModules.map { program.getStringLiteral(it) }),
function function
) )
@@ -93,16 +93,15 @@ object ModuleWrapperTranslation {
val requireName = scope.declareName("require") val requireName = scope.declareName("require")
val invocationArgs = importedModules.map { JsInvocation(requireName.makeRef(), program.getStringLiteral(it)) } val invocationArgs = importedModules.map { JsInvocation(requireName.makeRef(), program.getStringLiteral(it)) }
val invocation = JsInvocation(function, invocationArgs) val invocation = JsInvocation(function, listOf(JsNameRef("exports", moduleName.makeRef())) + invocationArgs)
val assignment = JsAstUtils.assignment(JsNameRef("exports", moduleName.makeRef()), invocation) return listOf(invocation.makeStmt())
return listOf(assignment.makeStmt())
} }
private fun wrapPlain( private fun wrapPlain(
moduleId: String, function: JsExpression, moduleId: String, function: JsExpression,
importedModules: List<String>, program: JsProgram importedModules: List<String>, program: JsProgram
): List<JsStatement> { ): List<JsStatement> {
val invocation = makePlainInvocation(function, importedModules, program) val invocation = makePlainInvocation(moduleId, function, importedModules, program)
val statement = if (Namer.requiresEscaping(moduleId)) { val statement = if (Namer.requiresEscaping(moduleId)) {
JsAstUtils.assignment(makePlainModuleRef(moduleId, program), invocation).makeStmt() JsAstUtils.assignment(makePlainModuleRef(moduleId, program), invocation).makeStmt()
@@ -114,9 +113,18 @@ object ModuleWrapperTranslation {
return listOf(statement) return listOf(statement)
} }
private fun makePlainInvocation(function: JsExpression, importedModules: List<String>, program: JsProgram): JsInvocation { private fun makePlainInvocation(
moduleId: String,
function: JsExpression,
importedModules: List<String>,
program: JsProgram
): JsInvocation {
val invocationArgs = importedModules.map { makePlainModuleRef(it, program) } val invocationArgs = importedModules.map { makePlainModuleRef(it, program) }
return JsInvocation(function, invocationArgs) val moduleRef = makePlainModuleRef(moduleId, program)
val testModuleDefined = JsAstUtils.typeOfIs(moduleRef, program.getStringLiteral("undefined"))
val selfArg = JsConditional(testModuleDefined, JsObjectLiteral(false), moduleRef.deepCopy())
return JsInvocation(function, listOf(selfArg) + invocationArgs)
} }
private fun makePlainModuleRef(moduleId: String, program: JsProgram): JsExpression { private fun makePlainModuleRef(moduleId: String, program: JsProgram): JsExpression {
@@ -269,6 +269,9 @@ public final class Translation {
mayBeGenerateTests(files, config, rootBlock, context); mayBeGenerateTests(files, config, rootBlock, context);
JsName rootPackageName = program.getRootScope().declareName(Namer.getRootPackageName());
rootFunction.getParameters().add(new JsParameter((rootPackageName)));
// Invoke function passing modules as arguments // Invoke function passing modules as arguments
// This should help minifier tool to recognize references to these modules as local variables and make them shorter. // This should help minifier tool to recognize references to these modules as local variables and make them shorter.
List<String> importedModuleList = new ArrayList<String>(); List<String> importedModuleList = new ArrayList<String>();
@@ -288,7 +291,7 @@ public final class Translation {
} }
} }
statements.add(new JsReturn(program.getRootScope().declareName(Namer.getRootPackageName()).makeRef())); statements.add(new JsReturn(rootPackageName.makeRef()));
JsBlock block = program.getGlobalBlock(); JsBlock block = program.getGlobalBlock();
block.getStatements().addAll(wrapIfNecessary(config.getModuleId(), rootFunction, importedModuleList, program, block.getStatements().addAll(wrapIfNecessary(config.getModuleId(), rootFunction, importedModuleList, program,
+2 -5
View File
@@ -8,11 +8,8 @@
} }
}(this, function () { }(this, function () {
var Kotlin = {}; var Kotlin = {};
function require() {
return Kotlin;
}
var module = {};
%output% %output%
Kotlin.modules.kotlin = Kotlin;
return Kotlin; return Kotlin;
})); }));
+417 -421
View File
@@ -14,90 +14,87 @@
* limitations under the License. * limitations under the License.
*/ */
(function (Kotlin) { // Shims for String
"use strict"; if (typeof String.prototype.startsWith === "undefined") {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
};
}
if (typeof String.prototype.endsWith === "undefined") {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
// Shims for String String.prototype.contains = function (s) {
if (typeof String.prototype.startsWith === "undefined") { return this.indexOf(s) !== -1;
String.prototype.startsWith = function(searchString, position) { };
position = position || 0;
return this.lastIndexOf(searchString, position) === position; // Kotlin stdlib
};
} Kotlin.equals = function (obj1, obj2) {
if (typeof String.prototype.endsWith === "undefined") { if (obj1 == null) {
String.prototype.endsWith = function(searchString, position) { return obj2 == null;
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
} }
String.prototype.contains = function (s) { if (obj2 == null) {
return this.indexOf(s) !== -1; return false;
}; }
// Kotlin stdlib if (typeof obj1 == "object" && typeof obj1.equals_za3rmp$ === "function") {
return obj1.equals_za3rmp$(obj2);
}
Kotlin.equals = function (obj1, obj2) { return obj1 === obj2;
if (obj1 == null) { };
return obj2 == null;
}
if (obj2 == null) { Kotlin.hashCode = function (obj) {
return false; if (obj == null) {
} return 0;
}
if ("function" == typeof obj.hashCode) {
return obj.hashCode();
}
var objType = typeof obj;
if ("object" == objType || "function" == objType) {
return getObjectHashCode(obj);
} else if ("number" == objType) {
// TODO: a more elaborate code is needed for floating point values.
return obj | 0;
} if ("boolean" == objType) {
return Number(obj)
}
if (typeof obj1 == "object" && typeof obj1.equals_za3rmp$ === "function") { var str = String(obj);
return obj1.equals_za3rmp$(obj2); return getStringHashCode(str);
} };
return obj1 === obj2; Kotlin.toString = function (o) {
}; if (o == null) {
return "null";
}
else if (Array.isArray(o)) {
return "[...]";
}
else {
return o.toString();
}
};
Kotlin.hashCode = function (obj) { Kotlin.arrayToString = function (a) {
if (obj == null) { return "[" + a.map(Kotlin.toString).join(", ") + "]";
return 0; };
}
if ("function" == typeof obj.hashCode) {
return obj.hashCode();
}
var objType = typeof obj;
if ("object" == objType || "function" == objType) {
return getObjectHashCode(obj);
} else if ("number" == objType) {
// TODO: a more elaborate code is needed for floating point values.
return obj | 0;
} if ("boolean" == objType) {
return Number(obj)
}
var str = String(obj); Kotlin.arrayDeepToString = function (a, visited) {
return getStringHashCode(str); visited = visited || [a];
}; return "[" + a.map(function(e) {
Kotlin.toString = function (o) {
if (o == null) {
return "null";
}
else if (Array.isArray(o)) {
return "[...]";
}
else {
return o.toString();
}
};
Kotlin.arrayToString = function (a) {
return "[" + a.map(Kotlin.toString).join(", ") + "]";
};
Kotlin.arrayDeepToString = function (a, visited) {
visited = visited || [a];
return "[" + a.map(function(e) {
if (Array.isArray(e) && visited.indexOf(e) < 0) { if (Array.isArray(e) && visited.indexOf(e) < 0) {
visited.push(e); visited.push(e);
var result = Kotlin.arrayDeepToString(e, visited); var result = Kotlin.arrayDeepToString(e, visited);
@@ -108,396 +105,395 @@
return Kotlin.toString(e); return Kotlin.toString(e);
} }
}).join(", ") + "]"; }).join(", ") + "]";
}; };
Kotlin.compareTo = function (a, b) { Kotlin.compareTo = function (a, b) {
var typeA = typeof a; var typeA = typeof a;
var typeB = typeof a; var typeB = typeof a;
if (Kotlin.isChar(a) && typeB == "number") { if (Kotlin.isChar(a) && typeB == "number") {
return Kotlin.primitiveCompareTo(a.charCodeAt(0), b); return Kotlin.primitiveCompareTo(a.charCodeAt(0), b);
} }
if (typeA == "number" && Kotlin.isChar(b)) { if (typeA == "number" && Kotlin.isChar(b)) {
return Kotlin.primitiveCompareTo(a, b.charCodeAt(0)); return Kotlin.primitiveCompareTo(a, b.charCodeAt(0));
} }
if (typeA == "number" || typeA == "string") { if (typeA == "number" || typeA == "string") {
return a < b ? -1 : a > b ? 1 : 0;
}
return a.compareTo_za3rmp$(b);
};
Kotlin.primitiveCompareTo = function (a, b) {
return a < b ? -1 : a > b ? 1 : 0; return a < b ? -1 : a > b ? 1 : 0;
}; }
return a.compareTo_za3rmp$(b);
};
Kotlin.isNumber = function (a) { Kotlin.primitiveCompareTo = function (a, b) {
return typeof a == "number" || a instanceof Kotlin.Long; return a < b ? -1 : a > b ? 1 : 0;
}; };
Kotlin.isChar = function (value) { Kotlin.isNumber = function (a) {
return (typeof value) == "string" && value.length == 1; return typeof a == "number" || a instanceof Kotlin.Long;
}; };
Kotlin.isComparable = function (value) { Kotlin.isChar = function (value) {
var type = typeof value; return (typeof value) == "string" && value.length == 1;
};
return type === "string" || Kotlin.isComparable = function (value) {
type === "boolean" || var type = typeof value;
Kotlin.isNumber(value) ||
Kotlin.isType(value, Kotlin.kotlin.Comparable);
};
Kotlin.isCharSequence = function (value) { return type === "string" ||
return typeof value === "string" || Kotlin.isType(value, Kotlin.kotlin.CharSequence); type === "boolean" ||
}; Kotlin.isNumber(value) ||
Kotlin.isType(value, Kotlin.kotlin.Comparable);
};
Kotlin.charInc = function (value) { Kotlin.isCharSequence = function (value) {
return String.fromCharCode(value.charCodeAt(0)+1); return typeof value === "string" || Kotlin.isType(value, Kotlin.kotlin.CharSequence);
}; };
Kotlin.charDec = function (value) { Kotlin.charInc = function (value) {
return String.fromCharCode(value.charCodeAt(0)-1); return String.fromCharCode(value.charCodeAt(0)+1);
}; };
Kotlin.toShort = function (a) { Kotlin.charDec = function (value) {
return (a & 0xFFFF) << 16 >> 16; return String.fromCharCode(value.charCodeAt(0)-1);
}; };
Kotlin.toByte = function (a) { Kotlin.toShort = function (a) {
return (a & 0xFF) << 24 >> 24; return (a & 0xFFFF) << 16 >> 16;
}; };
Kotlin.toChar = function (a) { Kotlin.toByte = function (a) {
return String.fromCharCode((((a | 0) % 65536) & 0xFFFF) << 16 >>> 16); return (a & 0xFF) << 24 >> 24;
}; };
Kotlin.numberToLong = function (a) { Kotlin.toChar = function (a) {
return a instanceof Kotlin.Long ? a : Kotlin.Long.fromNumber(a); return String.fromCharCode((((a | 0) % 65536) & 0xFFFF) << 16 >>> 16);
}; };
Kotlin.numberToInt = function (a) { Kotlin.numberToLong = function (a) {
return a instanceof Kotlin.Long ? a.toInt() : (a | 0); return a instanceof Kotlin.Long ? a : Kotlin.Long.fromNumber(a);
}; };
Kotlin.numberToShort = function (a) { Kotlin.numberToInt = function (a) {
return Kotlin.toShort(Kotlin.numberToInt(a)); return a instanceof Kotlin.Long ? a.toInt() : (a | 0);
}; };
Kotlin.numberToByte = function (a) { Kotlin.numberToShort = function (a) {
return Kotlin.toByte(Kotlin.numberToInt(a)); return Kotlin.toShort(Kotlin.numberToInt(a));
}; };
Kotlin.numberToDouble = function (a) { Kotlin.numberToByte = function (a) {
return +a; return Kotlin.toByte(Kotlin.numberToInt(a));
}; };
Kotlin.numberToChar = function (a) { Kotlin.numberToDouble = function (a) {
return Kotlin.toChar(Kotlin.numberToInt(a)); return +a;
}; };
Kotlin.intUpto = function (from, to) { Kotlin.numberToChar = function (a) {
return new Kotlin.kotlin.ranges.IntRange(from, to); return Kotlin.toChar(Kotlin.numberToInt(a));
}; };
Kotlin.intDownto = function (from, to) { Kotlin.intUpto = function (from, to) {
return new Kotlin.kotlin.ranges.IntProgression(from, to, -1); return new Kotlin.kotlin.ranges.IntRange(from, to);
}; };
Kotlin.Throwable = Error; Kotlin.intDownto = function (from, to) {
return new Kotlin.kotlin.ranges.IntProgression(from, to, -1);
};
Kotlin.throwNPE = function (message) { Kotlin.Throwable = Error;
throw new Kotlin.kotlin.NullPointerException(message);
};
Kotlin.throwCCE = function () { Kotlin.throwNPE = function (message) {
throw new Kotlin.kotlin.ClassCastException("Illegal cast"); throw new Kotlin.kotlin.NullPointerException(message);
}; };
/** @const */ Kotlin.throwCCE = function () {
var POW_2_32 = 4294967296; throw new Kotlin.kotlin.ClassCastException("Illegal cast");
// TODO: consider switching to Symbol type once we are on ES6. };
/** @const */
var OBJECT_HASH_CODE_PROPERTY_NAME = "kotlinHashCodeValue$";
function getObjectHashCode(obj) { /** @const */
if (!(OBJECT_HASH_CODE_PROPERTY_NAME in obj)) { var POW_2_32 = 4294967296;
var hash = (Math.random() * POW_2_32) | 0; // Make 32-bit singed integer. // TODO: consider switching to Symbol type once we are on ES6.
Object.defineProperty(obj, OBJECT_HASH_CODE_PROPERTY_NAME, { value: hash, enumerable: false }); /** @const */
} var OBJECT_HASH_CODE_PROPERTY_NAME = "kotlinHashCodeValue$";
return obj[OBJECT_HASH_CODE_PROPERTY_NAME];
function getObjectHashCode(obj) {
if (!(OBJECT_HASH_CODE_PROPERTY_NAME in obj)) {
var hash = (Math.random() * POW_2_32) | 0; // Make 32-bit singed integer.
Object.defineProperty(obj, OBJECT_HASH_CODE_PROPERTY_NAME, { value: hash, enumerable: false });
}
return obj[OBJECT_HASH_CODE_PROPERTY_NAME];
}
function getStringHashCode(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
hash = (hash * 31 + code) | 0; // Keep it 32-bit.
}
return hash;
}
Kotlin.PropertyMetadata = Kotlin.createClassNow(null,
function (name) {
this.name = name;
}
);
Kotlin.safeParseInt = function (str) {
var r = parseInt(str, 10);
return isNaN(r) ? null : r;
};
Kotlin.safeParseDouble = function (str) {
var r = parseFloat(str);
return isNaN(r) ? null : r;
};
Kotlin.arrayEquals = function (a, b) {
if (a === b) {
return true;
}
if (!Array.isArray(b) || a.length !== b.length) {
return false;
} }
function getStringHashCode(str) { for (var i = 0, n = a.length; i < n; i++) {
var hash = 0; if (!Kotlin.equals(a[i], b[i])) {
for (var i = 0; i < str.length; i++) { return false;
var code = str.charCodeAt(i);
hash = (hash * 31 + code) | 0; // Keep it 32-bit.
} }
return hash; }
return true;
};
Kotlin.arrayDeepEquals = function (a, b) {
if (a === b) {
return true;
}
if (!Array.isArray(b) || a.length !== b.length) {
return false;
} }
Kotlin.PropertyMetadata = Kotlin.createClassNow(null, for (var i = 0, n = a.length; i < n; i++) {
function (name) { if (Array.isArray(a[i])) {
this.name = name; if (!Kotlin.arrayDeepEquals(a[i], b[i])) {
}
);
Kotlin.safeParseInt = function (str) {
var r = parseInt(str, 10);
return isNaN(r) ? null : r;
};
Kotlin.safeParseDouble = function (str) {
var r = parseFloat(str);
return isNaN(r) ? null : r;
};
Kotlin.arrayEquals = function (a, b) {
if (a === b) {
return true;
}
if (!Array.isArray(b) || a.length !== b.length) {
return false;
}
for (var i = 0, n = a.length; i < n; i++) {
if (!Kotlin.equals(a[i], b[i])) {
return false; return false;
} }
} } else if (!Kotlin.equals(a[i], b[i])) {
return true;
};
Kotlin.arrayDeepEquals = function (a, b) {
if (a === b) {
return true;
}
if (!Array.isArray(b) || a.length !== b.length) {
return false; return false;
} }
}
return true;
};
for (var i = 0, n = a.length; i < n; i++) { Kotlin.arrayHashCode = function (arr) {
if (Array.isArray(a[i])) { var result = 1;
if (!Kotlin.arrayDeepEquals(a[i], b[i])) { for (var i = 0, n = arr.length; i < n; i++) {
return false; result = ((31 * result | 0) + Kotlin.hashCode(arr[i])) | 0;
} }
} else if (!Kotlin.equals(a[i], b[i])) { return result;
return false; };
Kotlin.arrayDeepHashCode = function (arr) {
var result = 1;
for (var i = 0, n = arr.length; i < n; i++) {
var e = arr[i];
result = ((31 * result | 0) + (Array.isArray(e) ? Kotlin.arrayDeepHashCode(e) : Kotlin.hashCode(e))) | 0;
}
return result;
};
var BaseOutput = Kotlin.createClassNow(null, null, {
println: function (a) {
if (typeof a !== "undefined") this.print(a);
this.print("\n");
},
flush: function () {
}
}
);
Kotlin.NodeJsOutput = Kotlin.createClassNow(BaseOutput,
function(outputStream) {
this.outputStream = outputStream;
}, {
print: function (a) {
this.outputStream.write(a);
}
}
);
Kotlin.OutputToConsoleLog = Kotlin.createClassNow(BaseOutput, null, {
print: function (a) {
console.log(a);
},
println: function (a) {
this.print(typeof a !== "undefined" ? a : "");
}
}
);
Kotlin.BufferedOutput = Kotlin.createClassNow(BaseOutput,
function() {
this.buffer = ""
}, {
print: function (a) {
this.buffer += String(a);
},
flush: function () {
this.buffer = "";
}
}
);
Kotlin.BufferedOutputToConsoleLog = Kotlin.createClassNow(Kotlin.BufferedOutput,
function() {
Kotlin.BufferedOutput.call(this);
}, {
print: function (a) {
var s = String(a);
var i = s.lastIndexOf("\n");
if (i != -1) {
this.buffer += s.substr(0, i);
this.flush();
s = s.substr(i + 1);
} }
this.buffer += s;
},
flush: function () {
console.log(this.buffer);
this.buffer = "";
} }
return true; }
}; );
Kotlin.out = function() {
var isNode = typeof process !== 'undefined' && process.versions && !!process.versions.node;
Kotlin.arrayHashCode = function (arr) { if (isNode) return new Kotlin.NodeJsOutput(process.stdout);
var result = 1;
for (var i = 0, n = arr.length; i < n; i++) { return new Kotlin.BufferedOutputToConsoleLog();
result = ((31 * result | 0) + Kotlin.hashCode(arr[i])) | 0; }();
Kotlin.println = function (s) {
Kotlin.out.println(s);
};
Kotlin.print = function (s) {
Kotlin.out.print(s);
};
Kotlin.collectionsMax = function (c, comp) {
if (c.isEmpty()) {
//TODO: which exception?
throw new Error();
}
var it = c.iterator();
var max = it.next();
while (it.hasNext()) {
var el = it.next();
if (comp.compare(max, el) < 0) {
max = el;
} }
return result; }
}; return max;
};
Kotlin.arrayDeepHashCode = function (arr) { Kotlin.collectionsSort = function (mutableList, comparator) {
var result = 1; var boundComparator = void 0;
for (var i = 0, n = arr.length; i < n; i++) { if (comparator !== void 0) {
var e = arr[i]; boundComparator = comparator.compare.bind(comparator);
result = ((31 * result | 0) + (Array.isArray(e) ? Kotlin.arrayDeepHashCode(e) : Kotlin.hashCode(e))) | 0; }
if (mutableList.size > 1) {
var array = Kotlin.copyToArray(mutableList);
array.sort(boundComparator);
for (var i = 0, n = array.length; i < n; i++) {
mutableList.set_vux3hl$(i, array[i]);
} }
return result; }
}; };
Kotlin.primitiveArraySort = function(array) {
array.sort(Kotlin.primitiveCompareTo)
};
var BaseOutput = Kotlin.createClassNow(null, null, { Kotlin.copyToArray = function (collection) {
println: function (a) { if (typeof collection.toArray !== "undefined") return collection.toArray();
if (typeof a !== "undefined") this.print(a); return Kotlin.copyToArrayImpl(collection);
this.print("\n"); };
},
flush: function () { Kotlin.copyToArrayImpl = function (collection) {
} var array = [];
var it = collection.iterator();
while (it.hasNext()) {
array.push(it.next());
}
return array;
};
Kotlin.splitString = function (str, regex, limit) {
return str.split(new RegExp(regex), limit);
};
Kotlin.nullArray = function (size) {
var res = [];
var i = size;
while (i > 0) {
res[--i] = null;
}
return res;
};
Kotlin.numberArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return 0;
});
};
Kotlin.charArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return '\0';
});
};
Kotlin.booleanArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return false;
});
};
Kotlin.longArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return Kotlin.Long.ZERO;
});
};
Kotlin.arrayFromFun = function (size, initFun) {
var result = new Array(size);
for (var i = 0; i < size; i++) {
result[i] = initFun(i);
}
return result;
};
Kotlin.deleteProperty = function (object, property) {
delete object[property];
};
Kotlin.jsonAddProperties = function (obj1, obj2) {
for (var p in obj2) {
if (obj2.hasOwnProperty(p)) {
obj1[p] = obj2[p];
} }
); }
return obj1;
};
Kotlin.NodeJsOutput = Kotlin.createClassNow(BaseOutput, Kotlin.identityHashCode = getObjectHashCode;
function(outputStream) {
this.outputStream = outputStream;
}, {
print: function (a) {
this.outputStream.write(a);
}
}
);
Kotlin.OutputToConsoleLog = Kotlin.createClassNow(BaseOutput, null, {
print: function (a) {
console.log(a);
},
println: function (a) {
this.print(typeof a !== "undefined" ? a : "");
}
}
);
Kotlin.BufferedOutput = Kotlin.createClassNow(BaseOutput,
function() {
this.buffer = ""
}, {
print: function (a) {
this.buffer += String(a);
},
flush: function () {
this.buffer = "";
}
}
);
Kotlin.BufferedOutputToConsoleLog = Kotlin.createClassNow(Kotlin.BufferedOutput,
function() {
Kotlin.BufferedOutput.call(this);
}, {
print: function (a) {
var s = String(a);
var i = s.lastIndexOf("\n");
if (i != -1) {
this.buffer += s.substr(0, i);
this.flush();
s = s.substr(i + 1);
}
this.buffer += s;
},
flush: function () {
console.log(this.buffer);
this.buffer = "";
}
}
);
Kotlin.out = function() {
var isNode = typeof process !== 'undefined' && process.versions && !!process.versions.node;
if (isNode) return new Kotlin.NodeJsOutput(process.stdout);
return new Kotlin.BufferedOutputToConsoleLog();
}();
Kotlin.println = function (s) {
Kotlin.out.println(s);
};
Kotlin.print = function (s) {
Kotlin.out.print(s);
};
Kotlin.collectionsMax = function (c, comp) {
if (c.isEmpty()) {
//TODO: which exception?
throw new Error();
}
var it = c.iterator();
var max = it.next();
while (it.hasNext()) {
var el = it.next();
if (comp.compare(max, el) < 0) {
max = el;
}
}
return max;
};
Kotlin.collectionsSort = function (mutableList, comparator) {
var boundComparator = void 0;
if (comparator !== void 0) {
boundComparator = comparator.compare.bind(comparator);
}
if (mutableList.size > 1) {
var array = Kotlin.copyToArray(mutableList);
array.sort(boundComparator);
for (var i = 0, n = array.length; i < n; i++) {
mutableList.set_vux3hl$(i, array[i]);
}
}
};
Kotlin.primitiveArraySort = function(array) {
array.sort(Kotlin.primitiveCompareTo)
};
Kotlin.copyToArray = function (collection) {
if (typeof collection.toArray !== "undefined") return collection.toArray();
return Kotlin.copyToArrayImpl(collection);
};
Kotlin.copyToArrayImpl = function (collection) {
var array = [];
var it = collection.iterator();
while (it.hasNext()) {
array.push(it.next());
}
return array;
};
Kotlin.splitString = function (str, regex, limit) {
return str.split(new RegExp(regex), limit);
};
Kotlin.nullArray = function (size) {
var res = [];
var i = size;
while (i > 0) {
res[--i] = null;
}
return res;
};
Kotlin.numberArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return 0;
});
};
Kotlin.charArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return '\0';
});
};
Kotlin.booleanArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return false;
});
};
Kotlin.longArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return Kotlin.Long.ZERO;
});
};
Kotlin.arrayFromFun = function (size, initFun) {
var result = new Array(size);
for (var i = 0; i < size; i++) {
result[i] = initFun(i);
}
return result;
};
Kotlin.deleteProperty = function (object, property) {
delete object[property];
};
Kotlin.jsonAddProperties = function (obj1, obj2) {
for (var p in obj2) {
if (obj2.hasOwnProperty(p)) {
obj1[p] = obj2[p];
}
}
return obj1;
};
Kotlin.identityHashCode = getObjectHashCode;
})(Kotlin);
+798 -802
View File
File diff suppressed because it is too large Load Diff
-12
View File
@@ -1,12 +0,0 @@
var require = (function () {
var builtins = module.exports.kotlin;
var propertyNames = Object.getOwnPropertyNames(builtins);
Kotlin.kotlin = Kotlin.kotlin || {};
for (var i = 0; i < propertyNames.length; ++i) {
var propertyName = propertyNames[i];
Kotlin.kotlin[propertyName] = builtins[propertyName];
}
return function() {
return Kotlin;
}
})();
-16
View File
@@ -1,16 +0,0 @@
(function () {
var stdlib = module.exports;
function copyProperties(from, to) {
var propertyNames = Object.getOwnPropertyNames(from);
for (var i = 0; i < propertyNames.length; ++i) {
var propertyName = propertyNames[i];
if (propertyName in to) {
copyProperties(from[propertyName], to[propertyName]);
}
else {
to[propertyName] = from[propertyName];
}
}
}
copyProperties(stdlib, Kotlin);
})();
+25 -20
View File
@@ -1,24 +1,29 @@
var emulatedModules = { kotlin: kotlin }; var emulatedModules = { kotlin: kotlin };
var module = { exports: {} }; var module = { exports: {} };
function require(moduleId) { function require(moduleId) {
return emulatedModules[moduleId]; return emulatedModules[moduleId];
}
function __beginModule__() {
module.exports = {};
}
function __endModule__(moduleId) {
emulatedModules[moduleId] = module.exports;
}
function define(moduleId, dependencies, body) {
var resolvedDependencies = [];
emulatedModules[moduleId] = {};
for (var i = 0; i < dependencies.length; ++i) {
var dependencyName = dependencies[i];
resolvedDependencies.push(emulatedModules[dependencyName === 'exports' ? moduleId : dependencyName]);
} }
var result = body.apply(null, resolvedDependencies);
function __beginModule__() { if (result != null) {
module.exports = {}; emulatedModules[moduleId] = result;
} }
}
function __endModule__(moduleId) { define.amd = {};
emulatedModules[moduleId] = module.exports;
}
function define(moduleId, dependencies, body) {
var resolvedDependencies = [];
for (var i = 0; i < dependencies.length; ++i) {
resolvedDependencies.push(emulatedModules[dependencies[i]]);
}
emulatedModules[moduleId] = body.apply(null, resolvedDependencies);
}
define.amd = {};