From ab77088b546118e1f11d383eb83760684dfdb8aa Mon Sep 17 00:00:00 2001 From: Ivan Danyliuk Date: Fri, 24 Nov 2017 15:54:05 +0200 Subject: [PATCH] Update bindings and autogenertor --- .gitignore | 1 + README.md | 17 +++++ src/status.cpp | 120 ++++++++++--------------------- test.js | 3 + tools/go2nodebinding/template.go | 3 - 5 files changed, 58 insertions(+), 86 deletions(-) create mode 100644 test.js diff --git a/.gitignore b/.gitignore index 219d995..fb015dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ bin/ build/ +go2nodebinding diff --git a/README.md b/README.md index 1820e85..c2f3095 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,20 @@ You should have resulting module in `build/Release` folder. # Package module TBD + +# Autogenerate bindings from Go code + +It's possible to autogenerate C++ code from status-go Go code. First, build autogenerator tool: + +``` +go build ./tools/go2nodebinding +``` + +then, run it: + +``` +./go2nodebinding ../status-go/lib/library.go > src/status.cpp +``` + + +check the diff and commit. diff --git a/src/status.cpp b/src/status.cpp index 45a8101..11e9fa4 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -1,4 +1,4 @@ -// DO NOT EDIT: code is autogenerated by go2nodebinding from Go code. +// DO NOT EDIT: code is autogenerated by ./go2nodebinding from Go code. // https://github.com/divan/go2nodebinding #include @@ -57,9 +57,6 @@ void _GenerateConfig(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _StartNode(const FunctionCallbackInfo& args) { @@ -90,9 +87,6 @@ void _StartNode(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _StopNode(const FunctionCallbackInfo& args) { @@ -116,8 +110,6 @@ void _StopNode(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - } void _ValidateNodeConfig(const FunctionCallbackInfo& args) { @@ -148,9 +140,6 @@ void _ValidateNodeConfig(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _ResetChainData(const FunctionCallbackInfo& args) { @@ -174,8 +163,6 @@ void _ResetChainData(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - } void _CallRPC(const FunctionCallbackInfo& args) { @@ -206,9 +193,6 @@ void _CallRPC(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _CreateAccount(const FunctionCallbackInfo& args) { @@ -239,9 +223,6 @@ void _CreateAccount(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _CreateChildAccount(const FunctionCallbackInfo& args) { @@ -279,10 +260,6 @@ void _CreateChildAccount(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _RecoverAccount(const FunctionCallbackInfo& args) { @@ -320,10 +297,6 @@ void _RecoverAccount(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _VerifyAccountPassword(const FunctionCallbackInfo& args) { @@ -368,11 +341,6 @@ void _VerifyAccountPassword(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - delete arg2; - } void _Login(const FunctionCallbackInfo& args) { @@ -410,10 +378,6 @@ void _Login(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _Logout(const FunctionCallbackInfo& args) { @@ -437,8 +401,6 @@ void _Logout(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - } void _CompleteTransaction(const FunctionCallbackInfo& args) { @@ -476,10 +438,6 @@ void _CompleteTransaction(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _CompleteTransactions(const FunctionCallbackInfo& args) { @@ -517,10 +475,6 @@ void _CompleteTransactions(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _DiscardTransaction(const FunctionCallbackInfo& args) { @@ -551,9 +505,6 @@ void _DiscardTransaction(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _DiscardTransactions(const FunctionCallbackInfo& args) { @@ -584,9 +535,6 @@ void _DiscardTransactions(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _InitJail(const FunctionCallbackInfo& args) { @@ -615,9 +563,43 @@ void _InitJail(const FunctionCallbackInfo& args) { InitJail(arg0); +} - delete arg0; +void _Parse(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + + if (args.Length() != 2) { + // Throw an Error that is passed back to JavaScript + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8(isolate, "Wrong number of arguments for Parse"))); + return; + } + + // Check the argument types + if (!args[0]->IsString()) { + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8(isolate, "Wrong argument type for 'chatID'"))); + return; + } + if (!args[1]->IsString()) { + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8(isolate, "Wrong argument type for 'js'"))); + return; + } + + + String::Utf8Value arg0Obj(args[0]->ToString()); + char *arg0 = *arg0Obj; + String::Utf8Value arg1Obj(args[1]->ToString()); + char *arg1 = *arg1Obj; + + // Call exported Go function, which returns a C string + char *c = Parse(arg0, arg1); + + Local ret = String::NewFromUtf8(isolate, c); + args.GetReturnValue().Set(ret); + delete c; } void _CreateAndInitCell(const FunctionCallbackInfo& args) { @@ -655,10 +637,6 @@ void _CreateAndInitCell(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _ExecuteJS(const FunctionCallbackInfo& args) { @@ -696,10 +674,6 @@ void _ExecuteJS(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - } void _Call(const FunctionCallbackInfo& args) { @@ -744,11 +718,6 @@ void _Call(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - delete arg2; - } void _StartCPUProfile(const FunctionCallbackInfo& args) { @@ -779,9 +748,6 @@ void _StartCPUProfile(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _StopCPUProfiling(const FunctionCallbackInfo& args) { @@ -805,8 +771,6 @@ void _StopCPUProfiling(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - } void _WriteHeapProfile(const FunctionCallbackInfo& args) { @@ -837,9 +801,6 @@ void _WriteHeapProfile(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _Notify(const FunctionCallbackInfo& args) { @@ -870,9 +831,6 @@ void _Notify(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - } void _NotifyUsers(const FunctionCallbackInfo& args) { @@ -917,11 +875,6 @@ void _NotifyUsers(const FunctionCallbackInfo& args) { Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c; - - delete arg0; - delete arg1; - delete arg2; - } void init(Local exports) { @@ -942,6 +895,7 @@ void init(Local exports) { NODE_SET_METHOD(exports, "DiscardTransaction", _DiscardTransaction); NODE_SET_METHOD(exports, "DiscardTransactions", _DiscardTransactions); NODE_SET_METHOD(exports, "InitJail", _InitJail); + NODE_SET_METHOD(exports, "Parse", _Parse); NODE_SET_METHOD(exports, "CreateAndInitCell", _CreateAndInitCell); NODE_SET_METHOD(exports, "ExecuteJS", _ExecuteJS); NODE_SET_METHOD(exports, "Call", _Call); diff --git a/test.js b/test.js new file mode 100644 index 0000000..53a0527 --- /dev/null +++ b/test.js @@ -0,0 +1,3 @@ +var c = require(".") + +console.log(c.GenerateConfig("data", 3, 0)) \ No newline at end of file diff --git a/tools/go2nodebinding/template.go b/tools/go2nodebinding/template.go index ed22121..a22d644 100644 --- a/tools/go2nodebinding/template.go +++ b/tools/go2nodebinding/template.go @@ -78,9 +78,6 @@ void _{{.Name}}(const FunctionCallbackInfo& args) { {{if eq .ReturnsCount 1}}Local ret = String::NewFromUtf8(isolate, c); args.GetReturnValue().Set(ret); delete c;{{end}} - - {{range $index, $arg := .Params}}{{if eq .Type "*C.char"}}delete arg{{$index}}; - {{end}}{{end}} } `))