HarmonyOS JSVM Demo不完整

按咨询文档(https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/napi/use-jsvm-runtime-task.md)运行Demo,发现不能正常编译,有关键几处实现希望能放开一下。

#include "common.h"  
#include "myobject.h" 

1.common文件的实现是什么?JSVM_CALL是里面定义的宏?myobject.h无意义吧。

2.PumpMessageLoop/PerformMicrotaskCheckpoint没有找到这2个方法的时候,是不是和V8一样的效果。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

按照以下操作:

1.如下两个文件更新:

1)Index.ets:

import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';

function MyCallback(a: string, b: string): string {
  console.log("TEST MyCallback run: " + a);
  b = "callback done";
  console.log("TEST MyCallback run: " + b);
  return "callback pass";
}

function MyCallback2(a: string, b: string): string {
  console.log("TEST MyCallback2 start: a = " + a);
  console.log("TEST MyCallback2 start: b = " + b);
  return "MyCallback2 pass";
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let a = "hello World";
            consoleinfo(a);
            const mPromise = createPromise();
            mPromise.then((result) => {
              assertEqual(result, 0);
              onJSResultCallback(result, "abc", "v");
            });
            let a = "second hello";
            consoleinfo(a);
            let b = add(99, 1);
            assertEqual(100, b);
            assertEqual(add(99, 1), 100);
            createPromise().then((result) => {
              assertEqual(result, 1);
              consoleinfo(onJSResultCallback(result, '999', '666'));
            });
          };
        // 创建首个运行环境,并绑定TS回调
        const coreId = testNapi.createJsCore(MyCallback);
        console.log("TEST coreId: " + coreId);
        // 在首个运行环境中执行JS代码
        console.log("TEST evalUateJS : " + testNapi.evalUateJS(coreId, sourceCodeStr));
        //创建第二个运行环境,并绑定TS回调
        const coreId1 = testNapi.createJsCore(MyCallback2);
        console.log("TEST coreId: " + coreId1);
        // 在第二个运行环境中执行JS代码
        console.log("TEST evalUateJS : " + testNapi.evalUateJS(coreId1, sourcecodestr1));
        // 释放第二个运行环境
        testNapi.releaseJsCore(coreId1);
        // 释放首个运行环境
        testNapi.releaseJsCore(coreId);
        hilog.info(0x0000, 'testTag', 'Test NAPI end');
      }
      )
    }
    .width('100%')
  }
  .height
  ('100%')
}
aboutToDisappear():
void {}
}

2)common.h文件:

#ifndef FOUNDATION_ACE_JSVM_INTERFACES_KITS_JSVM_NATIVE_COMMON_H
#define FOUNDATION_ACE_JSVM_INTERFACES_KITS_JSVM_NATIVE_COMMON_H

#define DEPRECATED __attribute__((__deprecated__))

#define JSVM_VERSION 8

#define JSVM_RETVAL_NOTHING

#define GET_AND_THROW_LAST_ERROR(env)
do {
  const JSVM_ExtendedErrorInfo* errorInfo = nullptr;
  OH_JSVM_GetLastErrorInfo((env), &errorInfo);
  bool isPending = false;
  OH_JSVM_IsExceptionPending((env), &isPending);
  if (!isPending && errorInfo != nullptr) {
    const char* errorMessage =
    errorInfo->errorMessage != nullptr ? errorInfo->errorMessage : "empty error message";
    OH_JSVM_ThrowError((env), nullptr, errorMessage);
  }
} while (0)

#define JSVM_ASSERT_BASE(env, assertion, message, retVal)
do {
  if (!(assertion)) {
    OH_JSVM_ThrowError((env), nullptr, "assertion (" #assertion ") failed: " message);
    return retVal;
  }
} while (0)

#define JSVM_ASSERT(env, assertion, message) JSVM_ASSERT_BASE(env, assertion, message, nullptr)

#define JSVM_ASSERT_RETURN_VOID(env, assertion, message) JSVM_ASSERT_BASE(env, assertion, message, JSVM_RETVAL_NOTHING)

#define JSVM_CALL_BASE(env, theCall, retVal)
do {
  if ((theCall) != JSVM_OK) {
    GET_AND_THROW_LAST_ERROR((env));
    return retVal;
  }
} while (0)

#define JSVM_CALL(env, theCall) JSVM_CALL_BASE(env, theCall, nullptr)

#define NODE_API_CALL(env, the_call)
NODE_API_CALL_BASE(env, the_call, NULL)

#define JSVM_CALL_RETURN_VOID(env, theCall) JSVM_CALL_BASE(env, theCall, JSVM_RETVAL_NOTHING)

#define DECLARE_JSVM_PROPERTY(name, val)
{
  (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr
}

#define DECLARE_JSVM_STATIC_PROPERTY(name, val)
{
  (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr
}

#define DECLARE_JSVM_FUNCTION(name, func)
{
  (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr
}

#define DECLARE_JSVM_FUNCTION_WITH_DATA(name, func, data)
{
  (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data
}

#define DECLARE_JSVM_STATIC_FUNCTION(name, func)
{
  (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr
}

#define DECLARE_JSVM_GETTER(name, getter)
{
  (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr
}

#define DECLARE_JSVM_SETTER(name, setter)
{
  (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr
}

#define DECLARE_JSVM_GETTER_SETTER(name, getter, setter)
{
  (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr  
}

#endif

2.PumpMessageLoop、PerformMicrotaskCheckpoint屏蔽掉。

3.myobject.h头文件的引用屏蔽调。

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS list组件显示不完整
42浏览 • 1回复 待解决
LocalSocket的完整demo
409浏览 • 1回复 待解决
HarmonyOS 应用导航设计完整demo
53浏览 • 1回复 待解决
HarmonyOS 地图显示不完
355浏览 • 1回复 待解决
HarmonyOS JSVM是否在Gitee上开源?
188浏览 • 1回复 待解决
HarmonyOS 系统完整性检测
26浏览 • 1回复 待解决
HarmonyOS 如何拿到完整的请求报文
8浏览 • 1回复 待解决
HarmonyOS 音频重新编码的完整例子
70浏览 • 1回复 待解决