Skip to content

Commit

Permalink
feitas as instruções relacionadas aos fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriserka committed Nov 27, 2019
1 parent 0d6a70d commit 22c86e2
Show file tree
Hide file tree
Showing 21 changed files with 391 additions and 58 deletions.
16 changes: 15 additions & 1 deletion .javasrc/Numerais.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
public class Numerais {
public final int um = 1;
public Numerais() {}

public Numerais(int x) {
this.um = x;
}

void setInteger(int x) {
this.um = x;
}

int getUm() {
return this.um;
}

private int um = 1;
public double d = 10.2;
private long pula = 2200;
protected static final float sss = 25.4f;
Expand Down
50 changes: 50 additions & 0 deletions .javasrc/Pessoa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Pessoa {

private String nome;
private int idade;
public double[] moedas = new double[]{0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0};
public static String nacionalidade = "valor padrao";

public Pessoa() {
this.nome = "anonimo";
}

public Pessoa(String nome, int idade) {
this.setNome(nome);
this.setIdade(idade);
}

void realizarCompra(double valor) {
double total = 0.0;
for (double d : this.moedas) {
total += d;
}
if (total < valor) {
System.out.println("Pse man... vai ficar sem");
return;
}
System.out.println("Transação aprovada");
}

void setNome(String nome) {
this.nome = nome;
}

String getNome() {
return this.nome;
}

void setIdade(int idade) {
if (idade <= 0) {
System.out.println("Vc tem idade negativa seu arrombado???");
// depois por pra lançar exceção
return;
} else {
this.idade = idade;
}
}

int getIdade() {
return this.idade;
}
}
29 changes: 29 additions & 0 deletions .javasrc/tests/ClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class ClassTest {
public static void main(String[] args) {
Numerais nums = new Numerais();
System.out.println("Field Numerais.d = " + nums.d);

int um_mais_um_pouco = nums.getUm() + 357;
// o javac n compila se for privado...
// System.out.println("Nums.str = " + nums.str);
um_mais_um_pouco++;
System.out.println("somando com local var = " + um_mais_um_pouco);
System.out.println("construtor custom = " + new Numerais(200).getUm());

Numerais num2 = new Numerais();
num2.setInteger(86451);
System.out.println("set = " + num2.getUm());

Pessoa p = new Pessoa();
System.out.println("pessoa linda de nome = " + p.getNome());
p.setNome("joão Kleber");
System.out.println("pessoa linda de nome = " + p.getNome());

System.out.println("nacionalidade da pessoa linda = " + p.nacionalidade);
p.nacionalidade = "Canadense";
System.out.println("nacionalidade da pessoa linda depois de fazer o passaporte = " + p.nacionalidade);

System.out.println("indo pro xops comprar pastel KKK");
p.realizarCompra(5.50);
}
}
Binary file added classes/ClassTest.class
Binary file not shown.
Binary file modified classes/Numerais.class
Binary file not shown.
Binary file added classes/Pessoa.class
Binary file not shown.
34 changes: 34 additions & 0 deletions include/utils/class_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef INCLUDE_UTILS_CLASS_T_H_
#define INCLUDE_UTILS_CLASS_T_H_

#include <map>
#include "utils/external/any.h"
#include "utils/field_t.h"
#include "utils/object.h"

namespace Utils {
struct Class_t {
Class_t() = default;

Class_t(const std::string &class_name) { this->class_name = class_name; }

void addField(const Any &val, const std::string &field_name,
const std::string &descriptor) {
auto f = Field_t(val);
if (!descriptor.compare("java/lang/String") ||
!descriptor.compare("java/lang/Object")) {
f.data = new Object(val);
}
this->fields[field_name] = f;
}

Field_t &getField(const std::string &field_name) {
return this->fields[field_name];
}

std::string class_name;
std::map<std::string, Field_t> fields;
};
} // namespace Utils

#endif // INCLUDE_UTILS_CLASS_T_H_
23 changes: 23 additions & 0 deletions include/utils/field_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef INCLUDE_UTILS_FIELD_T_H_
#define INCLUDE_UTILS_FIELD_T_H_

#include "utils/array_t.h"
#include "utils/external/any.h"

namespace Utils {
struct Field_t {
Field_t() = default;

Field_t(Any v) { this->data = v; }

~Field_t() {
if (this->data.is<Array_t *>()) {
delete this->data.as<Array_t *>();
}
}

Any data;
};
} // namespace Utils

#endif // INCLUDE_UTILS_FIELD_T_H_
23 changes: 16 additions & 7 deletions include/utils/memory_areas/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <algorithm>
#include <list>
#include <vector>
#include "utils/class_t.h"
#include "utils/helper_functions.h"
#include "utils/memory_areas/thread.h"
#include "utils/object.h"

Expand All @@ -19,15 +21,22 @@ class Heap {
});
}

