Skip to content

Commit

Permalink
feat(objectionary#329): integrate CheckCast handler
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 9, 2024
1 parent 6c72bdb commit 7e7917a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/eolang/opeo/ast/CheckCast.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public CheckCast(final Type type, final AstNode value) {

@Override
public Iterable<Directive> toXmir() {
return new Directives().add("o").attr("base", "cast")
return new Directives().add("o").attr("base", "checkcast")
.append(new DirectivesData(this.type))
.append(this.value.toXmir())
.up();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/eolang/opeo/compilation/XmirParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Attributes;
import org.eolang.opeo.ast.Cast;
import org.eolang.opeo.ast.CheckCast;
import org.eolang.opeo.ast.ClassField;
import org.eolang.opeo.ast.ClassName;
import org.eolang.opeo.ast.Constant;
Expand Down Expand Up @@ -175,6 +176,8 @@ public AstNode parse(final XmlNode node) {
result = new ArrayConstructor(node, this);
} else if ("return".equals(base)) {
result = new Return(node, this);
} else if ("checkcast".equals(base)) {
result = new CheckCast(node, this);
} else if (!base.isEmpty() && base.charAt(0) == '.') {
final Attributes attributes = new Attributes(node.firstChild());
if ("static".equals(attributes.type())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.opeo.decompilation.handlers;

import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.CheckCast;
import org.eolang.opeo.decompilation.DecompilerState;
import org.eolang.opeo.decompilation.InstructionHandler;
import org.objectweb.asm.Type;

/**
* CheckCast instruction handler.
*
* @since 0.5
*/
public final class CheckCastHandler implements InstructionHandler {

@Override
public void handle(final DecompilerState state) {
final AstNode value = state.stack().pop();
final Object type = state.operand(0);
state.stack().push(new CheckCast((Type) type, value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public RouterHandler(final boolean counting) {
new MapEntry<>(Opcodes.ASTORE, new StoreHandler(Type.getType(Object.class))),
new MapEntry<>(Opcodes.AASTORE, new StoreToArrayHandler()),
new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayHandler()),
new MapEntry<>(Opcodes.CHECKCAST, new CheckCastHandler()),
new MapEntry<>(Opcodes.NEW, new NewHandler()),
new MapEntry<>(Opcodes.DUP, new DupHandler()),
new MapEntry<>(Opcodes.BIPUSH, new BipushHandler()),
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/eolang/opeo/ast/CheckCastTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CheckCastTest {
*/
private static final String XMIR = String.join(
"\n",
"<o base='cast'>",
"<o base='checkcast'>",
" <o base='type' data='bytes'>4A</o>",
" <o base='int' data='bytes'>00 00 00 00 00 00 00 03</o>",
"</o>"
Expand Down

0 comments on commit 7e7917a

Please sign in to comment.