Bridge Classes: TJSObject

Most developers will spend the majority of their time interfacing with a TJSObject. This object permits the developer to call script functions, create and access script variables, etc.

Properties
  • Connected: Boolean When true the object is connected to an instance of TJSEngine, permitting interaction with script code.
  • Destroying: Boolean Developers should not need to access this property. It is used when an instance of TJSEngine is shut down.
  • Engine: TJSEngine Use this property to access the engine to which the TJSObject is connected.
  • JScriptVal: jsval Only advanced developers should use this property. It can be used in SpiderMonkey callback functions, or API calls, for instance.
  • JSName: string This is the name associated with the variable in javascript.
  • JSObject: PJSObject Only advanced users should use this property. A developer can use this value when working with the SpiderMonkey API.
  • Parent: TJSObject The parent is the scope from which the current TJSObject is accessible. If no parent was assigned when the TJSObject was created it will default to TJSEngine.Global.
Methods
  • constructor Create(AValue: PJSObject; AEngine: TJSEngine; const AName: string); Creates a new TJSObject at the global scope named AName.
  • constructor Create(AValue: PJSObject; AEngine: TJSEngine; const AName: string; AParent: TJSObject); Creates a new TJSObject at the specified scope named AName.
  • function AddMethod(const name: String; proc: JSNative; paramcount: Integer): TJSFunction; Intended for more advanced users. Adds the function called name to the current TJSObject's scope. proc, of type JSNative, is the procedure that SpiderMonkey will call for this function, and paramcount is the number of parameters that the function expects to receive. The resulting TJSFunction is merely a wrapper for the method. Keep in mind the TJSFunction instance cannot be retrieved once it goes out of scope, although the javascript function remains declared.
  • function AddMethods(var methods: TJSFunctionSpecArray): Boolean; Intended for more advanced users. Adds the list of functions specified in the methods array. A TJSFunctionSpecArray is an array of JSFunctionSpec records.
  • function AddNativeObject(Obj: TObject; const InstanceName: String): TJSObject; Processes a native Delphi/Kylix object so that its published methods and properties are accessible to script by the name in InstanceName. The returned TJSObject is merely a wrapper. Note: The object's class must be compiled with {$M+} and {$M-} around it.
  • function AddObject(var cls: JSClass; const AName: string): TJSObject; Intended for more advanced users. A developer may declare a custom javascript object, with Delphi or Kylix hooks, as seen in the jssamples.pas file. The new javascript object is given the name in AName.
  • function AddProperties(var props: TJSPropertySpecArray): Boolean; Intended for more advanced users. Adds the list of properties specified in the props array. A TJSPropertySpecArray is an array of JSPropertySpec records.
  • function Call(const Func: string; params: array of TJSBase; var str: string): Boolean; function Call(const Func: string; params: array of TJSBase; var int: Integer): Boolean; function Call(const Func: string; params: array of TJSBase; var dbl: Double): Boolean; function Call(const Func: string; params: array of TJSBase; var res: TJSObject): Boolean; function Call(const Func: string; params: array of TJSBase; var bool: Boolean): Boolean; These methods call the javascript function Func with parameters params, and attempts to return a value into the third parameter. If the method returns false the call, or the return value conversion, failed.

    For example: eng := TJSEngine.Create(40000);
    try
      global := eng.Global;
      global.Evaluate('function addSix(p_int) { return p_int +6; }');
      myint := global.Declare(16); // declared anonymous since it is only a parameter
      if (global.Call('addSix', [myint], integerResult)) then
        ShowMessage('addSix returned ' +IntToStr(integerResult));
    finally
      eng.Free;
    end;

  • procedure Connect(AEngine: TJSEngine; AName: string; AParent: TJSObject); procedure Connect(AEngine: TJSEngine; AName: string); Connects the TJSObject to the engine. The first declaration will assign the javascript object to the specified scope, while the second will default the scope to global.
  • function Declare(val: Double; const name: string = ''): TJSDouble; function Declare(val: Integer; const name: string = ''): TJSInteger; function Declare(const val: string; const name: string = ''): TJSString; function Declare(var val: TJSBaseArray; const name: string = ''): TJSArray; function Declare(val: Boolean; const name: string = ''): TJSBoolean; These methods create bridge variables of the specified type. Declaring a variable without a name creates an anonymous variable. For all other purposes provide a name for the variable as well.
  • function DeclareObject(const name: string): TJSObject; This method creates a new javascript object with the name name underneath the current object's scope, and returns a TJSObject wrapper for that object.
  • function Enumerate: TStringArray; This method returns a list of all of the properties and methods that are enumerable for the current object. See the jssamples.pas file for an example of how to use this method.
  • function Evaluate(const code: string): Boolean; This method runs the javascript in code within the scope of the current object. This method returns true if the execution completed successfully.
  • function GetFunction(const name: String): TJSFunction; This method is not yet finished. When finished it will allow developers to recover TJSFunction instances when they go out of scope, where now they cannot. Note that the javascript function will continue to execute even after a TJSFunction has gone out of scope.
  • function GetMethodResult(const name: TBridgeString): TResultType; Developers should not need to use this method at all. It is used by callbacks in jsintf_bridge.pas.
  • function GetNativeProperty(Obj: TObject; AName: PChar): jsval; Developers should not need to use this method at all. It is used by callbacks in jsintf_bridge.pas.
  • function GetParamCount(const name: TBridgeString): Integer; Developers should not need to use this method at all. It is used by callbacks in jsintf_bridge.pas.
  • function GetProperty(const name: string; var dbl: Double): Boolean; function GetProperty(const name: string; var int: Integer): Boolean; function GetProperty(const name: string; var ret: TJSObject): Boolean; function GetProperty(const name: string; var str: string): Boolean; function GetProperty(const name: string; var bool: Boolean): Boolean; These methods permit developers to retrieve any property of the current object, provided the type is correct. If the property does not exist, or the property type conversion fails, the method will return false.
  • function HasMethodInfo(const name: TBridgeString): Boolean; Developers should not need to use this method at all. It is used by callbacks in jsintf_bridge.pas.
  • function HasProperty(const name: String): Boolean; This method returns true if the current object has the property called name.
  • function IsFunction(const name: String): Boolean; This method allows developers to determine if the property called name is a function.
  • function IsInteger(const name: String): Boolean; This method allows developers to determine whether the property called name is an integer or a floating point value. (Usually used in conjunction with TJSObject.TypeOf.)
  • procedure RemoveObject(Obj: TJSBase); This method deletes the specified property from the current object, and frees the instance of Obj.
  • procedure SetMethodInfo(const name: TBridgeString; ParamCount: Integer; ResultType: TResultType); Allows developers to set the number of parameters and the return type, if any, for the method name. Note: This method must be called for each function callable by javascript.
  • function SetNativeProperty(Obj: TObject; AName, AValue: PChar): JSBool; Developers should not need to use this method at all. It is used by callbacks in jsintf_bridge.pas.
  • function SetProperty(const name: string; val: TJSBase): Boolean; This method sets the property specified by name to the value specified in val. Returns true if successful, false otherwise.
  • function TypeOf(const name: string): JSType; This method returns a JSType describing the property's data type.