updated canvas helper
and migrated to new code
This commit is contained in:
@@ -27,5 +27,9 @@ native
|
|||||||
fun setInterval(callback : ()-> Unit) {}
|
fun setInterval(callback : ()-> Unit) {}
|
||||||
|
|
||||||
|
|
||||||
library("collectionsMax")
|
native
|
||||||
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
|
open class DomElement() {
|
||||||
|
val offsetLeft = 0.0;
|
||||||
|
val offsetTop = 0.0;
|
||||||
|
val offsetParent : DomElement? = DomElement();
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,10 @@ package java.util
|
|||||||
|
|
||||||
import js.annotations.*;
|
import js.annotations.*;
|
||||||
|
|
||||||
|
|
||||||
|
library("collectionsMax")
|
||||||
|
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
|
||||||
|
|
||||||
library
|
library
|
||||||
public trait Comparator<T> {
|
public trait Comparator<T> {
|
||||||
fun compare(obj1 : T, obj2 : T) : Int;
|
fun compare(obj1 : T, obj2 : T) : Int;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package html5
|
package html5
|
||||||
|
|
||||||
import js.annotations.native
|
import js.annotations.native
|
||||||
|
import js.DomElement
|
||||||
|
|
||||||
native
|
native
|
||||||
class Context() {
|
class Context() {
|
||||||
@@ -11,6 +12,7 @@ class Context() {
|
|||||||
fun rotate(angle : Double) {}
|
fun rotate(angle : Double) {}
|
||||||
fun translate(x : Double, y : Double) {}
|
fun translate(x : Double, y : Double) {}
|
||||||
|
|
||||||
|
fun clearRect(x : Double, y : Double, w : Double, h : Double) {}
|
||||||
fun fillRect(x : Double, y : Double, w : Double, h : Double) {}
|
fun fillRect(x : Double, y : Double, w : Double, h : Double) {}
|
||||||
fun strokeRect(x : Double, y : Double, w : Double, h : Double) {}
|
fun strokeRect(x : Double, y : Double, w : Double, h : Double) {}
|
||||||
|
|
||||||
@@ -46,6 +48,12 @@ class Context() {
|
|||||||
fun measureText(text : String) : TextMetrics = TextMetrics();
|
fun measureText(text : String) : TextMetrics = TextMetrics();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
native
|
||||||
|
class Canvas() : DomElement() {
|
||||||
|
val width = 0.0;
|
||||||
|
val height = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
native
|
native
|
||||||
class TextMetrics() {
|
class TextMetrics() {
|
||||||
val width : Int = 0
|
val width : Int = 0
|
||||||
@@ -54,9 +62,5 @@ class TextMetrics() {
|
|||||||
/*custom helpers*/
|
/*custom helpers*/
|
||||||
native
|
native
|
||||||
fun getContext() : Context = Context();
|
fun getContext() : Context = Context();
|
||||||
//native
|
|
||||||
//fun random() : Double = 0.0
|
|
||||||
native
|
native
|
||||||
fun getCanvasWidth() : Double = 0.0
|
fun getCanvas() : Canvas = Canvas();
|
||||||
native
|
|
||||||
fun getCanvasHeight() : Double = 0.0
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package jquery;
|
package jquery;
|
||||||
|
|
||||||
import js.annotations.*;
|
import js.annotations.*;
|
||||||
|
import js.DomElement
|
||||||
|
|
||||||
native
|
native
|
||||||
class JQuery() {
|
class JQuery() {
|
||||||
@@ -19,8 +20,16 @@ class JQuery() {
|
|||||||
fun removeClass(className : String) = this
|
fun removeClass(className : String) = this
|
||||||
fun height() = 0
|
fun height() = 0
|
||||||
fun width() = 0
|
fun width() = 0
|
||||||
|
|
||||||
fun click() = this;
|
fun click() = this;
|
||||||
fun click(handler : DomElement.()->Unit) = this;
|
|
||||||
|
fun mousedown(handler : DomElement.(MouseEvent)->Unit) = this;
|
||||||
|
fun mouseup(handler : DomElement.(MouseEvent)->Unit) = this;
|
||||||
|
fun mousemove(handler : DomElement.(MouseEvent)->Unit) = this;
|
||||||
|
|
||||||
|
fun dblclick(handler : DomElement.(MouseClickEvent)->Unit) = this;
|
||||||
|
fun click(handler : DomElement.(MouseClickEvent)->Unit) = this;
|
||||||
|
|
||||||
fun append(str : String) = this;
|
fun append(str : String) = this;
|
||||||
fun ready(handler : ()->Unit) = this;
|
fun ready(handler : ()->Unit) = this;
|
||||||
fun text(text : String) = this;
|
fun text(text : String) = this;
|
||||||
@@ -29,11 +38,20 @@ class JQuery() {
|
|||||||
fun hover(handlerIn : DomElement.() -> Unit, handlerOut : DomElement.() -> Unit) = this;
|
fun hover(handlerIn : DomElement.() -> Unit, handlerOut : DomElement.() -> Unit) = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
open class DomElement() {
|
native
|
||||||
|
open class MouseEvent() {
|
||||||
|
val pageX : Double = 0.0;
|
||||||
|
val pageY : Double = 0.0;
|
||||||
|
fun preventDefault() {}
|
||||||
|
fun isDefaultPrevented() : Boolean = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//val document = object : DomElement() {}
|
native
|
||||||
|
class MouseClickEvent() : MouseEvent() {
|
||||||
|
val which : Int = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
native("$")
|
native("$")
|
||||||
fun jq(selector : String) = JQuery();
|
fun jq(selector : String) = JQuery();
|
||||||
|
|||||||
@@ -19,10 +19,7 @@ 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.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.StringTokenizer;
|
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
|
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
|
||||||
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
|
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
|
||||||
@@ -47,36 +44,32 @@ public final class K2JSTranslator {
|
|||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testTranslateFile(@NotNull String inputFile, @NotNull String outputFile) throws Exception {
|
public static void testTranslateFile(@NotNull String inputFile,
|
||||||
|
@NotNull String outputFile) throws Exception {
|
||||||
|
testTranslateFiles(Collections.singletonList(inputFile), outputFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void testTranslateFiles(@NotNull List<String> inputFiles,
|
||||||
|
@NotNull String outputFile) throws Exception {
|
||||||
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
|
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
|
||||||
JetFile psiFile = JetFileUtils.loadPsiFile(inputFile, translator.getProject());
|
List<JetFile> psiFiles = createPsiFileList(inputFiles, translator.getProject());
|
||||||
translator.generateAndSaveProgram(psiFile, outputFile);
|
JsProgram program = translator.generateProgram(psiFiles);
|
||||||
|
saveProgramToFile(outputFile, program);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void saveProgramToFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
|
||||||
|
CodeGenerator generator = new CodeGenerator();
|
||||||
|
generator.generateToFile(program, new File(outputFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Config config;
|
private Config config;
|
||||||
|
|
||||||
|
|
||||||
public K2JSTranslator(@NotNull Config config) {
|
public K2JSTranslator(@NotNull Config config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void generateAndSaveProgram(@NotNull JetFile psiFile,
|
|
||||||
@NotNull String outputFile) throws IOException {
|
|
||||||
JsProgram program = generateProgram(Arrays.asList(psiFile));
|
|
||||||
CodeGenerator generator = new CodeGenerator();
|
|
||||||
generator.generateToFile(program, new File(outputFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void testTranslateFiles(@NotNull List<String> inputFiles, @NotNull String outputFile)
|
|
||||||
throws Exception {
|
|
||||||
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
|
|
||||||
List<JetFile> psiFiles = createPsiFileList(inputFiles, translator.getProject());
|
|
||||||
JsProgram program = translator.generateProgram(psiFiles);
|
|
||||||
CodeGenerator generator = new CodeGenerator();
|
|
||||||
generator.generateToFile(program, new File(outputFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) {
|
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) {
|
||||||
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
|
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public abstract class AccessTranslator extends AbstractTranslator {
|
|||||||
public static AccessTranslator getAccessTranslator(@NotNull JetExpression referenceExpression,
|
public static AccessTranslator getAccessTranslator(@NotNull JetExpression referenceExpression,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
assert ((referenceExpression instanceof JetReferenceExpression) ||
|
assert ((referenceExpression instanceof JetReferenceExpression) ||
|
||||||
(referenceExpression instanceof JetDotQualifiedExpression));
|
(referenceExpression instanceof JetQualifiedExpression));
|
||||||
if (PropertyAccessTranslator.canBePropertyAccess(referenceExpression, context)) {
|
if (PropertyAccessTranslator.canBePropertyAccess(referenceExpression, context)) {
|
||||||
if (referenceExpression instanceof JetQualifiedExpression) {
|
if (referenceExpression instanceof JetQualifiedExpression) {
|
||||||
return QualifiedExpressionTranslator.getAccessTranslator((JetQualifiedExpression) referenceExpression, context);
|
return QualifiedExpressionTranslator.getAccessTranslator((JetQualifiedExpression) referenceExpression, context);
|
||||||
|
|||||||
@@ -67,12 +67,6 @@ public final class OperatorOverloadingTest extends TranslationTest {
|
|||||||
testFooBoxIsTrue("plusAssignNoReassign.kt");
|
testFooBoxIsTrue("plusAssignNoReassign.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testPlusAssignReassign() throws Exception {
|
|
||||||
testFooBoxIsTrue("plusAssignReassign.kt");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void testNotOverload() throws Exception {
|
public void testNotOverload() throws Exception {
|
||||||
testFooBoxIsTrue("notOverload.kt");
|
testFooBoxIsTrue("notOverload.kt");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ public abstract class TranslationTest extends BaseTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void translateFile(String filename) throws Exception {
|
protected void translateFile(String filename) throws Exception {
|
||||||
K2JSTranslator.testTranslateFile(getInputFilePath(filename), getOutputFilePath(filename));
|
K2JSTranslator.testTranslateFile(getInputFilePath(filename),
|
||||||
|
getOutputFilePath(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void translateFilesInDir(String dirName) throws Exception {
|
protected void translateFilesInDir(String dirName) throws Exception {
|
||||||
@@ -92,7 +93,8 @@ public abstract class TranslationTest extends BaseTest {
|
|||||||
fullFilePaths.add(getInputFilePath(dirName) + "\\" + fileName);
|
fullFilePaths.add(getInputFilePath(dirName) + "\\" + fileName);
|
||||||
}
|
}
|
||||||
assert dir.isDirectory();
|
assert dir.isDirectory();
|
||||||
K2JSTranslator.testTranslateFiles(fullFilePaths, getOutputFilePath(dirName + ".kt"));
|
K2JSTranslator.testTranslateFiles(fullFilePaths,
|
||||||
|
getOutputFilePath(dirName + ".kt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<String> generateFilenameList(String inputFile) {
|
protected List<String> generateFilenameList(String inputFile) {
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
function getContext() {
|
function getContext() {
|
||||||
return document.getElementsByTagName('canvas')[0].getContext('2d');
|
return getCanvas().getContext('2d');
|
||||||
}
|
}
|
||||||
|
|
||||||
function random() {
|
function getCanvas() {
|
||||||
return Math.random();
|
return document.getElementsByTagName('canvas')[0];
|
||||||
}
|
|
||||||
|
|
||||||
function getCanvasWidth() {
|
|
||||||
return document.getElementsByTagName('canvas')[0].width;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCanvasHeight() {
|
|
||||||
return document.getElementsByTagName('canvas')[0].height;
|
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,7 @@ var a = MyInt()
|
|||||||
class MyInt() {
|
class MyInt() {
|
||||||
var b = 0
|
var b = 0
|
||||||
|
|
||||||
fun inc() {
|
fun inc() = b + 1
|
||||||
b = b + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package foo
|
|
||||||
|
|
||||||
class A(i : Int) {
|
|
||||||
|
|
||||||
val a = i
|
|
||||||
|
|
||||||
fun plusAssign(other : A) = A(this.a + other.a)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fun box() : Boolean {
|
|
||||||
var c = A(2)
|
|
||||||
val d = c
|
|
||||||
c += A(3)
|
|
||||||
return (c.a == 5) && (d.a == 2) && (d != c)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user