Skip to content

Commit

Permalink
add doc
Browse files Browse the repository at this point in the history
JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
  • Loading branch information
jiangzidong committed May 3, 2017
1 parent 728c828 commit 6482201
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 62 deletions.
138 changes: 136 additions & 2 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,47 @@ jerry_value_is_object (const jerry_value_t value)
- [jerry_release_value](#jerry_release_value)


## jerry_value_is_promise

**Summary**

Returns whether the given `jerry_value_t` is a promise value.

*Note*: This API depends on the ES2015-subset profile.

**Prototype**

```c
bool
jerry_value_is_promise (const jerry_value_t value)
```

- `value` - api value
- return value
- true, if the given `jerry_value_t` is a promise
- false, otherwise

**Example**

```c
{
jerry_value_t value;
... // create or acquire value

if (jerry_value_is_promise (value))
{
...
}

jerry_release_value (value);
}
```

**See also**

- [jerry_release_value](#jerry_release_value)


## jerry_value_is_string

**Summary**
Expand Down Expand Up @@ -1980,6 +2021,64 @@ jerry_value_to_string (const jerry_value_t value);
- [jerry_value_to_primitive](#jerry_value_to_primitive)


# Functions for promise value

These APIs all depends on the ES2015-subset profile.

## jerry_resolve_or_reject_promise

**Summary**

Resolve or reject the promise with an argument.

**Prototype**

```c
jerry_value_t
jerry_resolve_or_reject_promise (jerry_value_t promise,
jerry_value_t argument,
bool is_resolve)
```

- `promise` - the promise value
- `argument` - the argument for resolve or reject
- `is_resolve` - whether the promise should be resolved or rejected
- return value
- undefined jerry value - resolve or reject successed
- jerry value with error flag - otherwise

**Example**

```c
{
jerry_value_t promise = ... // acquire/create a promise object.

...

bool is_resolve = ... // whether the promise should be resolve or reject
jerry_value_t argument = ... // prepare the argumnent for the resolve or reject.

jerry_value_t is_ok = jerry_resolve_or_reject_promise (promise,
argument,
is_resolve);

if (jerry_value_has_error_flag (is_ok))
{
// handle the error.
}

jerry_release_value (is_ok);
jerry_release_value (argument);
jerry_release_value (promise);
}
```

**See also**

- [jerry_release_value](#jerry_release_value)
- [jerry_value_has_error_flag](#jerry_value_has_error_flag)


# Acquire and release API values

## jerry_acquire_value
Expand Down Expand Up @@ -2425,6 +2524,41 @@ jerry_create_object (void);
- [jerry_release_value](#jerry_release_value)


## jerry_create_promise

**Summary**

Create an empty Promise object which can be resolve/reject later
by calling jerry_resolve_or_reject_promise.

*Note*: This API depends on the ES2015-subset profile.

**Prototype**

```c
jerry_value_t
jerry_create_promise (void)
```

- return value - value of the created promise

**Example**

```c
{
jerry_value_t p = jerry_create_promise ();

...// usage of the promise

jerry_release_value (p);
}

**See also**

- [jerry_resolve_or_reject_promise](#jerry_resolve_or_reject_promise)
- [jerry_release_value](#jerry_release_value)


## jerry_create_string

**Summary**
Expand Down Expand Up @@ -3528,7 +3662,7 @@ static const jerry_object_native_info_t native_obj_type_info =
{
// The type of this's native pointer matches what is expected.
// Only now is it safe to cast to native_obj_t * and dereference the
// pointer:
// pointer:
native_obj_t *native_obj = native_p;
native_obj->bar = ...; // Safe to access now!
}
Expand Down Expand Up @@ -3582,7 +3716,7 @@ jerry_set_object_native_pointer (const jerry_value_t obj_val,
**Example**

See [jerry_get_object_native_pointer](#jerry_get_object_native_pointer) for a
best-practice example.
best-practice example.

**See also**

Expand Down
48 changes: 0 additions & 48 deletions jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -2252,54 +2252,6 @@ jerry_resolve_or_reject_promise (jerry_value_t promise, /**< the promise value *
return ret;
} /* jerry_resolve_or_reject_promise */

/**
* Get the resolve function of the promise object.
*
* Note:
* returned value must be freed with jerry_release_value, when it is no longer needed.
*
* @return function value or error
*/
jerry_value_t
jerry_promise_get_resolve (jerry_value_t promise) /**< promise object */
{
jerry_assert_api_available ();

if (!ecma_is_value_object (promise) || !ecma_is_promise (ecma_get_object_from_value (promise)))
{
return ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p));
}

ecma_string_t str;
ecma_init_ecma_magic_string (&str, LIT_INTERNAL_MAGIC_STRING_RESOLVE_FUNCTION);

return ecma_op_object_get (ecma_get_object_from_value (promise), &str);
} /* jerry_promise_get_resolve */

/**
* Get the reject function of the promise object.
*
* Note:
* returned value must be freed with jerry_release_value, when it is no longer needed.
*
* @return function value or error
*/
jerry_value_t
jerry_promise_get_reject (jerry_value_t promise) /**< promise object */
{
jerry_assert_api_available ();

if (!ecma_is_value_object (promise) || !ecma_is_promise (ecma_get_object_from_value (promise)))
{
return ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p));
}

ecma_string_t str;
ecma_init_ecma_magic_string (&str, LIT_INTERNAL_MAGIC_STRING_REJECT_FUNCTION);

return ecma_op_object_get (ecma_get_object_from_value (promise), &str);
} /* jerry_promise_get_reject */

#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */

/**
Expand Down
2 changes: 0 additions & 2 deletions jerry-core/jerryscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,6 @@ bool jerry_foreach_object_property (const jerry_value_t obj_val, jerry_object_pr
* Promise resolve/reject functions.
*/
jerry_value_t jerry_resolve_or_reject_promise (jerry_value_t promise, jerry_value_t argument, bool is_resolve);
jerry_value_t jerry_promise_get_resolve (jerry_value_t promise);
jerry_value_t jerry_promise_get_reject (jerry_value_t promise);

/**
* Input validator functions.
Expand Down
10 changes: 0 additions & 10 deletions tests/unit-core/test-promise.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ main (void)
jerry_resolve_or_reject_promise (my_promise2, str_resolve, true);
jerry_resolve_or_reject_promise (my_promise1, str_reject, false);

/* Test jerry_promise_get_resolve/jerry_promise_get_reject. */
jerry_value_t resolve_func = jerry_promise_get_resolve (my_promise1);
jerry_value_t reject_func = jerry_promise_get_reject (my_promise1);

TEST_ASSERT (jerry_value_is_function (resolve_func));
TEST_ASSERT (jerry_value_is_function (reject_func));

jerry_release_value (resolve_func);
jerry_release_value (reject_func);

/* Run the jobqueue. */
res = jerry_port_default_jobqueue_run ();

Expand Down

0 comments on commit 6482201

Please sign in to comment.