-
Notifications
You must be signed in to change notification settings - Fork 683
Introduce the Array Buffer C API #2161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce the Array Buffer C API #2161
Conversation
As per #1832 I've created an API proposal for the Array Buffers. Some notes:
|
docs/02.API-REFERENCE.md
Outdated
there is no out of bounds reads or writes. | ||
|
||
After using the pointer the [jerry_arraybuffer_unmap][#jerry_arraybuffer_unmap) | ||
function must be called. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong bracket
docs/02.API-REFERENCE.md
Outdated
**Summary** | ||
|
||
Release the Array Buffer after a [jerry_arraybuffer_map][#jerry_arraybuffer_map) | ||
function call. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
docs/02.API-REFERENCE.md
Outdated
```c | ||
{ | ||
jerry_value_t buffer = jerry_create_arraybuffer (22); | ||
uint8_t * const data = jerry_arraybuffer_map (buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space only before the asterisk character
jerry-core/api/jerry.c
Outdated
|
||
ecma_object_t *buffer_p = ecma_get_object_from_value (buffer); | ||
lit_utf8_byte_t *mem_buffer_p = ecma_arraybuffer_get_buffer (buffer_p); | ||
return (uint8_t * const) mem_buffer_p; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
docs/02.API-REFERENCE.md
Outdated
// acquire the Array Buffer from somewhere | ||
|
||
uint8_t * const data = jerry_arraybuffer_map (array_buffer); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
uint8_t *buf_p, | ||
jerry_length_t buf_size); | ||
jerry_length_t jerry_arraybuffer_get_length (const jerry_value_t value); | ||
uint8_t * jerry_arraybuffer_map (const jerry_value_t value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
tests/unit-core/test-arraybuffer.c
Outdated
TEST_ASSERT (jerry_value_is_arraybuffer (buffer)); | ||
TEST_ASSERT (jerry_arraybuffer_get_length (buffer) == 20); | ||
|
||
uint8_t * const data = jerry_arraybuffer_map (buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (informally) after these minor fixes.
docs/02.API-REFERENCE.md
Outdated
|
||
jerry_value_t buffer; | ||
// ... create the Array Buffer or acuiqre it from somewhere. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*acquire
docs/02.API-REFERENCE.md
Outdated
|
||
**Summary** | ||
|
||
The function allows acces to the contents of the Array Buffer directly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
access
985816f
to
3cd07f6
Compare
I've updated the PR with the requested changes. Also I've removed the |
docs/02.API-REFERENCE.md
Outdated
|
||
```c | ||
jerry_length_t | ||
jerry_arraybuffer_get_length (const jerry_value_t value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a jerry_get_array_length
function already. I am thinking whether this could be combined with it. Maybe just introducing a jerry_get_length() function which works with any special object (list in the comment what types it supports). But that could increase the binary size. I am unsure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The jerry_arraybuffer_get_length
returns the byteLength
property (which is specified ad construction time) and the arraybuffer does not have a length
property. I'm not really sure we should remove this function, but we could change the name to have byte_length
suffix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a name like: jerry_get_arraybuffer_byte_length
? so we'll have a common naming scheme for "getters".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, actually sounds good.
docs/02.API-REFERENCE.md
Outdated
|
||
**Summary** | ||
|
||
Read the contents of the Array Buffer into a buffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy the portion of the Array Buffer into a user provided buffer.
docs/02.API-REFERENCE.md
Outdated
|
||
**Summary** | ||
|
||
Write the contents of a buffer into the Array Buffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy again
docs/02.API-REFERENCE.md
Outdated
jerry_arraybuffer_write (const jerry_value_t value, | ||
jerry_length_t offset, | ||
const uint8_t *buf_p, | ||
jerry_length_t buf_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong alignment
jerry-core/api/jerry.c
Outdated
|
||
ecma_object_t *buffer_p = ecma_get_object_from_value (buffer); | ||
jerry_length_t length = ecma_arraybuffer_get_length (buffer_p); | ||
jerry_length_t copy_count = JERRY_MIN (length - offset, buf_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens it offset > length? In some range, it will be a big number, and the min mitigates the issue, but if offset is 0xffffffff that could be exploited.
I would add an early return if offset >= length.
jerry-core/api/jerry.c
Outdated
|
||
ecma_object_t *buffer_p = ecma_get_object_from_value (buffer); | ||
jerry_length_t length = ecma_arraybuffer_get_length (buffer_p); | ||
jerry_length_t copy_count = JERRY_MIN (length - offset, buf_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
offset issue again
d4e26eb
to
ffea5b8
Compare
@zherczeg I've updated the PR with the requested changes, and also added two new api tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few changes. Patch is close to ready.
docs/02.API-REFERENCE.md
Outdated
jerry_create_arraybuffer (jerry_length_t size); | ||
``` | ||
|
||
- `size` - size of the ArrayBuffer to create |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
byte size. I think we need to emphasize this.
tests/unit-core/test-arraybuffer.c
Outdated
{ | ||
return jerry_create_boolean (true); | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need else after return.
ffea5b8
to
4d76a8c
Compare
Add C API to work with Array Buffers. The following methods are added: - jerry_value_is_arraybuffer - jerry_create_arraybuffer - jerry_arraybuffer_write - jerry_arraybuffer_read - jerry_get_arraybuffer_byte_length JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
4d76a8c
to
c90f081
Compare
@zherczeg updated PR based on requests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hey @galpeter, thanks for adding this. Looks great. |
Please check #2162 . The idea is that we allow arraybuffer to be created for external byte arrays. This allows communication between jerryscript instances, direct access for screen memory, etc. However there is no plan for accessing data of internally created arraybuffers. |
@zherczeg got it, makes sense, thanks! |
@martijnthe initially I had that non-copy api which returned the internal buffer pointer, but that was to risky so I've changed the logic to external buffers which can be seen in #2162. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Add C API to work with Array Buffers.
The following methods are added:
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]