Using External Code
From Morfikwiki.com
This text includes a general overview of how to use external code with Morfik AppsBuilder. The sections of the text that refer to importing JavaScript code apply equally to both Morfik Browser Application and Web Application projects, while the server side portions apply only to Morfik Web Application projects.
Morfik AppsBuilder was designed from the ground up to be a web application development tool which allows developers to write code in a high level language, regardless of whether the code is for the browser or server side of the application.
While writing a Morfik application, you might wish to use some code that has been written by someone else, in order to reduce the development effort. This can be JavaScript code for the browser side of your application or source/binary libraries (DLLs, SOs) for your server side.
During the application compile process, Morfik generates JavaScript code for the browser side of your application and intermediate source code for the server side. This intermediate code (which is in Object Pascal) is then compiled into the executable, through one of the supported platform compilers (pass 3 of the compile process).
In order to illustrate how to do this, I will present some code snippets from the System.mmd module of the Morfik Framework.
Contents |
[edit] Step 1 – Bringing your code in…
The first thing that a user needs to know when bringing external code into the Morfik high-level language universe is that it is a two step task. The first step is making the code you wish to bring in available at the JavaScript (browser side) or Intermediate Object Pascal (server side).
For the browser side, this is almost immediate. Just add the JavaScript code file to your application’s Resource Repository and you are ready to go to the next step. For server side code it is just as easy, but more options are available.
If you have a DLL (or shared object) that you wish to use from your Morfik application, you should write an Object Pascal import unit, which imports the functions you wish to have access to. If your code is already written in Object Pascal, you can bring it right in. All you need to do is to include the path to your source in the advanced configuration of the Project Options. It will work just as an import unit would, except that all code will be linked to the application, instead of depending on a DLL.
You might have to make small adjustments to your code, for compatibility, if it was written for Delphi. (This will not be necessary if you are using Delphi as your backend compiler.)
[edit] Step 2 – Making your code available to your application…
Once you have gone through the work in Step 1 and have made your JavaScript or your server side code accessible, you have to take an additional step to make the code available to the Morfik High-level language compiler.
In order to make the code available you have to create a module with the declaration of the functions or classes that you plan to use and mark them with the external metadata tag, informing where it is actually implemented.
For example, the FreeMem and GetMem functions in the System.mmd module are actually implemented in the SystemExt.pas unit. The following code informs the High-level compiler of that:
Function FreeMem(var P: Pointer; Size: Integer); ['External=SystemExt'];
Function GetMem (var P: Pointer; Size: Integer); ['External=SystemExt'];
The same principle applied to these functions can be applied to classes, as well. The following code is an example of how to do it:
Exception = Class(TException)
Private
FHelpContext: Integer;
Public
Constructor Create(Const Msg : String);
Constructor CreateFmt(Const Msg : String; Const Args : Array Of Const);
Constructor CreateRes(Ident : Integer);
Constructor CreateResFmt(Ident : Integer; Const Args : Array Of Const);
Constructor CreateHelp(Const Msg : String; AHelpContext : Integer);
Constructor CreateFmtHelp(Const Msg : String;
Const Args : Array Of Const;
AHelpContext: Integer);
Constructor CreateResHelp (Ident : Integer; AHelpContext: Integer);
Constructor CreateResFmtHelp(Ident : Integer;
Const Args : Array Of Const;
AHelpContext: Integer);
Property HelpContext : Integer Read FHelpContext Write FHelpContext;
Property Message : String Read Msg Write Msg;
End; ['External=SystemExt'];
These examples are of functions and classes which are made visible to all Morfik code through the System.mmd module, but which is actually implemented in the SystemExt.pas Object Pascal unit.
Of course, all of these examples are server side, but the browser side is even easier. On the Browser side you might be referencing some functionality which is made available by the browser itself, or by a JavaScript file. The two lines of code that follow show how to handle both situations, respectively.
Function IsModuleLoaded(m : String) : Boolean; ['External='];
Function getDomainSuffix(d : String) : String; ['External=brlJLib'];
The next code snippet is the declaration of the TXMLHTTPRequest class which is implemented within the browser itself.
TXMLHTTPRequest = Class
responseText : String;
responseXML : String;
onreadystatechange : Pointer;
readyState : Integer;
status : Integer;
wrapper: Pointer;
Function open(Method, Url : String; Async : Boolean;
User, Password : String);
Function send(Body : String);
Function setRequestHeader(HeaderName, HeaderValue : String);
Function abort;
End; ['External='];
As you can notice, whenever a function or class is implemented in the browser itself the right hand side of the equals sign for the External metadata tag is left empty.
[edit] Wrapping it up
Using code created with other tools in Morfik is not a hard task, if you are familiar with how to use those tools. Once this process is complete the functions or classes will be accessible to high-level code created in Morfik.
