Skip to content

Commit

Permalink
Merge pull request #1458 from svaarala/api-get-opt-xxx
Browse files Browse the repository at this point in the history
Add duk_opt_xxx() API calls with explicit default for undefined/oob index
  • Loading branch information
svaarala authored Apr 8, 2017
2 parents cbe05e7 + 439d6c6 commit 8d964eb
Show file tree
Hide file tree
Showing 70 changed files with 1,751 additions and 129 deletions.
6 changes: 6 additions & 0 deletions RELEASES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,12 @@ Planned

* Add shebang support to module-node (GH-1452)

* Add duk_opt_xxx() API calls which behave like duk_require_xxx() but allow a
default value to be used when the index doesn't exist or the value is
undefined (null is rejected with TypeError to mimic ES2015 optional
arguments); for example: "int port = duk_opt_int(ctx, -3, 80);"
(GH-1458)

* Spawn the ArrayBuffer object backing a typed array lazily when its .buffer
property is first read, reducing memory usage in common cases where the view
is constructed directly without needing the ArrayBuffer object (GH-1225)
Expand Down
5 changes: 4 additions & 1 deletion src-input/duk_api_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_context *ctx, duk_idx_t idx

DUK_ASSERT(duk_is_valid_index(ctx, idx)); /* checked by caller */

ptr = duk_get_buffer_data_raw(ctx, idx, out_len, 0 /*throw_flag*/, &isbuffer);
/* XXX: with def_ptr set to a stack related pointer, isbuffer could
* be removed from the helper?
*/
ptr = duk_get_buffer_data_raw(ctx, idx, out_len, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/, &isbuffer);
if (isbuffer) {
DUK_ASSERT(*out_len == 0 || ptr != NULL);
return (const duk_uint8_t *) ptr;
Expand Down
2 changes: 1 addition & 1 deletion src-input/duk_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_context *ctx, duk_idx_t idx);
DUK_INTERNAL_DECL duk_hcompfunc *duk_get_hcompfunc(duk_context *ctx, duk_idx_t idx);
DUK_INTERNAL_DECL duk_hnatfunc *duk_get_hnatfunc(duk_context *ctx, duk_idx_t idx);

DUK_INTERNAL_DECL void *duk_get_buffer_data_raw(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size, duk_bool_t throw_flag, duk_bool_t *out_found);
DUK_INTERNAL_DECL void *duk_get_buffer_data_raw(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_len, duk_bool_t throw_flag, duk_bool_t *out_isbuffer);

DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_with_class(duk_context *ctx, duk_idx_t idx, duk_small_uint_t classnum);

Expand Down
32 changes: 30 additions & 2 deletions src-input/duk_api_public.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,25 @@ DUK_EXTERNAL_DECL void *duk_get_pointer(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL duk_c_function duk_get_c_function(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL duk_context *duk_get_context(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL void *duk_get_heapptr(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t idx, duk_size_t len);

/*
* Opt operations: like require operations but with an explicit default value
* when value is undefined or index is invalid (null and non-matching types
* cause a TypeError).
*/

DUK_EXTERNAL_DECL duk_bool_t duk_opt_boolean(duk_context *ctx, duk_idx_t idx, duk_bool_t def_value);
DUK_EXTERNAL_DECL duk_double_t duk_opt_number(duk_context *ctx, duk_idx_t idx, duk_double_t def_value);
DUK_EXTERNAL_DECL duk_int_t duk_opt_int(duk_context *ctx, duk_idx_t idx, duk_int_t def_value);
DUK_EXTERNAL_DECL duk_uint_t duk_opt_uint(duk_context *ctx, duk_idx_t idx, duk_uint_t def_value);
DUK_EXTERNAL_DECL const char *duk_opt_string(duk_context *ctx, duk_idx_t idx, const char *def_ptr);
DUK_EXTERNAL_DECL const char *duk_opt_lstring(duk_context *ctx, duk_idx_t idx, duk_size_t *out_len, const char *def_ptr, duk_size_t def_len);
DUK_EXTERNAL_DECL void *duk_opt_buffer(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size);
DUK_EXTERNAL_DECL void *duk_opt_buffer_data(duk_context *ctx, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size);
DUK_EXTERNAL_DECL void *duk_opt_pointer(duk_context *ctx, duk_idx_t idx, void *def_value);
DUK_EXTERNAL_DECL duk_c_function duk_opt_c_function(duk_context *ctx, duk_idx_t idx, duk_c_function def_value);
DUK_EXTERNAL_DECL duk_context *duk_opt_context(duk_context *ctx, duk_idx_t idx, duk_context *def_value);
DUK_EXTERNAL_DECL void *duk_opt_heapptr(duk_context *ctx, duk_idx_t idx, void *def_value);

/*
* Require operations: no coercion, throw error if index or type
Expand Down Expand Up @@ -727,6 +744,17 @@ DUK_EXTERNAL_DECL const char *duk_safe_to_lstring(duk_context *ctx, duk_idx_t id
#define duk_safe_to_string(ctx,idx) \
duk_safe_to_lstring((ctx), (idx), NULL)

/*
* Value length
*/

DUK_EXTERNAL_DECL duk_size_t duk_get_length(duk_context *ctx, duk_idx_t idx);
DUK_EXTERNAL_DECL void duk_set_length(duk_context *ctx, duk_idx_t idx, duk_size_t len);
#if 0
/* duk_require_length()? */
/* duk_opt_length()? */
#endif

/*
* Misc conversion
*/
Expand Down
Loading

0 comments on commit 8d964eb

Please sign in to comment.