Add maven archetype. Add XMLHttpRequest binding. Minor bugfixes

This commit is contained in:
Alexey Andreev 2014-09-25 23:54:13 +04:00
parent 8696bff1cd
commit 15299213d2
31 changed files with 511 additions and 17 deletions

View File

@ -74,7 +74,7 @@
<modules> <modules>
<module>teavm-core</module> <module>teavm-core</module>
<module>teavm-classlib</module> <module>teavm-classlib</module>
<module>teavm-maven-plugin</module> <module>teavm-maven</module>
<module>teavm-dom</module> <module>teavm-dom</module>
<module>teavm-jso</module> <module>teavm-jso</module>
<module>teavm-html4j</module> <module>teavm-html4j</module>

View File

@ -0,0 +1,29 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.teavm.dom.ajax;
import org.teavm.jso.JSFunctor;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
*/
@JSFunctor
public interface ReadyStateChangeHandler extends JSObject {
void stateChanged();
}

View File

@ -0,0 +1,70 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.teavm.dom.ajax;
import org.teavm.dom.core.Document;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface XMLHttpRequest extends JSObject {
int UNSET = 0;
int OPENED = 1;
int HEADERS_RECEIVED = 2;
int LOADING = 3;
int DONE = 4;
void open(String method, String url);
void open(String method, String url, boolean async);
void open(String method, String url, boolean async, String user);
void open(String method, String url, boolean async, String user, String password);
void send();
void send(String data);
void setRequestHeader(String name, String value);
String getAllResponseHeaders();
@JSProperty("onreadystatechange")
void setOnReadyStateChange(ReadyStateChangeHandler handler);
@JSProperty
int getReadyState();
@JSProperty
String getResponseText();
@JSProperty
Document getResponseXML();
@JSProperty
Integer getStatus();
@JSProperty
String getStatusText();
}

View File

@ -15,7 +15,9 @@
*/ */
package org.teavm.dom.browser; package org.teavm.dom.browser;
import org.teavm.dom.ajax.XMLHttpRequest;
import org.teavm.dom.html.HTMLDocument; import org.teavm.dom.html.HTMLDocument;
import org.teavm.jso.JSConstructor;
import org.teavm.jso.JSGlobal; import org.teavm.jso.JSGlobal;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty; import org.teavm.jso.JSProperty;
@ -39,4 +41,7 @@ public interface Window extends JSGlobal {
int setInterval(TimerHandler handler, int delay); int setInterval(TimerHandler handler, int delay);
void clearInterval(int timeoutId); void clearInterval(int timeoutId);
@JSConstructor("XMLHttpRequest")
XMLHttpRequest createXMLHttpRequest();
} }

View File

@ -0,0 +1,47 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.teavm.dom.html;
import org.teavm.dom.events.EventListener;
import org.teavm.jso.JSProperty;
/**
*
* @author Alexey Andreev
*/
public interface HTMLBodyElement extends HTMLElement {
@JSProperty("onbeforeunload")
void setOnBeforeUnload(EventListener listener);
@JSProperty("onerror")
void setOnError(EventListener listener);
@JSProperty("onload")
void setOnLoad(EventListener listener);
@JSProperty("onmessage")
void setOnMessage(EventListener listener);
@JSProperty("onoffline")
void setOnOffline(EventListener listener);
@JSProperty("ononline")
void setOnOnline(EventListener listener);
@JSProperty("ononunload")
void setOnUnload(EventListener listener);
}

View File

@ -0,0 +1,64 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.teavm.dom.html;
import org.teavm.jso.JSProperty;
import org.w3c.dom.html.HTMLFormElement;
/**
*
* @author Alexey Andreev
*/
public interface HTMLButtonElement extends HTMLElement {
String TYPE_BUTTON = "button";
String TYPE_RESET = "reset";
String TYPE_SUBMIT = "submit";
@JSProperty
boolean isAutofocus();
@JSProperty
void setAutofocus(boolean autofocus);
@JSProperty
boolean isDisabled();
@JSProperty
void setDisabled(boolean disabled);
@JSProperty
HTMLFormElement getForm();
@JSProperty
String getName();
@JSProperty
void setName(String name);
@JSProperty
String getValue();
@JSProperty
void setValue(String value);
@JSProperty
String getType();
@JSProperty
void setType(String type);
}

View File

