1

I am trying to add two string arguments in my addon but I could not get it to work. I always get an error about the types of the two arguments.

My function:

void Add(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  // Check the number of arguments passed.
  if (args.Length() < 2) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong number of arguments")
            .ToLocalChecked()));
    return;
  }

  // Check the argument types
  if (!args[0]->IsString() || !args[1]->IsString()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
    return;
  }

  Local<String> arg1 = args[0]->ToString(context).ToLocalChecked();
  Local<String> arg2 = args[1]->ToString(context).ToLocalChecked();

  args.GetReturnValue().Set(arg1 + arg2);
}

The Whole File:

#include <node.h>
#include <string>

namespace demo {

using v8::Context;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;

void Add(const FunctionCallbackInfo<Value> &args) {...

void Init(Local<Object> exports) { NODE_SET_METHOD(exports, "add", Add); }

NODE_MODULE(NODE_GYP_MODULE_NAME, Init);

}

Any feedback would be appreciated.

Something Q
  • 125
  • 1
  • 7
  • 1
    What does " it seems not to be working alright" really mean? – U. W. Jan 27 '21 at 08:55
  • It means I couldn't get it to work (I probably should've rephrased it to be more descriptive), I always get an error about the types of the two arguments. – Something Q Jan 27 '21 at 09:21
  • Well, then give us everything you have: The complete source code needed to reproduce the problem, the error messages etc. As you realized, you have to be more descriptive. If someone gave you your own description, could you help that someone? Probably not. And that is why nobody helps you here. We just cannot. – U. W. Jan 27 '21 at 10:20
  • After some digging I found the solution. Thanks for your feedback though! – Something Q Jan 28 '21 at 06:21

1 Answers1

2

Found the solution. The only problem was that I should have converted the Local<String> type to std::string. And after adding them together, convert them back to Local<String> when returning the value.

Code:

void Add(const FunctionCallbackInfo<Value> &args) {
  Isolate *isolate = args.GetIsolate();
  Local<Context> context = isolate->GetCurrentContext();

  // Check the number of arguments passed.
  if (args.Length() < 2) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong number of arguments")
            .ToLocalChecked()));
    return;
  }

  // Check the argument types
  if (!args[0]->IsString() || !args[1]->IsString()) {
    isolate->ThrowException(Exception::TypeError(
        String::NewFromUtf8(isolate, "Wrong arguments").ToLocalChecked()));
    return;
  }

  v8::String::Utf8Value str1(isolate, args[0]);
  v8::String::Utf8Value str2(isolate, args[1]);
  std::string searchWord(*str1);
  std::string body(*str2);

  std::string res = searchWord + body;

  args.GetReturnValue().Set(
      String::NewFromUtf8(isolate, res.c_str()).ToLocalChecked());
}
Something Q
  • 125
  • 1
  • 7