From 26f2f12f9e360b879acb551ae130f91b8b6cdd11 Mon Sep 17 00:00:00 2001 From: Matteo Nastasi Date: Wed, 23 Nov 2011 19:33:03 +0100 Subject: [PATCH] Extens and ExtensInst completed --- test_base2.html | 18 ++++++++ xynt-base-test4.js | 102 +++++++++++++++++++++++++++++++++++++++++++++ xynt-base.js | 94 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 test_base2.html create mode 100644 xynt-base-test4.js diff --git a/test_base2.html b/test_base2.html new file mode 100644 index 0000000..ef53d89 --- /dev/null +++ b/test_base2.html @@ -0,0 +1,18 @@ + + + + + + + + + + + +
+ +
+ +
+ + diff --git a/xynt-base-test4.js b/xynt-base-test4.js new file mode 100644 index 0000000..5eb9ee8 --- /dev/null +++ b/xynt-base-test4.js @@ -0,0 +1,102 @@ +function A(a, b) { + this.a_dyn = "A DYN"; + this.a = a; + this.b = b; +} + +A.prototype = { + a_stat: "A STAT", + + a_func: function () { + console.log("a_func run"); + } +} + +function B(a, b) { + this.b_dyn = "B DYN"; + A.call(this, a, b); +} + +B.prototype = { + b_stat: "B STAT", + + b_func: function () { + console.log("b_func run"); + } +} +function C() { + this.c_dyn = "C DYN"; +} + +C.prototype = { + c_stat: "C STAT", + + c_func: function () { + console.log("c_func run"); + } +} + +Extends(B, A); + +function print_dynstat(inst, name) +{ + for (i = 0 ; i < inst.length ; i++) { + console.log(name[i]+".a_dyn: "+inst[i].a_dyn); + console.log(name[i]+".a_stat: "+inst[i].a_stat); + } +} + +window.onload = function () { + show_class(A); + show_class(B); + show_class(C); + + a1 = new A(1,2); + a2 = new A(3,4); + b1 = new B(5,6); + c1 = new C(); + + arr_inst = [ a1 , a2 , b1 , c1 ]; + arr_name = [ "a1", "a2", "b1", "c1" ]; + + print_dynstat(arr_inst, arr_name); + + console.log("---"); + console.log("ACTION: a1.a_stat = \"CHANGED\";"); + a1.a_stat = "CHANGED"; + + print_dynstat(arr_inst, arr_name); + + console.log("---"); + console.log("ACTION: A.prototype.a_stat = \"PROTO CHANGED\""); + A.prototype.a_stat = "PROTO CHANGED"; + + print_dynstat(arr_inst, arr_name); + + console.log("---"); + console.log("ACTION: ExtendsInst(c1, A, [1, 33])"); + + var ext = ExtendsInst(c1, A, [1, 33]); + console.log("ExtInst: "+(ext == true ? "TRUE" : "FALSE")); + + print_dynstat(arr_inst, arr_name); + + console.log("---"); + console.log("ACTION: B.prototype.a_stat = \"PROTO CHANGED\""); + B.prototype.a_stat = "PROTO CHANGED"; + + print_dynstat(arr_inst, arr_name); + + console.log("---"); + console.log("ACTION: C.prototype.a_stat = \"PROTO CHANGED\""); + C.prototype.a_stat = "PROTO CHANGED"; + + print_dynstat(arr_inst, arr_name); + + console.log("=== INSTANCE c1 ==="); + show_inst(c1); + console.log("=== INSTANCE c1.a_func() ==="); + c1.a_func(); + console.log("=== INSTANCE c1.c_func() ==="); + c1.c_func(); +} \ No newline at end of file diff --git a/xynt-base.js b/xynt-base.js index 80c9249..157141b 100644 --- a/xynt-base.js +++ b/xynt-base.js @@ -1,3 +1,8 @@ +/* + * TODO + * super methods caller + */ + function Extends(sub) { var proto; @@ -39,11 +44,96 @@ function Extends(sub) } multi(inh, sub, true); - inh.super = function(cl, method) { - return this.xynt_sup_cl[cl].method.apply(this,arguments); + /* + inh.superrr = function(cl, method) { + console.log("xyx "+cl); + // console.log("xx"+arguments[0]); + // return this.xynt_sup_cl[cl].method.apply(this,arguments.slice(2)); } + */ sub.prototype = inh; console.log(sub.prototype.xynt_sup); console.log(sub.prototype.xynt_sup_cl); } + +function ExtendsInst(sub) +{ + var proto, cl, args; + + console.log("INIZIO EXT INST"); + + var multi = function(sub, super, is_last) + { + var proto = super.prototype; + + if (typeof(sub.xynt_sup) == 'undefined') + sub.xynt_sup = new Array(); + if (typeof(sub.xynt_sup_cl) == 'undefined') + sub.xynt_sup_cl = new Array(); + + for (var f in proto) { + if (f == "xynt_sup" || f == "xynt_sup_cl") + continue; + + if (typeof(sub[f]) != 'undefined') + return false; + + console.log("LOOP: "+f); + if( f != "constructor" && typeof(proto[f]) == "function" && !is_last){ + console.log("M_FU: "+proto['name']+" F: "+f); + sub[f] = function(){ + console.log("INFU PR: " + proto[f] + " FUNC: "+f+" THIS: " + this); + return proto[f].apply(this, arguments); + } + } + else { + console.log("M_PR: "+proto.name+" A: "+f+" FUN: "+proto[f]); + sub[f] = proto[f]; + } + } + return true; + } + + console.log("POST FUNC"); + + for( var i=1; i < arguments.length ; i++){ + console.log("POST FUNC IN LOOP"); + cl = arguments[i]; + if (i+1 < arguments.length && typeof(arguments[i+1]) != 'function') { + i++; + args = arguments[i]; + } + if (multi(sub, cl, false) == false) { + console.log("POST FUNC FALSE 1"); + return (false); + } + + console.log("POST FUNC PRE APPLY"); + cl.apply(sub, args); + console.log("POST FUNC POST APPLY"); + + sub.xynt_sup.push(cl.prototype); + sub.xynt_sup_cl.push(cl); + } + + console.log("FINE EXT INST"); + return true; +} + +function show_class(cl) { + console.log("Attributes Class of "+cl); + for (f in cl.prototype) { + console.log(f+": ["+cl.prototype[f]+"]"); + } + console.log("---"); +} + +function show_inst(inst) { + console.log("Attributes Instance of "+inst); + for (f in inst) { + console.log(f+": ["+inst[f]+"]"); + } + console.log("---"); +} + -- 2.17.1