<commit_before>#include "commandqueue.h"
#include "types.h"

namespace opencl {

#ifndef CL_VERSION_2_0

// /* Command Queue APIs */
// extern CL_API_ENTRY cl_command_queue CL_API_CALL
// clCreateCommandQueue(cl_context                     /* context */,
//                      cl_device_id                   /* device */,
//                      cl_command_queue_properties    /* properties */,
//                      cl_int *                       /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateCommandQueue) {
  NanScope();
  REQ_ARGS(3);

  // Arg 0
  NOCL_UNWRAP(context, NoCLContext, args[0]);

  // Arg 1
  NOCL_UNWRAP(device, NoCLDeviceId, args[1]);

  cl_command_queue_properties properties = args[2]->Uint32Value();

  cl_int err;
  cl_command_queue q = ::clCreateCommandQueue(
    context->getRaw(), device->getRaw(), properties, &err);

  CHECK_ERR(err)
  NanReturnValue(NOCL_WRAP(NoCLCommandQueue, q));
}

#else

// extern CL_API_ENTRY cl_command_queue CL_API_CALL
// clCreateCommandQueueWithProperties(cl_context               /* context */,
//                                    cl_device_id              /* device */,
//                                    const cl_queue_properties *    /* properties */,
//                                    cl_int *                 /* errcode_ret */) CL_API_SUFFIX__VERSION_2_0;
NAN_METHOD(CreateCommandQueueWithProperties) {
  NanScope();
  REQ_ARGS(3);

  // Arg 1
  NOCL_UNWRAP(context, NoCLContext, args[0]);

  // Arg 2
  NOCL_UNWRAP(device, NoCLDeviceId, args[1]);

  // Arg 3
  Local<Array> properties = Local<Array>::Cast(args[2]);
  vector<cl_queue_properties> cl_properties;

  for (uint32_t i=0; i < properties->Length(); i+=2) {
    if (!properties->Get(i)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
    }
    cl_uint prop_id = properties->Get(i)->Uint32Value();
    cl_properties.push_back(prop_id);

    if(prop_id == CL_QUEUE_PROPERTIES) {
      if (!properties->Get(i+1)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
      }
      cl_queue_properties props = properties->Get(i+1)->Int32Value();
      cl_properties.push_back(props);
    } else if (prop_id == CL_QUEUE_SIZE) {
      if (!properties->Get(i+1)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
      }
      cl_queue_properties size = properties->Get(i+1)->Int32Value();
      cl_properties.push_back(size);
    } else {
      THROW_ERR(CL_INVALID_QUEUE_PROPERTIES)
    }
  }

  cl_int err;
  cl_command_queue q = ::clCreateCommandQueueWithProperties(
    context->getRaw(),
    device->getRaw(),
    cl_properties.data(),
    &err);

  CHECK_ERR(err)

  NanReturnValue(NOCL_WRAP(NoCLCommandQueue, q));
}
#endif

// extern CL_API_ENTRY cl_int CL_API_CALL
// clRetainCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(RetainCommandQueue) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_int count=clRetainCommandQueue(q->getRaw());

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clReleaseCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(ReleaseCommandQueue) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_int count=clReleaseCommandQueue(q->getRaw());

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetCommandQueueInfo(cl_command_queue      /* command_queue */,
//                       cl_command_queue_info /* param_name */,
//                       size_t                /* param_value_size */,
//                       void *                /* param_value */,
//                       size_t *              /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetCommandQueueInfo) {
  NanScope();
  REQ_ARGS(2);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_command_queue_info param_name = args[1]->Uint32Value();

  switch(param_name) {
    case CL_QUEUE_CONTEXT: {
      cl_context val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_context), &val, nullptr))
      NanReturnValue(NOCL_WRAP(NoCLContext, val));
    }
    case CL_QUEUE_DEVICE: {
      cl_device_id val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_device_id), &val, nullptr))
      NanReturnValue(NOCL_WRAP(NoCLDeviceId, val));
    }
    case CL_QUEUE_REFERENCE_COUNT: {
      cl_uint val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_uint), &val, nullptr))
      NanReturnValue(JS_INT(val));
    }
    case CL_QUEUE_PROPERTIES: {
      cl_command_queue_properties val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_command_queue_properties), &val, nullptr))
      NanReturnValue(JS_INT(val));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

