Files
nunuhara_xsystem4/test/Source/structs.jaf
T
Nunuhara Cabbage e5ef9f4d2d Implement constructors and methods
Implement CALLMETHOD, SH_STRUCTREF and PUSHSTRUCTPAGE instructions.

Constructors are a bit tricky since SH_LOCALCREATE has to implicitly
call an arbitrary number of functions before it returns (just changing
the instruction pointer only allows for calling a single function).

To get around this, there is now a vm_call() function which is used to
call a function synchronously, before returning from the current
instruction.

Methods are implemented as functions, except that they get a struct page
as local variable -1.

Destructors are still not implemented.
2019-10-20 17:33:22 -07:00

54 lines
1.0 KiB
C
Executable File

// -*-mode: C; coding: sjis; -*-
struct inner_struct {
int a;
};
struct my_struct {
int a;
int b;
string s;
inner_struct inner;
};
struct my_class_inner {
int a;
my_class_inner() {
a = 99;
}
};
struct my_class {
int a;
int b;
my_class_inner inner;
my_class() {
a = 42;
b = inner.a + 1;
}
void set_a(int a_);
};
void my_class::set_a(int a_) {
a = a_;
}
void test_structs(void)
{
my_struct s;
my_class c;
test_equal("s.a (uninitialized)", s.a, 0);
test_string("s.s (uninitialized)", s.s, "");
test_equal("s.inner.a (uninitialized)", s.inner.a, 0);
test_equal("s.a = 12", (s.a = 12, s.a), 12);
test_equal("s.b = 13", (s.b = 13, s.b), 13);
test_bool("s.s = \"test\"", (s.s = "test", s.s == "test"), true);
test_equal("s.inner.a = 14", (s.inner.a = 14, s.inner.a), 14);
test_equal("constructor", c.a, 42);
test_equal("nested constructor", c.inner.a, 99);
test_equal("nested constructor precedence", c.b, 100);
test_equal("c.set_a(22)", (c.set_a(22), c.a), 22);
}