@ -32,4 +32,10 @@ public interface HTMLDocument extends Document {
@Override @Override
HTMLElement getElementById(String elementId); HTMLElement getElementById(String elementId);
@JSProperty
HTMLBodyElement getBody();
@JSProperty
HTMLElement getHead();
} }

View File

@ -25,8 +25,8 @@
<artifactId>teavm-eclipse</artifactId> <artifactId>teavm-eclipse</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>TeaVM Eclipse plugins</name> <name>TeaVM Eclipse integration</name>
<description>TeaVM plugins for Eclipse</description> <description>Aggregate project containing all plugins for integration TeaVM with Eclipse</description>
<properties> <properties>
<p2-repo.url>http://download.eclipse.org/releases/juno</p2-repo.url> <p2-repo.url>http://download.eclipse.org/releases/juno</p2-repo.url>

View File

@ -12,14 +12,14 @@
<copyright url="http://www.example.com/copyright"> <copyright url="http://www.example.com/copyright">
Copyright 2014 Alexey Andreev. Copyright 2014 Alexey Andreev.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
@ -229,6 +229,10 @@
limitations under the License. limitations under the License.
</license> </license>
<requires>
<import feature="teavm-eclipse-feature" version="0.2.0.qualifier"/>
</requires>
<plugin <plugin
id="teavm-eclipse-m2e-plugin" id="teavm-eclipse-m2e-plugin"
download-size="0" download-size="0"

View File

@ -24,9 +24,13 @@
<version>0.2-SNAPSHOT</version> <version>0.2-SNAPSHOT</version>
</parent> </parent>
<artifactId>teavm-eclipse-m2e-plugin</artifactId> <artifactId>teavm-eclipse-m2e-plugin</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.2.0-SNAPSHOT</version> <version>0.2.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>TeaVM m2e plugin</name>
<description>Contains plugin that automatically configures TeaVM builder from pom.xml</description>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.teavm</groupId> <groupId>org.teavm</groupId>

View File

@ -25,8 +25,12 @@
</parent> </parent>
<artifactId>teavm-eclipse-plugin</artifactId> <artifactId>teavm-eclipse-plugin</artifactId>
<version>0.2.0-SNAPSHOT</version> <version>0.2.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging> <packaging>eclipse-plugin</packaging>
<name>TeaVM Eclipse plugin</name>
<description>Contains TeaVM builder and TeaVM debugger for Eclipse</description>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.teavm</groupId> <groupId>org.teavm</groupId>

View File

