* v8 :: String :: Utf8Value(args [0] - > ToString())不返回node.js的字符串addon参数

时间:2022-12-19 15:58:54

I've found that *v8::String::Utf8Value(args[0]->ToString()) returns the proper string on node 0.8.2 32-bit and does not return the proper string on node 0.8.8 64-bit.

我发现* v8 :: String :: Utf8Value(args [0] - > ToString())在节点0.8.2 32位上返回正确的字符串,并且不在节点0.8.8上返回正确的字符串64-位。

does anybody understand why?

有谁知道为什么?

My node.js addon looks like this:

我的node.js插件看起来像这样:

#define BUILDING_NODE_EXTENSION
#include <node.h>

#define MAX_OUTPUT_BUF  80
extern "C" char *do_sqlsig(char *in);

using namespace v8;

Handle<Value> Sqlsig(const Arguments& args) {
 HandleScope scope;
 char *c_arg, *ret;

 if (args.Length() < 1) {
   ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
   return scope.Close(Undefined());
 }

 c_arg = *v8::String::Utf8Value(args[0]->ToString());

 ret = c_arg; //do_sqlsig(c_arg);

 return scope.Close(String::New(ret));
}

void Init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("sqlsig"),
      FunctionTemplate::New(Sqlsig)->GetFunction());
}

NODE_MODULE(sqlsig, Init)

As you can see I'm writing a wrapper for the C function, do_sqlsig. I know C very well and know very little about C++

正如您所看到的,我正在为C函数do_sqlsig编写一个包装器。我非常了解C并且对C ++知之甚少

1 个解决方案

#1


10  

The string that the pointer returned from *v8::String::Utf8Value(args[0]->ToString()); is point at is destroyed at the end of this line (when Utf8Value is destroyed). You create and destroy a Utf8Value object in one line. It is undefined behavior when dereferencing a dangling pointer, and why you are seeing different results on different versions.

指针从* v8 :: String :: Utf8Value(args [0] - > ToString())返回的字符串;是在该行的末尾被销毁(当Utf8Value被销毁时)。您可以在一行中创建和销毁Utf8Value对象。取消引用悬空指针时,它是未定义的行为,以及为什么您在不同版本上看到不同的结果。

Break it up into two lines and the string will be valid as long as your Utf8Value object is in scope.

将其分解为两行,只要您的Utf8Value对象在范围内,该字符串就会有效。

v8::String::Utf8Value str(args[0]->ToString());
c_arg = *str;

#1


10  

The string that the pointer returned from *v8::String::Utf8Value(args[0]->ToString()); is point at is destroyed at the end of this line (when Utf8Value is destroyed). You create and destroy a Utf8Value object in one line. It is undefined behavior when dereferencing a dangling pointer, and why you are seeing different results on different versions.

指针从* v8 :: String :: Utf8Value(args [0] - > ToString())返回的字符串;是在该行的末尾被销毁(当Utf8Value被销毁时)。您可以在一行中创建和销毁Utf8Value对象。取消引用悬空指针时,它是未定义的行为,以及为什么您在不同版本上看到不同的结果。

Break it up into two lines and the string will be valid as long as your Utf8Value object is in scope.

将其分解为两行,只要您的Utf8Value对象在范围内,该字符串就会有效。

v8::String::Utf8Value str(args[0]->ToString());
c_arg = *str;