Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何调用C++;来自javascript的方法 我有一个C++方法(哪个角色正在杀死一些进程)。她需要两个参数:主机名和端口_Javascript_C++ - Fatal编程技术网

如何调用C++;来自javascript的方法 我有一个C++方法(哪个角色正在杀死一些进程)。她需要两个参数:主机名和端口

如何调用C++;来自javascript的方法 我有一个C++方法(哪个角色正在杀死一些进程)。她需要两个参数:主机名和端口,javascript,c++,Javascript,C++,另一方面,我正在开发一个在Google Chrome上运行的web应用程序(使用Nodejs和AngularJS)。 当我通过浏览器点击按钮时,我希望能够通过我的代码> App.js< /Cuff>文件调用C++函数。p> 我还没有发现如何用C++来绑定JavaScript。 编辑:我认为这个链接可能非常有用 您可以使用谷歌的V8V8是谷歌的开源JavaScript引擎。 V8可以独立运行或嵌入到任何C++应用程序中 下面来自github的示例演示了如何将C++类与GoogleV8绑定。 -作

另一方面,我正在开发一个在Google Chrome上运行的web应用程序(使用Nodejs和AngularJS)。
当我通过浏览器点击按钮时,我希望能够通过我的代码> App.js< /Cuff>文件调用C++函数。p> <>我还没有发现如何用C++来绑定JavaScript。 编辑:我认为这个链接可能非常有用

您可以使用
谷歌的V8
V8
是谷歌的开源
JavaScript
引擎。
V8
可以独立运行
嵌入到任何
C++
应用程序中

下面来自github的示例演示了如何将
C++
类与
GoogleV8
绑定。 -作者是尼古拉斯

/*
 * v8_wrap_class.cpp
 *
 *  Created on: 14/01/2013
 *      Author: nicholas
 *     License: public domain
 */


#include <v8.h>
#include <cstdio>
#include <string>
#include <stdexcept>
#include <memory>

using namespace v8;

/*
var Simple = function(v)
{
    this.value = v;
}
Simple.prototype.func = function()
{
    alert(this.value);
}

var obj = new Simple(4);
obj.func();
*/
struct Simple
{
    double value;

    Simple(double v)
     : value(v)
    {
        fprintf(stderr, "Simple::ctor\n");
    }

    void func()
    {
        fprintf(stderr, "Simple::func(%f)\n", value);
    }

    ~Simple()
    {
        fprintf(stderr, "Simple::dtor\n");
    }
};

namespace js
{

/*
 * Retrieve the c++ object pointer from the js object
 */
template <typename T>
T* unwrap(const Arguments& args)
{
    auto self = args.Holder();
    auto wrap = Local<External>::Cast(self->GetInternalField(0));
    return static_cast<T*>(wrap->Value());
}

/*
 * Construct a new c++ object and wrap it in a js object
 */
template <typename T, typename... Args>
Persistent<Object> make_object(Handle<Object> object, Args&&... args)
{
    auto x = new T(std::forward<Args>(args)...);
    auto obj = Persistent<Object>::New(object);
    obj->SetInternalField(0, External::New(x));

    obj.MakeWeak(x, [](Persistent<Value> obj, void* data)
    {
        auto x = static_cast<T*>(data);
        delete x;

        obj.Dispose();
        obj.Clear();
    });

    return obj;
}

}

void bind_Simple(Local<Object> global)
{
    // Name the class in js
    auto name = String::NewSymbol("Simple");

    auto tpl = FunctionTemplate::New([&](const Arguments& args) -> Handle<Value>
    {
        if (!args.IsConstructCall())
            return ThrowException(String::New("Cannot call constructor as function"));

        HandleScope scope;

        // Read and pass constructor arguments
        js::make_object<Simple>(args.This(), args[0]->NumberValue());

        return args.This();
    });

    tpl->SetClassName(name);
    tpl->InstanceTemplate()->SetInternalFieldCount(1);

    auto prototype = tpl->PrototypeTemplate();

    // Add object properties to the prototype
    // Methods, Properties, etc.
    prototype->Set(String::New("func"), FunctionTemplate::New([](const Arguments& args) -> Handle<Value>
    {
        auto s = js::unwrap<Simple>(args);
        s->func();
        return {};
    })->GetFunction());

    auto constructor = Persistent<Function>::New(tpl->GetFunction());
    global->Set(name, constructor);
}

