/* * SPL - The SPL Programming Language * Copyright (C) 2004, 2005 Clifford Wolf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * example38.spl: Some more advanced object tests */ function test1() { function getobj() { var strange_var = 1; object Foobar { method strange() { return "${ strange_var++ } ${ this } ${ strange }"; } } return Foobar; } var class1 = getobj(); var obj1 = new class1(); object Class2 class1 { } var obj2 = new Class2(); debug obj1.strange(); debug obj2.strange(); } function test2() { object Foo { method foo() { debug "Foo: ${this} -> foo()"; } method bar() { return "Bar: ${this} -> bar()"; } method init() { debug "Init: ${this}"; return this; } } object Bar Foo { var count = 12; method bar() { count = count + 111; debug "${ *Foo.bar() } --> ${ count }"; } } var f = new Foo(); var b = new Bar(); b.foo(); b.bar(); f.foo(); debug f.bar(); var x = new Bar(); x.bar(); var x = new Bar(); x.bar(); } function test3() { object Bla { var title = "a demo."; } object Foo { var y = 23; var xxx; method init() { y = 42; xxx[0] = new Bla(); xxx[0].title = "This is"; return this; } } var x = new Foo(); debug x.xxx[0].title; debug Bla.title; } function test4() { var foo; var bar; function regbar(blup) { bar = blup; } object Demo { var bla = 0; method getbla() { return bla; } method init(x) { bla = x; foo = getbla; regbar(getbla); return this; } } var foobar = new Demo(42); debug foobar.getbla(); debug foo(); debug bar(); } function test5() { object Value { var val; } var a = new Value(); var b = new Value(); function get_base_obj() { var x = a; object Foo { method set_a(val) { x.val = val; } } return Foo; } function get_derived_obj(base) { var x = b; object Foo base { method set_b(val) { x.val = val; } } return Foo; } var o = new (get_derived_obj(get_base_obj()))(); o.set_a(23); o.set_b(42); debug "a=$a.val"; debug "b=$b.val"; } debug "---- Test #1 ----"; test1(); debug "---- Test #2 ----"; test2(); debug "---- Test #3 ----"; test3(); debug "---- Test #4 ----"; test4(); debug "---- Test #5 ----"; test5();