V8中的JavaScript当量
Javascript equivalents in V8?
我正在使用nodejs和v8玩,以尝试两者。
我想在C 中翻译此简单的JS线。
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);
}
}
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)
app.js
global.Game = require('./build/Release/game');
console.log(global.Game); // this will print "{ sleep: [Function] }"
//now you can call native sleep
Game.sleep();
文档:http://nodejs.org/api/addons.html#addons_hello_world
我认为这是一个很好的睡眠功能,因为它在基于JS的其他平台上起作用。
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
相关文章:
- 如何在本地机器上运行c++和javascript客户端代码(hackerbank风格)
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 嘿,我正在尝试将此c ++转换为javascript,但有些东西不起作用
- C++中的 JavaScript 样式闭包
- OpenCV(C++)处理来自JavaScript / Web Assembly的图像数据
- 在C++中嵌入 Mozilla 的 JavaScript 引擎
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- 在Javascript和C++中计算MD5哈希的不同结果
- 将Wiimote MAC地址转换为PIN码,从C++转换为Python或Javascript
- 如何使用EM_JS从带有参数的C++调用javascript方法
- JavaScript 中的一等函数和 C++ 中的函数声明
- 在 v8 JavaScript 中重复调用C++是否有巨大的开销?
- 在QtWebEngine中捕获JavaScript事件
- JavaScript箭头函数:我们能否像C ++ lambdas一样捕获值
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 如何像在javascript中一样"push" c ++映射
- 如何读取 Javascript 文件
- 如何在 C++ 中从 V8 调用 Javascript 函数
- 在 c++ 中是否有像 javascript 中那样"includes()"的方法
- V8中的JavaScript当量