From f9372508ece5dbe6b3ed8451b06cc666d4688005 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 24 Oct 2024 20:35:24 +0200 Subject: [PATCH] jso: add methods that allow to convert without copying between JS typed arrays and Java arrays and buffers --- .../teavm/jso/typedarrays/ArrayBuffer.java | 73 +++++++++++++++++++ .../teavm/jso/typedarrays/Float32Array.java | 15 ++++ .../org/teavm/jso/typedarrays/Int16Array.java | 13 ++++ .../org/teavm/jso/typedarrays/Int32Array.java | 13 ++++ .../org/teavm/jso/typedarrays/Int8Array.java | 13 ++++ 5 files changed, 127 insertions(+) diff --git a/jso/apis/src/main/java/org/teavm/jso/typedarrays/ArrayBuffer.java b/jso/apis/src/main/java/org/teavm/jso/typedarrays/ArrayBuffer.java index 28cb3c6281..8516cc2fec 100644 --- a/jso/apis/src/main/java/org/teavm/jso/typedarrays/ArrayBuffer.java +++ b/jso/apis/src/main/java/org/teavm/jso/typedarrays/ArrayBuffer.java @@ -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 ArrayBuffer(int length) { @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()); + } } diff --git a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Float32Array.java b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Float32Array.java index 1ddc4ee065..97877432d2 100644 --- a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Float32Array.java +++ b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Float32Array.java @@ -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 Float32Array(ArrayBuffer buffer, int offset) { @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()); + } } diff --git a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int16Array.java b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int16Array.java index 87ce6444e5..3b1cc26d84 100644 --- a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int16Array.java +++ b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int16Array.java @@ -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 Int16Array(ArrayBuffer buffer, int offset) { @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()); + } } diff --git a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int32Array.java b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int32Array.java index 9dbaf3e817..8ada60978c 100644 --- a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int32Array.java +++ b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int32Array.java @@ -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 Int32Array(ArrayBuffer buffer, int offset) { @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()); + } } diff --git a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int8Array.java b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int8Array.java index c15c315461..7f3e0e615b 100644 --- a/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int8Array.java +++ b/jso/apis/src/main/java/org/teavm/jso/typedarrays/Int8Array.java @@ -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 Int8Array(ArrayBuffer buffer, int offset) { @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()); + } }