Skip to content

Commit

Permalink
[buffer] Make length attribute read-only
Browse files Browse the repository at this point in the history
Fixes issue intel#172.

Signed-off-by: Geoff Gustafson <[email protected]>
  • Loading branch information
grgustaf committed Dec 23, 2016
1 parent e0d4559 commit faeec02
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/zjs_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ jerry_value_t zjs_buffer_create(uint32_t size)
zjs_buffers = buf_item;

jerry_set_prototype(buf_obj, zjs_buffer_prototype);
zjs_obj_add_number(buf_obj, size, "length");
zjs_obj_add_readonly_number(buf_obj, size, "length");

// TODO: sign up to get callback when the object is freed, then free the
// buffer and remove it from the list
Expand All @@ -389,8 +389,8 @@ static jerry_value_t zjs_buffer(const jerry_value_t function_obj,
const jerry_value_t argv[],
const jerry_length_t argc)
{
// requires: single argument can be a numeric size in bytes, an array of uint8,
// or a string.
// requires: single argument can be a numeric size in bytes, an array of
// uint8, or a string
// effects: constructs a new JS Buffer object, and an associated buffer
// tied to it through a zjs_buffer_t struct stored in a global
// list
Expand Down
16 changes: 16 additions & 0 deletions src/zjs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ void zjs_obj_add_number(jerry_value_t obj, double num, const char *name)
jerry_release_value(jnum);
}

void zjs_obj_add_readonly_number(jerry_value_t obj, double num,
const char *name)
{
// requires: obj is an existing JS object
// effects: creates a new readonly field in parent named name, set to nval
jerry_value_t jname = jerry_create_string((const jerry_char_t *)name);
jerry_property_descriptor_t pd;
jerry_init_property_descriptor_fields(&pd);
pd.is_writable = false;
pd.is_value_defined = true;
pd.value = jerry_create_number(num);
jerry_define_own_property(obj, jname, &pd);
jerry_free_property_descriptor_fields(&pd);
jerry_release_value(jname);
}

bool zjs_obj_get_boolean(jerry_value_t obj, const char *name, bool *flag)
{
// requires: obj is an existing JS object, value name should exist as
Expand Down
2 changes: 2 additions & 0 deletions src/zjs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void zjs_obj_add_object(jerry_value_t parent, jerry_value_t child,
const char *name);
void zjs_obj_add_string(jerry_value_t obj, const char *str, const char *name);
void zjs_obj_add_number(jerry_value_t obj, double num, const char *name);
void zjs_obj_add_readonly_number(jerry_value_t obj, double num,
const char *name);

bool zjs_obj_get_boolean(jerry_value_t obj, const char *name, bool *flag);
bool zjs_obj_get_string(jerry_value_t obj, const char *name, char *buffer,
Expand Down

0 comments on commit faeec02

Please sign in to comment.