0

I'm making String Transformation library using Node.js c++ Addons.

I know I can easily make this library with JavaScript, but I need to do it with Node.js c++ Addons.

According to this answer Converting from v8::Arguments to C++ Types. I have checked This answer but its returns the same error

So I declared void uppercase to transform some String to Uppercase.

But there is an Error

D:\transformer\src\transformer.cc(30): error C2664: 'std::string Transformation::toUpperCase(std::string)': cannot convert argument 1 from 'v8::Local<v8::String>' to 'std::string'  [D:\transformer\build\transformer.vcxproj]

I tried to solve that but failed because I'm new to c++ addons. I also checked that error code C2664 which doesn't solve my problem.

transformer.cc

// transformer.cc
#include <node.h>
#include <iostream>
#include <algorithm>

using namespace v8;
using namespace std;

namespace Transformations {

    string toUpperCase(string str) {
        transform(str.begin(), str.end(), str.begin(), ::toupper);
        return str;
    }

}

namespace Transformer {

    void uppercase(const FunctionCallbackInfo<Value>& args) {
        Isolate *isolate = args.GetIsolate();
        
        Local<String> str = Local<String>::Cast(args[0]);

        if(!args[0]->IsString()) {
            isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong arguments")));
            return;
        }

        args.GetReturnValue().Set(Transformations::toUpperCase(str));
    }

    void lowercase(const FunctionCallbackInfo<Value>& args) { }

    void capitalize(const FunctionCallbackInfo<Value>& args) { }

    void reverse(const FunctionCallbackInfo<Value>& args) { }

    void init(Local<Object> exports) {
        NODE_SET_METHOD(exports, "uppercase", uppercase);
        NODE_SET_METHOD(exports, "lowercase", lowercase);
        NODE_SET_METHOD(exports, "capitalize", capitalize);
        NODE_SET_METHOD(exports, "reverse", reverse);
    }

    NODE_MODULE(transformer, init)

}

test.js

const transformer = require('bindings')('transformer');

console.log(transformer.uppercase("Dinindu"));

5
  • Looks like a dupe of stackoverflow.com/questions/7476145/… Commented Mar 5, 2017 at 17:11
  • @BaummitAugen I checked that stackoverflow.com/questions/7476145/… answer. But it also return same error. Thanks for the comment Commented Mar 5, 2017 at 17:14
  • @BaummitAugen Could your make answer for this please? Commented Mar 6, 2017 at 9:15
  • Nope, I don't know the technology. That link was my best guess I got from google. Commented Mar 6, 2017 at 13:04
  • It seems like an ambeguty issue with string maybe define the parameter with the namespace Commented Mar 6, 2017 at 16:19

1 Answer 1

1

I found the solution here, The Native Abstractions (nan) solved my problem.

1. First add this code to the binding.gyp file under the targets

"include_dirs" : ["<!(node -e \"require('nan')\")"]

2. After installing nan, import it by adding this line at the head of transformer.cc

#include <nan.h>

3. Now, change our uppercase function a bit:

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

    v8::String::Utf8Value value(args[0]->ToString());
    std::string name = std::string(*value);
    std::string uppercased_name = Transformations::toUpperCase(name);

    if(!args[0]->IsString()) {
        isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong arguments")));
        return;
    }

    args.GetReturnValue().Set(Nan::New(uppercased_name).ToLocalChecked());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.