Skip to content

Commit 74f98ec

Browse files
authored
Add Atomics support (#4721)
Creating atomics interface JerryScript-DCO-1.0-Signed-off-by: Gergo Csizi [email protected]
1 parent c446871 commit 74f98ec

12 files changed

+803
-122
lines changed

jerry-core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ set(SOURCE_CORE_FILES
166166
ecma/builtin-objects/ecma-builtin-async-generator-prototype.c
167167
ecma/builtin-objects/ecma-builtin-async-generator.c
168168
ecma/builtin-objects/ecma-builtin-async-iterator-prototype.c
169+
ecma/builtin-objects/ecma-builtin-atomics.c
169170
ecma/builtin-objects/ecma-builtin-bigint-prototype.c
170171
ecma/builtin-objects/ecma-builtin-bigint.c
171172
ecma/builtin-objects/ecma-builtin-boolean-prototype.c
@@ -265,6 +266,7 @@ set(SOURCE_CORE_FILES
265266
ecma/operations/ecma-array-object.c
266267
ecma/operations/ecma-arraybuffer-object.c
267268
ecma/operations/ecma-async-generator-object.c
269+
ecma/operations/ecma-atomics-object.c
268270
ecma/operations/ecma-big-uint.c
269271
ecma/operations/ecma-bigint-object.c
270272
ecma/operations/ecma-bigint.c
@@ -362,6 +364,7 @@ if(ENABLE_AMALGAM)
362364
ecma/builtin-objects/ecma-builtin-async-generator-prototype.inc.h
363365
ecma/builtin-objects/ecma-builtin-async-generator.inc.h
364366
ecma/builtin-objects/ecma-builtin-async-iterator-prototype.inc.h
367+
ecma/builtin-objects/ecma-builtin-atomics.inc.h
365368
ecma/builtin-objects/ecma-builtin-bigint-prototype.inc.h
366369
ecma/builtin-objects/ecma-builtin-bigint.inc.h
367370
ecma/builtin-objects/ecma-builtin-boolean-prototype.inc.h
@@ -440,6 +443,7 @@ if(ENABLE_AMALGAM)
440443
ecma/operations/ecma-array-object.h
441444
ecma/operations/ecma-arraybuffer-object.h
442445
ecma/operations/ecma-async-generator-object.h
446+
ecma/operations/ecma-atomics-object.h
443447
ecma/operations/ecma-big-uint.h
444448
ecma/operations/ecma-bigint-object.h
445449
ecma/operations/ecma-bigint.h

jerry-core/config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
# define JERRY_BUILTIN_SHAREDARRAYBUFFER JERRY_ESNEXT
116116
#endif /* !defined (JERRY_BUILTIN_SHAREDARRAYBUFFER) */
117117

118+
#ifndef JERRY_BUILTIN_ATOMICS
119+
# define JERRY_BUILTIN_ATOMICS JERRY_ESNEXT
120+
#endif /* !defined (JERRY_BUILTIN_ATOMICS) */
121+
118122
#ifndef JERRY_BUILTIN_WEAKREF
119123
# define JERRY_BUILTIN_WEAKREF JERRY_ESNEXT
120124
#endif /* !defined (JERRY_BUILTIN_WEAKREF) */
@@ -588,6 +592,10 @@
588592
|| ((JERRY_BUILTIN_SHAREDARRAYBUFFER != 0) && (JERRY_BUILTIN_SHAREDARRAYBUFFER != 1))
589593
# error "Invalid value for JERRY_BUILTIN_SHAREDARRAYBUFFER macro."
590594
#endif
595+
#if !defined (JERRY_BUILTIN_ATOMICS) \
596+
|| ((JERRY_BUILTIN_ATOMICS != 0) && (JERRY_BUILTIN_ATOMICS != 1))
597+
# error "Invalid value for JERRY_BUILTIN_ATOMICS macro."
598+
#endif
591599
#if !defined (JERRY_BUILTIN_BIGINT) \
592600
|| ((JERRY_BUILTIN_BIGINT != 0) && (JERRY_BUILTIN_BIGINT != 1))
593601
# error "Invalid value for JERRY_BUILTIN_BIGINT macro."
@@ -611,6 +619,9 @@
611619
#if (JERRY_BUILTIN_TYPEDARRAY == 0) && (JERRY_BUILTIN_SHAREDARRAYBUFFER == 1)
612620
# error "JERRY_BUILTIN_TYPEDARRAY should be enabled too to enable JERRY_BUILTIN_SHAREDARRAYBUFFER macro."
613621
#endif
622+
#if (JERRY_BUILTIN_SHAREDARRAYBUFFER == 0) && (JERRY_BUILTIN_ATOMICS == 1)
623+
# error "JERRY_BUILTIN_SHAREDARRAYBUFFER should be enabled too to enable JERRY_BUILTIN_ATOMICS macro."
624+
#endif
614625

615626
/**
616627
* Internal options.
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "ecma-atomics-object.h"
17+
#include "ecma-builtins.h"
18+
#include "ecma-globals.h"
19+
#include "ecma-helpers.h"
20+
#include "jrt.h"
21+
22+
#if JERRY_BUILTIN_ATOMICS
23+
24+
#define ECMA_BUILTINS_INTERNAL
25+
26+
#include "ecma-builtins-internal.h"
27+
28+
/**
29+
* This object has a custom dispatch function.
30+
*/
31+
#define BUILTIN_CUSTOM_DISPATCH
32+
33+
/**
34+
* List of built-in routine identifiers.
35+
*/
36+
enum
37+
{
38+
ECMA_ATOMICS_ROUTINE_START = 0, /**< Special value, should be ignored */
39+
ECMA_ATOMICS_ROUTINE_ADD, /**< Atomics add routine */
40+
ECMA_ATOMICS_ROUTINE_AND, /**< Atomics and routine */
41+
ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE, /**< Atomics compare exchange routine */
42+
ECMA_ATOMICS_ROUTINE_EXCHANGE, /**< Atomics exchange routine */
43+
ECMA_ATOMICS_ROUTINE_ISLOCKFREE, /**< Atomics is lock free routine */
44+
ECMA_ATOMICS_ROUTINE_LOAD, /**< Atomics load routine */
45+
ECMA_ATOMICS_ROUTINE_OR, /**< Atomics or routine */
46+
ECMA_ATOMICS_ROUTINE_STORE, /**< Atomics store routine */
47+
ECMA_ATOMICS_ROUTINE_SUB, /**< Atomics sub routine */
48+
ECMA_ATOMICS_ROUTINE_WAIT, /**< Atomics wait routine */
49+
ECMA_ATOMICS_ROUTINE_NOTIFY, /**< Atomics notify routine */
50+
ECMA_ATOMICS_ROUTINE_XOR, /**< Atomics xor routine */
51+
};
52+
53+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-atomics.inc.h"
54+
#define BUILTIN_UNDERSCORED_ID atomics
55+
56+
#include "ecma-builtin-internal-routines-template.inc.h"
57+
58+
/** \addtogroup ecma ECMA
59+
* @{
60+
*
61+
* \addtogroup ecmabuiltins
62+
* @{
63+
*
64+
* \addtogroup atomics ECMA Atomics object built-in
65+
* @{
66+
*/
67+
68+
/**
69+
* The Atomics object's 'compareExchange' routine
70+
*
71+
* See also: ES11 24.4.4
72+
*
73+
* @return ecma value
74+
* Returned value must be freed with ecma_free_value.
75+
*/
76+
static ecma_value_t
77+
ecma_builtin_atomics_compare_exchange (ecma_value_t typedarray, /**< typedArray argument */
78+
ecma_value_t index, /**< index argument */
79+
ecma_value_t expected_value, /**< expectedValue argument */
80+
ecma_value_t replacement_value) /**< replacementValue argument*/
81+
{
82+
JERRY_UNUSED (typedarray);
83+
JERRY_UNUSED (index);
84+
JERRY_UNUSED (expected_value);
85+
JERRY_UNUSED (replacement_value);
86+
87+
return ecma_make_uint32_value (0);
88+
} /* ecma_builtin_atomics_compare_exchange */
89+
90+
/**
91+
* The Atomics object's 'isLockFree' routine
92+
*
93+
* See also: ES11 24.4.6
94+
*
95+
* @return ecma value
96+
* Returned value must be freed with ecma_free_value.
97+
*/
98+
static ecma_value_t
99+
ecma_builtin_atomics_is_lock_free (ecma_value_t size) /**< size argument */
100+
{
101+
JERRY_UNUSED (size);
102+
103+
return ECMA_VALUE_FALSE;
104+
} /* ecma_builtin_atomics_is_lock_free */
105+
106+
/**
107+
* The Atomics object's 'store' routine
108+
*
109+
* See also: ES11 24.4.9
110+
*
111+
* @return ecma value
112+
* Returned value must be freed with ecma_free_value.
113+
*/
114+
static ecma_value_t
115+
ecma_builtin_atomics_store (ecma_value_t typedarray, /**< typedArray argument */
116+
ecma_value_t index, /**< index argument */
117+
ecma_value_t value) /**< value argument */
118+
{
119+
JERRY_UNUSED (typedarray);
120+
JERRY_UNUSED (index);
121+
JERRY_UNUSED (value);
122+
123+
return ecma_make_uint32_value (0);
124+
} /* ecma_builtin_atomics_store */
125+
126+
/**
127+
* The Atomics object's 'wait' routine
128+
*
129+
* See also: ES11 24.4.11
130+
*
131+
* @return ecma value
132+
* Returned value must be freed with ecma_free_value.
133+
*/
134+
static ecma_value_t
135+
ecma_builtin_atomics_wait (ecma_value_t typedarray, /**< typedArray argument */
136+
ecma_value_t index, /**< index argument */
137+
ecma_value_t value, /**< value argument */
138+
ecma_value_t timeout) /**< timeout argument */
139+
{
140+
JERRY_UNUSED (typedarray);
141+
JERRY_UNUSED (index);
142+
JERRY_UNUSED (value);
143+
JERRY_UNUSED (timeout);
144+
145+
return ecma_make_uint32_value (0);
146+
} /* ecma_builtin_atomics_wait */
147+
148+
/**
149+
* The Atomics object's 'notify' routine
150+
*
151+
* See also: ES11 24.4.12
152+
*
153+
* @return ecma value
154+
* Returned value must be freed with ecma_free_value.
155+
*/
156+
static ecma_value_t
157+
ecma_builtin_atomics_notify (ecma_value_t typedarray, /**< typedArray argument */
158+
ecma_value_t index, /**< index argument */
159+
ecma_value_t count) /**< count argument */
160+
{
161+
JERRY_UNUSED (typedarray);
162+
JERRY_UNUSED (index);
163+
JERRY_UNUSED (count);
164+
165+
return ecma_make_uint32_value (0);
166+
} /* ecma_builtin_atomics_notify */
167+
168+
/**
169+
* Dispatcher of the built-in's routines
170+
*
171+
* @return ecma value
172+
* Returned value must be freed with ecma_free_value.
173+
*/
174+
ecma_value_t
175+
ecma_builtin_atomics_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
176+
ecma_value_t this_arg, /**< 'this' argument value */
177+
const ecma_value_t arguments_list_p[], /**< list of arguments
178+
* passed to routine */
179+
uint32_t arguments_number) /**< length of arguments' list */
180+
{
181+
JERRY_UNUSED (this_arg);
182+
ecma_value_t arg1 = arguments_list_p[0];
183+
ecma_value_t arg2 = arguments_list_p[1];
184+
ecma_value_t arg3 = arguments_list_p[2];
185+
ecma_value_t arg4 = (arguments_number > 3) ? arguments_list_p[3] : ECMA_VALUE_UNDEFINED;
186+
187+
ecma_atomics_op_t type;
188+
189+
switch (builtin_routine_id)
190+
{
191+
case ECMA_ATOMICS_ROUTINE_ADD:
192+
{
193+
type = ECMA_ATOMICS_ADD;
194+
break;
195+
}
196+
case ECMA_ATOMICS_ROUTINE_AND:
197+
{
198+
type = ECMA_ATOMICS_AND;
199+
break;
200+
}
201+
case ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE:
202+
{
203+
return ecma_builtin_atomics_compare_exchange (arg1, arg2, arg3, arg4);
204+
}
205+
case ECMA_ATOMICS_ROUTINE_EXCHANGE:
206+
{
207+
type = ECMA_ATOMICS_EXCHANGE;
208+
break;
209+
}
210+
case ECMA_ATOMICS_ROUTINE_ISLOCKFREE:
211+
{
212+
return ecma_builtin_atomics_is_lock_free (arg1);
213+
}
214+
case ECMA_ATOMICS_ROUTINE_LOAD:
215+
{
216+
return ecma_atomic_load (arg1, arg2);
217+
}
218+
case ECMA_ATOMICS_ROUTINE_OR:
219+
{
220+
type = ECMA_ATOMICS_OR;
221+
break;
222+
}
223+
case ECMA_ATOMICS_ROUTINE_STORE:
224+
{
225+
return ecma_builtin_atomics_store (arg1, arg2, arg3);
226+
}
227+
case ECMA_ATOMICS_ROUTINE_SUB:
228+
{
229+
type = ECMA_ATOMICS_SUBTRACT;
230+
break;
231+
}
232+
case ECMA_ATOMICS_ROUTINE_WAIT:
233+
{
234+
return ecma_builtin_atomics_wait (arg1, arg2, arg3, arg4);
235+
}
236+
case ECMA_ATOMICS_ROUTINE_NOTIFY:
237+
{
238+
return ecma_builtin_atomics_notify (arg1, arg2, arg3);
239+
}
240+
case ECMA_ATOMICS_ROUTINE_XOR:
241+
{
242+
type = ECMA_ATOMICS_XOR;
243+
break;
244+
}
245+
default:
246+
{
247+
JERRY_UNREACHABLE ();
248+
}
249+
}
250+
return ecma_atomic_read_modify_write (arg1, arg2, arg3, type);
251+
} /* ecma_builtin_atomics_dispatch_routine */
252+
253+
/**
254+
* @}
255+
* @}
256+
* @}
257+
*/
258+
259+
#endif /* JERRY_BUILTIN_ATOMICS */
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/*
17+
* Atomics built-in description
18+
*/
19+
20+
#include "ecma-builtin-helpers-macro-defines.inc.h"
21+
22+
#if JERRY_BUILTIN_ATOMICS
23+
24+
/* ECMA-262 v11, 24.4.14 */
25+
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
26+
LIT_MAGIC_STRING_ATOMICS_U,
27+
ECMA_PROPERTY_FLAG_CONFIGURABLE)
28+
29+
/* Routine properties:
30+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
31+
ROUTINE (LIT_MAGIC_STRING_ADD, ECMA_ATOMICS_ROUTINE_ADD, 3, 3)
32+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_AND, ECMA_ATOMICS_ROUTINE_AND, 3, 3)
33+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_COMPAREEXCHANGE, ECMA_ATOMICS_ROUTINE_COMPAREEXCHANGE, 4, 4)
34+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_EXCHANGE, ECMA_ATOMICS_ROUTINE_EXCHANGE, 3, 3)
35+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_ISLOCKFREE, ECMA_ATOMICS_ROUTINE_ISLOCKFREE, 1, 1)
36+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_LOAD, ECMA_ATOMICS_ROUTINE_LOAD, 2, 2)
37+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_OR, ECMA_ATOMICS_ROUTINE_OR, 3, 3)
38+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_STORE, ECMA_ATOMICS_ROUTINE_STORE, 3, 3)
39+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_SUB, ECMA_ATOMICS_ROUTINE_SUB, 3, 3)
40+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_WAIT, ECMA_ATOMICS_ROUTINE_WAIT, 4, 4)
41+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_NOTIFY, ECMA_ATOMICS_ROUTINE_NOTIFY, 3, 3)
42+
ROUTINE (LIT_MAGIC_STRING_ATOMICS_XOR, ECMA_ATOMICS_ROUTINE_XOR, 3, 3)
43+
44+
#endif /* JERRY_BUILTIN_ATOMICS */
45+
46+
#include "ecma-builtin-helpers-macro-undefs.inc.h"

jerry-core/ecma/builtin-objects/ecma-builtin-global.inc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ OBJECT_VALUE (LIT_MAGIC_STRING_JSON_U,
153153
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
154154
#endif /* JERRY_BUILTIN_JSON */
155155

156+
#if JERRY_BUILTIN_ATOMICS
157+
/* ECMA-262 v5, 15.1.5.2 */
158+
OBJECT_VALUE (LIT_MAGIC_STRING_ATOMICS_U,
159+
ECMA_BUILTIN_ID_ATOMICS,
160+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
161+
#endif /* JERRY_BUILTIN_ATOMICS */
162+
156163
#if JERRY_BUILTIN_TYPEDARRAY
157164
OBJECT_VALUE (LIT_MAGIC_STRING_ARRAY_BUFFER_UL,
158165
ECMA_BUILTIN_ID_ARRAYBUFFER,

0 commit comments

Comments
 (0)