This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/unity by this push:
     new b3dd058ee3 [Unity][WEB] Enable String object (#14882)
b3dd058ee3 is described below

commit b3dd058ee39131d758dc600bc732420fb9122fbf
Author: Tianqi Chen <[email protected]>
AuthorDate: Fri May 19 14:58:50 2023 -0400

    [Unity][WEB] Enable String object (#14882)
    
    This PR enables string object in webruntime.
---
 web/emcc/wasm_runtime.cc      |  4 ++++
 web/src/runtime.ts            | 38 ++++++++++++++++++++++++++++++++++++++
 web/tests/node/test_object.js | 10 ++++++++++
 3 files changed, 52 insertions(+)

diff --git a/web/emcc/wasm_runtime.cc b/web/emcc/wasm_runtime.cc
index 5209a5899e..618e230282 100644
--- a/web/emcc/wasm_runtime.cc
+++ b/web/emcc/wasm_runtime.cc
@@ -98,6 +98,10 @@ TVM_REGISTER_GLOBAL("testing.echo").set_body([](TVMArgs 
args, TVMRetValue* ret)
   *ret = args[0];
 });
 
+TVM_REGISTER_GLOBAL("testing.ret_string").set_body([](TVMArgs args, 
TVMRetValue* ret) {
+  *ret = args[0].operator String();
+});
+
 TVM_REGISTER_GLOBAL("testing.log_info_str").set_body([](TVMArgs args, 
TVMRetValue* ret) {
   LOG(INFO) << args[0].operator String();
 });
diff --git a/web/src/runtime.ts b/web/src/runtime.ts
index fbceed1df9..577a50bfbc 100644
--- a/web/src/runtime.ts
+++ b/web/src/runtime.ts
@@ -143,6 +143,8 @@ class RuntimeContext implements Disposable {
   arrayGetItem : PackedFunc;
   arrayGetSize : PackedFunc;
   arrayMake : PackedFunc;
+  stringMake : PackedFunc;
+  getFFIString : PackedFunc;
   getSysLib : PackedFunc;
   arrayCacheGet : PackedFunc;
   arrayCacheUpdate : PackedFunc;
@@ -160,6 +162,8 @@ class RuntimeContext implements Disposable {
     this.arrayGetItem = getGlobalFunc("runtime.ArrayGetItem");
     this.arrayGetSize = getGlobalFunc("runtime.ArraySize");
     this.arrayMake = getGlobalFunc("runtime.Array");
+    this.stringMake = getGlobalFunc("runtime.String");
+    this.getFFIString = getGlobalFunc("runtime.GetFFIString");
     this.getSysLib = getGlobalFunc("runtime.SystemLib");
     this.arrayCacheGet = getGlobalFunc("vm.builtin.ndarray_cache.get");
     this.arrayCacheRemove = getGlobalFunc("vm.builtin.ndarray_cache.remove");
@@ -178,6 +182,8 @@ class RuntimeContext implements Disposable {
     this.arrayGetItem.dispose();
     this.arrayGetSize.dispose();
     this.arrayMake.dispose();
+    this.stringMake.dispose();
+    this.getFFIString.dispose();
     this.arrayCacheGet.dispose();
     this.arrayCacheRemove.dispose();
     this.arrayCacheUpdate.dispose();
@@ -866,6 +872,24 @@ export class TVMArray extends TVMObject {
   }
 }
 
+/** Runtime string object. */
+export class TVMString extends TVMObject {
+  constructor(
+    handle: Pointer,
+    lib: FFILibrary,
+    ctx: RuntimeContext
+  ) {
+    super(handle, lib, ctx);
+  }
+
+  /**
+   * @returns the size of the array.
+   */
+  toString() : string {
+    return this.ctx.getFFIString(this) as string;
+  }
+}
+
 export const enum VMAllocatorKind {
   NAIVE_ALLOCATOR = 1,
   POOLED_ALLOCATOR = 2,
@@ -1717,6 +1741,16 @@ export class Instance implements Disposable {
     return this.ctx.arrayMake(...inputs) as TVMArray;
   }
 
+  /**
+   * Create a {@link TVMString} that can be consumed by runtime.
+   *
+   * @param input The string.
+   * @returns The result TVMString.
+   */
+  makeString(input: string): TVMString {
+    return this.ctx.stringMake(input) as TVMString;
+  }
+
   /**
    * Create a shape tuple to pass to runtime.
    * @param shape The shape .
@@ -1890,6 +1924,10 @@ export class Instance implements Disposable {
       (handle: number, lib: FFILibrary, ctx: RuntimeContext) => {
         return new TVMArray(handle, lib, ctx);
     });
+    this.registerObjectConstructor("runtime.String",
+      (handle: number, lib: FFILibrary, ctx: RuntimeContext) => {
+        return new TVMString(handle, lib, ctx);
+    });
   }
 
   /** Register global packed functions needed by the backend to the env. */
diff --git a/web/tests/node/test_object.js b/web/tests/node/test_object.js
index 3b7ee5bd0c..edc7eae657 100644
--- a/web/tests/node/test_object.js
+++ b/web/tests/node/test_object.js
@@ -41,5 +41,15 @@ test("object", () => {
 
     let t1 = b.get(1);
     assert(t1.getHandle() == t.getHandle());
+
+    let s0 = tvm.makeString("hello world");
+    assert(s0.toString() == "hello world");
+    s0.dispose();
+
+    let ret_string = tvm.getGlobalFunc("testing.ret_string");
+    let s1 = ret_string("hello");
+    assert(s1.toString() == "hello");
+    ret_string.dispose();
+    s1.dispose();
   });
 });

Reply via email to