Skip to content

Commit 33b8b52

Browse files
authored
libprocessing ffi error handling (#1293)
* FFI error setup * Clear before init.
1 parent 7152360 commit 33b8b52

File tree

12 files changed

+5166
-225
lines changed

12 files changed

+5166
-225
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package processing.webgpu;
2+
3+
public class PGraphicsWebGPU {
4+
}
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package processing.webgpu;
22

33
import processing.core.NativeLibrary;
4-
import processing.ffi.processing_h;
4+
5+
import java.lang.foreign.MemorySegment;
6+
7+
import static java.lang.foreign.MemorySegment.NULL;
8+
import static processing.ffi.processing_h.processing_check_error;
9+
import static processing.ffi.processing_h.processing_init;
510

611
/**
712
* PWebGPU provides the native interface layer for libProcessing's WebGPU support.
813
*/
914
public class PWebGPU {
1015

1116
static {
12-
NativeLibrary.ensureLoaded();
17+
ensureLoaded();
18+
init();
1319
}
1420

1521
/**
@@ -20,9 +26,25 @@ public static void ensureLoaded() {
2026
}
2127

2228
/**
23-
* It's just math, silly!
29+
* Initializes the WebGPU subsystem. Must be called before any other WebGPU methods.
30+
*/
31+
public static void init() {
32+
processing_init();
33+
checkError();
34+
}
35+
36+
/**
37+
* Checks for errors from the native library and throws a PWebGPUException if an error occurred.
2438
*/
25-
public static long add(long left, long right) {
26-
return processing_h.processing_add(left, right);
39+
private static void checkError() {
40+
MemorySegment ret = processing_check_error();
41+
if (ret.equals(NULL)) {
42+
return;
43+
}
44+
45+
String errorMsg = ret.getString(0);
46+
if (errorMsg != null && !errorMsg.isEmpty()) {
47+
throw new PWebGPUException(errorMsg);
48+
}
2749
}
2850
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package processing.webgpu;
2+
3+
/**
4+
* Unchecked exception thrown for WebGPU-related errors.
5+
* <p>
6+
* WebGPU operations can fail for various reasons, such as unsupported hardware, but are not
7+
* expected to be recoverable by the Processing application.
8+
*/
9+
public class PWebGPUException extends RuntimeException {
10+
public PWebGPUException(String message) {
11+
super(message);
12+
}
13+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package processing.webgpu;
22

33
import org.junit.Test;
4-
import static org.junit.Assert.*;
54

65
/**
76
* Tests for the PWebGPU native interface.
87
*/
98
public class PWebGPUTest {
109
@Test
11-
public void testAddFunction() {
12-
long result = PWebGPU.add(2, 2);
13-
assertEquals("2 + 2 should equal 4", 4L, result);
10+
public void itLoads() {
11+
PWebGPU.ensureLoaded();
1412
}
1513
}

0 commit comments

Comments
 (0)