Add "use strict" to generated test files and to kotlin_lib.js

This commit is contained in:
pTalanov
2012-03-06 19:52:14 +04:00
parent c794da3c55
commit e890cc7750
4 changed files with 120 additions and 107 deletions
@@ -22,8 +22,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.facade.K2JSTranslator; import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.test.config.TestConfig; import org.jetbrains.k2js.test.config.TestConfig;
import java.io.File;
import java.io.FileWriter;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -49,7 +52,13 @@ public final class TranslationUtils {
@NotNull String outputFile) throws Exception { @NotNull String outputFile) throws Exception {
List<JetFile> psiFiles = createPsiFileList(inputFiles, project); List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
JsProgram program = getTranslator(project).generateProgram(psiFiles); JsProgram program = getTranslator(project).generateProgram(psiFiles);
K2JSTranslator.saveProgramToFile(outputFile, program); FileWriter writer = new FileWriter(new File(outputFile));
try {
writer.write("\"use strict\"\n;");
writer.write(CodeGenerator.toString(program));
} finally {
writer.close();
}
} }
@NotNull @NotNull
@@ -33,7 +33,6 @@ import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -59,7 +58,7 @@ public final class K2JSTranslator {
if (fileWithMain == null) { if (fileWithMain == null) {
throw new RuntimeException("No file with main detected."); throw new RuntimeException("No file with main detected.");
} }
String callToMain = translator.generateCallToMain(fileWithMain, ""); String callToMain = generateCallToMain(fileWithMain, "");
FileWriter writer = new FileWriter(new File(outputPath)); FileWriter writer = new FileWriter(new File(outputPath));
try { try {
writer.write(programCode + callToMain); writer.write(programCode + callToMain);
@@ -68,11 +67,6 @@ public final class K2JSTranslator {
} }
} }
public static void saveProgramToFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
@NotNull @NotNull
private final Config config; private final Config config;
@@ -37,6 +37,16 @@ public final class CodeGenerator {
public CodeGenerator() { public CodeGenerator() {
} }
public static void toFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
@NotNull
public static String toString(@NotNull JsProgram program) {
return (new CodeGenerator()).generateToString(program);
}
public void generateToFile(@NotNull JsProgram program, @NotNull File file) throws IOException { public void generateToFile(@NotNull JsProgram program, @NotNull File file) throws IOException {
generateCode(program); generateCode(program);
FileWriter writer = new FileWriter(file); FileWriter writer = new FileWriter(file);
+99 -99
View File
@@ -21,8 +21,9 @@
* For details, see the Prototype web site: http://www.prototypejs.org/ * For details, see the Prototype web site: http://www.prototypejs.org/
* *
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
var Kotlin;
(function () { (function () {
"use strict";
function $A(iterable) { function $A(iterable) {
if (!iterable) return []; if (!iterable) return [];
if ('toArray' in Object(iterable)) return iterable.toArray(); if ('toArray' in Object(iterable)) return iterable.toArray();
@@ -82,8 +83,8 @@
function argumentNames() { function argumentNames() {
var names = this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1] var names = this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
.replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '') .replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
.replace(/\s+/g, '').split(','); .replace(/\s+/g, '').split(',');
return names.length == 1 && !names[0] ? [] : names; return names.length == 1 && !names[0] ? [] : names;
} }
@@ -93,7 +94,7 @@
return function () { return function () {
var a = merge(args, arguments); var a = merge(args, arguments);
return __method.apply(context, a); return __method.apply(context, a);
} };
} }
function bindAsEventListener(context) { function bindAsEventListener(context) {
@@ -101,7 +102,7 @@
return function (event) { return function (event) {
var a = update([event || window.event], args); var a = update([event || window.event], args);
return __method.apply(context, a); return __method.apply(context, a);
} };
} }
function wrap(wrapper) { function wrap(wrapper) {
@@ -109,7 +110,7 @@
return function () { return function () {
var a = update([__method.bind(this)], arguments); var a = update([__method.bind(this)], arguments);
return wrapper.apply(this, a); return wrapper.apply(this, a);
} };
} }
return { return {
@@ -117,11 +118,11 @@
bind:bind, bind:bind,
bindAsEventListener:bindAsEventListener, bindAsEventListener:bindAsEventListener,
wrap:wrap wrap:wrap
} };
})()); })());
var isType = function (object, klass) { var isType = function (object, klass) {
current = object.get_class(); var current = object.get_class();
while (current !== klass) { while (current !== klass) {
if (current === null) { if (current === null) {
return false; return false;
@@ -138,7 +139,7 @@
function subclass() { function subclass() {
} }
;
function create() { function create() {
var parent = null, properties = $A(arguments); var parent = null, properties = $A(arguments);
if (typeof (properties[0]) == "function") { if (typeof (properties[0]) == "function") {
@@ -156,25 +157,25 @@
if (parent) { if (parent) {
subclass.prototype = parent.prototype; subclass.prototype = parent.prototype;
klass.prototype = new subclass; klass.prototype = new subclass();
parent.subclasses.push(klass); parent.subclasses.push(klass);
} }
klass.addMethods( klass.addMethods(
{
get_class:function () {
return klass;
}
});
if (parent != null) {
klass.addMethods(
{ {
super_init:function () { get_class:function () {
this.initializing = this.initializing.superclass; return klass;
this.initializing.prototype.initialize.apply(this, arguments)
} }
}); });
if (parent !== null) {
klass.addMethods(
{
super_init:function () {
this.initializing = this.initializing.superclass;
this.initializing.prototype.initialize.apply(this, arguments);
}
});
} }
for (var i = 0, length = properties.length; i < length; i++) for (var i = 0, length = properties.length; i < length; i++)
@@ -190,7 +191,7 @@
function addMethods(source) { function addMethods(source) {
var ancestor = this.superclass && this.superclass.prototype, var ancestor = this.superclass && this.superclass.prototype,
properties = Object.keys(source); properties = Object.keys(source);
for (var i = 0, length = properties.length; i < length; i++) { for (var i = 0, length = properties.length; i < length; i++) {
@@ -233,7 +234,7 @@
function create() { function create() {
result = {} var result = {};
for (var i = 0, length = arguments.length; i < length; i++) { for (var i = 0, length = arguments.length; i < length; i++) {
add(result, arguments[i]); add(result, arguments[i]);
} }
@@ -260,8 +261,9 @@
var object = (function () { var object = (function () {
function create() { function create() {
var singletonClass = Class.create.apply(Class, arguments); var singletonClass = Class.create.apply(Class, arguments);
return new singletonClass; return new singletonClass();
} }
return { return {
create:create create:create
}; };
@@ -276,16 +278,16 @@
Kotlin.equals = function (obj1, obj2) { Kotlin.equals = function (obj1, obj2) {
if (typeof obj1 == "object") { if (typeof obj1 == "object") {
if (obj1.equals != undefined) { if (obj1.equals !== undefined) {
return obj1.equals(obj2); return obj1.equals(obj2);
} }
} }
return (obj1 === obj2); return (obj1 === obj2);
}; };
Kotlin.Exceptions = {} Kotlin.Exceptions = {};
Kotlin.Exception = Kotlin.Class.create(); Kotlin.Exception = Kotlin.Class.create();
Kotlin.Exceptions.IndexOutOfBounds = {} Kotlin.Exceptions.IndexOutOfBounds = {};
Kotlin.ArrayList = Class.create({ Kotlin.ArrayList = Class.create({
@@ -312,7 +314,7 @@
return new Kotlin.ArrayIterator(this); return new Kotlin.ArrayIterator(this);
}, },
isEmpty:function () { isEmpty:function () {
return (this.$size == 0); return (this.$size === 0);
}, },
add:function (element) { add:function (element) {
this.array[this.$size++] = element; this.array[this.$size++] = element;
@@ -345,9 +347,9 @@
Kotlin.parseInt = Kotlin.parseInt =
function (str) { function (str) {
return parseInt(str); return parseInt(str, 10);
} }
; ;
Kotlin.System = function () { Kotlin.System = function () {
@@ -355,7 +357,7 @@
var print = function (obj) { var print = function (obj) {
if (obj !== undefined) { if (obj !== undefined) {
if (obj == null || typeof obj != "object") { if (obj === null || typeof obj !== "object") {
output += obj; output += obj;
} }
else { else {
@@ -386,11 +388,11 @@
Kotlin.println = function (s) { Kotlin.println = function (s) {
Kotlin.System.out().println(s); Kotlin.System.out().println(s);
} };
Kotlin.print = function (s) { Kotlin.print = function (s) {
Kotlin.System.out().print(s); Kotlin.System.out().print(s);
} };
Kotlin.AbstractFunctionInvokationError = Class.create(); Kotlin.AbstractFunctionInvokationError = Class.create();
@@ -481,18 +483,18 @@
}); });
Kotlin.Comparator = Kotlin.Class.create( Kotlin.Comparator = Kotlin.Class.create(
{ {
initialize:function () { initialize:function () {
}, },
compare:function (el1, el2) { compare:function (el1, el2) {
throw new Kotlin.AbstractFunctionInvokationError(); throw new Kotlin.AbstractFunctionInvokationError();
}
} }
}
); );
Kotlin.comparator = function(f) { Kotlin.comparator = function (f) {
var result = new Kotlin.Comparator; var result = new Kotlin.Comparator();
result.compare = function(el1, el2) { result.compare = function (el1, el2) {
return f(el1, el2); return f(el1, el2);
}; };
return result; return result;
@@ -515,20 +517,20 @@
}; };
Kotlin.StringBuilder = Kotlin.Class.create( Kotlin.StringBuilder = Kotlin.Class.create(
{ {
initialize:function () { initialize:function () {
this.string = ""; this.string = "";
}, },
append:function (obj) { append:function (obj) {
this.string = this.string + obj.toString(); this.string = this.string + obj.toString();
}, },
toString:function () { toString:function () {
return this.string; return this.string;
}
} }
}
); );
Kotlin.splitString = function(str, regex) { Kotlin.splitString = function (str, regex) {
return str.split(regex); return str.split(regex);
}; };
@@ -555,23 +557,23 @@
}; };
var intrinsicArrayIterator = Kotlin.Class.create( var intrinsicArrayIterator = Kotlin.Class.create(
Kotlin.Iterator, Kotlin.Iterator,
{ {
initialize:function (arr) { initialize:function (arr) {
this.arr = arr; this.arr = arr;
this.len = arr.length; this.len = arr.length;
this.i = 0; this.i = 0;
}, },
hasNext:function () { hasNext:function () {
return (this.i < this.len); return (this.i < this.len);
}, },
next:function () { next:function () {
return this.arr[this.i++]; return this.arr[this.i++];
}, },
get_hasNext:function () { get_hasNext:function () {
return this.hasNext() return this.hasNext();
}
} }
}
); );
Kotlin.arrayIterator = function (arr) { Kotlin.arrayIterator = function (arr) {
@@ -601,7 +603,7 @@
}; };
Kotlin.sure = function(obj) { Kotlin.sure = function (obj) {
return obj; return obj;
}; };
@@ -609,23 +611,23 @@
var FUNCTION = "function"; var FUNCTION = "function";
var arrayRemoveAt = (typeof Array.prototype.splice == FUNCTION) ? var arrayRemoveAt = (typeof Array.prototype.splice == FUNCTION) ?
function (arr, idx) { function (arr, idx) {
arr.splice(idx, 1); arr.splice(idx, 1);
} : } :
function (arr, idx) { function (arr, idx) {
var itemsAfterDeleted, i, len; var itemsAfterDeleted, i, len;
if (idx === arr.length - 1) { if (idx === arr.length - 1) {
arr.length = idx; arr.length = idx;
}
else {
itemsAfterDeleted = arr.slice(idx + 1);
arr.length = idx;
for (i = 0, len = itemsAfterDeleted.length; i < len; ++i) {
arr[idx + i] = itemsAfterDeleted[i];
} }
} else {
}; itemsAfterDeleted = arr.slice(idx + 1);
arr.length = idx;
for (i = 0, len = itemsAfterDeleted.length; i < len; ++i) {
arr[idx + i] = itemsAfterDeleted[i];
}
}
};
function hashObject(obj) { function hashObject(obj) {
var hashCode; var hashCode;
@@ -655,7 +657,7 @@
function equals_fixedValueNoEquals(fixedValue, variableValue) { function equals_fixedValueNoEquals(fixedValue, variableValue) {
return (typeof variableValue.equals == FUNCTION) ? return (typeof variableValue.equals == FUNCTION) ?
variableValue.equals(fixedValue) : (fixedValue === variableValue); variableValue.equals(fixedValue) : (fixedValue === variableValue);
} }
function createKeyValCheck(kvStr) { function createKeyValCheck(kvStr) {
@@ -957,23 +959,21 @@
res.add(keys[i]); res.add(keys[i]);
} }
return res; return res;
} };
}; };
Kotlin.HashTable = Hashtable; Kotlin.HashTable = Hashtable;
})(); })();
Kotlin.HashMap = Kotlin.Class.create( Kotlin.HashMap = Kotlin.Class.create(
{ {
initialize:function () { initialize:function () {
Kotlin.HashTable.call(this); Kotlin.HashTable.call(this);
}
} }
}
); );
(function () { (function () {
function HashSet(hashingFunction, equalityFunction) { function HashSet(hashingFunction, equalityFunction) {
var hashTable = new Kotlin.HashTable(hashingFunction, equalityFunction); var hashTable = new Kotlin.HashTable(hashingFunction, equalityFunction);
@@ -1065,12 +1065,12 @@
} }
Kotlin.HashSet = Kotlin.Class.create( Kotlin.HashSet = Kotlin.Class.create(
{ {
initialize:function () { initialize:function () {
HashSet.call(this); HashSet.call(this);
}
} }
} );
)
}()); }());
})(); })();