nodejs插件 hellowworld




#include <node.h>
#include <thread> 
#include <chrono>

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();
		Local<String> str =  String::NewFromUtf8(isolate, "world").ToLocalChecked();

		args.GetReturnValue().Set(str);
	}

	// void Method(const FunctionCallbackInfo<Value>& args) {
	// 	Isolate* isolate = args.GetIsolate();

	// 	Local<String> str =  String::NewFromUtf8(isolate, "world").ToLocalChecked();

	// 	// 测量代码执行时间
	// 	auto start = std::chrono::steady_clock::now();
	// 	// ... 执行一些代码 ...
	//     // 睡眠 3 秒
    //     std::this_thread::sleep_for(std::chrono::seconds(3));
	// 	auto end = std::chrono::steady_clock::now();
	// 	auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    
	// 	// / 返回字符串
	// 	std::string result = "Execution time: " + std::to_string(duration.count()) + " milliseconds";
	// 	Local<String> result_str = String::NewFromUtf8(isolate, result.c_str()).ToLocalChecked();
	// 	args.GetReturnValue().Set(result_str);

	// }






// void Method(const FunctionCallbackInfo<Value>& args) {
//     Isolate* isolate = args.GetIsolate();

//     Local<String> str = String::NewFromUtf8(isolate, "world").ToLocalChecked();

//     // 测量代码执行时间
//     auto start = std::chrono::steady_clock::now();
//     std::this_thread::sleep_for(std::chrono::seconds(3));
//     auto end = std::chrono::steady_clock::now();




//     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    
//     // 返回对象
//     Local<Object> result = Object::New(isolate);
//     Local<String> time_key = String::NewFromUtf8(isolate, "executionTime").ToLocalChecked();
//     Local<String> unit_key = String::NewFromUtf8(isolate, "unit").ToLocalChecked();
//     Local<String> unit_value = String::NewFromUtf8(isolate, "milliseconds").ToLocalChecked();
    
//     result->Set(isolate->GetCurrentContext(), time_key, 
//                 v8::Number::New(isolate, static_cast<double>(duration.count()))).Check();



//     //对象里面 添加  unit   milliseconds
//     result->Set(isolate->GetCurrentContext(), unit_key, unit_value).Check();
    
//     args.GetReturnValue().Set(result);
// }
















// void Method(const FunctionCallbackInfo<Value>& args) {
//     Isolate* isolate = args.GetIsolate();

//     Local<String> str = String::NewFromUtf8(isolate, "world").ToLocalChecked();

//     // 测量代码执行时间
//     auto start = std::chrono::steady_clock::now();
//     // ... 执行一些代码 ...
// std::this_thread::sleep_for(std::chrono::seconds(3));

//     auto end = std::chrono::steady_clock::now();
//     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    
//     // 返回数字
//     args.GetReturnValue().Set(static_cast<double>(duration.count()));
// }







	void init(Local<Object> exports) {
		NODE_SET_METHOD(exports, "hello", Method);
	}

	NODE_MODULE(addon, init)
}


posted @ 2025-07-15 14:21  AngDH  阅读(8)  评论(0)    收藏  举报