我有以下我不明白的情况。我有一个应用程序,我在 NodeJS 中使用 Nan 调用 C++ 函数。C++端的代码如下:
#include <nan.h>
#include <iostream>
using namespace std;
using namespace Nan;
using namespace v8;
//
// The function that we are going to call from NodeJS
//
NAN_METHOD(Combine)
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
//
// Send the buffer back to NodeJS with the result of our calculation.
//
info
.GetReturnValue()
.Set(
NewBuffer((char *) str, 80)
.ToLocalChecked());
}
//
// The constructor
//
NAN_MODULE_INIT(Init)
{
//
// Expose the method or methods to NodeJS
//
Nan::Set(
target,
New<String>("combine").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(Combine)).ToLocalChecked()
);
}
//
// Load the constructor
//
NODE_MODULE(basic_nan, Init)
当我将我的 char 变量发送回 NodeJS 时,我得到 80 个字节,但它们充满了随机值。看起来好像str
变量指向的地方在创建NewBuffer()
.
我的问题
我想得到一个关于发生了什么的解释,理想情况下得到一个潜在的解决方案。