如何返回目录图的内存地址以供以后检查项目?

时间:2019-01-28 16:12:31

标签: javascript emscripten webassembly

我想在我的JavaScript项目中使用dawg。我使用Emscripten将dawg代码here转换为webassembly。

该项目提供了一个dawgdic库,用于构建和访问使用有向非循环字图(DAWG)实现的字典。

通过将Trie最小化为确定性有限自动机(DFA)来构造dawg,因此dawg在内存使用方面具有优势

我参考了给定链接以转换为Webassemly。 here

The below code store items in dawg graph format.
    #include <cassert>
    #include <iostream>
    #include <string>
    #include <dawgdic/dawg-builder.h>
    #include <dawgdic/dictionary-builder.h>
    #include <emscripten/emscripten.h>
    using namespace std;

    #ifdef __cplusplus
    extern "C" {
    #endif
    int EMSCRIPTEN_KEEPALIVE addToDawg(void(*f)(dawgdic::Dawg *c)) {
         dawgdic::DawgBuilder dawg_builder;
  assert(dawg_builder.Insert("apple"));
  assert(dawg_builder.Insert("cherry"));
  assert(!dawg_builder.Insert("banana"));
  assert(dawg_builder.Insert("durian"));
  dawgdic::Dawg dawg;
  dawg_builder.Finish(&dawg);
        return 0;
      }

    #ifdef __cplusplus
    }
    #endif

    Here is link to see output: [here](https://demophp.digi-corp.com/nitin/dawg-builder-test.html)

    How can I return address of memory segment where dawg is stored to Javascript such that later I can call another method to check if item exist in dawg graph? like dawg_dic.Contains("apple").

1 个答案:

答案 0 :(得分:0)

听起来您想在堆上创建dawgdic::Dawg(使用new),然后返回其地址。在JS端,此返回值将只是数字,然后您可以将其传递给采用dawgdic::Dawq*的函数。

因此您的API可能如下:

dawgdic::Dawg* createDawg() EMSCRIPTEN_KEEPALIVE;
void addToDawg(dawgdic::Dawg *c) EMSCRIPTEN_KEEPALIVE;