diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/EoCodec.java b/src/main/java/org/eolang/jeo/representation/bytecode/EoCodec.java new file mode 100644 index 000000000..1e9c0f111 --- /dev/null +++ b/src/main/java/org/eolang/jeo/representation/bytecode/EoCodec.java @@ -0,0 +1,116 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.jeo.representation.bytecode; + +import java.nio.ByteBuffer; + +public final class EoCodec implements Codec { + + /** + * Maximum long value that can be represented as double. + * Any value greater than this will be represented incorrectly. + */ + private static final long MAX_LONG_DOUBLE = 9_007_199_254_740_992L; + + /** + * Minimum long value that can be represented as double. + * Any value less than this will be represented incorrectly. + */ + private static final long MIN_LONG_DOUBLE = -9_007_199_254_740_992L; + + /** + * Origin codec. + */ + private final Codec origin; + + public EoCodec() { + this(new PlainCodec()); + } + + private EoCodec(final Codec origin) { + this.origin = origin; + } + + @Override + public byte[] bytes(final Object object, final DataType type) { + switch (type) { + case BOOL: + case CHAR: + case STRING: + case BYTES: + case LABEL: + case TYPE_REFERENCE: + case CLASS_REFERENCE: + case NULL: + return this.origin.bytes(object, type); + case BYTE: + case SHORT: + case INT: + case FLOAT: + case DOUBLE: + return ByteBuffer.allocate(Double.BYTES).putDouble((double) object).array(); + case LONG: + if (EoCodec.MIN_LONG_DOUBLE <= (long) object && (long) object <= EoCodec.MAX_LONG_DOUBLE) { + return this.origin.bytes(object, type); + } else { + return ByteBuffer.allocate(Long.BYTES).putLong((long) object).array(); + } + default: + throw new IllegalArgumentException( + String.format("Unsupported data type: %s", type) + ); + } + } + + @Override + public Object object(final byte[] bytes, final DataType type) { + switch (type) { + case BOOL: + case CHAR: + case STRING: + case BYTES: + case LABEL: + case TYPE_REFERENCE: + case CLASS_REFERENCE: + case NULL: + return this.origin.object(bytes, type); + case BYTE: + return (byte) ByteBuffer.wrap(bytes).getDouble(); + case SHORT: + return (short) ByteBuffer.wrap(bytes).getDouble(); + case INT: + return (int) ByteBuffer.wrap(bytes).getDouble(); + case LONG: + return (long) ByteBuffer.wrap(bytes).getDouble(); + case FLOAT: + return (float) ByteBuffer.wrap(bytes).getDouble(); + case DOUBLE: + return ByteBuffer.wrap(bytes).getDouble(); + default: + throw new IllegalArgumentException( + String.format("Unsupported data type: %s", type) + ); + } + } +} diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/EoLongCodec.java b/src/main/java/org/eolang/jeo/representation/bytecode/EoLongCodec.java new file mode 100644 index 000000000..e71cea35d --- /dev/null +++ b/src/main/java/org/eolang/jeo/representation/bytecode/EoLongCodec.java @@ -0,0 +1,50 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.jeo.representation.bytecode; + +import java.nio.ByteBuffer; +import org.apache.bcel.classfile.Code; + +public final class EoLongCodec implements Codec { + + private final Codec origin; + + public EoLongCodec(final Codec origin) { + this.origin = origin; + } + + @Override + public byte[] bytes(final Object object, final DataType type) { + return this.origin.bytes(object, type); + } + + @Override + public Object object(final byte[] bytes, final DataType type) { + if (type == DataType.LONG) { + return ByteBuffer.wrap(bytes).getLong(); + } else { + return this.origin.object(bytes, type); + } + } +} diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/PlainCodec.java b/src/main/java/org/eolang/jeo/representation/bytecode/PlainCodec.java index 07e9be9a6..58645f295 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/PlainCodec.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/PlainCodec.java @@ -28,7 +28,7 @@ import java.util.Optional; import org.objectweb.asm.Type; -public final class PlainCodec implements Codec { +final class PlainCodec implements Codec { private static final byte[] EMPTY = new byte[0]; @Override diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java index 1791fe2df..2c5ad5c3f 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValue.java @@ -118,22 +118,6 @@ public Iterator iterator() { break; } return res.iterator(); -// if ("string".equals(type)) { -// res = new DirectivesEoObject( -// type, -// this.name, -// new DirectivesComment(this.comment()), -// new DirectivesBytes(this.hex()) -// ); -// } else { -// res = new DirectivesJeoObject( -// type, -// this.name, -// new DirectivesComment(this.comment()), -// new DirectivesBytes(this.hex()) -// ); -// } -// return res.iterator(); } /**