2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / Synchronizer.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  * The contents of this file are subject to the Mozilla Public License\r
3  * Version 1.1 (the "License"); you may not use this file except in\r
4  * compliance with the License. You may obtain a copy of the License at\r
5  * http://www.mozilla.org/MPL/\r
6  *\r
7  * Software distributed under the License is distributed on an "AS IS"\r
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the\r
9  * License for the specific language governing rights and limitations\r
10  * under the License.\r
11  *\r
12  * The Original Code is Synchronizer.java code, released Sep 27, 2000.\r
13  *\r
14  * The Initial Developer of the Original Code is Matthias Radestock\r
15  * <matthias@sorted.org>. Portions created by Matthias Radestock are\r
16  * Copyright (C) 2000 Matthias Radestock. All Rights Reserved.\r
17  *\r
18  * Contributor(s):\r
19  *      Redfig Ltd (http://www.redfig.com)\r
20  *      LShift Ltd (http://www.lshift.net)\r
21  *\r
22  * Alternatively, the contents of this file may be used under the terms\r
23  * of the GNU Public License (the  "GPL License"), in which case the\r
24  * provisions of the GPL License are applicable instead of those\r
25  * above.  If you wish to allow use of your version of this file only\r
26  * under the terms of the GPL License and not to allow others to use\r
27  * your version of this file under the MPL, indicate your decision by\r
28  * deleting  the provisions above and replace  them with the notice and\r
29  * other provisions required by the GPL License.  If you do not delete\r
30  * the provisions above, a recipient may use your version of this file\r
31  * under either the MPL or the GPL License.\r
32  */\r
33 \r
34 // API class\r
35 \r
36 package org.mozilla.javascript;\r
37 \r
38 /**\r
39  * This class provides support for implementing Java-style synchronized\r
40  * methods in Javascript.\r
41  *\r
42  * Synchronized functions are created from ordinary Javascript\r
43  * functions by the <code>Synchronizer</code> constructor, e.g.\r
44  * <code>new Packages.org.mozilla.javascript.Synchronizer(fun)</code>.\r
45  * The resulting object is a function that establishes an exclusive\r
46  * lock on the <code>this</code> object of its invocation.\r
47  *\r
48  * The Rhino shell provides a short-cut for the creation of\r
49  * synchronized methods: <code>sync(fun)</code> has the same effect as\r
50  * calling the above constructor.\r
51  *\r
52  * @see org.mozilla.javascript.Delegator\r
53  * @author Matthias Radestock\r
54  */\r
55 \r
56 public class Synchronizer extends Delegator {\r
57 \r
58     /**\r
59      * Create a new synchronized function from an existing one.\r
60      *\r
61      * @param obj the existing function\r
62      */\r
63     public Synchronizer(Scriptable obj) {\r
64         super(obj);\r
65     }\r
66 \r
67     /**\r
68      * @see org.mozilla.javascript.Function#call\r
69      */\r
70     public Object call(Context cx, Scriptable scope, Scriptable thisObj,\r
71                        Object[] args)\r
72         throws JavaScriptException {\r
73         synchronized(thisObj) {\r
74             return ((Function)obj).call(cx,scope,thisObj,args);\r
75         }\r
76     }\r
77 }\r