| 10 | import org.dynjs.runtime.builtins.types.string.DynString; |
| 11 | |
| 12 | public class Types { |
| 13 | |
| 14 | public static final Undefined UNDEFINED = new Undefined(); |
| 15 | public static final Null NULL = new Null(); |
| 16 | |
| 17 | public static void checkObjectCoercible(ExecutionContext context, Object o) { |
| 18 | if (o == Types.UNDEFINED) { |
| 19 | throw new ThrowException(context, context.createTypeError("undefined cannot be coerced to an object")); |
| 20 | } |
| 21 | |
| 22 | if (o == Types.NULL) { |
| 23 | throw new ThrowException(context, context.createTypeError("null cannot be coerced to an object")); |
| 24 | } |
| 25 | |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | public static void checkObjectCoercible(ExecutionContext context, Object o, String debug) { |
| 30 | if (o == Types.UNDEFINED) { |
| 31 | throw new ThrowException(context, context.createTypeError("undefined cannot be coerced to an object: " + debug)); |
| 32 | } |
| 33 | |
| 34 | if (o == Types.NULL) { |
| 35 | throw new ThrowException(context, context.createTypeError("null cannot be coerced to an object: " + debug)); |
| 36 | } |
| 37 | |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | public static boolean sameValue(Object left, Object right) { |
| 42 | |
| 43 | if (left.getClass() != right.getClass()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (left == UNDEFINED) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | if (left == NULL) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | return left.equals(right); |
| 56 | } |
| 57 | |
| 58 | public static JSObject toObject(ExecutionContext context, Object o) { |
| 59 | if (o == Types.UNDEFINED) { |
| 60 | throw new ThrowException(context, context.createTypeError("undefined cannot be converted to an object")); |
| 61 | } |
| 62 | if (o == Types.NULL) { |
| 63 | throw new ThrowException(context, context.createTypeError("null cannot be converted to an object")); |
| 64 | } |
| 65 | if (o instanceof JSObject) { |
| 66 | return (JSObject) o; |
| 67 | } |
| 68 | if (o instanceof String) { |
| 69 | return new DynString(context.getGlobalContext(), (String) o); |