V8中的Javascript等价物?
我正在玩NodeJS和V8,试图学习两者 我想在C++中翻译这个简单的JS线。V8中的Javascript等价物?,javascript,c++,node.js,v8,Javascript,C++,Node.js,V8,我正在玩NodeJS和V8,试图学习两者 我想在C++中翻译这个简单的JS线。 global.Game = { sleep: call_some_CPP_function }; 在过去的两天里,我一直在拼凑在互联网上和其他人的源代码中找到的代码,试图了解它是如何工作的,但最后我没有得到多少 下面的代码不起作用,如果我执行console.log(global.Game),我将一无所获 #include "node.h" #include "v8.h" namespace node{
global.Game = { sleep: call_some_CPP_function };
在过去的两天里,我一直在拼凑在互联网上和其他人的源代码中找到的代码,试图了解它是如何工作的,但最后我没有得到多少
下面的代码不起作用,如果我执行console.log(global.Game)
,我将一无所获
#include "node.h"
#include "v8.h"
namespace node{
using namespace v8; // make life easier
// define a sleepy thread blocker
Handle<Value> call_some_CPP_function(const FunctionCallbackInfo<Value>& a){
HandleScope scope(node_isolate);
Sleep(3);
return scope.Close(Undefined());
}
// let's create the object here
// I'm calling this function elsewhere
void execme(){
// I've read somewhere that I have to do this
Locker locker(node_isolate);
HandleScope scope(node_isolate);
// I think these two set the execution context (variables) and "this" accordingly
// but I'm not sure
Local<Context> context = node_isolate->GetCurrentContext();
Context::Scope context_scope(context);
// I need a reference to the global object, to assign my "Game" object
Local<Object> global = node_env->context()->Global();
// The only way is to invent a constructor for it
Local<FunctionTemplate> function_template = FunctionTemplate::New();
function_template->SetClassName(String::New("GameClass"));
// and create a new instance using that constructor
Local<Object> game_object = function_template->GetFunction()->NewInstance();
// after that, add the "sleep" function, which executes the function above
NODE_SET_METHOD(game_object, "sleep", call_some_CPP_function);
// and finally do the global.Game assignment
global->Set(String::New("Game"), game_object);
}
}
#包括“node.h”
#包括“v8.h”
名称空间节点{
使用命名空间v8;//使生活更轻松
//定义一个休眠线程阻止程序
Handle call\u some\u CPP\u函数(const function callbackinfo&a){
手镜范围(节点隔离);
睡眠(3);
返回scope.Close(未定义());
}
//让我们在这里创建对象
//我在别处调用这个函数
void execme(){
//我在某个地方读到,我必须这样做
储物柜(节点_隔离);
手镜范围(节点隔离);
//我认为这两个变量相应地设置了执行上下文(变量)和“this”
//但我不确定
本地上下文=节点_隔离->GetCurrentContext();
上下文::范围上下文\范围(上下文);
//我需要一个全局对象的引用,来分配我的“游戏”对象
局部全局=节点_env->context()->global();
//唯一的方法是为它发明一个构造函数
本地函数_template=FunctionTemplate::New();
function_template->SetClassName(String::New(“GameClass”);
//并使用该构造函数创建一个新实例
本地游戏\对象=函数\模板->GetFunction()->NewInstance();
//之后,添加“sleep”函数,该函数执行上述函数
节点设置方法(游戏对象,“睡眠”,调用一些CPP函数);
//最后做全局游戏任务
全局->设置(字符串::新建(“游戏”),游戏对象);
}
}
game.cc
#include <node.h>
#include <v8.h>
using namespace v8;
// sleep 3 seconds
Handle<Value> Sleep(const Arguments& args) {
HandleScope scope;
Sleep(3000);
return scope.Close(Undefined());
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("sleep"),
FunctionTemplate::New(Sleep)->GetFunction());
}
NODE_MODULE(game, init)
文档:我认为这是一个很好的睡眠功能,因为它可以在其他基于JS的平台上工作
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
函数睡眠(毫秒){
const date=date.now();
让currentDate=null;
做{
currentDate=Date.now();
}while(currentDate-日期<毫秒);
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}