Version: 1.7
语言 : 中文
TypeScript source plug-ins
Call C# scripts from TypeScript

Call TypeScript plug-in code from C# scripts

To call TypeScript code from C# scripts, Tuanjie provides C# APIs that communicate with the OpenHarmony. Tuanjie provides a high level API that you can use to interact with TypeScript code.

High-level API

The high-level OpenHarmonyJSObject, OpenHarmonyJSClass APIs automate a lot of tasks required for calls from C# to TypeScript.

Instances of OpenHarmonyJSObject and OpenHarmonyJSClass have a one-to-one mapping to an instance of Object and Class respectively. They provide three types of interactions with TypeSCript code:

  • Call a method.
  • Get the value of a field.
  • Set the value of a field.

Each interaction also has a static version:

When you get the value of a field or call a method that returns a value, you use generics to specify the return type. When you set the value of a field, you also use generics to specify the type of the field that you are setting. For methods that don’t return a value, there is a regular, non-generic, version of Call.

Demo

This section contains code samples that show how to use the high-level OpenHarmonyJSObject and OpenHarmonyJSClass APIs.

Call method of Custom TypeScript Object

First, declare your TypeScript class and create an instance of it, then register it. Your plugin’s extension should be .tslib or .etslib. Note that You can place the source files (.tslib,.etslib) in any folder in your Project, except in special use locations such as StreamingAssets. Moreover, Tuanjie exports .ts and .ets files along with the plugins only if they’re placed in Plugin/OpenHarmony. The name of register function should match the following format: “Register” + your plugin name.

// in TestClass.tslib

export class ClassObjectTest {
  intFunc(a, b) {
    return a + b;
  }
}

export function RegisterTestClass() {
  var register = {}
  register["ClassObjectTest"] = new ClassObjectTest();
  return register;
}

Second, in your C# file, create an OpenHarmonyJSObject and call “intFunc”.

class TestScript: MonoBehaviour
{
    private void Start()
    {
        OpenHarmonyJSObject openHarmonyJSObject = new OpenHarmonyJSObject("ClassObjectTest");
        Debug.Log("Call-intFunc->" + openHarmonyJSObject.Call<int>("intFunc", 1, 2));
    }
}

Call method of Custom TypeScript Class

Similar to previous example, declare your class and register it.

// in TestClass.tslib

export class StaticClassTest {
  static stringArrayFunc(tmpStringArray){
    return tmpStringArray;
  }
}

export function RegisterTestClass() {
  var register = {}
  register["StaticClassTest"] = StaticClassTest;
  return register;
}

Then in your C# file, create an OpenHarmonyJSClass and call “stringArrayFunc”.

class TestScript: MonoBehaviour
{
    private string ArrayToString(Array array)
    {
        string log = array.Length + ":[";
        for (int i = 0; i < array.Length; i++)
        {
            log += array.GetValue(i) + ",";
        }

        log += "]";
        return log;
    }

    private void Start()
    {
        OpenHarmonyJSClass openHarmonyJSClass = new OpenHarmonyJSClass("StaticClassTest");
        string[] stringArray = new[] { "AA", "BB", "CC" };
        Debug.Log("CallStatic-stringArrayFunc->" + ArrayToString(openHarmonyJSClass.CallStatic<Array>("stringArrayFunc", stringArray)));
    }
}

Call method of OpenHarmony API

The following example shows how to get full name of an OpenHarmony device.

// in TestClass.tslib
import deviceInfo from '@ohos.deviceInfo';

export function RegisterTestClass() {
  var register = {}
  register["deviceInfo"] = deviceInfo;
  return register;
}
class TestScript: MonoBehaviour
{
    private void Start()
    {
        OpenHarmonyJSObject deviceInfoJSObject = new OpenHarmonyJSObject("deviceInfo");
        Debug.Log("CShapeCallJS-deviceInfo-->" + deviceInfoJSObject.Get<string>("osFullName"));
    }
}

More examples