@ -79,8 +79,14 @@ class JavascriptNativeProcessor {
MethodReader method = getMethod(invoke.getMethod()); MethodReader method = getMethod(invoke.getMethod());
if (method.getAnnotations().get(JSProperty.class.getName()) != null) { if (method.getAnnotations().get(JSProperty.class.getName()) != null) {
if (isProperGetter(method.getDescriptor())) { if (isProperGetter(method.getDescriptor())) {
String propertyName = method.getName().charAt(0) == 'i' ? cutPrefix(method.getName(), 2) : String propertyName;
AnnotationReader annot = method.getAnnotations().get(JSProperty.class.getName());
if (annot.getValue("value") != null) {
propertyName = annot.getValue("value").getString();
} else {
propertyName = method.getName().charAt(0) == 'i' ? cutPrefix(method.getName(), 2) :
cutPrefix(method.getName(), 3); cutPrefix(method.getName(), 3);
}
Variable result = invoke.getReceiver() != null ? program.createVariable() : null; Variable result = invoke.getReceiver() != null ? program.createVariable() : null;
addPropertyGet(propertyName, invoke.getInstance(), result); addPropertyGet(propertyName, invoke.getInstance(), result);
if (result != null) { if (result != null) {
@ -88,8 +94,15 @@ class JavascriptNativeProcessor {
copyVar(result, invoke.getReceiver()); copyVar(result, invoke.getReceiver());
} }
} else if (isProperSetter(method.getDescriptor())) { } else if (isProperSetter(method.getDescriptor())) {
Variable wrapped = wrap(invoke.getArguments().get(0), method.parameterType(0)); String propertyName;
addPropertySet(cutPrefix(method.getName(), 3), invoke.getInstance(), wrapped); AnnotationReader annot = method.getAnnotations().get(JSProperty.class.getName());
if (annot.getValue("value") != null) {
propertyName = annot.getValue("value").getString();
} else {
propertyName = cutPrefix(method.getName(), 3);
}
Variable wrapped = wrapArgument(invoke.getArguments().get(0), method.parameterType(0));
addPropertySet(propertyName, invoke.getInstance(), wrapped);
} else { } else {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + throw new RuntimeException("Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript property declaration"); "a proper native JavaScript property declaration");

2
teavm-maven/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.settings
/.project

37
teavm-maven/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<!--
Copyright 2014 Alexey Andreev.
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>teavm-maven</artifactId>
<packaging>pom</packaging>
<name>TeaVM maven</name>
<description>TeaVM maven integration aggregate project</description>
<url>http://teavm.org</url>
<modules>
<module>teavm-maven-plugin</module>
<module>teavm-maven-webapp</module>
</modules>
</project>

View File

@ -19,7 +19,7 @@
<parent> <parent>
<groupId>org.teavm</groupId> <groupId>org.teavm</groupId>
<artifactId>teavm</artifactId> <artifactId>teavm-maven</artifactId>
<version>0.2-SNAPSHOT</version> <version>0.2-SNAPSHOT</version>
</parent> </parent>
<artifactId>teavm-maven-plugin</artifactId> <artifactId>teavm-maven-plugin</artifactId>

View File

@ -0,0 +1,4 @@
/target
/.settings
/.classpath
/.project

View File

@ -0,0 +1,36 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven</artifactId>
<version>0.2-SNAPSHOT</version>
</parent>
<artifactId>teavm-maven-webapp</artifactId>
<name>TeaVM maven web application archetype</name>
<packaging>maven-archetype</packaging>
<description>An archetype that creates a simple web application with enabled TeaVM</description>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>2.2</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,25 @@
<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0
http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
name="TeaVM web application" partial="false">
<requiredProperties>
<requiredProperty key="package">
<defaultValue></defaultValue>
</requiredProperty>
</requiredProperties>
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
</fileSet>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory>.</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory>src/main/webapp</directory>
</fileSet>
</fileSets>
</archetype-descriptor>

View File

@ -0,0 +1,109 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>war</packaging>
<properties>
<java.version>1.7</java.version>
<teavm.version>0.2-SNAPSHOT</teavm.version>
</properties>
<dependencies>
<!-- Emulator of Java class library for TeaVM -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${teavm.version}</version>
</dependency>
<!-- JavaScriptObjects (JSO) - a JavaScript binding for TeaVM -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>${teavm.version}</version>
</dependency>
<!-- Different browser APIs for TeaVM in terms of JSO -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-dom</artifactId>
<version>${teavm.version}</version>
</dependency>
<!-- Servlet 3.1 specification -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Configure Java compiler to use Java 7 syntax -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Configure WAR plugin to include JavaScript files generated by TeaVM -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/generated/js</directory>
</resource>
</webResources>
</configuration>
</plugin>
<!-- Configure TeaVM -->
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${teavm.version}</version>
<executions>
<execution>
<id>web-client</id>
<phase>prepare-package</phase>
<goals>
<goal>build-javascript</goal>
</goals>
<configuration>
<!-- Directory where TeaVM should put generated files. This configuration conforms to the settings
of the WAR plugin -->
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
<!-- Main class, containing static void main(String[]) -->
<mainClass>${package}.Client</mainClass>
<!-- How to attach runtime.js. Possible values are: SEPARATE, MERGED and NONE -->
<runtime>SEPARATE</runtime>
<!-- Whether TeaVM should produce minified JavaScript. Can reduce JavaScript file size more than
two times -->
<minifying>true</minifying>
<!-- Whether TeaVM should produce debug information for its built-in debugger -->
<debugInformationGenerated>true</debugInformationGenerated>
<!-- Whether TeaVM should produce source maps file -->
<sourceMapsGenerated>true</sourceMapsGenerated>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package ${package};
import org.teavm.dom.browser.Window;
import org.teavm.dom.html.HTMLDocument;
import org.teavm.dom.html.HTMLElement;
import org.teavm.jso.JS;
public class Client {
private static Window window = (Window)JS.getGlobal();
private static HTMLDocument document = window.getDocument();
public static void main(String[] args) {
HTMLElement div = document.createElement("div");
div.appendChild(document.createTextNode("TeaVM generated element"));
document.getBody().appendChild(div);
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- TODO: configure web application -->
</web-app>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Main page</title>
<script type="text/javascript" src="teavm/runtime.js"></script>
<script type="text/javascript" src="teavm/classes.js"></script>
</head>
<body onload="main()">
<!-- TODO: add HTML content -->
</body>
</html>