<commit_before>#include "nodehun.hpp"

using namespace v8;

Persistent<Function> Nodehun::SpellDictionary::constructor;

void Nodehun::SpellDictionary::Init(Handle<Object> exports, Handle<Object> module)
{
  Isolate* isolate = Isolate::GetCurrent();
  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
  tpl->SetClassName(String::NewFromUtf8(isolate, "NodehunDictionary"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  //static
  NODE_SET_METHOD((v8::Local<v8::Template>) tpl, "createNewNodehun" , createNewNodehun);    
  //prototype
  NODE_SET_PROTOTYPE_METHOD(tpl, "isCorrect", isCorrect);
  NODE_SET_PROTOTYPE_METHOD(tpl, "isCorrectSync", isCorrectSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggest", spellSuggest);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestSync", spellSuggestSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestions", spellSuggestions);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestionsSync", spellSuggestionsSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addDictionary", addDictionary);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addDictionarySync", addDictionarySync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addWord", addWord);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addWordSync", addWordSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "removeWord", removeWord);
  NODE_SET_PROTOTYPE_METHOD(tpl, "removeWordSync", removeWordSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "stem", stem);
  NODE_SET_PROTOTYPE_METHOD(tpl, "stemSync", stemSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "generate", generate);
  NODE_SET_PROTOTYPE_METHOD(tpl, "generateSync", generateSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "analyze", analyze);
  NODE_SET_PROTOTYPE_METHOD(tpl, "analyzeSync", analyzeSync);

  constructor.Reset(isolate, tpl->GetFunction());
  module->Set(String::NewFromUtf8(isolate, "exports"), tpl->GetFunction());
}

void Nodehun::SpellDictionary::createNewNodehun(const FunctionCallbackInfo<Value>& args)
{
  Isolate* isolate = args.GetIsolate();
  HandleScope scope(isolate);

  int argl = args.Length();
  if(argl > 2 && args[2]->IsFunction()) {
    Local<Function> callback = Local<Function>::Cast(args[2]);
    const unsigned argc = 2;
    Local<Value> argv[argc];
    argv[1] = Local<Value>::New(isolate, Null(isolate));
    if(!node::Buffer::HasInstance(args[0])) {
      argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer"));
      callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
    }
    else if(!node::Buffer::HasInstance(args[1])) {
      argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "Second argument must be a buffer"));
      callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
    }
    else {
      Nodehun::NodehunData* nodeData = new Nodehun::NodehunData();
      nodeData->isolate = isolate;
      nodeData->callback.Reset(isolate, callback);
      nodeData->aff = new char[node::Buffer::Length(args[0])];
      strcpy(nodeData->aff, node::Buffer::Data(args[0].As<Object>()));
      nodeData->dict = new char[node::Buffer::Length(args[1])];
      strcpy(nodeData->dict, node::Buffer::Data(args[1].As<Object>()));
      nodeData->request.data = nodeData;
      uv_queue_work(uv_default_loop(), &nodeData->request,
		    Nodehun::SpellDictionary::createNewNodehunWork, Nodehun::SpellDictionary::createNewNodehunFinish);
    }
  }
  args.GetReturnValue().SetUndefined();
}

void Nodehun::SpellDictionary::createNewNodehunWork(uv_work_t* request)
{
  Nodehun::NodehunData* nodeData = static_cast<Nodehun::NodehunData*>(request->data);
  nodeData->obj = new Hunspell(nodeData->aff, nodeData->dict, NULL, true);
  delete nodeData->aff;
  delete nodeData->dict;
}

void Nodehun::SpellDictionary::createNewNodehunFinish(uv_work_t* request, int i)
{
  Nodehun::NodehunData* nodeData = static_cast<Nodehun::NodehunData*>(request->data);
  Isolate *isolate = nodeData->isolate;;
  HandleScope scope(isolate);
  const unsigned argc = 2;
  Local<Value> argv[argc];
  Handle<Value> ext = External::New(isolate, nodeData->obj);
  argv[0] = Local<Value>::New(isolate, Null(isolate));
  Local<Function> cons = Local<Function>::New(isolate, constructor);
  argv[1] = cons->NewInstance(1, &ext);
  Local<Function> cb = Local<Function>::New(isolate, nodeData->callback);
  cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
  nodeData->callback.Reset();
  delete nodeData;
}


Nodehun::SpellDictionary::SpellDictionary(const char *affbuf, const char *dictbuf)
{
  spellClass = new Hunspell(affbuf, dictbuf, NULL, true);
}<commit_msg>Fixed deprecated function call; resolves #63<commit_after>#include "nodehun.hpp"

using namespace v8;

Persistent<Function> Nodehun::SpellDictionary::constructor;

