Mark some JS native methods as not having side effects

This commit is contained in:
Alexey Andreev 2019-03-25 12:47:26 +03:00
parent 103affa392
commit 70ff079113
8 changed files with 57 additions and 0 deletions

View File

@ -34,6 +34,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
<artifactId>teavm-jso</artifactId> <artifactId>teavm-jso</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-interop</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSIndexer; import org.teavm.jso.JSIndexer;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
@ -83,9 +84,11 @@ public abstract class JSArray<T extends JSObject> implements JSArrayReader<T> {
public abstract void setLength(int len); public abstract void setLength(int len);
@JSBody(script = "return new Array();") @JSBody(script = "return new Array();")
@NoSideEffects
public static native <T extends JSObject> JSArray<T> create(); public static native <T extends JSObject> JSArray<T> create();
@JSBody(params = "size", script = "return new Array(size);") @JSBody(params = "size", script = "return new Array(size);")
@NoSideEffects
public static native <T extends JSObject> JSArray<T> create(int size); public static native <T extends JSObject> JSArray<T> create(int size);
@SafeVarargs @SafeVarargs

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
@ -27,8 +28,10 @@ public abstract class JSBoolean implements JSObject {
} }
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
private static native boolean booleanValue(JSBoolean value); private static native boolean booleanValue(JSBoolean value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSBoolean valueOf(boolean value); public static native JSBoolean valueOf(boolean value);
} }

View File

