Skip to main content

Calling ObjectScript Methods and Functions

This chapter describes methods of class iris that allow you to call ObjectScript class methods and functions directly from your JavaScript application. See the following sections for details and examples:

  • Class Method Calls — demonstrates how to call ObjectScript class methods.

  • Function Calls — demonstrates how to call ObjectScript functions and procedures.

Class Method Calls

The following Native SDK 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 (listed immediately after this example).

JavaScript calls to ObjectScript class methods

In this example, assume that variable irisjs is a previously defined instance of class Iris and is currently connected to the server.

  const className = 'User.NativeTest';
  let cmValue = "";
  let comment = "";

  comment = ".cmBoolean() tests whether arguments 2 and 3 are equal: "
  cmValue = irisjs.classMethodValue(className,'cmBoolean',2,3);
  console.log(className + comment + cmValue);

  comment = ".cmBytes() returns integer arguments 72,105,33 as a byte array (string value 'Hi!'): "
  cmValue = irisjs.classMethodValue(className,'cmBytes',72,105,33); //ASCII 'Hi!'
  console.log(className + comment + cmValue);

  comment = ".cmString() concatenates 'Hello' with argument string 'World': "
  cmValue = irisjs.classMethodValue(className,'cmString','World');
  console.log(className + comment + cmValue);

  comment = ".cmLong() returns the sum of arguments 7+8: "
  cmValue = irisjs.classMethodValue(className,'cmLong',7,8);
  console.log(className + comment + cmValue);

  comment = ".cmDouble() multiplies argument 4.5 by 1.5: "
  cmValue = irisjs.classMethodValue(className,'cmDouble',4.5);
  console.log(className + comment + cmValue);

  comment = ".cmList() returns a $LIST containing arguments 'The answer is ' and 42: "
  cmValue = irisjs.classMethodIRISList(className,"cmList","The answer is ",42);
  console.log(className + comment+cmValue.get(1)+cmValue.get(2) )

  comment = ".cmVoid() assigns argument value 75 to global node ^cmGlobal: "
  try {
    irisjs.kill('cmGlobal') // delete ^cmGlobal if it exists
    irisjs.classMethodVoid(className,'cmVoid',75);
    cmValue = irisjs.get("cmGlobal");  //get current value of ^cmGlobal
  }
  catch {cmValue = 'method failed.'}
  console.log(className + comment + cmValue);
ObjectScript Class User.NativeTest
Class User.NativeTest Extends %Persistent
  {

  ClassMethod cmBoolean(cm1 As %Integer, cm2 As %Integer) As %Boolean
  {
     Quit (cm1=cm2)
  }

  ClassMethod cmBytes(cm1 As %Integer, cm2 As %Integer, cm3 As %Integer) As %Binary
  {
     Quit $CHAR(cm1,cm2,cm3)
  }

  ClassMethod cmString(cm1 As %String) As %String
  {
     Quit "Hello "_cm1
  }

  ClassMethod cmLong(cm1 As %Integer, cm2 As %Integer) As %Integer
  {
     Quit cm1+cm2
  }

  ClassMethod cmDouble(cm1 As %Double) As %Double
  {
     Quit cm1 * 1.5
  }

  ClassMethod cmVoid(cm1 As %Integer)
  {
     Set ^cmGlobal=cm1
     Quit
  }

  ClassMethod cmList(cm1 As %String, cm2 As %Integer)
  {
     Set list = $LISTBUILD(cm1,cm2)
     Quit list
  }
}

You can test these methods by calling them from the Terminal. For example:

USER>write ##class(User.NativeTest).cmString("World")
Hello World

Function Calls

The following Native SDK 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 (file NativeRoutine.mac, listed immediately after this example).

JavaScript calls to ObjectScript routines

The code in this example calls functions of each supported datatype from ObjectScript routine NativeRoutine. Assume that irispy is an existing instance of class Iris, and is currently connected to the server.

  const routineName = 'NativeRoutine';
  let fnValue = "";
  let comment = "";

  comment = ".fnBoolean() tests whether arguments 2 and 3 are equal: "
  fnValue = irisjs.function(routineName,'fnBoolean',2,3);
  console.log(routineName + comment + fnValue);

  comment = ".fnBytes() returns integer arguments 72,105,33 as a byte array (string value 'Hi!'): "
  fnValue = irisjs.function(routineName,'fnBytes',72,105,33);  // ASCII 'Hi!'
  console.log(routineName + comment + fnValue);

  comment = ".fnString() concatenates 'Hello' with argument string 'World': "
  fnValue = irisjs.function(routineName,"fnString","World");
  console.log(routineName + comment + fnValue);

  comment = ".fnLong() returns the sum of arguments 7+8: "
  fnValue = irisjs.function(routineName,'fnLong',7,8);
  console.log(routineName + comment + fnValue);

  comment = ".fnDouble() multiplies argument 4.5 by 1.5: "
  fnValue = irisjs.function(routineName,'fnDouble',4.5);
  console.log(routineName + comment + fnValue);

  comment = ".fnList() returns a $LIST containing arguments 'The answer is ' and 42: "
  fnValue = irisjs.functionIRISList(routineName,"fnList","The answer is ",42);
  console.log(routineName + comment + fnValue.get(1)+fnValue.get(2) );

  comment = ".fnProcedure() assigns argument value 66 to global node ^fnGlobal: "
  try {
    irisjs.kill('fnGlobal') // delete ^fnGlobal if it exists
    irisjs.procedure(routineName,"fnProcedure",66);
    fnValue = irisjs.get("fnGlobal"); //  get current value of node ^fnGlobal
  }
  catch {fnValue = 'procedure failed.'}
  console.log(routineName + comment + fnValue);
ObjectScript Routine NativeRoutine.mac

To run the previous example, this ObjectScript routine must be compiled and available on the server:

  fnBoolean(fn1,fn2) public {
     quit (fn1=fn2)
  }
  fnBytes(fn1,fn2,fn3) public {
      quit $CHAR(fn1,fn2,fn3)
  }
  fnString(fn1) public {
      quit "Hello "_fn1
  }
  fnLong(fn1,fn2) public {
      quit fn1+fn2
  }
  fnDouble(fn1) public {
      quit fn1 * 1.5
  }
  fnProcedure(fn1) public {
      set ^fnGlobal=fn1
      quit
  }
  fnList(fn1,fn2) public {
      set list = $LISTBUILD(fn1,fn2)
      quit list
  }

You can test these functions by calling them from the Terminal. For example:

USER>write $$fnString^NativeRoutine("World")
Hello World
Feedback