Learning
Community
Open Exchange
Global Masters
InterSystems IRIS Data Platform 2019.3 / Application Development / Using the Native API for Node.js / Calling ObjectScript Methods and Functions
Previous section   Next section

Calling ObjectScript Methods and Functions

This chapter describes methods of class Iris that allow an application to call class methods and functions from user defined ObjectScript classes and routines.

Class Method Calls

The following Native API methods call a specified ObjectScript class method. They take string arguments for className and methodName, plus an array containing 0 or more method arguments:
Trailing arguments may be omitted in argument lists, causing default values to be used for those arguments, either by passing fewer than the full number of arguments, or by passing null for trailing arguments. An exception will be thrown if a non-null argument is passed to the right of a null argument.
The code in this example calls class methods of several datatypes from an ObjectScript test class named User.NativeTest. (For a listing, see “Sample ObjectScript Methods and Functions” at the end of this chapter).
Calling class methods from ObjectScript class User.NativeTest
In this example, assume that native is an existing instance of class Iris, and is currently connected to the server.
  const className = 'User.NativeTest';
  let cmValue = "";

  cmValue = native.classMethodValue(className,'cmBoolean',false);
  console.log(className+'.cmBoolean() returned value: ' + cmValue);

  cmValue = native.classMethodValue(className,'cmBytes','byteArray');
  console.log(className+'.cmBytes() returned value: ' + cmValue);

  cmValue = native.classMethodValue(className,'cmString','Test String');
  console.log(className+'.cmString() returned value: ' + cmValue);

  cmValue = native.classMethodValue(className,'cmLong',7,8);
  console.log(className+'.cmLong() returned value: ' + cmValue);

  cmValue = native.classMethodValue(className,'cmDouble',7.56);
  console.log(className+'.cmDouble() returned value: ' + cmValue);

  try {
    native.classMethodVoid(className,'cmVoid',67);
    cmValue = native.get("p1");
  }
  catch {cmValue = 'method failed.'}
  console.log(className+'.cmVoid() set global array p1 to value: ' + cmValue);

Function Calls

The following Native API methods call user-defined ObjectScript functions or procedures (see “Callable User-defined Code Modules” in Using ObjectScript). They take string arguments for className and methodName, plus an array containing 0 or more method arguments:
  • Iris.function() — calls a user-defined function and gets the return value.
  • Iris.procedure() calls a user-defined procedure and discards any returned value.
Trailing arguments may be omitted in argument lists, causing default values to be used for those arguments, either by passing fewer than the full number of arguments, or by passing null for trailing arguments. An exception will be thrown if a non-null argument is passed to the right of a null argument.
The code in this example calls functions of several datatypes from an ObjectScript test routine named NativeRoutine. (For a listing of NativeRoutine.mac, see “Sample ObjectScript Methods and Functions” at the end of this chapter).
Calling functions from NativeRoutine.mac
In this example, assume that native is an existing instance of Iris, and is currently connected to the server.
  const routineName = 'NativeRoutine';
  let fnValue = "";

  fnValue = native.function('funcBoolean',routineName,false);
  console.log(routineName+'.funcBoolean() returned value: ' + fnValue);

  fnValue = native.function('funcBytes',routineName,'byteArray');
  console.log(routineName+'.funcBytes() returned value: ' + fnValue);

  fnValue = native.function("funcString",routineName,"Test String");
  console.log(routineName+'.funcString() returned value: ' + fnValue);

  fnValue = native.function('funcLong',routineName,7,8);
  console.log(routineName+'.funcLong() returned value: ' + fnValue);

  fnValue = native.function('funcDouble',routineName,7.56);
  console.log(routineName+'.funcDouble() returned value: ' + fnValue);

  try {
    native.procedure("funcProcedure",routineName,67);
    fnValue = native.get("p1");
  }
  catch {fnValue = 'procedure failed.'}
  console.log(routineName+'.funcVoid() set global array p1 to value: ' + fnValue);

Sample ObjectScript Methods and Functions

To run the examples, these ObjectScript class methods and functions must be compiled and available on the server:
ObjectScript Class User.NativeTest
Class User.NativeTest Extends %Persistent
{
  ClassMethod cmBoolean(p1 As %Boolean) As %Boolean
  {
   Quit 0
  }
  ClassMethod cmBytes(p1 As %String) As %Binary
  {
   Quit $C(65,66,67,68,69,70,71,72,73,74)
  }
  ClassMethod cmString(p1 As %String) As %String
  {
   Quit "Hello "_p1
  }
  ClassMethod cmLong(p1 As %Integer, p2 As %Integer) As %Integer
  {
   Quit p1+p2
  }
  ClassMethod cmDouble(p1 As %Double) As %Double
  {
   Quit p1 * 100
  }
  ClassMethod cmVoid(p1 As %Integer)
  {
   Set ^p1=p1
   Quit
  }
}
ObjectScript Routine NativeRoutine.mac
funcBoolean(p1) public {
    Quit 0
}
funcBytes(p1) public {
    Quit $C(65,66,67,68,69,70,71,72,73,74)
}
funcString(p1) public {
    Quit "Hello "_p1
}
funcLong(p1,p2) public {
    Quit p1+p2
}
funcDouble(p1) public {
    Quit p1 * 100
}
funcProcedure(p1) public {
    Set ^p1=p1
    Quit
}
Previous section   Next section