@ -15,63 +15,80 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSMethod; import org.teavm.jso.JSMethod;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
public abstract class JSDate implements JSObject { public abstract class JSDate implements JSObject {
@JSBody(script = "return new Date();") @JSBody(script = "return new Date();")
@NoSideEffects
public static native JSDate create(); public static native JSDate create();
@JSBody(params = "millis", script = "return new Date(millis);") @JSBody(params = "millis", script = "return new Date(millis);")
@NoSideEffects
public static native JSDate create(double millis); public static native JSDate create(double millis);
@JSBody(params = { "year", "month" }, script = "return new Date(year, month);") @JSBody(params = { "year", "month" }, script = "return new Date(year, month);")
@NoSideEffects
public static native JSDate create(int year, int month); public static native JSDate create(int year, int month);
@JSBody(params = { "year", "month", "day" }, script = "return new Date(year, month, day);") @JSBody(params = { "year", "month", "day" }, script = "return new Date(year, month, day);")
@NoSideEffects
public static native JSDate create(int year, int month, int day); public static native JSDate create(int year, int month, int day);
@JSBody(params = { "year", "month", "day", "hour" }, script = "return new Date(year, month, day, hour);") @JSBody(params = { "year", "month", "day", "hour" }, script = "return new Date(year, month, day, hour);")
@NoSideEffects
public static native JSDate create(int year, int month, int day, int hour); public static native JSDate create(int year, int month, int day, int hour);
@JSBody(params = { "year", "month", "day", "hour", "minute" }, @JSBody(params = { "year", "month", "day", "hour", "minute" },
script = "return new Date(year, month, day, hour, minute);") script = "return new Date(year, month, day, hour, minute);")
@NoSideEffects
public static native JSDate create(int year, int month, int day, int hour, int minute); public static native JSDate create(int year, int month, int day, int hour, int minute);
@JSBody(params = { "year", "month", "day", "hour", "minute", "second" }, @JSBody(params = { "year", "month", "day", "hour", "minute", "second" },
script = "return new Date(year, month, day, hour, minute, second);") script = "return new Date(year, month, day, hour, minute, second);")
@NoSideEffects
public static native JSDate create(int year, int month, int day, int hour, int minute, int second); public static native JSDate create(int year, int month, int day, int hour, int minute, int second);
@JSBody(params = { "year", "month", "day", "hour", "minute", "second", "millisecond" }, @JSBody(params = { "year", "month", "day", "hour", "minute", "second", "millisecond" },
script = "return new Date(year, month, day, hour, minute, second, millisecond);") script = "return new Date(year, month, day, hour, minute, second, millisecond);")
@NoSideEffects
public static native JSDate create(int year, int month, int day, int hour, int minute, int second, int millisecond); public static native JSDate create(int year, int month, int day, int hour, int minute, int second, int millisecond);
@JSBody(params = {}, script = "return Date.now();") @JSBody(params = {}, script = "return Date.now();")
@NoSideEffects
public static native double now(); public static native double now();
@JSBody(params = "stringValue", script = "return Date.parse(stringValue);") @JSBody(params = "stringValue", script = "return Date.parse(stringValue);")
@NoSideEffects
public static native double parse(String stringValue); public static native double parse(String stringValue);
@JSBody(params = { "year", "month" }, script = "return Date.UTC(year, month);") @JSBody(params = { "year", "month" }, script = "return Date.UTC(year, month);")
@NoSideEffects
public static native double UTC(int year, int month); public static native double UTC(int year, int month);
@JSBody(params = { "year", "month", "day" }, script = "return Date.UTC(year, month, day);") @JSBody(params = { "year", "month", "day" }, script = "return Date.UTC(year, month, day);")
@NoSideEffects
public static native double UTC(int year, int month, int day); public static native double UTC(int year, int month, int day);
@JSBody(params = { "year", "month", "day", "hour" }, script = "return Date.UTC(year, month, day, hour);") @JSBody(params = { "year", "month", "day", "hour" }, script = "return Date.UTC(year, month, day, hour);")
@NoSideEffects
public static native double UTC(int year, int month, int day, int hour); public static native double UTC(int year, int month, int day, int hour);
@JSBody(params = { "year", "month", "day", "hour", "minute" }, @JSBody(params = { "year", "month", "day", "hour", "minute" },
script = "return Date.UTC(year, month, day, hour, minute);") script = "return Date.UTC(year, month, day, hour, minute);")
@NoSideEffects
public static native double UTC(int year, int month, int day, int hour, int minute); public static native double UTC(int year, int month, int day, int hour, int minute);
@JSBody(params = { "year", "month", "day", "hour", "minute", "second" }, @JSBody(params = { "year", "month", "day", "hour", "minute", "second" },
script = "return Date.UTC(year, month, day, hour, minute, second);") script = "return Date.UTC(year, month, day, hour, minute, second);")
@NoSideEffects
public static native double UTC(int year, int month, int day, int hour, int minute, int second); public static native double UTC(int year, int month, int day, int hour, int minute, int second);
@JSBody(params = { "year", "month", "day", "hour", "minute", "second", "millisecond" }, @JSBody(params = { "year", "month", "day", "hour", "minute", "second", "millisecond" },
script = "return Date.UTC(year, month, day, hour, minute, second, millisecond);") script = "return Date.UTC(year, month, day, hour, minute, second, millisecond);")
@NoSideEffects
public static native double UTC(int year, int month, int day, int hour, int minute, int second, int millisecond); public static native double UTC(int year, int month, int day, int hour, int minute, int second, int millisecond);
public abstract int getDate(); public abstract int getDate();

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
@ -27,6 +28,7 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native double doubleValue(JSNumber number); private static native double doubleValue(JSNumber number);
public final int intValue() { public final int intValue() {
@ -34,6 +36,7 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native int intValue(JSNumber number); private static native int intValue(JSNumber number);
public final char charValue() { public final char charValue() {
@ -41,6 +44,7 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native char charValue(JSNumber number); private static native char charValue(JSNumber number);
public final byte byteValue() { public final byte byteValue() {
@ -48,6 +52,7 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native byte byteValue(JSNumber number); private static native byte byteValue(JSNumber number);
public final short shortValue() { public final short shortValue() {
@ -55,6 +60,7 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native short shortValue(JSNumber number); private static native short shortValue(JSNumber number);
public final float floatValue() { public final float floatValue() {
@ -62,23 +68,30 @@ public abstract class JSNumber implements JSObject {
} }
@JSBody(params = "number", script = "return number;") @JSBody(params = "number", script = "return number;")
@NoSideEffects
private static native float floatValue(JSNumber number); private static native float floatValue(JSNumber number);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(byte value); public static native JSNumber valueOf(byte value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(short value); public static native JSNumber valueOf(short value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(int value); public static native JSNumber valueOf(int value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(char value); public static native JSNumber valueOf(char value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(float value); public static native JSNumber valueOf(float value);
@JSBody(params = "value", script = "return value;") @JSBody(params = "value", script = "return value;")
@NoSideEffects
public static native JSNumber valueOf(double value); public static native JSNumber valueOf(double value);
} }

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
@ -23,26 +24,33 @@ public final class JSObjects {
} }
@JSBody(params = "object", script = "return Object.getOwnPropertyNames(object);") @JSBody(params = "object", script = "return Object.getOwnPropertyNames(object);")
@NoSideEffects
public static native String[] getOwnPropertyNames(JSObject object); public static native String[] getOwnPropertyNames(JSObject object);
@JSBody(script = "return {};") @JSBody(script = "return {};")
@NoSideEffects
public static native <T extends JSObject> T create(); public static native <T extends JSObject> T create();
@JSBody(script = "return Object.create(null);") @JSBody(script = "return Object.create(null);")
@NoSideEffects
public static native <T extends JSObject> T createWithoutProto(); public static native <T extends JSObject> T createWithoutProto();
@JSBody(params = "object", script = "return typeof object === 'undefined';") @JSBody(params = "object", script = "return typeof object === 'undefined';")
@NoSideEffects
public static native boolean isUndefined(JSObject object); public static native boolean isUndefined(JSObject object);
@JSBody(script = "return void 0;") @JSBody(script = "return void 0;")
@NoSideEffects
public static native JSObject undefined(); public static native JSObject undefined();
@JSBody(params = "object", script = "return typeof object;") @JSBody(params = "object", script = "return typeof object;")
@NoSideEffects
public static native String typeOf(JSObject object); public static native String typeOf(JSObject object);
@JSBody(params = "object", script = "return object.toString();") @JSBody(params = "object", script = "return object.toString();")
public static native String toString(JSObject object); public static native String toString(JSObject object);
@JSBody(params = { "object", "name" }, script = "return name in object;") @JSBody(params = { "object", "name" }, script = "return name in object;")
@NoSideEffects
public static native boolean hasProperty(JSObject object, String name); public static native boolean hasProperty(JSObject object, String name);
} }

View File

@ -15,15 +15,18 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty; import org.teavm.jso.JSProperty;
public abstract class JSRegExp implements JSObject { public abstract class JSRegExp implements JSObject {
@JSBody(params = "pattern", script = "return new RegExp(pattern);") @JSBody(params = "pattern", script = "return new RegExp(pattern);")
@NoSideEffects
public static native JSRegExp create(String pattern); public static native JSRegExp create(String pattern);
@JSBody(params = { "pattern", "flags" }, script = "return new RegExp(pattern, flags);") @JSBody(params = { "pattern", "flags" }, script = "return new RegExp(pattern, flags);")
@NoSideEffects
public static native JSRegExp create(String pattern, String flags); public static native JSRegExp create(String pattern, String flags);
public static JSRegExp create(String pattern, JSRegExpFlag... flags) { public static JSRegExp create(String pattern, JSRegExpFlag... flags) {

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.jso.core; package org.teavm.jso.core;
import org.teavm.interop.NoSideEffects;
import org.teavm.jso.JSBody; import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject; import org.teavm.jso.JSObject;
import org.teavm.jso.JSProperty; import org.teavm.jso.JSProperty;
@ -28,12 +29,15 @@ public abstract class JSString implements JSObject {
} }
@JSBody(params = "str", script = "return str;") @JSBody(params = "str", script = "return str;")
@NoSideEffects
private static native String stringValue(JSString str); private static native String stringValue(JSString str);
@JSBody(params = "str", script = "return str;") @JSBody(params = "str", script = "return str;")
@NoSideEffects
public static native JSString valueOf(String str); public static native JSString valueOf(String str);
@JSBody(params = "code", script = "return String.fromCharCode(code)") @JSBody(params = "code", script = "return String.fromCharCode(code)")
@NoSideEffects
public static native JSString fromCharCode(int code); public static native JSString fromCharCode(int code);
@JSProperty @JSProperty
@ -96,5 +100,6 @@ public abstract class JSString implements JSObject {
public abstract JSString trim(); public abstract JSString trim();
@JSBody(params = "obj", script = "return typeof obj === 'string';") @JSBody(params = "obj", script = "return typeof obj === 'string';")
@NoSideEffects
public static native boolean isInstance(JSObject obj); public static native boolean isInstance(JSObject obj);
} }