classlib: directly use Math.random() in Random

Math.random() already handles the implementation for the backends,
delegating to JavaScript's Math.random() or the C runtime
implementation.

Fixes duplicate symbols when using the WASM backend.
This commit is contained in:
Bernd Busse 2024-01-05 14:17:55 +01:00 committed by Alexey Andreev
parent 6de4560282
commit 1964235fef

View File

@ -15,15 +15,10 @@
*/ */
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import org.teavm.backend.wasm.runtime.WasmSupport;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.impl.RandomUtils; import org.teavm.classlib.impl.RandomUtils;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TObject; import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.util.random.TRandomGenerator; import org.teavm.classlib.java.util.random.TRandomGenerator;
import org.teavm.interop.Import;
import org.teavm.interop.Unmanaged;
import org.teavm.jso.JSBody;
public class TRandom extends TObject implements TRandomGenerator, TSerializable { public class TRandom extends TObject implements TRandomGenerator, TSerializable {
/** A stored gaussian value for nextGaussian() */ /** A stored gaussian value for nextGaussian() */
@ -66,19 +61,9 @@ public class TRandom extends TObject implements TRandomGenerator, TSerializable
@Override @Override
public double nextDouble() { public double nextDouble() {
if (PlatformDetector.isC()) { return Math.random();
return crand();
} else if (PlatformDetector.isWebAssembly()) {
return WasmSupport.random();
} else {
return random();
}
} }
@Import(name = "teavm_rand")
@Unmanaged
private static native double crand();
/** /**
* Generate a random number with Gaussian distribution: * Generate a random number with Gaussian distribution:
* centered around 0 with a standard deviation of 1.0. * centered around 0 with a standard deviation of 1.0.
@ -101,9 +86,4 @@ public class TRandom extends TObject implements TRandomGenerator, TSerializable
return pair[0]; return pair[0];
} }
@JSBody(script = "return Math.random();")
@Import(module = "teavmMath", name = "random")
@Unmanaged
private static native double random();
} }