SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
PascalScript
Development
Goal: Build a script in your App to be more
"other language friendly" and less IDE or
platform dependent (interpreter).


                                              1
What’s a Script Engine ?

  Why use a scripting engine?

  A scripting engine allows an end user to customize an
  application to his or her needs without having to
  recompile it. In addition, you can update your
  applications by just sending a new script file that could
  even be compiled to byte code, which cannot easily be
  transformed back to source code.
  Advantages of Scripting:
  scripting is specific to runtime
  testing or simulate becomes more plan able
  no installation, setup's or configuration needed
  scalable in product, e.g. a light- or professional version
                                                               2
How it works ?

  procedure ExecuteScript(const Script: string);
  var
    Compiler: TPSPascalCompiler;
  { TPSPascalCompiler is the compiler part of the
  scriptengine. This translates a (macro stack machine)
  script into a compiled form the executer understands. }

  Exec: TPSExec; //Runtimer
  { TPSExec is the executer part of the scriptengine. It
  uses the output of the compiler to run a script. }
                                                            3
PAC - Implementation




                       4
Data types

# Variables, Constants
# Standard language constructs:
# Functions inside the script
# Calling any external DLL function (no special function
                                         headers required)
# Calling registered external methods
# All common types like Byte, Shortint, Char, Word, SmallInt,
  Cardinal, Longint, Integer, String, Real, Double, Single,
  Extended, Boolean, Array, Record, Enumerations,
  Variants
                                                            5
Minimal Class of Compiler / Executer
type
  TPSCE = class
  protected
   FScr: TPSScript;
   procedure SaveCompiled(var Data: String);
   procedure SaveDissasembly(var Data: String);
   procedure OnCompile(Sender: TPSScript);
   procedure OnExecImport(Sender: TObject; se: TPSExec; x:
                                     TPSRuntimeClassImporter);
  public
   constructor Create;
   function Compile(const FileName: string): Boolean;
   function Execute: Boolean;
  end;
                                                                 6
