Skip to content

Commit

Permalink
jso: improve JS class import to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Feb 24, 2024
1 parent 393cd7f commit 69d1ff8
Show file tree
Hide file tree
Showing 61 changed files with 1,580 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void connect() throws IOException {
return;
}

xhr = XMLHttpRequest.create();
xhr = new XMLHttpRequest();
xhr.open(method, url.toString());
for (Map.Entry<String, List<String>> entry : getRequestProperties().entrySet()) {
for (String value : entry.getValue()) {
Expand Down Expand Up @@ -97,7 +97,7 @@ private void performRequest(AsyncCallback<Boolean> callback) {
responseCode = -1;
}

Int8Array array = Int8Array.create((ArrayBuffer) xhr.getResponse());
var array = new Int8Array((ArrayBuffer) xhr.getResponse());
byte[] bytes = new byte[array.getLength()];
for (int i = 0; i < bytes.length; ++i) {
bytes[i] = array.get(i);
Expand All @@ -119,7 +119,7 @@ private void performRequest(AsyncCallback<Boolean> callback) {

if (outputStream != null) {
byte[] bytes = outputStream.toByteArray();
Int8Array array = Int8Array.create(bytes.length);
var array = new Int8Array(bytes.length);
for (int i = 0; i < bytes.length; ++i) {
array.set(i, bytes[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ private TByteOrder(String name) {
public static TByteOrder nativeOrder() {
if (nativeOrder == null) {
if (PlatformDetector.isJavaScript()) {
var buffer = ArrayBuffer.create(2);
var shortArray = Int16Array.create(buffer);
var buffer = new ArrayBuffer(2);
var shortArray = new Int16Array(buffer);
shortArray.set(0, (short) 1);
var byteArray = Int8Array.create(buffer);
var byteArray = new Int8Array(buffer);
nativeOrder = byteArray.get(0) == 0 ? BIG_ENDIAN : LITTLE_ENDIAN;
} else {
var array = new short[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected int next(int bits) {
@Override
public void nextBytes(byte[] bytes) {
if (PlatformDetector.isJavaScript() && Crypto.isSupported()) {
Uint8Array buffer = Uint8Array.create(bytes.length);
var buffer = new Uint8Array(bytes.length);
Crypto.current().getRandomValues(buffer);

for (int i = 0; i < bytes.length; ++i) {
Expand Down
22 changes: 11 additions & 11 deletions classlib/src/main/java/org/teavm/classlib/java/util/TDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public TDate(int year, int month, int date, int hrs, int min) {
public TDate(int year, int month, int date, int hrs, int min, int sec) {
this(PlatformDetector.isLowLevel()
? initDateLowLevel(year, month, date, hrs, min, sec)
: (long) JSDate.create(year, month, date, hrs, min, sec).getTime());
: (long) new JSDate(year, month, date, hrs, min, sec).getTime());
if (!PlatformDetector.isLowLevel()) {
setYear(year);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public int getYear() {
if (PlatformDetector.isLowLevel()) {
return getYearLowLevel(value);
}
return JSDate.create(value).getFullYear() - 1900;
return new JSDate(value).getFullYear() - 1900;
}

@Import(name = "teavm_date_getYear")
Expand All @@ -143,7 +143,7 @@ public void setYear(int year) {
value = setYearLowLevel(value, year);
return;
}
JSDate date = JSDate.create(value);
var date = new JSDate(value);
date.setFullYear(year + 1900);
value = (long) date.getTime();
}
Expand All @@ -160,7 +160,7 @@ public int getMonth() {
if (PlatformDetector.isLowLevel()) {
return getMonthLowLevel(value);
}
return JSDate.create(value).getMonth();
return new JSDate(value).getMonth();
}

@Import(name = "teavm_date_getMonth")
Expand All @@ -176,7 +176,7 @@ public void setMonth(int month) {
value = setMonthLowLevel(value, month);
return;
}
JSDate date = JSDate.create(value);
var date = new JSDate(value);
date.setMonth(month);
value = (long) date.getTime();
}
Expand All @@ -193,7 +193,7 @@ public int getDate() {
if (PlatformDetector.isLowLevel()) {
return getDateLowLevel(value);
}
return JSDate.create(value).getDate();
return new JSDate(value).getDate();
}

@Import(name = "teavm_date_getDate")
Expand All @@ -209,7 +209,7 @@ public void setDate(int date) {
value = setDateLowLevel(value, date);
return;
}
JSDate d = JSDate.create(value);
var d = new JSDate(value);
d.setDate(date);
this.value = (long) d.getTime();
}
Expand All @@ -226,7 +226,7 @@ public int getDay() {
if (PlatformDetector.isLowLevel()) {
return getDayLowLevel(value);
}
return JSDate.create(value).getDay();
return new JSDate(value).getDay();
}

@Import(name = "teavm_date_getDay")
Expand All @@ -239,7 +239,7 @@ public int getHours() {
if (PlatformDetector.isLowLevel()) {
return getHoursLowLevel(value);
}
return JSDate.create(value).getHours();
return new JSDate(value).getHours();
}

@Import(name = "teavm_date_getHours")
Expand Down Expand Up @@ -389,12 +389,12 @@ public String toString() {

@Deprecated
public String toLocaleString() {
return JSDate.create(value).toLocaleFormat("%c");
return new JSDate(value).toLocaleFormat("%c");
}

@Deprecated
public String toGMTString() {
return JSDate.create(value).toUTCString();
return new JSDate(value).toUTCString();
}

@Deprecated
Expand Down
83 changes: 52 additions & 31 deletions jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
package org.teavm.jso.ajax;

import org.teavm.jso.JSBody;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.xml.Document;

public abstract class XMLHttpRequest implements JSObject, EventTarget {
@JSClass
public class XMLHttpRequest implements JSObject, EventTarget {
public static final int UNSET = 0;

public static final int OPENED = 1;
Expand All @@ -34,52 +36,55 @@ public abstract class XMLHttpRequest implements JSObject, EventTarget {

public static final int DONE = 4;

public abstract void open(String method, String url);
public XMLHttpRequest() {
}

public native void open(String method, String url);

public abstract void open(String method, String url, boolean async);
public native void open(String method, String url, boolean async);

public abstract void open(String method, String url, boolean async, String user);
public native void open(String method, String url, boolean async, String user);

public abstract void open(String method, String url, boolean async, String user, String password);
public native void open(String method, String url, boolean async, String user, String password);

public abstract void send();
public native void send();

public abstract void send(String data);
public native void send(String data);

public abstract void send(JSObject data);
public native void send(JSObject data);

public abstract void setRequestHeader(String name, String value);
public native void setRequestHeader(String name, String value);

public abstract String getAllResponseHeaders();
public native String getAllResponseHeaders();

public abstract String getResponseHeader(String name);
public native String getResponseHeader(String name);

@JSProperty("onreadystatechange")
public abstract void setOnReadyStateChange(ReadyStateChangeHandler handler);
public native void setOnReadyStateChange(ReadyStateChangeHandler handler);

@JSProperty("onreadystatechange")
public abstract void setOnReadyStateChange(EventListener<Event> handler);
public native void setOnReadyStateChange(EventListener<Event> handler);

@JSProperty("onabort")
public abstract void onAbort(EventListener<ProgressEvent> eventListener);
public native void onAbort(EventListener<ProgressEvent> eventListener);

@JSProperty("onerror")
public abstract void onError(EventListener<ProgressEvent> eventListener);
public native void onError(EventListener<ProgressEvent> eventListener);

@JSProperty("onload")
public abstract void onLoad(EventListener<ProgressEvent> eventListener);
public native void onLoad(EventListener<ProgressEvent> eventListener);

@JSProperty("onloadstart")
public abstract void onLoadStart(EventListener<ProgressEvent> eventListener);
public native void onLoadStart(EventListener<ProgressEvent> eventListener);

@JSProperty("onloadend")
public abstract void onLoadEnd(EventListener<ProgressEvent> eventListener);
public native void onLoadEnd(EventListener<ProgressEvent> eventListener);

@JSProperty("onprogress")
public abstract void onProgress(EventListener<ProgressEvent> eventListener);
public native void onProgress(EventListener<ProgressEvent> eventListener);

@JSProperty("ontimeout")
public abstract void onTimeout(EventListener<ProgressEvent> eventListener);
public native void onTimeout(EventListener<ProgressEvent> eventListener);

public final void onComplete(Runnable runnable) {
setOnReadyStateChange(() -> {
Expand All @@ -89,37 +94,53 @@ public final void onComplete(Runnable runnable) {
});
}

public abstract void overrideMimeType(String mimeType);
public native void overrideMimeType(String mimeType);

@JSProperty
public abstract int getReadyState();
public native int getReadyState();

@JSProperty
public abstract String getResponseText();
public native String getResponseText();

@JSProperty
public abstract Document getResponseXML();
public native Document getResponseXML();

@JSProperty
public abstract JSObject getResponse();
public native JSObject getResponse();

@JSProperty
public abstract int getStatus();
public native int getStatus();

@JSProperty
public abstract String getStatusText();
public native String getStatusText();

@JSProperty
public abstract void setResponseType(String type);
public native void setResponseType(String type);

@JSProperty
public abstract String getResponseType();
public native String getResponseType();

@JSBody(script = "return new XMLHttpRequest();")
@Deprecated
public static native XMLHttpRequest create();

public abstract void abort();
public native void abort();

@JSProperty
public abstract String getResponseURL();
public native String getResponseURL();

@Override
public native void addEventListener(String type, EventListener<?> listener, boolean useCapture);

@Override
public native void addEventListener(String type, EventListener<?> listener);

@Override
public native void removeEventListener(String type, EventListener<?> listener, boolean useCapture);

@Override
public native void removeEventListener(String type, EventListener<?> listener);

@Override
public native boolean dispatchEvent(Event evt);
}
13 changes: 8 additions & 5 deletions jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@
package org.teavm.jso.browser;

import org.teavm.jso.JSBody;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSProperty;
import org.teavm.jso.gamepad.Gamepad;
import org.teavm.jso.geolocation.Geolocation;

@JSClass(name = "navigator")
public final class Navigator {
private Navigator() {
}

@JSBody(script = "return navigator.onLine;")
@JSProperty("onLine")
public static native boolean isOnline();

@JSBody(script = "return navigator.geolocation;")
@JSProperty
public static native Geolocation getGeolocation();

@JSBody(script = "return (\"geolocation\" in navigator);")
public static native boolean isGeolocationAvailable();

@JSBody(script = "return navigator.language;")
@JSProperty
public static native String getLanguage();

@JSBody(script = "return navigator.languages;")
@JSProperty
public static native String[] getLanguages();

@JSBody(script = "return navigator.getGamepads();")
@JSProperty
public static native Gamepad[] getGamepads();

@JSBody(script = "return navigator.hardwareConcurrency")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package org.teavm.jso.browser;

import org.teavm.jso.JSBody;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;

@JSClass(name = "performance")
public final class Performance implements JSObject {
private Performance() {
}

@JSBody(script = "return performance.now();")
public static native double now();

@JSBody(script = "return typeof(performance) !== 'undefined';")
Expand Down
Loading

0 comments on commit 69d1ff8

Please sign in to comment.