void initialize(Thread *th, const std::string &name) {
this->initialized_classes.push_back(name);
// th->executeMethod("<clinit>", );
void addClass(Thread *th, const std::string &name);

Utils::Class_t &getClass(std::string &name) {
return *std::find_if(this->initialized_classes.begin(),
this->initialized_classes.end(),
[&name](const Utils::Class_t &c) {
return !c.class_name.compare(name);
});
}

bool isInitialized(const std::string &name) {
return std::find(this->initialized_classes.begin(),
this->initialized_classes.end(),
name) != this->initialized_classes.end();
return std::find_if(this->initialized_classes.begin(),
this->initialized_classes.end(),
[&name](const Utils::Class_t &c) {
return !c.class_name.compare(name);
}) != this->initialized_classes.end();
}

Utils::Object *pushReference(Utils::Object *obj) {
Expand All @@ -40,7 +49,7 @@ class Heap {
private:
int last_obj_index = 0;
std::list<Utils::Object *> object_refs;
std::vector<std::string> initialized_classes;
std::vector<Utils::Class_t> initialized_classes;
};
} // namespace MemoryAreas

Expand Down
2 changes: 1 addition & 1 deletion include/utils/memory_areas/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class Thread {
MethodArea *method_area;
Heap *heap;
Utils::Frame *current_frame;
const ClassFile *current_class;

private:
const ClassFile *current_class;
JavaStack jvm_stack;

std::string current_method;
Expand Down
19 changes: 16 additions & 3 deletions include/utils/object.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#ifndef INCLUDE_UTILS_OBJECT_H_
#define INCLUDE_UTILS_OBJECT_H_

#include <ostream>
#include <map>
#include "utils/array_t.h"
#include "utils/external/any.h"
#include "utils/field_t.h"
#include "utils/reference_kind.h"

// java/lang/String
// java/lang/Object
// [...
namespace Utils {
class Object {
public:
struct Object {
Object() = default;

~Object() {
Expand All @@ -17,14 +20,24 @@ class Object {
}
}

template <typename T>
Object(T v) {
this->data = v;
}

template <typename T>
Object(T v, int ref_type) {
this->data = v;
this->type = ref_type;
}

void addField(const std::string &field_name, const Field_t &f) {
this->fields[field_name] = f;
}

int type;
Any data;
std::map<std::string, Field_t> fields;
};
} // namespace Utils

Expand Down
15 changes: 8 additions & 7 deletions src/instructions/execution_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
auto opcode = **code_it;
auto pc_increment = 0;
Instruction *i = nullptr;
bool finished = false;

switch (opcode) {
case Opcodes::kAALOAD: {
Expand Down Expand Up @@ -76,7 +77,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kARETURN: {
i = new Reference::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kARRAYLENGTH: {
Expand Down Expand Up @@ -249,7 +250,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kDRETURN: {
i = new Double::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kDSTORE: {
Expand Down Expand Up @@ -414,7 +415,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kFRETURN: {
i = new Float::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kFSTORE: {
Expand Down Expand Up @@ -735,7 +736,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kIRETURN: {
i = new Integer::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kISHL: {
Expand Down Expand Up @@ -923,7 +924,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kLRETURN: {
i = new Long::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kLSHL: {
Expand Down Expand Up @@ -1037,7 +1038,7 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
}
case Opcodes::kRETURN: {
i = new Misc::Return();
i->execute(code_it, th, &pc_increment, wide);
finished = i->execute(code_it, th, &pc_increment, wide)[0];
break;
}
case Opcodes::kSALOAD: {
Expand Down Expand Up @@ -1075,6 +1076,6 @@ bool runBytecode(std::vector<Utils::Types::u1>::iterator *code_it,
if (i) delete i;
*pc += (wide ? 0 : 1) + pc_increment;

return false;
return finished;
}
} // namespace Instructions
2 changes: 1 addition & 1 deletion src/instructions/instruction_set/double.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ std::vector<int> Return::execute(
auto retval = th->current_frame->popOperand<double>();
th->pushReturnValue(retval);
th->current_frame->cleanOperands();
return {};
return {1};
}
// ----------------------------------------------------------------------------
std::vector<int> Store::execute(
Expand Down
2 changes: 1 addition & 1 deletion src/instructions/instruction_set/float.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ std::vector<int> Return::execute(
auto retval = th->current_frame->popOperand<float>();
th->pushReturnValue(retval);
th->current_frame->cleanOperands();
return {};
return {1};
}
// ----------------------------------------------------------------------------
std::vector<int> Store::execute(
Expand Down
2 changes: 1 addition & 1 deletion src/instructions/instruction_set/integer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ std::vector<int> Return::execute(
auto retval = th->current_frame->popOperand<int>();
th->pushReturnValue(retval);
th->current_frame->cleanOperands();
return {};
return {1};
}
// ----------------------------------------------------------------------------
std::vector<int> ShiftLeft::execute(
Expand Down
2 changes: 1 addition & 1 deletion src/instructions/instruction_set/long.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ std::vector<int> Return::execute(
auto retval = th->current_frame->popOperand<long>();
th->pushReturnValue(retval);
th->current_frame->cleanOperands();
return {};
return {1};
}
// ----------------------------------------------------------------------------
std::vector<int> ShiftLeft::execute(
Expand Down
Loading

0 comments on commit 22c86e2

Please sign in to comment.