Pre-processor included

 The $I parameter directive instructs the compiler to include the named
 file in the compilation. In effect, the file is inserted in the compiled text
 right after the {$I filename} directive.
 type
   TPSPreProcessor = class;
   TPSPascalPreProcessorParser = class;
   {Event}
   TPSOnNeedFile = function (Sender: TPSPreProcessor; const
       callingfilename: string; var FileName, Output: string): Boolean;
   { Line info structure (internal debug info)
 To specify a filename that includes a space, surround the file name
 with single quotation marks: {$I 'My file'}.
 Ex.: maXbox pascalscript .inc

                                                                                 7
The Executer

  CI: TPSRuntimeClassImporter;
  RuntimeClass: TPSRuntimeClass;
  ftest: TMyTestObject;
  begin
  Compiler:= TPSPascalCompiler.Create;
  // create an instance of the compiler.
  Compiler.OnUses:= ScriptOnUses;
  // assign the OnUses event.
  if not Compiler.Compile(Script) then
  // Compile the Pascal script into bytecode.
  Compiler.Free;
                                                8
Or not going…

  if not Exec.LoadData(Data) then
  // Load the data from the Data string.
  { For some reason the script could not be loaded. This is
  usually the case when a library that has been used at
  compile time isn't registered at runtime. }
  Exec.Free; // You could raise an exception here.
  Exit; end;
  Exec.RunScript; // Run the script.
  Exec.Free; // Free the executer.
  end;
                                                              9
…to The Debugger (into the night)

  Debug Data:
   #0+
  Proc0Name+#1+Proc1Name+#1+Proc2Name+#1#0

   #1+ Var0Name+#1+Var1Name+#1+Var2Name+#1#0

   #2+ MI2S(FuncNo)+
  Param0Name+#1+Param0Name+#1#0

   #3+ MI2S(FuncNo)+ Var1Name+#1+Var1Name+#1#0

   #4+ FileName + #1 + MI2S(FuncNo)+ MI2S(Pos)+
   MI2s(Position)+MI2S(Row)+MI2s(Col)
                                                   10
The Byte Code

 Bytecode Format:
  Address Space
  0..GlobalVarCount -1 = GlobalVars
  IFPSAddrStackStart div 2 .. IFPSAddrStackStart -1 = neg. stack
  IFPSAddrStackStart... = positive stack
  TPSVariable = packed record
  TPSHeader = packed record
  TPSAttributes = packed record, TPSAttribute = packed record
      TPSType = packed record
      TPSProc = packed record
      TPSVar = packed record
                                                                11
The call of a procedure

  <i> Flags:
   1 = Imported; (nameLen: Byte; Name:
  array[0..namelen-1] of byte) else (Offset, Length:
  Longint);
   2 = Export; (only for internal procs); Name, Decl:
  MyString;
   3 = Imported2; nameLen: Byte; Name:
  array[0..namelen-1] of byte; ParamsLength: Longint;
  Params: array[0..paramslength-1] of byte;
   4 = With attributes (attr: TPSAttributes)
                                                        12
Your own Functions

  Exec.RegisterDelphiFunction(@MyOwnFunction,
  'MYOWNFUNCTION', cdRegister);
  { This will register the function to the executer. The first
  parameter is a pointer to the function. The second
  parameter is the name of the function (in uppercase).
  And the last parameter is the calling convention (usually
  Register). }


  Demo and Example: maxboxkonsole.dpr
                                                                 13
Your own Classes

Exec:= TPSExec.Create; // an instance of the executer.
CI:= PSRuntimeClassImporter.CreateAndRegister(Exec,
false); RIRegisterTObject(CI);
RuntimeClass:= CI.Add(TMyTestObject);
RuntimeClass.RegisterConstructor(@TMyTestObject.Create,
'Create');
RuntimeClass.RegisterMethod(@TMyTestObject.Print,'Print');
ftest:= TMyTestObject.Create();
SetVariantToClass(Exec.GetVarNo(Exec.GetVar('T')), fTest);
Demo of BinImportTool PSUnitImporter.exe
                                                         14
PS Multi Environment

                                                compiler
                           DLL                          Other Platform




                                Callings
                                                  iv es
                                                ct
                                           dire

                                              imports
                           PS                           Include/Units

             atic
           st         ic
                    m
                 na
  maXbox      dy
                      Advantage: want the biggest amount of
                      people being able to use scripts

                                                                         15
This is maXbox




 softwareschule.ch/maxbox.htm   or Delphi in a Box…
                                                  16
GUI




      17
MP3 example I

In EXE:
procedure playMP3(mp3path: string);
begin
mciSendString(PChar('open "' + mp3path + '" alias MP3'),
   NIL, 0, 0);
 //player.handle
 mciSendString('play MP3', NIL, 0, 0);
end;

In Script:
   playMP3(maXboxpath+'examplesmaxbox.mp3');

  Demo and Example: 109_pas_mp3_download.txt
                                                           18
Import functions with DLL’s

Import Units wraps the functions of the API in DLL’s

const
 {$EXTERNALSYM SC_SCREENSAVE}
 SC_SCREENSAVE = $F140;

 mmsyst = 'winmm.dll';

implementation

function auxGetDevCaps; external mmsyst name 'auxGetDevCapsA';
function auxGetNumDevs; external mmsyst name 'auxGetNumDevs';
function auxOutMessage; external mmsyst name 'auxOutMessage';
function CloseDriver; external mmsyst name 'CloseDriver';
                                                                 19
As Testing Tool example II


 Procedure ShuffleList(var vQ: TStringList);
 var j, k: integer;
    tmp: String;
 begin
   randomize;
   for j:= vQ.count -1 downto 0 do begin
     k:= Random(j+1);
     tmp:= (vQ[j]);
     vQ[j]:= vQ[k];
     vQ[k]:= tmp;
   end;
 end;
                                               20
Source Code Versioning

Author: ck
  Date: 2010-05-18 10:14:09 +0200 (Tue, 18 May 2010)
  New Revision: 224
  Modified:
  Source/uPSComponent.pas
  Source/uPSRuntime.pas
  Log:
  0: memory leak
  Modified: Source/uPSComponent.pas
  ===================================================
  ================
  --- Source/uPSComponent.pas 2010-05-11 15:08:04 UTC (rev
  223)
  +++ Source/uPSComponent.pas 2010-05-18 08:14:09 UTC (rev
  224)
  @@ -204,6 +204,8 @@

                                                         21
Some Notes 1


# For some functions / constants, it might be necessary to
   add: uPSCompiler.pas, uPSRuntime.pas and/or
   uPSUtils.pas to your uses list.
# It's possible to import your own classes in the script
   engine. PascalScript includes a tool to create import
   libraries in the Bin directory.
# For examples on how to use the compiler and runtime
   separately, see the Import samples.
# The Debug requires SynEdit
   http://synedit.sourceforge.net/.
                                                             22
PS License 2 with FPC Interop


PascalScript 3 is free and comes with source code.

You can also access the latest version of PS directly in our
   open-source SVN version control system, at
   code.remobjects.com. Supported Tools and Platforms:
PascalScript supports Delphi 4 through 7 and Delphi 2006
   through 2009, as well as the latest Free Pascal 2.x.

Supported Platforms are Win32 Ansi and Unicode
   (Delphi + FPC), Win64 (FPC), Linux x86 (FPC
   or CLX) and Linux x64 (FPC).
                                                               23
Call from various Clients 3

begin
   {$IFDEF LINUX}
   dllhandle:= dlopen(PChar(s2), RTLD_LAZY);
   {$ELSE}
   dllhandle:= LoadLibrary(Pchar(s2));
   {$ENDIF}
   if dllhandle = {$IFDEF LINUX} NIL {$ELSE} 0 {$ENDIF} then

{$IFDEF LINUX}
 p.Ext1:= dlsym(dllhandle, pchar(copy(s, 1, pos(#0, s)-1)));
 {$ELSE}
 p.Ext1:= GetProcAddress(dllhandle, pchar(copy(s, 1, pos(#0, s)-1)));
 {$ENDIF}

                                                                        24
Conclusion

 PascalScript is for Delphi /.NET

 PascalScript is a free scripting engine that allows
 you to use most of the Object Pascal language
 within your Delphi or Free Pascal projects at
 runtime.
 Written completely in Delphi, it is composed of a
 set of units that can be compiled into your apps,
 eliminating the need to distribute any external files
 or components.
                                                    25
Links and Tools on board

  softwareschule.ch/maxbox.htm
  http://delphi-jedi.org/
  http://www.remobjects.com/ps.aspx
  http://sourceforge.net/projects/maxbox
  http://sourceforge.net/apps/mediawiki/maxbox/

  {SAFECODE ON};)
  DCC32 compiler with JHPNE as option will generate C++ headers (with
                               .hpp extension) for your units!
  DCC32 -JHPHN -N D:DEVPPT –O D:DEVPPT –U
         D:COMPONENTSSC2_2securePtBase.pas

                                                                        26
Questions and hope answers ?
     max@kleiner.com




                               27

Contenu connexe

Tendances

Algoritmi e Calcolo Parallelo 2012/2013 - OpenMP
Algoritmi e Calcolo Parallelo 2012/2013 - OpenMPAlgoritmi e Calcolo Parallelo 2012/2013 - OpenMP
Algoritmi e Calcolo Parallelo 2012/2013 - OpenMPPier Luca Lanzi
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaEdureka!
 
Sour Pickles
Sour PicklesSour Pickles
Sour PicklesSensePost
 
Socket programming
Socket programmingSocket programming
Socket programmingNemiRathore
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using PythonKarim Sonbol
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,AAlha PaiKra
 
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...Flink Forward
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersAlessandro Sanino
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snakeSławomir Zborowski
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)Douglas Chen
 
NativeBoost
NativeBoostNativeBoost
NativeBoostESUG
 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-pythonYuvaraja Ravi
 
Glow user review
Glow user reviewGlow user review
Glow user review冠旭 陳
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsDavid Beazley (Dabeaz LLC)
 
Twisted logic
Twisted logicTwisted logic
Twisted logicashfall
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialJustin Lin
 

Tendances (20)

Algoritmi e Calcolo Parallelo 2012/2013 - OpenMP
Algoritmi e Calcolo Parallelo 2012/2013 - OpenMPAlgoritmi e Calcolo Parallelo 2012/2013 - OpenMP
Algoritmi e Calcolo Parallelo 2012/2013 - OpenMP
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
Sour Pickles
Sour PicklesSour Pickles
Sour Pickles
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
Easy native wrappers with SWIG
Easy native wrappers with SWIGEasy native wrappers with SWIG
Easy native wrappers with SWIG
 
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
Flink Forward San Francisco 2019: Deploying ONNX models on Flink - Isaac Mcki...
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snake
 
C - ISRO
C - ISROC - ISRO
C - ISRO
 
不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)不深不淺,帶你認識 LLVM (Found LLVM in your life)
不深不淺,帶你認識 LLVM (Found LLVM in your life)
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-python
 
Glow user review
Glow user reviewGlow user review
Glow user review
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
Twisted logic
Twisted logicTwisted logic
Twisted logic
 
PyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 TutorialPyCon Taiwan 2013 Tutorial
PyCon Taiwan 2013 Tutorial
 
Building Netty Servers
Building Netty ServersBuilding Netty Servers
Building Netty Servers
 

Similaire à Build a more platform-independent script with PascalScript

maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingMax Kleiner
 
Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Vincenzo Iozzo
 
Dive into exploit development
Dive into exploit developmentDive into exploit development
Dive into exploit developmentPayampardaz
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisFastly
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...Andrey Karpov
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentOOO "Program Verification Systems"
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPIyaman dua
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通炜龙 何
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdbRoman Podoliaka
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DMithun Hunsur
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docxwkyra78
 

Similaire à Build a more platform-independent script with PascalScript (20)

maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage coding
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
MPI
MPIMPI
MPI
 
Dive into exploit development
Dive into exploit developmentDive into exploit development
Dive into exploit development
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
srgoc
srgocsrgoc
srgoc
 
PVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications developmentPVS-Studio, a solution for resource intensive applications development
PVS-Studio, a solution for resource intensive applications development
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPI
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通
 
Debugging Python with gdb
Debugging Python with gdbDebugging Python with gdb
Debugging Python with gdb
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in D
 
Os lab final
Os lab finalOs lab final
Os lab final
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 

Plus de Max Kleiner

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfMax Kleiner
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfMax Kleiner
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementMax Kleiner
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87Max Kleiner
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapMax Kleiner
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detectionMax Kleiner
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationMax Kleiner
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_editionMax Kleiner
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP Max Kleiner
 
EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures CodingMax Kleiner
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70Max Kleiner
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIIMax Kleiner
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VIMax Kleiner
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning VMax Kleiner
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3Max Kleiner
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsMax Kleiner
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Max Kleiner
 
EKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningEKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningMax Kleiner
 
TensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningTensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningMax Kleiner
 

Plus de Max Kleiner (20)

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdf
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdf
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_Implement
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmap
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detection
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data Visualisation
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_edition
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP
 
EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures Coding
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning V
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_Diagrams
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2
 
EKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningEKON22 Introduction to Machinelearning
EKON22 Introduction to Machinelearning
 
TensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningTensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 Machinelearning
 

Build a more platform-independent script with PascalScript

  • 1. PascalScript Development Goal: Build a script in your App to be more "other language friendly" and less IDE or platform dependent (interpreter). 1
  • 2. What’s a Script Engine ? Why use a scripting engine? A scripting engine allows an end user to customize an application to his or her needs without having to recompile it. In addition, you can update your applications by just sending a new script file that could even be compiled to byte code, which cannot easily be transformed back to source code. Advantages of Scripting: scripting is specific to runtime testing or simulate becomes more plan able no installation, setup's or configuration needed scalable in product, e.g. a light- or professional version 2
  • 3. How it works ? procedure ExecuteScript(const Script: string); var Compiler: TPSPascalCompiler; { TPSPascalCompiler is the compiler part of the scriptengine. This translates a (macro stack machine) script into a compiled form the executer understands. } Exec: TPSExec; //Runtimer { TPSExec is the executer part of the scriptengine. It uses the output of the compiler to run a script. } 3
  • 5. Data types # Variables, Constants # Standard language constructs: # Functions inside the script # Calling any external DLL function (no special function headers required) # Calling registered external methods # All common types like Byte, Shortint, Char, Word, SmallInt, Cardinal, Longint, Integer, String, Real, Double, Single, Extended, Boolean, Array, Record, Enumerations, Variants 5
  • 6. Minimal Class of Compiler / Executer type TPSCE = class protected FScr: TPSScript; procedure SaveCompiled(var Data: String); procedure SaveDissasembly(var Data: String); procedure OnCompile(Sender: TPSScript); procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter); public constructor Create; function Compile(const FileName: string): Boolean; function Execute: Boolean; end; 6
  • 7. Pre-processor included The $I parameter directive instructs the compiler to include the named file in the compilation. In effect, the file is inserted in the compiled text right after the {$I filename} directive. type TPSPreProcessor = class; TPSPascalPreProcessorParser = class; {Event} TPSOnNeedFile = function (Sender: TPSPreProcessor; const callingfilename: string; var FileName, Output: string): Boolean; { Line info structure (internal debug info) To specify a filename that includes a space, surround the file name with single quotation marks: {$I 'My file'}. Ex.: maXbox pascalscript .inc 7
  • 8. The Executer CI: TPSRuntimeClassImporter; RuntimeClass: TPSRuntimeClass; ftest: TMyTestObject; begin Compiler:= TPSPascalCompiler.Create; // create an instance of the compiler. Compiler.OnUses:= ScriptOnUses; // assign the OnUses event. if not Compiler.Compile(Script) then // Compile the Pascal script into bytecode. Compiler.Free; 8
  • 9. Or not going… if not Exec.LoadData(Data) then // Load the data from the Data string. { For some reason the script could not be loaded. This is usually the case when a library that has been used at compile time isn't registered at runtime. } Exec.Free; // You could raise an exception here. Exit; end; Exec.RunScript; // Run the script. Exec.Free; // Free the executer. end; 9
  • 10. …to The Debugger (into the night) Debug Data: #0+ Proc0Name+#1+Proc1Name+#1+Proc2Name+#1#0 #1+ Var0Name+#1+Var1Name+#1+Var2Name+#1#0 #2+ MI2S(FuncNo)+ Param0Name+#1+Param0Name+#1#0 #3+ MI2S(FuncNo)+ Var1Name+#1+Var1Name+#1#0 #4+ FileName + #1 + MI2S(FuncNo)+ MI2S(Pos)+ MI2s(Position)+MI2S(Row)+MI2s(Col) 10
  • 11. The Byte Code Bytecode Format: Address Space 0..GlobalVarCount -1 = GlobalVars IFPSAddrStackStart div 2 .. IFPSAddrStackStart -1 = neg. stack IFPSAddrStackStart... = positive stack TPSVariable = packed record TPSHeader = packed record TPSAttributes = packed record, TPSAttribute = packed record TPSType = packed record TPSProc = packed record TPSVar = packed record 11
  • 12. The call of a procedure <i> Flags: 1 = Imported; (nameLen: Byte; Name: array[0..namelen-1] of byte) else (Offset, Length: Longint); 2 = Export; (only for internal procs); Name, Decl: MyString; 3 = Imported2; nameLen: Byte; Name: array[0..namelen-1] of byte; ParamsLength: Longint; Params: array[0..paramslength-1] of byte; 4 = With attributes (attr: TPSAttributes) 12
  • 13. Your own Functions Exec.RegisterDelphiFunction(@MyOwnFunction, 'MYOWNFUNCTION', cdRegister); { This will register the function to the executer. The first parameter is a pointer to the function. The second parameter is the name of the function (in uppercase). And the last parameter is the calling convention (usually Register). } Demo and Example: maxboxkonsole.dpr 13
  • 14. Your own Classes Exec:= TPSExec.Create; // an instance of the executer. CI:= PSRuntimeClassImporter.CreateAndRegister(Exec, false); RIRegisterTObject(CI); RuntimeClass:= CI.Add(TMyTestObject); RuntimeClass.RegisterConstructor(@TMyTestObject.Create, 'Create'); RuntimeClass.RegisterMethod(@TMyTestObject.Print,'Print'); ftest:= TMyTestObject.Create(); SetVariantToClass(Exec.GetVarNo(Exec.GetVar('T')), fTest); Demo of BinImportTool PSUnitImporter.exe 14
  • 15. PS Multi Environment compiler DLL Other Platform Callings iv es ct dire imports PS Include/Units atic st ic m na maXbox dy Advantage: want the biggest amount of people being able to use scripts 15
  • 16. This is maXbox softwareschule.ch/maxbox.htm or Delphi in a Box… 16
  • 17. GUI 17
  • 18. MP3 example I In EXE: procedure playMP3(mp3path: string); begin mciSendString(PChar('open "' + mp3path + '" alias MP3'), NIL, 0, 0); //player.handle mciSendString('play MP3', NIL, 0, 0); end; In Script: playMP3(maXboxpath+'examplesmaxbox.mp3'); Demo and Example: 109_pas_mp3_download.txt 18
  • 19. Import functions with DLL’s Import Units wraps the functions of the API in DLL’s const {$EXTERNALSYM SC_SCREENSAVE} SC_SCREENSAVE = $F140; mmsyst = 'winmm.dll'; implementation function auxGetDevCaps; external mmsyst name 'auxGetDevCapsA'; function auxGetNumDevs; external mmsyst name 'auxGetNumDevs'; function auxOutMessage; external mmsyst name 'auxOutMessage'; function CloseDriver; external mmsyst name 'CloseDriver'; 19
  • 20. As Testing Tool example II Procedure ShuffleList(var vQ: TStringList); var j, k: integer; tmp: String; begin randomize; for j:= vQ.count -1 downto 0 do begin k:= Random(j+1); tmp:= (vQ[j]); vQ[j]:= vQ[k]; vQ[k]:= tmp; end; end; 20
  • 21. Source Code Versioning Author: ck Date: 2010-05-18 10:14:09 +0200 (Tue, 18 May 2010) New Revision: 224 Modified: Source/uPSComponent.pas Source/uPSRuntime.pas Log: 0: memory leak Modified: Source/uPSComponent.pas =================================================== ================ --- Source/uPSComponent.pas 2010-05-11 15:08:04 UTC (rev 223) +++ Source/uPSComponent.pas 2010-05-18 08:14:09 UTC (rev 224) @@ -204,6 +204,8 @@ 21
  • 22. Some Notes 1 # For some functions / constants, it might be necessary to add: uPSCompiler.pas, uPSRuntime.pas and/or uPSUtils.pas to your uses list. # It's possible to import your own classes in the script engine. PascalScript includes a tool to create import libraries in the Bin directory. # For examples on how to use the compiler and runtime separately, see the Import samples. # The Debug requires SynEdit http://synedit.sourceforge.net/. 22
  • 23. PS License 2 with FPC Interop PascalScript 3 is free and comes with source code. You can also access the latest version of PS directly in our open-source SVN version control system, at code.remobjects.com. Supported Tools and Platforms: PascalScript supports Delphi 4 through 7 and Delphi 2006 through 2009, as well as the latest Free Pascal 2.x. Supported Platforms are Win32 Ansi and Unicode (Delphi + FPC), Win64 (FPC), Linux x86 (FPC or CLX) and Linux x64 (FPC). 23
  • 24. Call from various Clients 3 begin {$IFDEF LINUX} dllhandle:= dlopen(PChar(s2), RTLD_LAZY); {$ELSE} dllhandle:= LoadLibrary(Pchar(s2)); {$ENDIF} if dllhandle = {$IFDEF LINUX} NIL {$ELSE} 0 {$ENDIF} then {$IFDEF LINUX} p.Ext1:= dlsym(dllhandle, pchar(copy(s, 1, pos(#0, s)-1))); {$ELSE} p.Ext1:= GetProcAddress(dllhandle, pchar(copy(s, 1, pos(#0, s)-1))); {$ENDIF} 24
  • 25. Conclusion PascalScript is for Delphi /.NET PascalScript is a free scripting engine that allows you to use most of the Object Pascal language within your Delphi or Free Pascal projects at runtime. Written completely in Delphi, it is composed of a set of units that can be compiled into your apps, eliminating the need to distribute any external files or components. 25
  • 26. Links and Tools on board softwareschule.ch/maxbox.htm http://delphi-jedi.org/ http://www.remobjects.com/ps.aspx http://sourceforge.net/projects/maxbox http://sourceforge.net/apps/mediawiki/maxbox/ {SAFECODE ON};) DCC32 compiler with JHPNE as option will generate C++ headers (with .hpp extension) for your units! DCC32 -JHPHN -N D:DEVPPT –O D:DEVPPT –U D:COMPONENTSSC2_2securePtBase.pas 26
  • 27. Questions and hope answers ? max@kleiner.com 27