jso: add methods that allow to convert without copying between JS typed arrays and Java arrays and buffers

This commit is contained in:
Alexey Andreev 2024-10-24 20:35:24 +02:00
parent 2e0864017b
commit f9372508ec
5 changed files with 127 additions and 0 deletions

View File

@ -15,7 +15,14 @@
*/
package org.teavm.jso.typedarrays;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty;
@ -36,4 +43,70 @@ public class ArrayBuffer implements JSObject {
@JSBody(params = "length", script = "return new ArrayBuffer(length);")
@Deprecated
public static native ArrayBuffer create(int length);
@JSBody(script = "return new Int8Array(this);")
@JSByRef
public native byte[] asByteArray();
@JSBody(script = "return new Int16Array(this);")
@JSByRef
public native short[] asShortArray();
@JSBody(script = "return new Int32Array(this);")
@JSByRef
public native int[] asIntArray();
@JSBody(script = "return new Int64Array(this);")
@JSByRef
public native long[] asLongArray();
@JSBody(script = "return new Float32Array(this);")
@JSByRef
public native float[] asFloatArray();
@JSBody(script = "return new Double32Array(this);")
@JSByRef
public native double[] asDoubleArray();
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef byte[] array);
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef short[] array);
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef int[] array);
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef long[] array);
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef float[] array);
@JSBody(params = "array", script = "return array.buffer;")
public static native ArrayBuffer from(@JSByRef double[] array);
public static ArrayBuffer from(@JSByRef ByteBuffer buffer) {
return from(buffer.array());
}
public static ArrayBuffer from(@JSByRef ShortBuffer buffer) {
return from(buffer.array());
}
public static ArrayBuffer from(@JSByRef IntBuffer buffer) {
return from(buffer.array());
}
public static ArrayBuffer from(@JSByRef LongBuffer buffer) {
return from(buffer.array());
}
public static ArrayBuffer from(@JSByRef FloatBuffer buffer) {
return from(buffer.array());
}
public static ArrayBuffer from(@JSByRef DoubleBuffer buffer) {
return from(buffer.array());
}
}

View File

@ -15,7 +15,10 @@
*/
package org.teavm.jso.typedarrays;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSIndexer;
@ -61,4 +64,16 @@ public class Float32Array extends ArrayBufferView {
@JSBody(params = { "buffer", "offset" }, script = "return new Float32Array(buffer, offset);")
@Deprecated
public static native Float32Array create(ArrayBuffer buffer, int offset);
@JSBody(script = "return this;")
@JSByRef
public native float[] asFloatArray();
@JSBody(params = "array", script = "return array;")
public static native Float32Array from(@JSByRef int[] array);
public static Float32Array from(@JSByRef FloatBuffer buffer) {
return from(buffer.array());
}
}

View File

@ -15,7 +15,9 @@
*/
package org.teavm.jso.typedarrays;
import java.nio.ShortBuffer;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSIndexer;
@ -61,4 +63,15 @@ public class Int16Array extends ArrayBufferView {
@JSBody(params = { "buffer", "offset" }, script = "return new Int16Array(buffer, offset);")
@Deprecated
public static native Int16Array create(ArrayBuffer buffer, int offset);
@JSBody(script = "return this;")
@JSByRef
public native short[] asShortArray();
@JSBody(params = "array", script = "return array;")
public static native Int16Array from(@JSByRef short[] array);
public static Int16Array from(@JSByRef ShortBuffer buffer) {
return from(buffer.array());
}
}

View File

@ -15,6 +15,8 @@
*/
package org.teavm.jso.typedarrays;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
@ -68,4 +70,15 @@ public class Int32Array extends ArrayBufferView {
@JSBody(params = { "buffer", "offset" }, script = "return new Int32Array(buffer, offset);")
@Deprecated
public static native Int32Array create(ArrayBuffer buffer, int offset);
@JSBody(script = "return this;")
@JSByRef
public native int[] asIntArray();
@JSBody(params = "array", script = "return array;")
public static native Int32Array from(@JSByRef int[] array);
public static Int32Array from(@JSByRef IntBuffer buffer) {
return from(buffer.array());
}
}

View File

@ -15,7 +15,9 @@
*/
package org.teavm.jso.typedarrays;
import java.nio.ByteBuffer;
import org.teavm.jso.JSBody;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSClass;
import org.teavm.jso.JSIndexer;
@ -61,4 +63,15 @@ public class Int8Array extends ArrayBufferView {
@JSBody(params = { "buffer", "offset" }, script = "return new Int8Array(buffer, offset);")
@Deprecated
public static native Int8Array create(ArrayBuffer buffer, int offset);
@JSBody(script = "return this;")
@JSByRef
public native byte[] asByteArray();
@JSBody(params = "array", script = "return array;")
public static native Int8Array from(@JSByRef byte[] array);
public static Int8Array from(@JSByRef ByteBuffer buffer) {
return from(buffer.array());
}
}