Delphi
| projects: | Home Articles JavaScript Bridge |
|---|---|
| resources: | Bugs Mailing List Source Code Sidebar |
Bridge Classes: TJSEngine
All script interactivity begins with this component and all of the other bridge classes depend
on this one. If your application is multithreaded, you'll create a new instance of TJSEngine
for each thread.
Properties
- Context: PJSContext
Only advanced users should use this property in their code. A developer can use this value when working with the SpiderMonkey API. - Global: TJSObject
This property grants access to the global scope when interfacing with script. From here, users can create variables, call functions, etc, which are accessible to the global scope.
Methods
- constructor Create(MaxMemory: Cardinal);
The one parameter to the constructor is a maximum-memory limit. In other words, the amount of memory used by your JavaScript will never exceed this number. Note that it is not allocated all at once. It is simply the point where SpiderMonkey will forcibly deallocate memory. - function Declare(val: Integer; const name: string = ''): TJSInteger;
function Declare(var val: TJSBaseArray; const name: string = ''): TJSArray;
function Declare(val: Double; const name: string = ''): TJSDouble;
function Declare(const val: string; const name: string = ''): TJSString;
function Declare(val: Boolean; const name: string = ''): TJSBoolean;
These methods create bridge variables A bridge variable is one that can be modified by both Delphi / Kylix code and script code. Any change made in one will be reflected in the other. of the specified type. Declaring a variable without anamecreates an anonymous variableAnonymous variables do not have names, and are only usable as parameters when calling a JavaScript function. Assign the variable a name to use it within script code. (See theCallmethod of TJSObject.). For all other purposes provide a name for the variable as well. - function Evaluate(const code: string; scope: TJSObject): Boolean;
This method runs the JavaScript incodewithin the scope of the provided object. Ifscopeis nil, the code will run within theTJSEngine.Globalscope. This method returnstrueif the execution completed successfully. - function Evaluate(const code: string): Boolean;
This method runs the JavaScript incodewithin theTJSEngine.Globalscope. This method returnstrueif the execution completed successfully. - procedure GarbageCollect;
While garbage collecting works, it is not necessary except under very specific conditions. You shouldn't need to call this method at any time. - function IsExceptionRaised: Boolean;
This is more of a token method. This was only necessary before I added custom error reporters. When an exception is raised, this function will return true. However, the error reporter will get those details long before you think of checking this function. - function IsValidCode(const code: String): Boolean;
Returns true if the JavaScript incodewill compile. As far as I know, this method does not perform any error checking. - function NewJSObject: TJSObject;
Creates an emptyTJSObjectat the global scope. The resulting object does not have a name until you assign one via theNameproperty. In the meantime it is an anonymous variableAnonymous variables do not have names, and are only usable as parameters when calling a JavaScript function. Assign the variable a name to use it within script code. (See theCallmethod of TJSObject.). - function NewJSObject(const name: string): TJSObject;
This creates an empty, namedTJSObjectat the global scope. Properties and methods can be added to this object from either script or Delphi / Kylix. - function NewJSObject(const name: string; parent: TJSObject): TJSObject;
This creates an empty, namedTJSObjectat the scope specified inparent. Properties and methods can be added to this object from either script or Delphi / Kylix. - procedure SetErrorReporter(proc: JSErrorReporter);
The definition of this method will change by the next release to accept aTJSErrorReporter, which will wrap error notifications in an object. You can find a default error reporter calledIntfBridge_ErrorReporterinjsintf_bridge.pas.