mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
8 years ago
committed by
GitHub
9 changed files with 167 additions and 16 deletions
@ -0,0 +1,92 @@ |
|||
/*
|
|||
* duk_get_prop_desc() |
|||
*/ |
|||
|
|||
/*===
|
|||
*** test_basic (duk_safe_call) |
|||
top before: 4 |
|||
top after: 4 |
|||
top before: 5 |
|||
top after: 5 |
|||
{value:"foo",writable:true,enumerable:true,configurable:false} |
|||
{get:{_func:true},set:{_func:true},enumerable:false,configurable:true} |
|||
final top: 6 |
|||
==> rc=0, result='undefined' |
|||
*** test_nonobject (duk_safe_call) |
|||
==> rc=1, result='TypeError: object required, found null (stack index 0)' |
|||
*** test_invalid_index (duk_safe_call) |
|||
==> rc=1, result='TypeError: object required, found none (stack index 3)' |
|||
===*/ |
|||
|
|||
static duk_ret_t test_basic(duk_context *ctx, void *udata) { |
|||
(void) udata; |
|||
|
|||
duk_push_string(ctx, "dummy"); |
|||
duk_eval_string(ctx, |
|||
"(function () {\n" |
|||
" var obj = {};\n" |
|||
" Object.defineProperties(obj, {\n" |
|||
" normal: { value: 'foo', writable: true, enumerable: true, configurable: false },\n" |
|||
" accessor: { get: function mygetter() {}, set: function mysetter() {}, enumerable: false, configurable: true }\n" |
|||
" });\n" |
|||
" return obj;\n" |
|||
"})()\n"); |
|||
|
|||
duk_push_string(ctx, "dummy"); |
|||
|
|||
/* [ dummy obj dummy ] */ |
|||
|
|||
duk_push_string(ctx, "normal"); |
|||
printf("top before: %ld\n", (long) duk_get_top(ctx)); |
|||
duk_get_prop_desc(ctx, -3, 0 /*flags*/); |
|||
printf("top after: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_push_string(ctx, "accessor"); |
|||
printf("top before: %ld\n", (long) duk_get_top(ctx)); |
|||
duk_get_prop_desc(ctx, 1, 0 /*flags*/); |
|||
printf("top after: %ld\n", (long) duk_get_top(ctx)); |
|||
|
|||
/* [ dummy obj dummy desc1 desc2 ] */ |
|||
|
|||
duk_eval_string(ctx, |
|||
"(function (d1, d2) {\n" |
|||
" print(Duktape.enc('jx', d1));\n" |
|||
" print(Duktape.enc('jx', d2));\n" |
|||
"})"); |
|||
duk_dup(ctx, 3); |
|||
duk_dup(ctx, 4); |
|||
duk_call(ctx, 2); |
|||
|
|||
/* [ dummy obj dummy desc1 desc2 result ] */ |
|||
|
|||
printf("final top: %ld\n", (long) duk_get_top(ctx)); |
|||
return 0; |
|||
} |
|||
|
|||
static duk_ret_t test_nonobject(duk_context *ctx, void *udata) { |
|||
(void) udata; |
|||
|
|||
duk_push_null(ctx); |
|||
duk_push_string(ctx, "prop"); |
|||
duk_get_prop_desc(ctx, 0, 0 /*flags*/); |
|||
|
|||
printf("final top: %ld\n", (long) duk_get_top(ctx)); |
|||
return 0; |
|||
} |
|||
|
|||
static duk_ret_t test_invalid_index(duk_context *ctx, void *udata) { |
|||
(void) udata; |
|||
|
|||
duk_push_null(ctx); |
|||
duk_push_string(ctx, "prop"); |
|||
duk_get_prop_desc(ctx, 3, 0 /*flags*/); |
|||
|
|||
printf("final top: %ld\n", (long) duk_get_top(ctx)); |
|||
return 0; |
|||
} |
|||
|
|||
void test(duk_context *ctx) { |
|||
TEST_SAFE_CALL(test_basic); |
|||
TEST_SAFE_CALL(test_nonobject); |
|||
TEST_SAFE_CALL(test_invalid_index); |
|||
} |
@ -0,0 +1,43 @@ |
|||
name: duk_get_prop_desc |
|||
|
|||
proto: | |
|||
void duk_get_prop_desc(duk_context *ctx, duk_idx_t obj_idx, duk_uint_t flags); |
|||
|
|||
stack: | |
|||
[ ... obj! ... key! ] -> [ ... obj! ... desc! ] (if property exists) |
|||
[ ... obj! ... key! ] -> [ ... obj! ... undefined! ] (if property doesn't exist) |
|||
|
|||
summary: | |
|||
<p>Equivalent of <code>Object.getOwnPropertyDescriptor()</code> in the C API: |
|||
pushes a property descriptor object for a named property of the object at |
|||
<code>obj_idx</code>. If the target is not an object (or the index is invalid) |
|||
an error is thrown.</p> |
|||
|
|||
<p>No flags are defined yet, use 0 for <code>flags</code>.</p> |
|||
|
|||
example: | |
|||
duk_idx_t obj_idx = /* ... */; |
|||
|
|||
/* Check if property "my_prop" is a getter. */ |
|||
|
|||
duk_push_string(ctx, "my_prop"); |
|||
duk_get_prop_desc(ctx, obj_idx, 0 /*flags*/); |
|||
if (duk_is_object(ctx, -1)) { |
|||
/* Property found. */ |
|||
if (duk_has_prop_string(ctx, -1, "get")) { |
|||
printf("my_prop is a getter\n"); |
|||
} else { |
|||
printf("my_prop is not a getter\n"); |
|||
} |
|||
} else { |
|||
printf("my_prop not found\n"); |
|||
} |
|||
|
|||
tags: |
|||
- property |
|||
- sandbox |
|||
|
|||
seealso: |
|||
- duk_def_prop |
|||
|
|||
introduced: 2.0.0 |
Loading…
Reference in new issue