import { TuanjieLog } from 'classesLib';
import deviceInfo from '@ohos.deviceInfo';
import sensor from '@ohos.sensor';

export class StaticClassTest {
  static stringValue = "StaticClassTestJS";
  static voidFunc() {
    TuanjieLog.info("CShapeCallJS voidFunc call from C# !!!");
  }

  static voidFuncWithArg(intValue, boolValue, byteValue, sbyteValue, int16Value, int64Value: Number, doubleValue, charValue, stringValue) {
    TuanjieLog.info("voidFuncWithArg call from C# !!!");
    if (intValue == 2147483647 && boolValue && byteValue == 255 && sbyteValue == -128 && int16Value == 32767 && int64Value == -9223372036854776000
    && doubleValue == 1.7976931348623157e+308 && charValue == 'R' && stringValue == "HelloWorld")
      return;
    TuanjieLog.error("CShapeCallJS voidFuncWithArg call from C# !! Value not equal!!!");
  }

  static intArray = [1,2,3];
  static boolArray = [false, true, false];
  static charArray = ["A", "B", "C"];
  static stringArray = ["AA", "BB", "CC"];
  static objArray = [deviceInfo, sensor];

  static intFunc(a, b) {
    return a + b;
  }

  static boolFunc(){
    return true;
  }

  static byteFunc(){
    return 255;
  }

  static sbyteFunc(){
    return -128;
  }

  static int16Func(){
    return 32767;
  }

  static int64Func(){
    return -9223372036854776000;
  }

  static charFunc(){
    return "Z";
  }

  static stringFunc(aaStr, bbStr){
    return aaStr + "HelloWorldJS" + bbStr;
  }

  static objFunc(){
    return deviceInfo;
  }

  static stringArrayFunc(tmpStringArray){
    return tmpStringArray;
  }
  
  static uint16Func(){
        let  uint16 = new Uint16Array([255, 128, 64, 32]);
      return uint16[0];
    }
    
  static typedArrayFunc(){
    return new Int16Array([255, 128, 64, 32]);
  }
  static arr = [1,2,3];
  static typedArr:Int32Array = new Int32Array([1,2,3]);
   static inTypedArrayFunc(arr:Int32Array){
    let aa = [1,2,3];
    let bb:Int32Array = new Int32Array(aa);
      for (let i = 0; i < bb.length; i++) {
          TuanjieLog.info(bb[i].toString());
      }
      return "";
   }
}

export class ClassObjectTest {
  stringValue = "ClassObjectTestJS";
  voidFunc() {
    TuanjieLog.info("CShapeCallJS voidFunc call from C# !!!");
  }

  voidFuncWithArg(intValue, boolValue, byteValue, sbyteValue, int16Value, int64Value: Number, doubleValue, charValue, stringValue) {
    TuanjieLog.info("voidFuncWithArg call from C# !!!");
    if (intValue == 2147483647 && boolValue && byteValue == 255 && sbyteValue == -128 && int16Value == 32767 && int64Value == -9223372036854776000
    && doubleValue == 1.7976931348623157e+308 && charValue == 'R' && stringValue == "HelloWorld")
      return;
    TuanjieLog.error("CShapeCallJS voidFuncWithArg call from C# !! Value not equal!!!");
  }

  intArray = [1,2,3];
  boolArray = [false, true, false];
  charArray = ["A", "B", "C"];
  stringArray = ["AA", "BB", "CC"];
  objArray = [deviceInfo, sensor];
  typedArr:Int32Array = new Int32Array([1,2,3]);

  intFunc(a, b) {
    return a + b;
  }

  boolFunc(){
    return true;
  }

  byteFunc(){
    return 255;
  }

  sbyteFunc(){
    return -128;
  }

  int16Func(){
    return 32767;
  }

  int64Func(){
    return -9223372036854776000;
  }

  charFunc(){
    return "Z";
  }

  stringFunc(){
    return "HelloWorldJS";
  }

