Add couple native declarations to JSO APIs. Update sample

This commit is contained in:
Alexey Andreev 2023-11-19 20:25:29 +01:00
parent 7108dfbac7
commit 810012872c
6 changed files with 20 additions and 11 deletions

View File

@ -16,6 +16,9 @@
package org.teavm.classlib.java.lang;
import org.teavm.interop.DelegateTo;
import org.teavm.interop.Platforms;
import org.teavm.interop.SupportedOn;
import org.teavm.jso.browser.Navigator;
import org.teavm.runtime.GC;
/**
@ -85,4 +88,9 @@ public class TRuntime {
private long totalMemoryLowLevel() {
return GC.availableBytes();
}
@SupportedOn(Platforms.JAVASCRIPT)
public int availableProcessors() {
return Navigator.hardwareConcurrency();
}
}

View File

@ -40,4 +40,7 @@ public final class Navigator {
@JSBody(script = "return navigator.getGamepads();")
public static native Gamepad[] getGamepads();
@JSBody(script = "return navigator.hardwareConcurrency")
public static native int hardwareConcurrency();
}

View File

@ -155,6 +155,8 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv
public abstract void postMessage(JSObject message);
public abstract void postMessage(JSObject message, JSArrayReader<JSObject> transfer);
public abstract void postMessage(JSObject message, String targetOrigin);
public abstract void postMessage(JSObject message, String targetOrigin, JSArrayReader<JSObject> transfer);
@ -163,6 +165,10 @@ public abstract class Window implements JSObject, WindowEventTarget, StorageProv
postMessage(message, targetOrigin, JSArray.of(transfer));
}
public final void postMessage(JSObject message, JSObject... transfer) {
postMessage(message, JSArray.of(transfer));
}
@JSBody(script = "return window;")
public static native Window current();

View File

@ -65,7 +65,7 @@ class RenderWorker {
renderer.render()
val perfEnd = System.nanoTime()
val buffer = extractBuffer(raster.flip())
postMessageFromWorker(JSObjects.createWithoutProto<JSMapLike<JSObject>>().apply {
Window.worker().postMessage(JSObjects.createWithoutProto<JSMapLike<JSObject>>().apply {
set("data", buffer)
set("time", JSNumber.valueOf((perfEnd - perfStart).toInt()))
}, JSArray.of(buffer))

View File

@ -35,7 +35,7 @@ fun main(args: Array<out String>) {
fun runController() {
val performanceIndicator = HTMLDocument.current().getElementById("performance-indicator")
var performanceIndicatorByWorkers: List<HTMLElement> = emptyList()
val maxWorkers = cpuCount()
val maxWorkers = Runtime.getRuntime().availableProcessors()
var workerType = WorkerType.JS
val controller = Controller(SCENE_WIDTH, SCENE_HEIGHT) { index, value ->
if (index == -1) {

View File

@ -18,15 +18,7 @@ package org.teavm.samples.software3d.teavm
import org.teavm.jso.JSBody
import org.teavm.jso.JSByRef
import org.teavm.jso.JSObject
import org.teavm.jso.core.JSArray
import org.teavm.jso.typedarrays.ArrayBuffer
@JSBody(params = ["data"], script = "return data.buffer;")
external fun extractBuffer(@JSByRef data: IntArray): ArrayBuffer
@JSBody(params = ["message", "transferable"], script = "self.postMessage(message, transferable);")
external fun postMessageFromWorker(message: JSObject, transferable: JSArray<out JSObject>)
@JSBody(script = "return navigator.hardwareConcurrency;")
external fun cpuCount(): Int