Skip to content

Commit

Permalink
jso: add files and streams API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihromant authored and konsoletyper committed May 4, 2024
1 parent 6df39dc commit d40bd99
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 0 deletions.
67 changes: 67 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/ajax/FormData.java
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/ajax/XMLHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 {
Expand Down Expand Up @@ -52,6 +53,10 @@ public XMLHttpRequest() {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,4 +80,7 @@ public abstract class HTMLInputElement extends HTMLElement {

@JSProperty
public abstract void setPlaceholder(String value);

@JSProperty
public abstract FileList getFiles();
}
60 changes: 60 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/file/Blob.java
Original file line number Diff line number Diff line change
@@ -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();
}
51 changes: 51 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/file/BlobPropertyBag.java
Original file line number Diff line number Diff line change
@@ -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);
}
44 changes: 44 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/file/File.java
Original file line number Diff line number Diff line change
@@ -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();
}
25 changes: 25 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/file/FileList.java
Original file line number Diff line number Diff line change
@@ -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);
}
48 changes: 48 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/file/FilePropertyBag.java
Original file line number Diff line number Diff line change
@@ -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);
}
42 changes: 42 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/streams/ReadableStream.java
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit d40bd99

Please sign in to comment.