Skip to content

Instantly share code, notes, and snippets.

@QQBoxy
Last active December 30, 2015 21:59
Show Gist options
  • Save QQBoxy/7891082 to your computer and use it in GitHub Desktop.
Save QQBoxy/7891082 to your computer and use it in GitHub Desktop.
#include <node.h>
using namespace v8;
Handle<Value> parseJson(Handle<Value> jsonString) {
HandleScope scope;
Handle<Context> context = Context::GetCurrent();
Handle<Object> global = context->Global();
Handle<Object> JSON = global->Get(String::New("JSON"))->ToObject();
Handle<Function> JSON_parse = Handle<Function>::Cast(JSON->Get(String::New("parse")));
// return JSON.parse.apply(JSON, jsonString);
return scope.Close(JSON_parse->Call(JSON, 1, &jsonString));
}
void Initialize(Handle<Object> exports) {
exports->Set(String::NewSymbol("parseJson"), FunctionTemplate::New(parseJson)->GetFunction());
//IntelliSense: argument of type "v8::Handle<v8::Value> (*)(v8::Handle<v8::Value> jsonString)" is incompatible with parameter of type "v8::InvocationCallback"
}
NODE_MODULE(hello, Initialize)
/*
//test.js
var hello = require('./build/Release/hello');
console.log( hello.parseJson( "{\"a\":1, \"b\":2}" ) );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment