Repository: jerryscript-project/jerryscript
File: tests/unit-core/test-arraybuffer.c (C)

Code before:

    TEST_ASSERT (jerry_value_is_arraybuffer (arrayb));
    jerry_release_value (arrayb);
  }

  /* Test ArrayBuffer external memory map/unmap */
  {
    const uint32_t buffer_size = 20;
    /* cppcheck-suppress variableScope */
    JERRY_VLA (uint8_t, buffer_p, buffer_size);
    {
      jerry_value_t input_buffer = jerry_create_arraybuffer_external (buffer_size, buffer_p, NULL);
      register_js_value ("input_buffer", input_buffer);
      jerry_release_value (input_buffer);
    }

    const jerry_char_t eval_arraybuffer_src[] = TEST_STRING_LITERAL (
      "var array = new Uint8Array(input_buffer);"
      "for (var i = 0; i < array.length; i++)"
      "{"
      "  array[i] = i * 2;"
      "};"
      "array.buffer"
    );
    jerry_value_t buffer = jerry_eval (eval_arraybuffer_src,
                                       sizeof (eval_arraybuffer_src) - 1,
                                       JERRY_PARSE_STRICT_MODE);

    TEST_ASSERT (!jerry_value_is_error (buffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (buffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (buffer) == 20);

    uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);

    /* test memory read */
    for (int i = 0; i < 20; i++)
    {
      TEST_ASSERT (data[i] == (uint8_t) (i * 2));
    }

    /* "upload" new data */
    double sum = 0;
    for (int i = 0; i < 20; i++)
    {
      data[i] = (uint8_t)(i * 3);
      sum += data[i];
    }

    const jerry_char_t eval_test_arraybuffer[] = TEST_STRING_LITERAL (
      "var sum = 0;"
      "for (var i = 0; i < array.length; i++)"

Review comment (suggestion):
False warning, buffer_p can't be moved to the next block, because it is used from JS code: "var array = new Uint8Array(input_buffer);". Of course cppcheck shouldn't know that jerry_create_arraybuffer_external  and register_js_value propagates buffer_p to a JS code. I can't see any better fix than suppressing the warning.

Code after:
    TEST_ASSERT (buffer_p[0] == new_value);
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (arrayb) == buffer_size);

    for (uint32_t i = 1; i < buffer_size; i++)
    {
      TEST_ASSERT (buffer_p[i] == base_value);
    }

    JERRY_VLA (uint8_t, test_buffer, buffer_size);
    jerry_length_t read = jerry_arraybuffer_read (arrayb, 0, test_buffer, buffer_size);
    TEST_ASSERT (read == buffer_size);
    TEST_ASSERT (test_buffer[0] == new_value);

    for (uint32_t i = 1; i < buffer_size; i++)
    {
      TEST_ASSERT (test_buffer[i] == base_value);
    }

    TEST_ASSERT (jerry_value_is_arraybuffer (arrayb));
    jerry_release_value (arrayb);
  }

  /* Test ArrayBuffer external memory map/unmap */
  {
    const uint32_t buffer_size = 20;
    /* cppcheck-suppress variableScope */
    JERRY_VLA (uint8_t, buffer_p, buffer_size);
    {
      jerry_value_t input_buffer = jerry_create_arraybuffer_external (buffer_size, buffer_p, NULL);
      register_js_value ("input_buffer", input_buffer);
      jerry_release_value (input_buffer);
    }

    const jerry_char_t eval_arraybuffer_src[] = TEST_STRING_LITERAL (
      "var array = new Uint8Array(input_buffer);"
      "for (var i = 0; i < array.length; i++)"
      "{"
      "  array[i] = i * 2;"
      "};"
      "array.buffer"
    );
    jerry_value_t buffer = jerry_eval (eval_arraybuffer_src,
                                       sizeof (eval_arraybuffer_src) - 1,
                                       JERRY_PARSE_STRICT_MODE);

    TEST_ASSERT (!jerry_value_is_error (buffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (buffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (buffer) == 20);

    uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);