int main()
{
    std::string js_source = R"(
        for(var i = 0; i < 1000; ++i)
        {
            var s = new Simple(4);
            s.value = 5;
            s.func();
        }
    )";

    /*
     * This code is mostly uninteresting.
     * Just run the vm with the script provided.
     */
    {
        HandleScope handle_scope;
        Handle<ObjectTemplate> global_template = ObjectTemplate::New();

        Persistent<Context> context = Context::New(0, global_template);
        Context::Scope context_scope(context);

        auto global = context->Global();

        // Wrap the class and bind to the global scope.
        bind_Simple(global);

        {
            HandleScope handle_scope;

            TryCatch trycatch;

            Local<String> source = String::New(js_source.c_str(), js_source.size());

            Local<Script> script = Script::Compile(source);
            if (script.IsEmpty())
            {
                Handle<Value> exception = trycatch.Exception();
                String::AsciiValue exception_str(exception);
                throw std::runtime_error(*exception_str);
            }

            Local<Value> result = script->Run();
            if (result.IsEmpty())
            {
                Local<Value> exception = trycatch.Exception();
                String::AsciiValue exception_str(exception);
                throw std::runtime_error(*exception_str);
            }
        }

        context.Dispose();
        context.Clear();
    }

    // Run the GC until there is nothing to reclaim.
    while (!V8::IdleNotification())
        ;
    return 0;
}
/*
*v8_wrap_class.cpp
*
*创建日期:14/01/2013
*作者:尼古拉斯
*许可证:公共领域
*/
#包括
#包括
#包括
#包括
#包括
使用名称空间v8;
/*
简单变量=函数(v)
{
该值=v;
}
Simple.prototype.func=函数()
{
警报(该值);
}
var obj=新的简单(4);
obj.func();
*/
结构简单
{
双重价值;
简单(双v)
:值(v)
{
fprintf(stderr,“Simple::ctor\n”);
}
void func()
{
fprintf(stderr,“Simple::func(%f)\n)”,值;
}
~Simple()
{
fprintf(stderr,“Simple::dtor\n”);
}
};
命名空间js
{
/*
*从JS对象检索C++对象指针
*/
模板
T*展开(常量参数和参数)
{
auto self=args.Holder();
自动换行=Local::Cast(self->GetInternalField(0));
返回静态_cast(wrap->Value());
}
/*
*构造一个新的C++对象并将其封装在JS对象中
*/
模板
持久化make_对象(句柄对象、参数&&…参数)
{
自动x=新T(标准:正向(参数)…);
auto obj=持久::新建(对象);
obj->SetInternalField(0,外部::新建(x));
对象MakeWeak(x,[](持久对象,void*数据)
{
自动x=静态(数据);
删除x;
obj.Dispose();
obj.Clear();
});
返回obj;
}
}
void bind_Simple(局部全局)
{
//用js命名这个类
自动名称=字符串::NewSymbol(“简单”);
auto tpl=FunctionTemplate::新建([&](常量参数和参数)->句柄
{
如果(!args.IsConstructCall())
返回ThrowException(String::New(“不能将构造函数作为函数调用”);
手镜镜;
//读取并传递构造函数参数
js::make_对象(args.This(),args[0]->NumberValue());
返回args.This();
});
tpl->SetClassName(名称);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
auto prototype=tpl->PrototypeTemplate();
//向原型添加对象属性
//方法、性质等。
prototype->Set(String::New(“func”)、FunctionTemplate::New([](常量参数和参数)->句柄
{
自动s=js::展开(args);
s->func();
返回{};
})->GetFunction());
auto constructor=Persistent::New(tpl->GetFunction());
全局->设置(名称、构造函数);
}
int main()
{
std::string js_source=R“(
对于(变量i=0;i<1000;++i)
{
var s=新的简单(4);
s、 数值=5;
s、 func();
}
)";
/*
*这段代码基本上是无趣的。
*只需使用提供的脚本运行vm。
*/
{
手柄镜手柄镜;
Handle global_template=ObjectTemplate::New();
持久上下文=上下文::新建(0,全局_模板);
上下文::范围上下文\范围(上下文);
自动全局=上下文->全局();
//包装该类并绑定到全局范围。
简单绑定(全局);
{
手柄镜手柄镜;
TryCatch TryCatch;
localsource=String::New(js_source.c_str(),js_source.size());
本地脚本=脚本::编译(源代码);
if(script.IsEmpty())
{
handleexception=trycatch.exception();
字符串::ascivalue异常\u str(异常);
抛出std::运行时错误(*异常错误);
}
本地结果=脚本->运行();
if(result.IsEmpty())
{
本地异常=trycatch.exception();
字符串::ascivalue异常\u str(异常);
抛出std::运行时错误(*异常错误);
}
}
context.Dispose();
context.Clear();
}
//运行GC,直到没有可回收的内容。
而(!V8::IdleNotification())
;
返回0;
}

<代码> > p>您可以向服务器发出Ajax请求,并在后端调用C++,或者在PHP中调用。
或者这个有用的< /P>你会如何使用它来调用JS的C++函数?你能举个例子吗?你必须把整个事情包装成一个节点插件模块。另外,将C++编译成一个小的可执行文件,运行在<代码>子进程> /Calp>。将[.net]添加到[c#]问题本身并没有帮助,请停止为了追求+2而淹没审查队列。