// /* Flush and Finish APIs */
// extern CL_API_ENTRY cl_int CL_API_CALL
// clFlush(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(Flush) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  CHECK_ERR(::clFlush(q->getRaw()));
  NanReturnUndefined();
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clFinish(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(Finish) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  CHECK_ERR(::clFinish(q->getRaw()));
  NanReturnUndefined();
}

// /* Enqueued Commands APIs */
// extern CL_API_ENTRY cl_int CL_API_CALL
// clEnqueueReadBuffer(cl_command_queue    /* command_queue */,
//                     cl_mem              /* buffer */,
//                     cl_bool             /* blocking_read */,
//                     size_t              /* offset */,
//                     size_t              /* size */,
//                     void *              /* ptr */,
//                     cl_uint             /* num_events_in_wait_list */,
//                     const cl_event *    /* event_wait_list */,
//                     cl_event *          /* event */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(EnqueueReadBuffer) {
  NanScope();
  REQ_ARGS(8);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  // Arg 1
  NOCL_UNWRAP(buffer, NoCLMem, args[1]);

  cl_bool blocking_read = args[2]->BooleanValue();
  size_t offset = args[3]->Uint32Value();
  size_t size = args[4]->Uint32Value();

  void *ptr=nullptr;
  int len=0;
  if(args[5]->IsUndefined() || args[5]->IsNull()) {
    CHECK_ERR(CL_INVALID_VALUE);
  }
  else
    getPtrAndLen(args[5],ptr,len);

  std::vector<NoCLEvent> cl_events;
  Local<Array> js_events = Local<Array>::Cast(args[6]);
  NOCL_TO_ARRAY(cl_events, js_events, NoCLEvent);

  cl_event event=nullptr;
  if(ARG_EXISTS(7)) {
    NOCL_UNWRAP(evt, NoCLEvent, args[7]);<commit_msg>[Fix] enqueueReadBuffer - bad required args<commit_after>#include "commandqueue.h"
#include "types.h"

namespace opencl {

#ifndef CL_VERSION_2_0

// /* Command Queue APIs */
// extern CL_API_ENTRY cl_command_queue CL_API_CALL
// clCreateCommandQueue(cl_context                     /* context */,
//                      cl_device_id                   /* device */,
//                      cl_command_queue_properties    /* properties */,
//                      cl_int *                       /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(CreateCommandQueue) {
  NanScope();
  REQ_ARGS(3);

  // Arg 0
  NOCL_UNWRAP(context, NoCLContext, args[0]);

  // Arg 1
  NOCL_UNWRAP(device, NoCLDeviceId, args[1]);

  cl_command_queue_properties properties = args[2]->Uint32Value();

  cl_int err;
  cl_command_queue q = ::clCreateCommandQueue(
    context->getRaw(), device->getRaw(), properties, &err);

  CHECK_ERR(err)
  NanReturnValue(NOCL_WRAP(NoCLCommandQueue, q));
}

#else

// extern CL_API_ENTRY cl_command_queue CL_API_CALL
// clCreateCommandQueueWithProperties(cl_context               /* context */,
//                                    cl_device_id              /* device */,
//                                    const cl_queue_properties *    /* properties */,
//                                    cl_int *                 /* errcode_ret */) CL_API_SUFFIX__VERSION_2_0;
NAN_METHOD(CreateCommandQueueWithProperties) {
  NanScope();
  REQ_ARGS(3);

  // Arg 1
  NOCL_UNWRAP(context, NoCLContext, args[0]);

  // Arg 2
  NOCL_UNWRAP(device, NoCLDeviceId, args[1]);

  // Arg 3
  Local<Array> properties = Local<Array>::Cast(args[2]);
  vector<cl_queue_properties> cl_properties;

  for (uint32_t i=0; i < properties->Length(); i+=2) {
    if (!properties->Get(i)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
    }
    cl_uint prop_id = properties->Get(i)->Uint32Value();
    cl_properties.push_back(prop_id);

    if(prop_id == CL_QUEUE_PROPERTIES) {
      if (!properties->Get(i+1)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
      }
      cl_queue_properties props = properties->Get(i+1)->Int32Value();
      cl_properties.push_back(props);
    } else if (prop_id == CL_QUEUE_SIZE) {
      if (!properties->Get(i+1)->IsNumber()) {
        THROW_ERR(CL_INVALID_VALUE);
      }
      cl_queue_properties size = properties->Get(i+1)->Int32Value();
      cl_properties.push_back(size);
    } else {
      THROW_ERR(CL_INVALID_QUEUE_PROPERTIES)
    }
  }

  cl_int err;
  cl_command_queue q = ::clCreateCommandQueueWithProperties(
    context->getRaw(),
    device->getRaw(),
    cl_properties.data(),
    &err);

  CHECK_ERR(err)

  NanReturnValue(NOCL_WRAP(NoCLCommandQueue, q));
}
#endif

// extern CL_API_ENTRY cl_int CL_API_CALL
// clRetainCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(RetainCommandQueue) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_int count=clRetainCommandQueue(q->getRaw());

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clReleaseCommandQueue(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(ReleaseCommandQueue) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_int count=clReleaseCommandQueue(q->getRaw());

  NanReturnValue(JS_INT(count));
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clGetCommandQueueInfo(cl_command_queue      /* command_queue */,
//                       cl_command_queue_info /* param_name */,
//                       size_t                /* param_value_size */,
//                       void *                /* param_value */,
//                       size_t *              /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(GetCommandQueueInfo) {
  NanScope();
  REQ_ARGS(2);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  cl_command_queue_info param_name = args[1]->Uint32Value();

  switch(param_name) {
    case CL_QUEUE_CONTEXT: {
      cl_context val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_context), &val, nullptr))
      NanReturnValue(NOCL_WRAP(NoCLContext, val));
    }
    case CL_QUEUE_DEVICE: {
      cl_device_id val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_device_id), &val, nullptr))
      NanReturnValue(NOCL_WRAP(NoCLDeviceId, val));
    }
    case CL_QUEUE_REFERENCE_COUNT: {
      cl_uint val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_uint), &val, nullptr))
      NanReturnValue(JS_INT(val));
    }
    case CL_QUEUE_PROPERTIES: {
      cl_command_queue_properties val;
      CHECK_ERR(::clGetCommandQueueInfo(q->getRaw(),param_name,sizeof(cl_command_queue_properties), &val, nullptr))
      NanReturnValue(JS_INT(val));
    }
  }
  return NanThrowError(JS_INT(CL_INVALID_VALUE));
}

