Skip to main content

This documentation is for an older version of this product. See the latest version of this content.Opens in a new tab

ObjectScript メソッドおよび関数の呼び出し

この章では、ObjectScript クラス・メソッドおよび関数を JavaScript アプリケーションから直接呼び出すことを可能にするクラス iris のメソッドを説明します。詳細および例は、以下のセクションを参照してください。

クラス・メソッドの呼び出し

以下の Native SDK メソッドは、指定された ObjectScript クラス・メソッドを呼び出します。これらは、className および methodName の文字列引数に加え、0 個以上のメソッド引数を含む配列を取ります。

  • Iris.ClassMethodValue() — クラス・メソッドを呼び出して、返り値を取得します。

  • Iris.ClassMethodVoid() — クラス・メソッドを呼び出して、返り値を破棄します。

引数リストでは、末尾の引数を省略できます。すべての引数の数よりも少ない数の引数が渡されるか、末尾の引数に対して null が渡されることでそれらの引数には既定の値が使用されるようになります。非 null 引数が null 引数の右側に渡されると、例外がスローされます。

この例のコードでは、User.NativeTest という ObjectScript テスト・クラスからいくつかのデータ型のクラス・メソッドを呼び出します (この例の直後に表示されています)。

JavaScript からの ObjectScript クラス・メソッドの呼び出し

この例では、変数 irisjs はクラス Iris の以前に定義されたインスタンスで、現在サーバに接続されていると想定します。

  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 = ".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 クラス 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
  }
}

ターミナルからこれらのメソッドを呼び出すことで、それらをテストできます。例を以下に示します。

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

関数の呼び出し

以下の Native SDK メソッドは、ユーザ定義の ObjectScript 関数またはプロシージャを呼び出します ("ObjectScript の使用法" の “呼び出し可能なユーザ定義コード・モジュール”" を参照)。これらは、className および methodName の文字列引数に加え、0 個以上のメソッド引数を含む配列を取ります。

  • Iris.function() — ユーザ定義関数を呼び出して、返り値を取得します。

  • Iris.procedure() — ユーザ定義プロシージャを呼び出して、返り値を破棄します。

引数リストでは、末尾の引数を省略できます。すべての引数の数よりも少ない数の引数が渡されるか、末尾の引数に対して null が渡されることでそれらの引数には既定の値が使用されるようになります。非 null 引数が null 引数の右側に渡されると、例外がスローされます。

この例のコードでは、NativeRoutine という ObjectScript テスト・ルーチンからいくつかのデータ型の関数を呼び出します (この例の直後に表示されているファイル NativeRoutine.mac)。

JavaScript からの ObjectScript ルーチンの呼び出し

この例のコードでは、ObjectScript ルーチン NativeRoutine からサポートされている各データ型の関数を呼び出します。irispy はクラス Iris の既存のインスタンスで、現在サーバに接続されていると想定します。

  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 = ".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 ルーチン NativeRoutine.mac

前の例を実行するには、この ObjectScript ルーチンがコンパイルされ、サーバで使用可能である必要があります。

  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
  }

ターミナルからこれらの関数を呼び出すことで、それらをテストできます。例を以下に示します。

USER>write $$fnString^NativeRoutine("World")
Hello World
FeedbackOpens in a new tab