28

Interface

The functional interface of So-o in JavaScript consists of 3 functions: defclass which defines a new class, sendmsg which is systematically used to send a message to a class or an instance, and supersend which runs a method inherited from a superclass.

All the code for the So-o interface is in the file So-o.js.

  1. import 'Root';

Loading the code for So-o automatically includes the Root class.

defclass
SYNOPSIS

defclass(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages)

DESCRIPTION

defclass defines a class.

name specifies the name of the class. name must be a valid JavaScript variable name.

superclass is the global reference of the superclass of the new class. If superclass is null, the new class inherits by default from the Root class, Root.

revision gives the revision number of the class. revision is an integer > 0 which can be used to differentiate successive versions of the class.

classProperties and instanceProperties list the properties of the class and instances of the class. A property is a character string.

classMessages and instanceMessages lists of messages and methods of the class and instances of the class. A message is a character string. A method is the code of a function.

Each of the parameters classProperties, instanceProperties, classMessages and instanceMessages can be null.

defclass creates the global variable name. Notice that a class can be redefined.

A new class automatically receives the message initialize. NOTE: The initialize class method defined by the Root class does nothing.

EXAMPLE
  1. import { defclass } from 'So-o';

Imports the function defclass of So-o. The file So-o.js automatically includes the Root class.

  1. defclass('Hello', null, 1,
  2.     null,
  3.     null,
  4.     null,
  5.     {   'hello':    (self) => {
  6.                         console.log('Hello from So-o!');
  7.                        
  8.                         return self;
  9.                     }
  10.     }
  11. );

Defines the class Hello and associates it to the global variable Hello. The Hello class inherits by default from the Root class. Its revision number is 1. It doesn't have any class properties, class messages or instance properties. It has an instance message: hello.

The first argument of a method is always the class or the instance which receives the message. Conventionally, this argument is called self. A method which has nothing in particular to return generally returns self.

A few lines of code to illustrate the use of the Hello class:

  1. import { sendmsg } from 'So-o';
  2.  
  3. import 'Hello';
  4.  
  5. var hello = sendmsg(Hello, 'new');
  6.  
  7. sendmsg(hello, 'hello');

Imports the function sendmsg of So-o. Imports the class Hello. Sends the message new to the class Hello to create the instance hello assigning to it the value returned by sendmsg. Sends the message hello to the instance hello to display the welcome message.

$ ln Hello.js node_modules/Hello.mjs
$ ln testHello.js testHello.mjs
$ nodejs --experimental-modules testHello
Hello from So-o!
CODE
  1. import { Definition } from 'OL';

Imports the prototype Definiton of the Object Layer.

  1. export function defclass(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages) {

defclass takes 7 arguments. name specifies the name of the class. superclass is the global reference of the superclass of the new class. revision gives the revision number of the class. classProperties, instanceProperties, classMessages and instancMessages list the properties and the messages of the class and the instances of the class.

  1.     let c = new Definition(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages);

Creates an object of the type Definition defined by the Object Layer with the parameters of the call to defclass.

  1.     global[name] = c;

Assigns the class to a global variable whose name is the name of the class.

  1.     if ('Root' !== name)
  2.         sendmsg(c, 'initialize');

Sends the message initialize to the new class if it's not the Root class.

  1.     return c;
  2. }

Returns the class.

sendmsg
SYNOPSIS

sendmsg(rcv, msg[, arg ...])

DESCRIPTION

sendmsg returns the result of sending the message msg and its parameters arg to the instance or the class rcv.

CODE
  1. export function sendmsg(rcv, msg, ...args) {
  2.     return rcv.sendself(msg, args);
  3. }

sendmsg calls the function sendself of rcv with in argument msg and an array containing all the other arguments following msg.

supersend
SYNOPSIS

sendmsg(fc, rcv, msg[, arg ...])

DESCRIPTION

supersend returns the result of sending the message msg and its parameters to an instance or a class in the context of the superclass of the class fc.

supersend is generally used to call the inherited version of a method which is redefined by a subclass. In this case, c is the class of the method which redefines msg and calls supersend.

CODE
  1. export function supersend(fc, rcv, msg, ...args) {
  2.     return rcv.sendsuper(fc, msg, args);
  3. }

supersend calls the function sendsuper of rcv with in argument fc, msg and array containing all the arguments following msg.

EXAMPLE
  1. import { defclass, sendmsg, supersend } from 'So-o';

Imports the functions defclass, sendmsg and supersend from So-o.

  1. defclass('X', null, 1,
  2.         ['count'],
  3.         ['value'],

The X class inherits from the Root class, adds the class property count and the instance property value, redefines the class messages initialize and new, adds the class message count, redefines the instance messages init and free.

  1.         {   'new':  (self, ...args) => {
  2.                         let i = supersend(X, self, 'new', ...args);
  3.  
  4.                         sendmsg(self, 'set', 'count', sendmsg(self, 'get', 'count') + 1);
  5.  
  6.                         return i;
  7.                     },

new creates an instance of X by calling the method new inherited from the superclass of X with all the parameters of the call. NOTE: The new method of the Root class passes its arguments to the instance method init which is redefined by the X class.

  1.             'initialize':   (self) => sendmsg(self, 'set', 'count', 0),

initialize sets the class property count to 0. NOTE : initialize is called once automatically by defclass.

  1.             'count':    (self) => sendmsg(self, 'get', 'count')
  2.         },

count returns the valeur of the class property count.

  1.         {   'init': (self, value = 0) => {
  2.                 supersend(X, self, 'init');
  3.  
  4.                 sendmsg(self, 'set', 'value', value);
  5.  
  6.                 return self;
  7.             },

init calls the init method inherited from the superclass of X and initializes the instance property value.

  1.             'free': (self) => {
  2.                 let count = sendmsg(sendmsg(self, 'class'), 'get', 'count');
  3.  
  4.                 sendmsg(sendmsg(self, 'class'), 'set', 'count', count -1);
  5.  
  6.                 supersend(X, self, 'free');
  7.             }
  8.         }
  9. );

free decrements by 1 the counter of instances of the X class and calls the free method inherited from the superclass of X.

  1. import { sendmsg } from 'So-o';
  2.  
  3. import 'X';
  4.  
  5. console.log(sendmsg(X, 'count'));

Imports the function sendmsg from So-o. Imports the X class. Displays the instance counter of the X class.

  1. let x1 = sendmsg(X, 'new');
  2.  
  3. console.log(sendmsg(x1, 'get', 'value'));

Creates an instance of X with the default value. Displays the value of the instance.

  1. let x2 = sendmsg(X, 'new', 1);
  2.  
  3. console.log(sendmsg(x2, 'get', 'value'));

Creates an instance of X with the value 1. Displays the value of the instance.

  1. console.log(sendmsg(X, 'count'));

Displays the instance counter of the X class.

  1. sendmsg(x1, 'free');
  2.  
  3. x1 = undefined;
  4.  
  5. console.log(sendmsg(X, 'count'));

Frees an instance of X. Displays the instance counter of the X class.

$ ln X.js node_modules/X.mjs
$ ln testX.js test.mjs
$ nodejs --experimental-modules testX
0
0
1
2
1
SEE ALSO

Object Layer, Root

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].