2002/03/21 01:19:33
[org.ibex.core.git] / src / org / mozilla / javascript / Synchronizer.java
diff --git a/src/org/mozilla/javascript/Synchronizer.java b/src/org/mozilla/javascript/Synchronizer.java
new file mode 100644 (file)
index 0000000..3f35750
--- /dev/null
@@ -0,0 +1,77 @@
+/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
+ * The contents of this file are subject to the Mozilla Public License\r
+ * Version 1.1 (the "License"); you may not use this file except in\r
+ * compliance with the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS"\r
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\r
+ * License for the specific language governing rights and limitations\r
+ * under the License.\r
+ *\r
+ * The Original Code is Synchronizer.java code, released Sep 27, 2000.\r
+ *\r
+ * The Initial Developer of the Original Code is Matthias Radestock\r
+ * <matthias@sorted.org>. Portions created by Matthias Radestock are\r
+ * Copyright (C) 2000 Matthias Radestock. All Rights Reserved.\r
+ *\r
+ * Contributor(s):\r
+ *     Redfig Ltd (http://www.redfig.com)\r
+ *     LShift Ltd (http://www.lshift.net)\r
+ *\r
+ * Alternatively, the contents of this file may be used under the terms\r
+ * of the GNU Public License (the  "GPL License"), in which case the\r
+ * provisions of the GPL License are applicable instead of those\r
+ * above.  If you wish to allow use of your version of this file only\r
+ * under the terms of the GPL License and not to allow others to use\r
+ * your version of this file under the MPL, indicate your decision by\r
+ * deleting  the provisions above and replace  them with the notice and\r
+ * other provisions required by the GPL License.  If you do not delete\r
+ * the provisions above, a recipient may use your version of this file\r
+ * under either the MPL or the GPL License.\r
+ */\r
+\r
+// API class\r
+\r
+package org.mozilla.javascript;\r
+\r
+/**\r
+ * This class provides support for implementing Java-style synchronized\r
+ * methods in Javascript.\r
+ *\r
+ * Synchronized functions are created from ordinary Javascript\r
+ * functions by the <code>Synchronizer</code> constructor, e.g.\r
+ * <code>new Packages.org.mozilla.javascript.Synchronizer(fun)</code>.\r
+ * The resulting object is a function that establishes an exclusive\r
+ * lock on the <code>this</code> object of its invocation.\r
+ *\r
+ * The Rhino shell provides a short-cut for the creation of\r
+ * synchronized methods: <code>sync(fun)</code> has the same effect as\r
+ * calling the above constructor.\r
+ *\r
+ * @see org.mozilla.javascript.Delegator\r
+ * @author Matthias Radestock\r
+ */\r
+\r
+public class Synchronizer extends Delegator {\r
+\r
+    /**\r
+     * Create a new synchronized function from an existing one.\r
+     *\r
+     * @param obj the existing function\r
+     */\r
+    public Synchronizer(Scriptable obj) {\r
+       super(obj);\r
+    }\r
+\r
+    /**\r
+     * @see org.mozilla.javascript.Function#call\r
+     */\r
+    public Object call(Context cx, Scriptable scope, Scriptable thisObj,\r
+                      Object[] args)\r
+       throws JavaScriptException {\r
+       synchronized(thisObj) {\r
+           return ((Function)obj).call(cx,scope,thisObj,args);\r
+       }\r
+    }\r
+}\r