Skip to content

Commit

Permalink
jso: add methods that allow to convert without copying between JS typ…
Browse files Browse the repository at this point in the history
…ed arrays and Java arrays and buffers
  • Loading branch information
konsoletyper committed Oct 24, 2024
1 parent 2e08640 commit f937250
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
73 changes: 73 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/typedarrays/ArrayBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
15 changes: 15 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/typedarrays/Float32Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
13 changes: 13 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/typedarrays/Int16Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
13 changes: 13 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/typedarrays/Int32Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
13 changes: 13 additions & 0 deletions jso/apis/src/main/java/org/teavm/jso/typedarrays/Int8Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit f937250

Please sign in to comment.