// /* Flush and Finish APIs */
// extern CL_API_ENTRY cl_int CL_API_CALL
// clFlush(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(Flush) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  CHECK_ERR(::clFlush(q->getRaw()));
  NanReturnUndefined();
}

// extern CL_API_ENTRY cl_int CL_API_CALL
// clFinish(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(Finish) {
  NanScope();
  REQ_ARGS(1);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  CHECK_ERR(::clFinish(q->getRaw()));
  NanReturnUndefined();
}

// /* Enqueued Commands APIs */
// extern CL_API_ENTRY cl_int CL_API_CALL
// clEnqueueReadBuffer(cl_command_queue    /* command_queue */,
//                     cl_mem              /* buffer */,
//                     cl_bool             /* blocking_read */,
//                     size_t              /* offset */,
//                     size_t              /* size */,
//                     void *              /* ptr */,
//                     cl_uint             /* num_events_in_wait_list */,
//                     const cl_event *    /* event_wait_list */,
//                     cl_event *          /* event */) CL_API_SUFFIX__VERSION_1_0;
NAN_METHOD(EnqueueReadBuffer) {
  NanScope();
  REQ_ARGS(6);

  // Arg 0
  NOCL_UNWRAP(q, NoCLCommandQueue, args[0]);

  // Arg 1
  NOCL_UNWRAP(buffer, NoCLMem, args[1]);

  cl_bool blocking_read = args[2]->BooleanValue();
  size_t offset = args[3]->Uint32Value();
  size_t size = args[4]->Uint32Value();

  void *ptr=nullptr;
  int len=0;
  if(args[5]->IsUndefined() || args[5]->IsNull()) {
    CHECK_ERR(CL_INVALID_VALUE);
  }
  else
    getPtrAndLen(args[5],ptr,len);

  std::vector<NoCLEvent> cl_events;

  if (ARG_EXISTS(6)) {
    Local<Array> js_events = Local<Array>::Cast(args[6]);
    NOCL_TO_ARRAY(cl_events, js_events, NoCLEvent);
  }

  cl_event event=nullptr;
  if(ARG_EXISTS(7)) {
    NOCL_UNWRAP(evt, NoCLEvent, args[7]);
{'fraction_non_alphanumeric': 0.13260269995645232, 'fraction_numerical': 0.012265931194658151, 'mean_word_length': 2.7688730853391683, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4513218', 'n_tokens_mistral': 4896, 'n_tokens_neox': 4684, 'n_words': 693}