From a49f8b5e5cd30752111efdc6ecf46ab4e518c9bc Mon Sep 17 00:00:00 2001 From: romanman Date: Mon, 2 Jun 2014 18:00:24 +0300 Subject: [PATCH] A draft for ProgramPlayDialog --- .../org/ethereum/gui/ProgramPlayDialog.java | 150 ++++++++++++++++++ .../main/java/org/ethereum/vm/Program.java | 18 +++ .../src/main/java/org/ethereum/vm/VM.java | 12 +- 3 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 ethereumj-core/src/main/java/org/ethereum/gui/ProgramPlayDialog.java diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ProgramPlayDialog.java b/ethereumj-core/src/main/java/org/ethereum/gui/ProgramPlayDialog.java new file mode 100644 index 00000000..9a123997 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ProgramPlayDialog.java @@ -0,0 +1,150 @@ +package org.ethereum.gui; + +import org.ethereum.vm.Program; +import org.ethereum.vm.VM; +import org.spongycastle.util.encoders.Hex; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowListener; +import java.util.*; +import java.util.List; + +/** + * www.ethereumJ.com + * User: Roman Mandeleil + * Created on: 02/06/2014 16:58 + */ + +public class ProgramPlayDialog extends JPanel implements ActionListener, ChangeListener, Program.ProgramListener { + + public List outputList; + public JTextArea console; + + public ProgramPlayDialog() { + + outputList = new ArrayList(); + + VM vm = new VM(); + Program program = new Program(Hex.decode("630000000060445960CC60DD611234600054615566602054")); + program.addListener(this); + program.fullTrace(); + + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + vm.step(program); + + + + setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + + //Create the slider. + JSlider stepSlider = new JSlider(JSlider.HORIZONTAL, + 0, outputList.size() - 1, 0); + + + stepSlider.addChangeListener(this); + + //Turn on labels at major tick marks. + + stepSlider.setMajorTickSpacing(1); + stepSlider.setMinorTickSpacing(1); + stepSlider.setPaintTicks(true); + stepSlider.setPaintLabels(true); + stepSlider.setBorder( + BorderFactory.createEmptyBorder(0, 0, 10, 0)); + Font font = new Font("Serif", Font.ITALIC, 15); + stepSlider.setFont(font); + stepSlider.addChangeListener(this); + + + //Create the label that displays the animation. + add(stepSlider); + + int i = stepSlider.getValue(); + console = new JTextArea(outputList.get(i)); + console.setFont(new Font("Courier New", Font.PLAIN, 13)); + console.setForeground(Color.GRAY); + + + add(console); + + + this.setPreferredSize(new Dimension(600, 300)); + } + + @Override + public void actionPerformed(ActionEvent e) { + + + + } + + @Override + public void stateChanged(ChangeEvent e) { + JSlider source = (JSlider)e.getSource(); + int step = (int)source.getValue(); + + int i = source.getValue(); + String out = outputList.get(i); + + console.setText(out); + } + + + /** + * Create the GUI and show it. For thread safety, + * this method should be invoked from the + * event-dispatching thread. + */ + private static void createAndShowGUI() { + + + + ProgramPlayDialog ppd = new ProgramPlayDialog(); + + //Create and set up the window. + JFrame frame = new JFrame("SliderDemo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + //Add content to the window. + frame.add(ppd, BorderLayout.CENTER); + + //Display the window. + frame.pack(); + frame.setVisible(true); + } + + @Override + public void output(String out) { + outputList.add(out); + } + + public static void main(String []args){ + + /* Turn off metal's use of bold fonts */ + UIManager.put("swing.boldMetal", Boolean.FALSE); + + + //Schedule a job for the event-dispatching thread: + //creating and showing this application's GUI. + javax.swing.SwingUtilities.invokeLater(new Runnable() { + public void run() { + createAndShowGUI(); + } + }); + + } +} diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java index 648538c0..41e82497 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java @@ -20,6 +20,7 @@ import java.util.*; public class Program { Logger logger = LoggerFactory.getLogger("VM"); + ProgramListener listener; Stack stack = new Stack(); Map storage = new HashMap(); @@ -209,7 +210,24 @@ public class Program { logger.debug(" -- STORAGE -- {}\n", storageData); + StringBuilder global = new StringBuilder(); + global.append(" -- OPS -- ").append(opsString).append("\n"); + global.append(" -- STACK -- ").append(stackData).append("\n"); + global.append(" -- MEMORY -- ").append(memoryData).append("\n"); + global.append(" -- STORAGE -- ").append(storageData).append("\n"); + + if (listener != null){ + listener.output(global.toString()); + } + }; } + public void addListener(ProgramListener listener){ + this.listener = listener; + } + + public interface ProgramListener{ + public void output(String out); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index e3718632..e4a25a3a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -71,10 +71,10 @@ public class VM { program.stackPush(word1); program.step(); }break; -// case SLT: -// break; -// case SGT: -// break; + case SLT: + break; + case SGT: + break; case GT:{ DataWord word1 = program.stackPull(); DataWord word2 = program.stackPull(); @@ -165,8 +165,8 @@ public class VM { * SHA3 */ -// case SHA3: -// break; + case SHA3: + break; /** * Environmental Information