我刚开始学习 node.js,尤其是它的 C++ 插件。我稍微修改了 hello world 示例,看看它是如何工作的。然后我发现它在 Linux 和 Windows 中的运行方式不同。
基本上,我添加了一个使用 cout 输出到控制台的内部函数。在 Linux 中,输出为
worldtest
yes
但在 Windows 中,它是
yes
worldtest
基本上输出的顺序是不同的。看来 Windows 输出是我所期望的。有什么想法我在这里想念的吗?谢谢!
你好.cc:
#include <node.h>
#include <v8.h>
#include <string>
#include <iostream>
using namespace v8;
using namespace std;
string changestring(string tmp);
void Method(const v8::FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
string tmp=changestring("world");
const char * c=tmp.c_str();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, c));
}
void Init(Handle<Object> exports) {
Isolate* isolate = Isolate::GetCurrent();
exports->Set(String::NewFromUtf8(isolate, "hello"),
FunctionTemplate::New(isolate, Method)->GetFunction());
}
string changestring(string tmp)
{
cout<<"yes\n";
return (tmp+"test\n");
}
NODE_MODULE(hello, Init)
你好.js
var addon = require('bindings')('hello');
console.log(addon.hello()); // 'world'