-
[기타] node.js 모듈(addon) 만드는 중.. async 콜백함수 호출2015.07.02 AM 10:42
node.js 버전은 v0.12.3
async callback 하는 부분이 대부분 v0.10 이하에서만 정보가 많아서, 정보대로 했는데, 잘 안되네요.
이래저래 찾다보니 v0.10과 v0.12 에서 바뀐게 있어서 그부분 적용된 코드를 만들어 봤는데, 작동은 되는것 같은데, 맞게 한건지..ㅎ
// nodeINB.cpp : 해당 DLL의 초기화 루틴을 정의합니다.
//
#include "stdafx.h"
#include "nodeINB.h"
#include
#include
#include
#if defined(_M_X64)
#pragma comment(lib, ".\\node\\x64\\node.lib")
#else
#pragma comment(lib, ".\\node\\x86\\node.lib")
#endif
using namespace v8;
struct Baton
{
uv_work_t request;
Persistent
int err_code;
std::string error_message;
};
void AsyncWork(uv_work_t* req)
{
printf("asyncwork sleep now\n");
Sleep(5000);
}
void AsyncAfter(uv_work_t* req, int status)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
printf("after\n");
Baton* baton = static_cast
const unsigned argc = 1;
Local
Local
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
baton->callback.Reset();
delete baton;
}
//-----------------------------------------------------------------------------
void CallbackFunction(const FunctionCallbackInfo
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local
const unsigned argc = 1;
Local
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
void Async(const FunctionCallbackInfo
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if (!args[0]->IsFunction())
{
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Callback function required")));
return;
}
Local
Baton* baton = new Baton();
baton->callback.Reset(isolate, callback);
baton->request.data = baton;
uv_queue_work(uv_default_loop(), &baton->request, AsyncWork, AsyncAfter);
Undefined(isolate);
return;
}
void test(const FunctionCallbackInfo
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
CString str;
AfxExtractSubString(str, "TEST|한글", 1, '|');
USES_CONVERSION;
const WCHAR* wStr = T2CW(str);
int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, NULL, 0, NULL, NULL);
char *utf8Str = new char[nUTF8codeSize];
WideCharToMultiByte(CP_UTF8, 0, wStr, -1, utf8Str, nUTF8codeSize, 0, 0);
args.GetReturnValue().Set(String::NewFromUtf8(isolate, utf8Str));
delete utf8Str;
}
//-----------------------------------------------------------------------------
void Init(Handle
댓글 : 1 개
- 네이미즈
- 2015/07/02 AM 10:44
https://strongloop.com/strongblog/node-js-v0-12-c-apis-breaking/
user error : Error. B.