Index: src/harmony-atomics.js |
diff --git a/src/harmony-atomics.js b/src/harmony-atomics.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cb63fa4b97b41014a9f54d1f8972e383bfb36d5e |
--- /dev/null |
+++ b/src/harmony-atomics.js |
@@ -0,0 +1,170 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+(function(global, shared, exports) { |
+ |
+"use strict"; |
+ |
+%CheckIsBootstrapping(); |
+ |
+var GlobalObject = global.Object; |
+ |
+ |
+// TODO(binji): something better here? We want this to match the behavior of |
+// integer indexed exotic object access, e.g. |
+// a = new Uint32Array(10); |
+// a[0] == 0 |
+// a['0'] == 0 |
+// a[new Number(0)] == 0 |
+// a[{valueOf: function(){return 0;}}] == 0 |
+// a[-1] == undefined |
+// a[11] == undefined |
+// a[0.4] == undefined |
+// a[null] == undefined |
+// a[undefined] == undefined |
+// a[false] == undefined |
+// a[{}] == undefined |
+// a['hello'] == undefined |
+// etc. |
+function ToIndex(x) { |
+ if (%_IsSmi(x)) return x; |
+ if (IS_NUMBER(x)) return %NumberToInteger(x); |
+ if (IS_STRING(x)) { |
+ if (%_HasCachedArrayIndex(x)) { |
+ return %_GetCachedArrayIndex(x); |
+ } |
+ |
+ var n = %StringToNumber(x); |
+ if (NUMBER_IS_NAN(n)) return UNDEFINED; |
+ |
+ return %NumberToInteger(n); |
+ } |
+ return UNDEFINED; |
+} |
+ |
+function CheckSharedTypedArray(sta) { |
+ if (!%_IsSharedTypedArray(sta)) { |
+ throw MakeTypeError(kNotSharedTypedArray, sta); |
+ } |
+} |
+ |
+function CheckSharedIntegerTypedArray(ia) { |
+ if (!%_IsSharedIntegerTypedArray(ia)) { |
+ throw MakeTypeError(kNotIntegerSharedTypedArray, ia); |
+ } |
+} |
+ |
+//------------------------------------------------------------------- |
+ |
+function AtomicsCompareExchange(sta, index, oldValue, newValue) { |
+ CheckSharedTypedArray(sta); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= sta.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsCompareExchange(sta, index, oldValue, newValue); |
+} |
+ |
+function AtomicsLoad(sta, index) { |
+ CheckSharedTypedArray(sta); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= sta.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsLoad(sta, index); |
+} |
+ |
+function AtomicsStore(sta, index, value) { |
+ CheckSharedTypedArray(sta); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= sta.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsStore(sta, index, value); |
+} |
+ |
+function AtomicsAdd(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsAdd(ia, index, value); |
+} |
+ |
+function AtomicsSub(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsSub(ia, index, value); |
+} |
+ |
+function AtomicsAnd(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsAnd(ia, index, value); |
+} |
+ |
+function AtomicsOr(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsOr(ia, index, value); |
+} |
+ |
+function AtomicsXor(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsXor(ia, index, value); |
+} |
+ |
+function AtomicsExchange(ia, index, value) { |
+ CheckSharedIntegerTypedArray(ia); |
+ index = ToIndex(index); |
+ if (IS_UNDEFINED(index) || index < 0 || index >= ia.length) { |
+ return UNDEFINED; |
+ } |
+ return %_AtomicsExchange(ia, index, value); |
+} |
+ |
+function AtomicsIsLockFree(size) { |
+ return %_AtomicsIsLockFree(size); |
+} |
+ |
+// ------------------------------------------------------------------- |
+ |
+function AtomicsConstructor() {} |
+ |
+var Atomics = new AtomicsConstructor(); |
+ |
+%InternalSetPrototype(Atomics, GlobalObject.prototype); |
+%AddNamedProperty(global, "Atomics", Atomics, DONT_ENUM); |
+%FunctionSetInstanceClassName(AtomicsConstructor, 'Atomics'); |
+ |
+%AddNamedProperty(Atomics, symbolToStringTag, "Atomics", READ_ONLY | DONT_ENUM); |
+ |
+$installFunctions(Atomics, DONT_ENUM, [ |
+ "compareExchange", AtomicsCompareExchange, |
+ "load", AtomicsLoad, |
+ "store", AtomicsStore, |
+ "add", AtomicsAdd, |
+ "sub", AtomicsSub, |
+ "and", AtomicsAnd, |
+ "or", AtomicsOr, |
+ "xor", AtomicsXor, |
+ "exchange", AtomicsExchange, |
+ "isLockFree", AtomicsIsLockFree, |
+]); |
+ |
+}) |