  objFunc(){
    return deviceInfo;
  }

  stringArrayFunc(tmpStringArray){
    return tmpStringArray;
  }
}


export function RegisterTestClass() {
  var register = {}
  register["StaticClassTest"] = StaticClassTest;
  register["ClassObjectTest"] = new ClassObjectTest();
  register["deviceInfo"] = deviceInfo;
  register["sensor"] = sensor;
  return register;
}

public class OhJsTest : MonoBehaviour
{
    private Text textCom = null;
    private string logString = "";
    private OpenHarmonyJSObject sensorJSObject = null;
    private OpenHarmonyJSCallback sensorOnCallback = null;
    
    private void Start()
    {
        textCom = this.GetComponent<Text>();
        
        OpenHarmonyJSClass openHarmonyJSClass = new OpenHarmonyJSClass("StaticClassTest");

        Log("GetStatic-arrayFunc->" + ArrayToString(openHarmonyJSClass.CallStatic<Array>("typedArrayFunc")));
        Log("GetStatic-arrayFunc->" + openHarmonyJSClass.CallStatic<Array>("inTypedArrayFunc", new int[]{23,45,565}));

        openHarmonyJSClass.CallStatic("voidFunc");
        openHarmonyJSClass.CallStatic("voidFuncWithArg", int.MaxValue, true, Byte.MaxValue, SByte.MinValue,
            Int16.MaxValue, Int64.MinValue, Double.MaxValue, 'R', "HelloWorld");
        Log("CallStatic-intFunc->" + openHarmonyJSClass.CallStatic<int>("intFunc", 1, 2));
        Log("CallStatic-boolFunc->" + openHarmonyJSClass.CallStatic<bool>("boolFunc"));
        Log("CallStatic-byteFunc->" + openHarmonyJSClass.CallStatic<Byte>("byteFunc"));
        Log("CallStatic-sbyteFunc->" + openHarmonyJSClass.CallStatic<SByte>("sbyteFunc"));
        Log("CallStatic-int16Func->" + openHarmonyJSClass.CallStatic<Int16>("int16Func"));
        Log("CallStatic-int64Func->" + openHarmonyJSClass.CallStatic<Int64>("int64Func"));
        Log("CallStatic-charFunc->" + openHarmonyJSClass.CallStatic<Char>("charFunc"));
        Log("CallStatic-stringFunc->" + openHarmonyJSClass.CallStatic<String>("stringFunc", "AA", "BB"));
        string[] stringArray = new[] { "AA", "BB", "CC" };
        Log("CallStatic-stringArrayFunc->" + ArrayToString(openHarmonyJSClass.CallStatic<Array>("stringArrayFunc", stringArray, "DD")));
        Log("CallStatic-objFunc->" + openHarmonyJSClass.CallStatic<OpenHarmonyJSObject>("objFunc"));
        Log("GetStatic-stringValue->" + openHarmonyJSClass.GetStatic<string>("stringValue"));
        Log("SetStatic-stringValue->" + openHarmonyJSClass.SetStatic("stringValue", "ModifiedHelloWorld"));
        Log("GetStatic-stringValue->" + openHarmonyJSClass.GetStatic<string>("stringValue"));
        Log("SetStatic-intValue->" + openHarmonyJSClass.SetStatic("intValue", int.MaxValue));
        Log("GetStatic-intArray->" + ArrayToString(openHarmonyJSClass.GetStatic<Array>("intArray")));
        Log("GetStatic-boolArray->" + ArrayToString(openHarmonyJSClass.GetStatic<Array>("boolArray")));
        Log("GetStatic-charArray->" + ArrayToString(openHarmonyJSClass.GetStatic<Array>("charArray")));
        Log("GetStatic-stringArray->" + ArrayToString(openHarmonyJSClass.GetStatic<Array>("stringArray")));
        Log("GetStatic-objArray->" + ArrayToString(openHarmonyJSClass.GetStatic<Array>("objArray")));
        
        OpenHarmonyJSObject openHarmonyJSObject = new OpenHarmonyJSObject("ClassObjectTest");
        openHarmonyJSObject.Call("voidFunc");
        openHarmonyJSObject.Call("voidFuncWithArg", int.MaxValue, true, Byte.MaxValue, SByte.MinValue,
            Int16.MaxValue, Int64.MinValue, Double.MaxValue, 'R', "HelloWorld");
        Log("Call-intFunc->" + openHarmonyJSObject.Call<int>("intFunc", 1, 2));
        Log("Call-boolFunc->" + openHarmonyJSObject.Call<bool>("boolFunc"));
        Log("Call-byteFunc->" + openHarmonyJSObject.Call<Byte>("byteFunc"));
        Log("Call-sbyteFunc->" + openHarmonyJSObject.Call<SByte>("sbyteFunc"));
        Log("Call-int16Func->" + openHarmonyJSObject.Call<Int16>("int16Func"));
        Log("Call-int64Func->" + openHarmonyJSObject.Call<Int64>("int64Func"));
        Log("Call-charFunc->" + openHarmonyJSObject.Call<Char>("charFunc"));
        Log("Call-stringFunc->" + openHarmonyJSObject.Call<String>("stringFunc"));
        Log("Call-objFunc->" + openHarmonyJSObject.Call<OpenHarmonyJSObject>("objFunc"));
        Log("Call-stringArrayFunc->" + ArrayToString(openHarmonyJSObject.Call<Array>("stringArrayFunc", stringArray, "DD")));
        Log("Get-stringValue->" + openHarmonyJSObject.Get<string>("stringValue"));
        Log("Set-stringValue->" + openHarmonyJSObject.Set("stringValue", "ModifiedHelloWorld"));
        Log("Get-stringValue->" + openHarmonyJSObject.Get<string>("stringValue"));
        Log("Set-intValue->" + openHarmonyJSObject.Set("intValue", int.MaxValue));
        Log("Get-intValue->" + openHarmonyJSObject.Get<int>("intValue"));
        Log("Get-intArray->" + ArrayToString(openHarmonyJSObject.Get<Array>("intArray")));
        Log("Get-boolArray->" + ArrayToString(openHarmonyJSObject.Get<Array>("boolArray")));
        Log("Get-charArray->" + ArrayToString(openHarmonyJSObject.Get<Array>("charArray")));
        Log("Get-stringArray->" + ArrayToString(openHarmonyJSObject.Get<Array>("stringArray")));
        Log("Get-objArray->" + ArrayToString(openHarmonyJSObject.Get<Array>("objArray")));
        
        OpenHarmonyJSObject deviceInfoJSObject = new OpenHarmonyJSObject("deviceInfo");
        Debug.Log("CShapeCallJS-deviceInfo-->" + deviceInfoJSObject.Get<string>("osFullName"));

        sensorJSObject = new OpenHarmonyJSObject("sensor");
        sensorOnCallback = new OpenHarmonyJSCallback(SensorOnCallback);
        sensorJSObject.Call("on", 1, sensorOnCallback);
        textCom.text = logString;
    }

    public object SensorOnCallback(params OpenHarmonyJSObject[] args)
    {
        OpenHarmonyJSObject data = args[0];
        Debug.Log("Sensor:" + data.Get<float>("x") + "," + data.Get<float>("y") + "," + data.Get<float>("z"));
        return null;
    }

    private string ArrayToString(Array array)
    {
        string log = array.Length + ":[";
        for (int i = 0; i < array.Length; i++)
        {
            log += array.GetValue(i) + ",";
        }

        log += "]";
        return log;
    }

    private void OnDestroy()
    {
        sensorJSObject.Call("off", 1, sensorOnCallback);
        sensorOnCallback.Dispose();
    }

    private void Log(string log)
    {
        Debug.Log(log);
        logString += log.Replace('\0', ' ') + "\n";
    }
}
TypeScript source plug-ins
Call C# scripts from TypeScript