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.
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:
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.
This section contains code samples that show how to use the high-level OpenHarmonyJSObject
and OpenHarmonyJSClass
APIs.
First, declare your TypeScript class and create an instance of it, then register it. Your plugin’s extension should be .tslib, .ts or .ets. Note that Tuanjie treat .ts and .ets files as 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));
}
}
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, "DD")));
}
}
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"));
}
}