<commit_before>#include <v8.h>
#include <nan.h>

#include "../common.h"
#include "../structures/handles.h"
#include "../structures/oc-payload.h"

extern "C" {
#include <ocstack.h>
#include <ocpayload.h>
}

using namespace v8;
using namespace node;

NAN_METHOD(bind_OCNotifyAllObservers) {
  NanScope();

  VALIDATE_ARGUMENT_COUNT(args, 2);
  VALIDATE_ARGUMENT_TYPE(args, 0, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 1, IsNumber);

  OCResourceHandle handle;
  if (!c_OCResourceHandle(Local<Array>::Cast(args[0]), &handle)) {
    NanReturnUndefined();
  }

  NanReturnValue(NanNew<Number>(OCNotifyAllObservers(
      handle, (OCQualityOfService)args[1]->Uint32Value())));
}

NAN_METHOD(bind_OCNotifyListOfObservers) {
  NanScope();

  VALIDATE_ARGUMENT_COUNT(args, 5);
  VALIDATE_ARGUMENT_TYPE(args, 0, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 1, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 2, IsNumber);
  VALIDATE_ARGUMENT_TYPE(args, 3, IsObject);
  VALIDATE_ARGUMENT_TYPE(args, 4, IsNumber);

  OCResourceHandle handle;
  if (!c_OCResourceHandle(Local<Array>::Cast(args[0]), &handle)) {
    NanReturnUndefined();
  }

  // Construct the C array of observation IDs.
  Local<Array> obsIds = Local<Array>::Cast(args[1]);
  uint8_t arrayLength = (uint8_t)obsIds->Length();
  uint8_t numberOfIds = (uint8_t)args[2]->Uint32Value();

  // If the array contains fewer IDs than the reported number, we use the lower
  // number
  numberOfIds = (numberOfIds < arrayLength ? numberOfIds : arrayLength);

  OCObservationId *c_observations =
      (OCObservationId *)malloc(numberOfIds * sizeof(OCObservationId));
  if (!c_observations && numberOfIds > 0) {
    NanThrowError(
        "OCNotifyListOfObservers: Failed to allocate list of observers");
    NanReturnUndefined();
  }

  // Populate a C-like array from the V8 array
  int index;
  for (index = 0; index < numberOfIds; index++) {
    Local<Value> oneObservationId = obsIds->Get(index);
    if (!(oneObservationId->IsNumber())) {
      NanThrowTypeError("OCObservationID must satisfy IsNumber()");
      NanReturnUndefined();
    }
    c_observations[index] = (OCObservationId)oneObservationId->Uint32Value();
  }

  OCRepPayload *payload;
  if (args[3]->IsObject()) {
    if (!c_OCPayload(args[3]->ToObject(), (OCPayload **)&payload)) {
      NanReturnUndefined();
    }
  }

  Local<Number> returnValue = NanNew<Number>(
      OCNotifyListOfObservers(handle, c_observations, numberOfIds, payload,
                              (OCQualityOfService)args[4]->Uint32Value()));

  free(c_observations);
  OCRepPayloadDestroy(payload);

  NanReturnValue(returnValue);
}<commit_msg>Bindings: Simplify and fix memory leak in OCNotifyListOfObservers()<commit_after>#include <v8.h>
#include <nan.h>

#include "../common.h"
#include "../structures/handles.h"
#include "../structures/oc-payload.h"

extern "C" {
#include <ocstack.h>
#include <ocpayload.h>
}

using namespace v8;
using namespace node;

NAN_METHOD(bind_OCNotifyAllObservers) {
  NanScope();

  VALIDATE_ARGUMENT_COUNT(args, 2);
  VALIDATE_ARGUMENT_TYPE(args, 0, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 1, IsNumber);

  OCResourceHandle handle;
  if (!c_OCResourceHandle(Local<Array>::Cast(args[0]), &handle)) {
    NanReturnUndefined();
  }

  NanReturnValue(NanNew<Number>(OCNotifyAllObservers(
      handle, (OCQualityOfService)args[1]->Uint32Value())));
}

NAN_METHOD(bind_OCNotifyListOfObservers) {
  NanScope();

  VALIDATE_ARGUMENT_COUNT(args, 4);
  VALIDATE_ARGUMENT_TYPE(args, 0, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 1, IsArray);
  VALIDATE_ARGUMENT_TYPE(args, 2, IsObject);
  VALIDATE_ARGUMENT_TYPE(args, 3, IsNumber);

  OCResourceHandle handle;
  if (!c_OCResourceHandle(Local<Array>::Cast(args[0]), &handle)) {
    NanReturnUndefined();
  }

  // Construct the C array of observation IDs.
  Local<Array> obsIds = Local<Array>::Cast(args[1]);
  uint8_t arrayLength = (uint8_t)obsIds->Length();

  OCObservationId *c_observations = 0;

  if (arrayLength > 0) {
    c_observations =
        (OCObservationId *)malloc(arrayLength * sizeof(OCObservationId));
    if (!c_observations && arrayLength > 0) {
      NanThrowError(
          "OCNotifyListOfObservers: Failed to allocate list of observers");
      NanReturnUndefined();
    }
  }

  // Populate a C-like array from the V8 array
  int index;
  for (index = 0; index < arrayLength; index++) {
    Local<Value> oneObservationId = obsIds->Get(index);
    if (!(oneObservationId->IsUint32())) {
      free(c_observations);
      NanThrowTypeError("OCObservationID must satisfy IsNumber()");
      NanReturnUndefined();
    }
    c_observations[index] = (OCObservationId)oneObservationId->Uint32Value();
  }

  OCRepPayload *payload = 0;
  if (!c_OCPayload(args[2]->ToObject(), (OCPayload **)&payload)) {
    free(c_observations);
    NanReturnUndefined();
  }

  Local<Number> returnValue = NanNew<Number>(
      OCNotifyListOfObservers(handle, c_observations, arrayLength, payload,
                              (OCQualityOfService)args[3]->Uint32Value()));

  free(c_observations);
  OCRepPayloadDestroy(payload);

  NanReturnValue(returnValue);
}
{'fraction_non_alphanumeric': 0.1356263435606801, 'fraction_numerical': 0.012898182528825484, 'mean_word_length': 4.6993318485523385, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 46, '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': '3485895', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1718, 'n_words': 323}