More documentation.

This commit is contained in:
Jindra Petřík
2024-08-08 19:27:14 +02:00
parent 5c1811582a
commit f219b49372
394 changed files with 13018 additions and 552 deletions
@@ -26,10 +26,20 @@ import java.util.List;
*/
public class ArrayType extends ObjectType {
/**
* Empty array object.
*/
public static final ObjectType EMPTY_ARRAY = new ArrayType();
/**
* Values
*/
public List<Object> values;
/**
* Constructor.
* @param values Values
*/
public ArrayType(List<Object> values) {
this.values = values;
}
@@ -31,6 +31,11 @@ import java.util.List;
*/
public class EcmaScript {
/**
* Converts value to a number.
* @param o Value to convert
* @return Number
*/
public static Double toNumber(Object o) {
if (o == null) {
return 0.0;
@@ -73,6 +78,12 @@ public class EcmaScript {
return toNumber(toPrimitive(o, "Number"));
}
/**
* Converts value to a primitive.
* @param o Value to convert
* @param prefferedType Preferred type
* @return Primitive value
*/
public static Object toPrimitive(Object o, String prefferedType) {
if (o == Undefined.INSTANCE) {
return o;
@@ -95,20 +106,43 @@ public class EcmaScript {
return Undefined.INSTANCE; //??
}
/**
* Object.get.
* @param o Object
* @param p Property
* @return Value
*/
public static Object object_get(ObjectType o, String p) {
//TODO: isDataDesciptor, etc. ECMA 8.12.3
return object_getProperty(o, p);
}
/**
* Object.getProperty.
* @param o
* @param p
* @return
*/
public static Object object_getProperty(ObjectType o, String p) {
//TODO: getownproperty, etc... ECMA 8.12.2
return o.getAttribute(p);
}
/**
* Object.defaultvalue.
* @param o Object
* @return Default value
*/
public static Object object_defaultValue(ObjectType o) {
return object_defaultValue(o, "Number");
}
/**
* Object.defaultvalue.
* @param o Object
* @param hint Hint
* @return Default value
*/
public static Object object_defaultValue(ObjectType o, String hint) {
switch (hint) {
case "String":
@@ -123,6 +157,11 @@ public class EcmaScript {
}
/**
* Converts value to a number. AS2 version.
* @param o Value to convert
* @return Number
*/
public static Double toNumberAs2(Object o) {
if (o == Null.INSTANCE) {
return Double.NaN;
@@ -135,6 +174,11 @@ public class EcmaScript {
return toNumber(o);
}
/**
* Gets type of value.
* @param o Value
* @return Type
*/
public static EcmaType type(Object o) {
if (o == null) {
return EcmaType.NULL;
@@ -163,6 +207,11 @@ public class EcmaScript {
return EcmaType.OBJECT;
}
/**
* Converts value to a type string.
* @param o Value
* @return Type string
*/
public static String typeString(Object o) {
EcmaType type = EcmaScript.type(o);
String typeStr;
@@ -195,10 +244,23 @@ public class EcmaScript {
return typeStr;
}
/**
* Compares two values.
* @param x Value 1
* @param y Value 2
* @return Comparison result
*/
public static Object compare(Object x, Object y) {
return compare(x, y, false);
}
/**
* Compares two values.
* @param x Value 1
* @param y Value 2
* @param as2 AS2 mode
* @return Comparison result
*/
public static Object compare(Object x, Object y, boolean as2) {
Object px = x;
Object py = y;
@@ -284,10 +346,23 @@ public class EcmaScript {
}
}
/**
* Compares two values strictly.
* @param x Value 1
* @param y Value 2
* @return Comparison result
*/
public static boolean strictEquals(Object x, Object y) {
return strictEquals(false, x, y);
}
/**
* Compares two values strictly.
* @param as2 AS2 mode
* @param x Value 1
* @param y Value 2
* @return Comparison result
*/
public static boolean strictEquals(boolean as2, Object x, Object y) {
if (type(x) != type(y)) {
return false;
@@ -296,10 +371,23 @@ public class EcmaScript {
return equals(as2, x, y);
}
/**
* Checks if two values are equal.
* @param x Value 1
* @param y Value 2
* @return True if values are equal
*/
public static boolean equals(Object x, Object y) {
return equals(false, x, y);
}
/**
* Checks if two values are equal.
* @param as2 AS2 mode
* @param x Value 1
* @param y Value 2
* @return True if values are equal
*/
public static boolean equals(boolean as2, Object x, Object y) {
EcmaType typeX = type(x);
EcmaType typeY = type(y);
@@ -381,6 +469,11 @@ public class EcmaScript {
return false;
}
/**
* Converts value to a boolean.
* @param o Value
* @return Boolean
*/
public static boolean toBoolean(Object o) {
if (o == null) {
return false;
@@ -421,10 +514,20 @@ public class EcmaScript {
return true; //other Object
}
/**
* Converts value to an int32.
* @param o Value
* @return Int32
*/
public static int toInt32(Object o) {
return (int) toUint32(o);
}
/**
* Converts value to an uint32.
* @param o Value
* @return Uint32
*/
public static long toUint32(Object o) {
Double n = toNumber(o);
if (n.isNaN()) {
@@ -444,6 +547,11 @@ public class EcmaScript {
return posInt;
}
/**
* Converts value to string.
* @param o Value
* @return String
*/
public static String toString(Object o) {
if (o == null) {
return "null";
@@ -474,6 +582,12 @@ public class EcmaScript {
return o.toString();
}
/**
* Converts value to string.
* @param o Value
* @param constantPool Constant pool
* @return String
*/
public static String toString(Object o, List<String> constantPool) {
if (o instanceof ConstantIndex) {
int index = ((ConstantIndex) o).index;
@@ -484,6 +598,11 @@ public class EcmaScript {
return toString(o);
}
/**
* Parses float value.
* @param string String
* @return Float value
*/
public static Double parseFloat(Object string) {
String inputString = toString(string);
int startPos = 0;
@@ -503,14 +622,30 @@ public class EcmaScript {
}
/**
* Checks if value is NaN.
* @param number Value
* @return True if value is NaN
*/
public static Boolean isNaN(Object number) {
return Double.isNaN(toNumber(number));
}
/**
* Checks if value is finite.
* @param number Value
* @return True if value is finite
*/
public static Boolean isFinite(Object number) {
return Double.isFinite(toNumber(number));
}
/**
* Parses int value.
* @param string String
* @param radix Radix
* @return Int value
*/
public static Object parseInt(Object string, Object radix) {
String inputString = toString(string);
int startPos = 0;
@@ -645,26 +780,56 @@ public class EcmaScript {
}
}
/**
* Encodes URI component.
* @param s String
* @return Encoded URI component
*/
public static String encodeUriComponent(Object s) {
return simpleCustomEncode(toString(s), "-_.!~*'()");
}
/**
* Encodes URI.
* @param s String
* @return Encoded URI
*/
public static String encodeUri(Object s) {
return simpleCustomEncode(toString(s), ";/?:@&=+$,#-_.!~*'()");
}
/**
* Escapes string.
* @param s String
* @return Escaped string
*/
public static String escape(Object s) {
return simpleCustomEncode(toString(s), "@-_.*+/");
}
/**
* Decodes URI component.
* @param s String
* @return Decoded URI component
*/
public static String decodeUriComponent(Object s) {
return simpleCustomDecode(toString(s), "-_.!~*'()");
}
/**
* Decodes URI.
* @param s String
* @return Decoded URI
*/
public static String decodeUri(Object s) {
return simpleCustomDecode(toString(s), ";/?:@&=+$,#-_.!~*'()");
}
/**
* Unescapes string.
* @param s String
* @return Unescaped string
*/
public static String unescape(Object s) {
return simpleCustomDecode(toString(s), "@-_.*+/");
}
@@ -23,11 +23,29 @@ package com.jpexs.decompiler.flash.ecma;
*/
public enum EcmaType {
/**
* Null
*/
NULL(null),
/**
* String
*/
STRING("String"),
/**
* Number
*/
NUMBER("Number"),
/**
* Undefined
*/
UNDEFINED(null),
/**
* Object
*/
OBJECT("Object"),
/**
* Boolean
*/
BOOLEAN("Boolean");
private final String clsName;
@@ -36,10 +54,20 @@ public enum EcmaType {
this.clsName = clsName;
}
/**
* Gets class name.
* @return Class name
*/
public String getClassName() {
return clsName;
}
/**
* Gets property of object.
* @param val Object
* @param propName Property name
* @return Property value
*/
public Object getProperty(Object val, String propName) {
String cls = getClassName();
if (cls == null) {
@@ -25,8 +25,14 @@ import java.io.Serializable;
*/
public class NotCompileTime implements Serializable {
/**
* Singleton instance.
*/
public static NotCompileTime INSTANCE = new NotCompileTime();
/**
* Private constructor.
*/
private NotCompileTime() {
}
@@ -25,8 +25,14 @@ import java.io.Serializable;
*/
public class Null implements Serializable {
/**
* Singleton instance of Null.
*/
public static Null INSTANCE = new Null();
/**
* Private constructor.
*/
private Null() {
}
@@ -29,26 +29,53 @@ import java.util.Set;
*/
public class ObjectType implements Callable {
/**
* Empty object
*/
public static final ObjectType EMPTY_OBJECT = new ObjectType();
/**
* Attributes
*/
protected Map<String, Object> attributes;
/**
* Sets attribute.
* @param name Name
* @param value Value
*/
public void setAttribute(String name, Object value) {
attributes.put(name, value);
}
/**
* Gets attribute names.
* @return Attribute names
*/
public Set<String> getAttributeNames() {
return attributes.keySet();
}
/**
* Constructor.
*/
protected ObjectType() {
this.attributes = new HashMap<>();
}
/**
* Constructor.
* @param attributes Attributes
*/
public ObjectType(Map<String, Object> attributes) {
this.attributes = attributes;
}
/**
* Gets attribute.
* @param name Name
* @return Attribute
*/
public Object getAttribute(String name) {
if (attributes.containsKey(name)) {
return attributes.get(name);
@@ -61,14 +88,26 @@ public class ObjectType implements Callable {
return "[object " + getTypeName() + "]";
}
/**
* Gets type name.
* @return Type name
*/
public String getTypeName() {
return "Object";
}
/**
* Converts to primitive.
* @return Primitive
*/
public Object toPrimitive() {
return toString();
}
/**
* Converts to number.
* @return Number
*/
public Object valueOf() {
return EcmaScript.toNumber(toString());
}
@@ -25,8 +25,14 @@ import java.io.Serializable;
*/
public class Undefined implements Serializable {
/**
* Singleton instance of Undefined.
*/
public static Undefined INSTANCE = new Undefined();
/**
* Private constructor.
*/
private Undefined() {
}