void Nodehun::SpellDictionary::Init(Handle<Object> exports, Handle<Object> module)
{
  Isolate* isolate = Isolate::GetCurrent();
  Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
  tpl->SetClassName(String::NewFromUtf8(isolate, "NodehunDictionary"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  //static
  NODE_SET_METHOD((v8::Local<v8::Template>) tpl, "createNewNodehun" , createNewNodehun);    
  //prototype
  NODE_SET_PROTOTYPE_METHOD(tpl, "isCorrect", isCorrect);
  NODE_SET_PROTOTYPE_METHOD(tpl, "isCorrectSync", isCorrectSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggest", spellSuggest);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestSync", spellSuggestSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestions", spellSuggestions);
  NODE_SET_PROTOTYPE_METHOD(tpl, "spellSuggestionsSync", spellSuggestionsSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addDictionary", addDictionary);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addDictionarySync", addDictionarySync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addWord", addWord);
  NODE_SET_PROTOTYPE_METHOD(tpl, "addWordSync", addWordSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "removeWord", removeWord);
  NODE_SET_PROTOTYPE_METHOD(tpl, "removeWordSync", removeWordSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "stem", stem);
  NODE_SET_PROTOTYPE_METHOD(tpl, "stemSync", stemSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "generate", generate);
  NODE_SET_PROTOTYPE_METHOD(tpl, "generateSync", generateSync);
  NODE_SET_PROTOTYPE_METHOD(tpl, "analyze", analyze);
  NODE_SET_PROTOTYPE_METHOD(tpl, "analyzeSync", analyzeSync);

  constructor.Reset(isolate, tpl->GetFunction());
  module->Set(String::NewFromUtf8(isolate, "exports"), tpl->GetFunction());
}

void Nodehun::SpellDictionary::createNewNodehun(const FunctionCallbackInfo<Value>& args)
{
  Isolate* isolate = args.GetIsolate();
  HandleScope scope(isolate);

  int argl = args.Length();
  if(argl > 2 && args[2]->IsFunction()) {
    Local<Function> callback = Local<Function>::Cast(args[2]);
    const unsigned argc = 2;
    Local<Value> argv[argc];
    argv[1] = Local<Value>::New(isolate, Null(isolate));
    if(!node::Buffer::HasInstance(args[0])) {
      argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "First argument must be a buffer"));
      callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
    }
    else if(!node::Buffer::HasInstance(args[1])) {
      argv[0] = Exception::TypeError(String::NewFromUtf8(isolate, "Second argument must be a buffer"));
      callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
    }
    else {
      Nodehun::NodehunData* nodeData = new Nodehun::NodehunData();
      nodeData->isolate = isolate;
      nodeData->callback.Reset(isolate, callback);
      nodeData->aff = new char[node::Buffer::Length(args[0])];
      strcpy(nodeData->aff, node::Buffer::Data(args[0].As<Object>()));
      nodeData->dict = new char[node::Buffer::Length(args[1])];
      strcpy(nodeData->dict, node::Buffer::Data(args[1].As<Object>()));
      nodeData->request.data = nodeData;
      uv_queue_work(uv_default_loop(), &nodeData->request,
		    Nodehun::SpellDictionary::createNewNodehunWork, Nodehun::SpellDictionary::createNewNodehunFinish);
    }
  }
  args.GetReturnValue().SetUndefined();
}

void Nodehun::SpellDictionary::createNewNodehunWork(uv_work_t* request)
{
  Nodehun::NodehunData* nodeData = static_cast<Nodehun::NodehunData*>(request->data);
  nodeData->obj = new Hunspell(nodeData->aff, nodeData->dict, NULL, true);
  delete nodeData->aff;
  delete nodeData->dict;
}

void Nodehun::SpellDictionary::createNewNodehunFinish(uv_work_t* request, int i)
{
  Nodehun::NodehunData* nodeData = static_cast<Nodehun::NodehunData*>(request->data);
  Isolate *isolate = nodeData->isolate;;
  HandleScope scope(isolate);
  const unsigned argc = 2;
  Local<Value> argv[argc];
  Handle<Value> ext = External::New(isolate, nodeData->obj);
  argv[0] = Local<Value>::New(isolate, Null(isolate));
  Local<Function> cons = Local<Function>::New(isolate, constructor);
  argv[1] = cons->NewInstance(v8::Context::New(isolate), 1, &ext).ToLocalChecked();
  Local<Function> cb = Local<Function>::New(isolate, nodeData->callback);
  cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
  nodeData->callback.Reset();
  delete nodeData;
}


Nodehun::SpellDictionary::SpellDictionary(const char *affbuf, const char *dictbuf)
{
  spellClass = new Hunspell(affbuf, dictbuf, NULL, true);
}
{'fraction_non_alphanumeric': 0.15203402770203947, 'fraction_numerical': 0.005780346820809248, 'mean_word_length': 6.960069444444445, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7558742', 'n_tokens_mistral': 3156, 'n_tokens_neox': 3091, 'n_words': 478}