diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TRuntime.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TRuntime.java index e5df6a87f..343e0dac3 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TRuntime.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TRuntime.java @@ -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(); + } } \ No newline at end of file diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java index 254953411..4dfb4d99e 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Navigator.java @@ -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(); } diff --git a/jso/apis/src/main/java/org/teavm/jso/browser/Window.java b/jso/apis/src/main/java/org/teavm/jso/browser/Window.java index 1b7e57faa..4f0dc7372 100644 --- a/jso/apis/src/main/java/org/teavm/jso/browser/Window.java +++ b/jso/apis/src/main/java/org/teavm/jso/browser/Window.java @@ -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 transfer); + public abstract void postMessage(JSObject message, String targetOrigin); public abstract void postMessage(JSObject message, String targetOrigin, JSArrayReader 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(); diff --git a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/Worker.kt b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/Worker.kt index d64b45b8b..47baf9a30 100644 --- a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/Worker.kt +++ b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/Worker.kt @@ -65,7 +65,7 @@ class RenderWorker { renderer.render() val perfEnd = System.nanoTime() val buffer = extractBuffer(raster.flip()) - postMessageFromWorker(JSObjects.createWithoutProto>().apply { + Window.worker().postMessage(JSObjects.createWithoutProto>().apply { set("data", buffer) set("time", JSNumber.valueOf((perfEnd - perfStart).toInt())) }, JSArray.of(buffer)) diff --git a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt index fb9100c1b..a4d5a2860 100644 --- a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt +++ b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/main.kt @@ -35,7 +35,7 @@ fun main(args: Array) { fun runController() { val performanceIndicator = HTMLDocument.current().getElementById("performance-indicator") var performanceIndicatorByWorkers: List = 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) { diff --git a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/nativeJs.kt b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/nativeJs.kt index 1d64ca742..62f205423 100644 --- a/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/nativeJs.kt +++ b/samples/software3d/src/jvmMain/kotlin/org/teavm/samples/software3d/teavm/nativeJs.kt @@ -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) - -@JSBody(script = "return navigator.hardwareConcurrency;") -external fun cpuCount(): Int \ No newline at end of file +external fun extractBuffer(@JSByRef data: IntArray): ArrayBuffer \ No newline at end of file