Skip to content

Instantly share code, notes, and snippets.

@sridatta
Created April 26, 2011 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sridatta/941913 to your computer and use it in GitHub Desktop.
Save sridatta/941913 to your computer and use it in GitHub Desktop.
Persistent<String> callback_symbol;
Persistent<String> json_symbol;
Persistent<String> parse_symbol;
Persistent<Object> module_handle;
Persistent<Context> parser_context;
Persistent<Function> parserfunc;
// The background thread. Tries to call parserfunc every so often.
static void* TheThread(void *)
{
HandleScope scope;
Context::Scope scopeA(parser_context);
while(true){
Handle<Value> argv[1];
argv[0] = String::New("{}");
parserfunc->Call(parser_context->Global(), 1, argv);
sleep(5);
}
return NULL;
}
void Initialize(Handle<Object> target)
{
HandleScope scope;
//Some node.js specific macros. Basically creates Persistent<String>'s
json_symbol = NODE_PSYMBOL("JSON");
parse_symbol = NODE_PSYMBOL("parse");
callback_symbol = NODE_PSYMBOL("callback");
// store handle for thread context
module_handle = Persistent<Object>::New(target);
parser_context = Context::New();
Context::Scope scopeA(parser_context);
//Find JSON.parse and put it into parserfunc
Local<Object> JSON = Local<Object>::Cast(parser_context->Global()->Get(json_symbol));
Handle<Function> parser_v = Handle<Function>::Cast(JSON->Get(parse_symbol));
parserfunc = Persistent<Function>::New(parser_v);
//Start the thread
pthread_create(&texample_thread, NULL, TheThread, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment