nodejs 传string到c++

Author Avatar ZTFtrue 发表于 • 2017年10月16日 16:45 • 共 • 483 • 次浏览

node 与C++扩展看这篇

http://www.cnblogs.com/nullcc/p/5846751.html

node 传v8::String 转std::string

参考地址:

https://stackoverflow.com/questions/16613828/how-to-convert-stdstring-to-v8s-localstring

		Isolate *isolate = args.GetIsolate();
		std::cout << "Hello, World!" << std::endl;
		v8::String::Utf8Value param1(args[0]->ToString());
		std::string from = std::string(*param1);
		std::cout << from << std::endl;

		std::string something("I am return:hello world ");
//  Local<String> something_else = String::NewFromUtf8(isolate, something.c_str() );
		 args.GetReturnValue().Set(String::NewFromUtf8(isolate, something.c_str()));
			

全部代码

#include <iostream>
#include <node/node.h>
#include <list>

using namespace v8;
using namespace std;


namespace demo {
    using v8::FunctionCallbackInfo;
    using v8::Isolate;
    using v8::Local;
    using v8::Object;
    using v8::String;
    using v8::Value;

    void Method(const FunctionCallbackInfo<Value> &args) {//方法
        Isolate *isolate = args.GetIsolate();
        std::cout << "Hello, World!" << std::endl;
        v8::String::Utf8Value param1(args[0]->ToString());
        std::string from = std::string(*param1);
        std::cout << from << std::endl;

        std::string something("I am return:hello world ");
//      Local<String> something_else = String::NewFromUtf8(isolate, something.c_str() );
        args.GetReturnValue().Set(String::NewFromUtf8(isolate, something.c_str()));
    }

    void init(Local<Object> exports) {
        NODE_SET_METHOD(exports, "name", Method);//调用Method()
    }

    NODE_MODULE(addon, init)
} // namespace demo

node

const addon = require('./build/Release/addon');
console.log(addon.name("I am input")); // 'world'

相关命令

npm install -g node-gyp
node-gyp configure
node-gyp build

其它

{
    "targets": [
        {
            "target_name": "addon",
            "sources": [ "main.cpp","ImageTools.cpp","ImageTools.h",
            "Distance.h","Distance.cpp",
             "out_line.cpp","out_line.h",
             ],
            "cflags_cc!" : [ "-fno-rtti", "-fno-exceptions"],
            "cflags_cc+" : [ "-frtti", "-fexceptions"],
            "include_dirs": [ " " ," "],
            "libraries": [ "" ],

        }
    ]
}
最后编辑于 • 2017年10月30日 14:42 •  
2 回复
avatar
0

avatar
0
发放红包 2017年10月30日 17:10   # 2 楼

目前看来还没有支持C++等语言。😰

你尚未登录,无法进行回复。