Fix promise thenable bug

Related Issue: #2060

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
Zidong Jiang 2017-10-27 13:23:10 +08:00 committed by yichoi
parent d7c710d3d4
commit 0bc4bb056d
4 changed files with 43 additions and 9 deletions

View File

@ -232,14 +232,20 @@ ecma_process_promise_resolve_thenable_job (void *obj_p) /**< the job to be opera
{
ecma_job_promise_resolve_thenable_t *job_p = (ecma_job_promise_resolve_thenable_t *) obj_p;
ecma_object_t *promise_p = ecma_get_object_from_value (job_p->promise);
ecma_promise_resolving_functions_t *funcs = ecma_promise_create_resolving_functions (promise_p);
ecma_string_t str_resolve, str_reject;
ecma_init_ecma_magic_string (&str_resolve, LIT_INTERNAL_MAGIC_STRING_RESOLVE_FUNCTION);
ecma_init_ecma_magic_string (&str_reject, LIT_INTERNAL_MAGIC_STRING_REJECT_FUNCTION);
ecma_op_object_put (promise_p,
&str_resolve,
funcs->resolve,
false);
ecma_op_object_put (promise_p,
&str_reject,
funcs->reject,
false);
ecma_value_t resolve = ecma_op_object_get (promise_p, &str_resolve);
ecma_value_t reject = ecma_op_object_get (promise_p, &str_reject);
ecma_value_t argv[] = { resolve, reject };
ecma_value_t argv[] = { funcs->resolve, funcs->reject };
ecma_value_t ret;
ecma_value_t then_call_result = ecma_op_function_call (ecma_get_object_from_value (job_p->then),
job_p->thenable,
@ -250,7 +256,7 @@ ecma_process_promise_resolve_thenable_job (void *obj_p) /**< the job to be opera
if (ECMA_IS_VALUE_ERROR (then_call_result))
{
ret = ecma_op_function_call (ecma_get_object_from_value (reject),
ret = ecma_op_function_call (ecma_get_object_from_value (funcs->reject),
ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED),
&then_call_result,
1);
@ -258,8 +264,7 @@ ecma_process_promise_resolve_thenable_job (void *obj_p) /**< the job to be opera
ecma_free_value (then_call_result);
}
ecma_free_value (resolve);
ecma_free_value (reject);
ecma_promise_free_resolving_functions (funcs);
ecma_free_promise_resolve_thenable_job (job_p);
return ret;

View File

@ -412,7 +412,7 @@ ecma_call_builtin_executor (ecma_object_t *executor_p, /**< the executor object
*
* @return pointer to the resolving functions
*/
static ecma_promise_resolving_functions_t *
ecma_promise_resolving_functions_t *
ecma_promise_create_resolving_functions (ecma_object_t *object_p) /**< the promise object */
{
/* 1. */
@ -463,7 +463,7 @@ ecma_promise_create_resolving_functions (ecma_object_t *object_p) /**< the promi
/**
* Free the heap and the member of the resolving functions.
*/
static void
void
ecma_promise_free_resolving_functions (ecma_promise_resolving_functions_t *funcs) /**< points to the functions */
{
ecma_free_value (funcs->resolve);

View File

@ -97,6 +97,9 @@ ecma_value_t
ecma_promise_then (ecma_value_t promise,
ecma_value_t on_fulfilled,
ecma_value_t on_rejected);
ecma_promise_resolving_functions_t *
ecma_promise_create_resolving_functions (ecma_object_t *object_p);
void ecma_promise_free_resolving_functions (ecma_promise_resolving_functions_t *funcs);
/**
* @}

View File

@ -0,0 +1,26 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Test the thenable job.
var p = Promise.resolve(1).then(function(x) {
assert(x === 1);
return Promise.resolve(2);
}).then(function(x) {
assert(x === 2);
return Promise.reject(3);
}).catch(function(x) {
assert(x === 3);
});