Skip to content

Implement the basic Promise #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions jerry-core/ecma/base/ecma-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
#include "ecma-typedarray-object.h"
#endif
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
#include "ecma-promise-object.h"
#endif

#define JERRY_INTERNAL
#include "jerry-internal.h"
Expand Down Expand Up @@ -177,7 +180,7 @@ ecma_gc_mark_property (ecma_property_pair_t *property_pair_p, /**< property pair
case ECMA_PROPERTY_TYPE_NAMEDDATA:
{
if (ECMA_PROPERTY_GET_NAME_TYPE (property) == ECMA_STRING_CONTAINER_MAGIC_STRING
&& property_pair_p->names_cp[index] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT)
&& property_pair_p->names_cp[index] >= LIT_NEED_MARK_MAGIC_STRING__COUNT)
{
break;
}
Expand Down Expand Up @@ -260,6 +263,41 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */

switch (ecma_get_object_type (object_p))
{
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
case ECMA_OBJECT_TYPE_CLASS:
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;

if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_PROMISE_UL)
{
/* Mark promise result. */
ecma_value_t result = ext_object_p->u.class_prop.u.value;

if (ecma_is_value_object (result))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (result), true);
}

/* Mark all reactions. */
ecma_collection_iterator_t iter;
ecma_collection_iterator_init (&iter, ((ecma_promise_object_t *) ext_object_p)->fulfill_reactions);

while (ecma_collection_iterator_next (&iter))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*iter.current_value_p), true);
}

ecma_collection_iterator_init (&iter, ((ecma_promise_object_t *) ext_object_p)->reject_reactions);

while (ecma_collection_iterator_next (&iter))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*iter.current_value_p), true);
}
}

break;
}
#endif /*! CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
Expand All @@ -274,14 +312,14 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
ecma_gc_set_object_visited (lex_env_p, true);
break;
}
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
case ECMA_PSEUDO_ARRAY_TYPEDARRAY:
case ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO:
{
ecma_gc_set_object_visited (ecma_typedarray_get_arraybuffer (object_p), true);
break;
}
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
default:
{
JERRY_UNREACHABLE ();
Expand Down Expand Up @@ -532,7 +570,18 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
ecma_dealloc_extended_object ((ecma_extended_object_t *) object_p, size);
return;
}

#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
case LIT_MAGIC_STRING_PROMISE_UL:
{
ecma_free_value_if_not_object (ext_object_p->u.class_prop.u.value);
ecma_free_values_collection (((ecma_promise_object_t *) object_p)->fulfill_reactions, false);
ecma_free_values_collection (((ecma_promise_object_t *) object_p)->reject_reactions, false);
ecma_dealloc_extended_object ((ecma_extended_object_t *) object_p, sizeof (ecma_promise_object_t));
return;
}
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
default:
{
JERRY_UNREACHABLE ();
Expand Down
6 changes: 6 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-global.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ OBJECT_VALUE (LIT_MAGIC_STRING_UINT8_CLAMPED_ARRAY_UL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)

#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
OBJECT_VALUE (LIT_MAGIC_STRING_PROMISE_UL,
ECMA_BUILTIN_ID_PROMISE,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */

/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */

Expand Down
79 changes: 79 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-promise-prototype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* 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.
*/

#include "ecma-exceptions.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-promise-object.h"

#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN

#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"

#define BUILTIN_INC_HEADER_NAME "ecma-builtin-promise-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID promise_prototype
#include "ecma-builtin-internal-routines-template.inc.h"

/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup promiseprototype ECMA Promise.prototype object built-in
* @{
*/

/**
* Promise routine: then.
*
* See also: 25.4.5.3
*
* @return ecma value of a new promise object
* Returned value must be freed with ecma_free_value
*/
static ecma_value_t
ecma_builtin_promise_prototype_then (ecma_value_t this_arg, /**< this argument */
ecma_value_t on_fulfilled, /**< on_fulfilled function */
ecma_value_t on_rejected) /**< on_rejected function */
{
ecma_object_t *obj = ecma_get_object_from_value (this_arg);

if (!ecma_is_promise (obj))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a Promise."));
}

ecma_value_t result_capability = ecma_promise_new_capability ();

if (ECMA_IS_VALUE_ERROR (result_capability))
{
return result_capability;
}

ecma_value_t ret = ecma_promise_then (this_arg, on_fulfilled, on_rejected, result_capability);
ecma_free_value (result_capability);

return ret;
} /* ecma_builtin_promise_prototype_then */

/**
* @}
* @}
* @}
*/

#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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.
*/

#include "ecma-builtin-helpers-macro-defines.inc.h"

/* Object properties:
* (property name, object pointer getter) */

OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_PROMISE,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)

NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
1,
ECMA_PROPERTY_FLAG_WRITABLE)

ROUTINE (LIT_MAGIC_STRING_THEN, ecma_builtin_promise_prototype_then, 2, 2)

#include "ecma-builtin-helpers-macro-undefs.inc.h"
83 changes: 83 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-promise.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* 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.
*/

#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-globals.h"
#include "ecma-promise-object.h"

#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN

#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"

#define BUILTIN_INC_HEADER_NAME "ecma-builtin-promise.inc.h"
#define BUILTIN_UNDERSCORED_ID promise
#include "ecma-builtin-internal-routines-template.inc.h"

/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup promise ECMA Promise object built-in
* @{
*/

/**
* Handle calling [[Call]] of built-in Promise object.
*
* ES2015 25.4.3 Promise is not intended to be called
* as a function and will throw an exception when called
* in that manner.
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_promise_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);

return ecma_raise_type_error (ECMA_ERR_MSG ("Constructor Promise requires 'new'."));
} /* ecma_builtin_promise_dispatch_call */

/**
* Handle calling [[Construct]] of built-in Promise object.
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_promise_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);

if (arguments_list_len == 0 || !ecma_op_is_callable (arguments_list_p[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("First parameter must be callable."));
}

return ecma_op_create_promise_object (arguments_list_p[0], true);
} /* ecma_builtin_promise_dispatch_construct */

/**
* @}
* @}
* @}
*/

#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
39 changes: 39 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-promise.inc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* 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.
*/

/*
* Promose built-in description
*/

#include "ecma-builtin-helpers-macro-defines.inc.h"

/* Number properties:
* (property name, number value, writable, enumerable, configurable) */

NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
1,
ECMA_PROPERTY_FIXED)

/* Object properties:
* (property name, object pointer getter) */

OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_PROMISE_PROTOTYPE,
ECMA_PROPERTY_FIXED)

/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */

#include "ecma-builtin-helpers-macro-undefs.inc.h"
18 changes: 18 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtins.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,24 @@ BUILTIN (ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY,

#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */

#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN

BUILTIN (ECMA_BUILTIN_ID_PROMISE_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
true,
promise_prototype)

BUILTIN (ECMA_BUILTIN_ID_PROMISE,
ECMA_OBJECT_TYPE_FUNCTION,
ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE,
true,
true,
promise)

#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */

/* The Global object (15.1) */
BUILTIN (ECMA_BUILTIN_ID_GLOBAL,
ECMA_OBJECT_TYPE_GENERAL,
Expand Down
Loading