Revision 13, for version 8.2
\\ This is an advanced example of using events, and references
\\ M2000 never expose actual references.
\\ All items are searched by a hash function internal,
\\ using name of item and module name
\\ (maybe that name changed internal from interpreter)
\\ function weak$() return the internal name of any item included array item.
\\
\\ To run this write in M2000 CLI> edit a <enter> and then copy this
\\ press Esc and write a <enter> to run
\\ use Save pr1 <enter> to save it
\\ Nothing is static in M2000, except for "running" code.
\\ so Module deep1 isn't exist until first we make it a module to call
\\ and then when we call it, make anything as it runs
\\ Three steps: part of a source - source with a name - call that name
\\ M2000 interpreter use always source, never made any code of it.
\\ Using the source M2000 makes real objects and execute command on those.
\\
\\ Groups are static objects as list of items (we can reference specific item)
\\ ... like variables, modules, functions, events, lambdas, etc
\\ Here the task is to raise an event from an object (Call Event)
\\ and sending a value to this and to another object, calling functions
\\ and each function read a property y (a variable in the group)
\\ We test the raise of event when we pass the object (group)
\\ by value, by referenec, by weak reference
\\ and then we put copies of these groups in an array, and return it
\\ Then we fix the references, in the new copied array, and test again
\\ the raise of event from stored group (in array) and from passing
\\ with weak reference to another module.
Module deep1 {
Group aa {
y=123
Event b { Read x }
Function aa { Read x : Print x+.y }
Module callme {
Call Event .b, number
}
}
Group bb {
y=456
Function bb { Read x : Print x+.y}
}
Event aa.b New aa.aa(), bb.bb()
Print "From Module a"
aa.callme 5
Module testme {
Read c
Print "From testme inside Module Deep1"
c.callme
}
\\ Here we copy aa Group to c
testme aa, 5
\\ 133
\\ 466
\\ 128
\\ 461 (here is the fix, old versions print 128)
\\ passing by reference was ok
Module testme2 {
Read &c
Print "From testme2 inside Module Deep1"
c.callme
}
testme2 &aa, 5
\\ using weak reference was ok too
\\ no copy or reference for each element in Group aa
\\ just Call it like it is a global Module (using c$ as prefix value)
Module testme3 {
Read c$
Print "From testme3 inside Module Deep1"
c$.callme
}
testme3 &aa, 5
\\ we can put copies of static objects in arrays items
Print "Test Event from object in item in array"
Dim A(10)
A(0)=aa
A(0).y=1000
A(0).callme 5
testme3 weak$(A(0)), 5
\\ if array returned from this Module then event find error
\\ because group bb deleted.
A(1)=bb
push A()
}
Deep1
\\ now all modules and variables in Deep1 are destroyed
\\ except stack values. And in stack we have an array
Print StackType$(1) \\ print mArray - the inside object for arrays in M2000
Print Match("A") \\ Print -1 (True), is Array
Dim A()
\\ first we create the array and then we put the object on it
Read A()
Print "From exported array"
\\ need to restore weak reference
For A(0) {
\\ this block make A(0) static with a hidden name
\\ and this name used every time
Event .b Clear \\ clear all functions in event chain
\\ here we don't need to give a lazy$() function.
Event .b New .aa()
\\ function lazy$() return an anonymus function with lazy evaluation
\\ when this anonymus function run, set the module name to A
\\ so A(1) can be found, also we have to write the parameters to pass
Event .b New lazy$(A(1).bb(number))
\\ this happen because we can't get reference from array items.
}
\\ Now we Call a non static group
Print "From Module a"
A(0).callme 15
Module testme3 {
Read c$
Print "From testme3 inside Module a"
c$.callme
}
\\ Now we send it to module testme3 (module's deep1 modules are erased)
testme3 Weak$(A(0)), 15
\\ so now we have two objects, non static, but for this momemt they have
\\ one point static...the name of the array (A() is inside the module a)
\\ event b is calling from A(0) and opens A(1) reading variabe (property) y.
\\ Events may have by reference values as parameters and this works with multicast
\\ This is an advanced example of using events, and references
\\ M2000 never expose actual references.
\\ All items are searched by a hash function internal,
\\ using name of item and module name
\\ (maybe that name changed internal from interpreter)
\\ function weak$() return the internal name of any item included array item.
\\
\\ To run this write in M2000 CLI> edit a <enter> and then copy this
\\ press Esc and write a <enter> to run
\\ use Save pr1 <enter> to save it
\\ Nothing is static in M2000, except for "running" code.
\\ so Module deep1 isn't exist until first we make it a module to call
\\ and then when we call it, make anything as it runs
\\ Three steps: part of a source - source with a name - call that name
\\ M2000 interpreter use always source, never made any code of it.
\\ Using the source M2000 makes real objects and execute command on those.
\\
\\ Groups are static objects as list of items (we can reference specific item)
\\ ... like variables, modules, functions, events, lambdas, etc
\\ Here the task is to raise an event from an object (Call Event)
\\ and sending a value to this and to another object, calling functions
\\ and each function read a property y (a variable in the group)
\\ We test the raise of event when we pass the object (group)
\\ by value, by referenec, by weak reference
\\ and then we put copies of these groups in an array, and return it
\\ Then we fix the references, in the new copied array, and test again
\\ the raise of event from stored group (in array) and from passing
\\ with weak reference to another module.
Module deep1 {
Group aa {
y=123
Event b { Read x }
Function aa { Read x : Print x+.y }
Module callme {
Call Event .b, number
}
}
Group bb {
y=456
Function bb { Read x : Print x+.y}
}
Event aa.b New aa.aa(), bb.bb()
Print "From Module a"
aa.callme 5
Module testme {
Read c
Print "From testme inside Module Deep1"
c.callme
}
\\ Here we copy aa Group to c
testme aa, 5
\\ 133
\\ 466
\\ 128
\\ 461 (here is the fix, old versions print 128)
\\ passing by reference was ok
Module testme2 {
Read &c
Print "From testme2 inside Module Deep1"
c.callme
}
testme2 &aa, 5
\\ using weak reference was ok too
\\ no copy or reference for each element in Group aa
\\ just Call it like it is a global Module (using c$ as prefix value)
Module testme3 {
Read c$
Print "From testme3 inside Module Deep1"
c$.callme
}
testme3 &aa, 5
\\ we can put copies of static objects in arrays items
Print "Test Event from object in item in array"
Dim A(10)
A(0)=aa
A(0).y=1000
A(0).callme 5
testme3 weak$(A(0)), 5
\\ if array returned from this Module then event find error
\\ because group bb deleted.
A(1)=bb
push A()
}
Deep1
\\ now all modules and variables in Deep1 are destroyed
\\ except stack values. And in stack we have an array
Print StackType$(1) \\ print mArray - the inside object for arrays in M2000
Print Match("A") \\ Print -1 (True), is Array
Dim A()
\\ first we create the array and then we put the object on it
Read A()
Print "From exported array"
\\ need to restore weak reference
For A(0) {
\\ this block make A(0) static with a hidden name
\\ and this name used every time
Event .b Clear \\ clear all functions in event chain
\\ here we don't need to give a lazy$() function.
Event .b New .aa()
\\ function lazy$() return an anonymus function with lazy evaluation
\\ when this anonymus function run, set the module name to A
\\ so A(1) can be found, also we have to write the parameters to pass
Event .b New lazy$(A(1).bb(number))
\\ this happen because we can't get reference from array items.
}
\\ Now we Call a non static group
Print "From Module a"
A(0).callme 15
Module testme3 {
Read c$
Print "From testme3 inside Module a"
c$.callme
}
\\ Now we send it to module testme3 (module's deep1 modules are erased)
testme3 Weak$(A(0)), 15
\\ so now we have two objects, non static, but for this momemt they have
\\ one point static...the name of the array (A() is inside the module a)
\\ event b is calling from A(0) and opens A(1) reading variabe (property) y.
\\ Events may have by reference values as parameters and this works with multicast
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.