Почему N-API не может найти некоторые привязки при использовании массива napi_property_descriptor? - PullRequest
0 голосов
/ 26 апреля 2020

У меня есть следующий код ...

// robot_node.c
#include <stdio.h>
#include <string.h>
#include "robot_node.h"
#include "robot.h"

napi_value node_forward(napi_env env, napi_callback_info info){
    napi_value result;
    napi_status status;
    int answer = forward();
    status = napi_create_int64(env, answer, &result);
    if (status != napi_ok) return NULL;
    return result;
}
napi_value node_stop(napi_env env, napi_callback_info info){
    napi_value result;
    napi_status status;
    int answer = stop();
    status = napi_create_int64(env, answer, &result);
    if (status != napi_ok) return NULL;
    return result;
}
napi_value helloWorld(napi_env env, napi_callback_info info) {
  napi_value world;
  napi_status status;
  const char* str = "world";
  size_t str_len = strlen(str);
  status = napi_create_string_utf8(env, str, str_len, &world);
  if (status != napi_ok) return NULL;
  return world;
}

//module.c
#include "robot_node.h"
#include "node_common.h"

napi_value init(napi_env env, napi_value exports) {
  napi_status status;
  napi_value fn;
  napi_property_descriptor commands[] = {
    DECLARE_NAPI_PROPERTY("hello", helloWorld),
    DECLARE_NAPI_PROPERTY("forward", node_forward),
    DECLARE_NAPI_PROPERTY("stop", node_stop)
  };
  status = napi_define_properties(env, exports, 1, commands);
  if (status != napi_ok) return NULL;
  return exports;
}

//test.mjs
import Bind from 'bindings';

const bindings = Bind("robot-ui.node");
const result = bindings.hello();

console.log(`The result is ${result}`);
bindings.forward();
setTimeout(()=>{
  bindings.stop();
}, 1000);

При запуске часть hello world работает нормально, но функции перемотки вперед и остановки завершаются с ошибкой ...

TypeError : bindings.forward не является функцией

Я довольно новичок в C разработке и не понимаю, как найти ошибку. Почему функция hello world работает нормально, а функция forward не работает?

1 Ответ

0 голосов
/ 26 апреля 2020

Упс только что понял

status = napi_define_properties(env, exports, 1, commands);

Должно быть

status = napi_define_properties(env, exports, 3, commands);
...