jso: add files and streams API

This commit is contained in:
ihromant 2024-03-27 16:24:25 +02:00 committed by Alexey Andreev
parent 6df39dca2f
commit d40bd9989b
11 changed files with 411 additions and 0 deletions

View File

@ -0,0 +1,67 @@
/*
* Copyright 2024 ihromant.
*
* 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.jso.ajax;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSArray;
import org.teavm.jso.dom.html.HTMLElement;
import org.teavm.jso.dom.html.HTMLFormElement;
import org.teavm.jso.file.Blob;
@JSClass
public class FormData implements JSObject {
public FormData() {
}
public FormData(HTMLFormElement form) {
}
public FormData(HTMLFormElement form, HTMLElement submitter) {
}
public native void append(String name, String value);
public native void append(String name, Blob value);
public native void append(String name, String value, String fileName);
public native void append(String name, Blob value, String fileName);
public native void delete(String name);
/**
* Actual element is either {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
// TODO: update signature when union types supported in TeaVM
public native JSObject get(String name);
/**
* Actual elements are {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
// TODO: update signature when union types supported in TeaVM
public native JSArray<JSObject> getAll(String name);
public native boolean has(String name);
public native void set(String name, String value);
public native void set(String name, Blob value);
public native void set(String name, String value, String fileName);
public native void set(String name, Blob value, String fileName);
}

View File

@ -24,6 +24,7 @@ import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.EventTarget;
import org.teavm.jso.dom.events.Registration;
import org.teavm.jso.dom.xml.Document;
import org.teavm.jso.file.Blob;
@JSClass
public class XMLHttpRequest implements JSObject, EventTarget {
@ -52,6 +53,10 @@ public class XMLHttpRequest implements JSObject, EventTarget {
public native void send(String data);
public native void send(Blob blob);
public native void send(FormData formData);
public native void send(JSObject data);
public native void setRequestHeader(String name, String value);

View File

@ -16,6 +16,7 @@
package org.teavm.jso.dom.html;
import org.teavm.jso.JSProperty;
import org.teavm.jso.file.FileList;
public abstract class HTMLInputElement extends HTMLElement {
@JSProperty
@ -79,4 +80,7 @@ public abstract class HTMLInputElement extends HTMLElement {
@JSProperty
public abstract void setPlaceholder(String value);
@JSProperty
public abstract FileList getFiles();
}

View File

@ -0,0 +1,60 @@
/*
* Copyright 2024 ihromant.
*
* 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.jso.file;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSArrayReader;
import org.teavm.jso.core.JSPromise;
import org.teavm.jso.streams.ReadableStream;
import org.teavm.jso.typedarrays.ArrayBuffer;
@JSClass
public class Blob implements JSObject {
/**
* Actual elements within array are either {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
// TODO: update signature when
public Blob(JSArrayReader<? extends JSObject> array) {
}
/**
* Actual elements within array are either {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
public Blob(JSArrayReader<? extends JSObject> array, BlobPropertyBag options) {
}
@JSProperty
public native int getSize();
@JSProperty
public native String getType();
public native JSPromise<ArrayBuffer> arrayBuffer();
public native Blob slice();
public native Blob slice(int start);
public native Blob slice(int start, int end);
public native Blob slice(int start, int end, String contentType);
public native ReadableStream stream();
public native JSPromise<String> text();
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2024 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.jso.file;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSObjects;
@JSClass(transparent = true)
public abstract class BlobPropertyBag implements JSObject {
public static final String ENDING_TYPE_TRANSPARENT = "transparent";
public static final String ENDING_TYPE_NATIVE = "native";
BlobPropertyBag() {
}
public static BlobPropertyBag create() {
return JSObjects.create();
}
public BlobPropertyBag type(String type) {
setType(type);
return this;
}
// TODO: update signature when union of strings (lighweight enums) are supported in TeaVM
public BlobPropertyBag endingType(String endingType) {
setEndingType(endingType);
return this;
}
@JSProperty
private native void setType(String type);
@JSProperty
private native void setEndingType(String endingType);
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2024 ihromant.
*
* 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.jso.file;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSArrayReader;
@JSClass
public class File extends Blob implements JSObject {
/**
* Actual elements within array are either {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
public File(JSArrayReader<JSObject> array, String fileName) {
super(null);
}
/**
* Actual elements within array are either {@link Blob} or {@link org.teavm.jso.core.JSString}
*/
public File(JSArrayReader<JSObject> array, String fileName, JSObject options) {
super(null, null);
}
@JSProperty
public native double getLastModified();
@JSProperty
public native String getName();
}

View File

@ -0,0 +1,25 @@
/*
* Copyright 2024 ihromant.
*
* 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.jso.file;
import org.teavm.jso.JSIndexer;
import org.teavm.jso.JSObject;
import org.teavm.jso.core.JSArrayReader;
public interface FileList extends JSObject, JSArrayReader<File> {
@JSIndexer
File item(int index);
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2024 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.jso.file;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSObjects;
@JSClass(transparent = true)
public class FilePropertyBag extends BlobPropertyBag {
FilePropertyBag() {
}
public static FilePropertyBag create() {
return JSObjects.create();
}
@Override
public FilePropertyBag type(String type) {
return (FilePropertyBag) super.type(type);
}
@Override
public FilePropertyBag endingType(String endingType) {
return (FilePropertyBag) super.endingType(endingType);
}
public FilePropertyBag lastModified(long lastModified) {
setLastModified(lastModified);
return this;
}
@JSProperty
private native void setLastModified(double lastModified);
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2024 ihromant.
*
* 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.jso.streams;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSArrayReader;
import org.teavm.jso.core.JSPromise;
import org.teavm.jso.core.JSUndefined;
@JSClass
public class ReadableStream implements JSObject {
private ReadableStream() {
}
@JSProperty
public native boolean isLocked();
public native JSPromise<JSUndefined> abort(String reason);
public native JSPromise<JSUndefined> cancel();
public native JSPromise<JSUndefined> cancel(String reason);
public native JSArrayReader<? extends ReadableStream> tee();
public native ReadableStreamDefaultReader getReader();
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2024 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.jso.streams;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.core.JSPromise;
import org.teavm.jso.core.JSUndefined;
@JSClass
public class ReadableStreamDefaultReader implements JSObject {
private ReadableStreamDefaultReader() {
}
@JSProperty
public native JSPromise<JSUndefined> getClosed();
public native JSPromise<JSUndefined> cancel(JSObject reason);
public native JSPromise<ReadableStreamReadResult> read();
public native void releaseLock();
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2024 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.jso.streams;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
import org.teavm.jso.typedarrays.Int8Array;
public interface ReadableStreamReadResult extends JSObject {
@JSProperty
Int8Array getValue();
@JSProperty
boolean isDone();
}