1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla Communicator client code, released
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-1999
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* -*- Mode: C; tab-width: 8 -*-
39 * Copyright (C) 1998-1999 Netscape Communications Corporation, All Rights Reserved.
43 * This file is part of the Java-vendor-neutral implementation of LiveConnect
45 * It contains the native code implementation of JS's JavaMember class.
46 * JavaMember's are a strange beast required only to handle the special case
47 * of a public field and a public method that appear in the same class and
48 * have the same name. When such a field/method is used in Java, the compiler
49 * can statically determine from context whether the field or the method is
50 * being referenced, but that is not possible with JavaScript. For example:
52 * ambiguousVal = javaObj.fieldOrMethod; // ambiguousVal is a JavaMember object
53 * a = ambiguousVal(); // ambiguousVal used as a method value
54 * b = ambiguousVal + 4; // ambiguousVal used as a field value
56 * A JavaMember instance carries both the captured value of the Java field and
57 * the method value until the context that the value is to be used in is known,
58 * at which point conversion forces the use of one or the other.
64 #include "jsj_private.h" /* LiveConnect internals */
66 /* Private, native portion of a JavaMember */
67 typedef struct JavaMethodOrFieldValue
{
70 } JavaMethodOrFieldValue
;
73 jsj_CreateJavaMember(JSContext
*cx
, jsval method_val
, jsval field_val
)
75 JavaMethodOrFieldValue
*member_val
;
76 JSObject
*JavaMember_obj
;
78 member_val
= (JavaMethodOrFieldValue
*)JS_malloc(cx
, sizeof(*member_val
));
82 JavaMember_obj
= JS_NewObject(cx
, &JavaMember_class
, 0, 0);
83 if (!JavaMember_obj
) {
84 JS_free(cx
, member_val
);
88 member_val
->method_val
= method_val
;
89 member_val
->field_val
= field_val
;
90 JS_SetPrivate(cx
, JavaMember_obj
, (void *)member_val
);
92 return JavaMember_obj
;
96 JavaMember_finalize(JSContext
*cx
, JSObject
*obj
)
98 JavaMethodOrFieldValue
*member_val
;
100 member_val
= JS_GetPrivate(cx
, obj
);
103 JS_free(cx
, member_val
);
107 JavaMember_convert(JSContext
*cx
, JSObject
*obj
, JSType type
, jsval
*vp
)
109 JavaMethodOrFieldValue
*member_val
;
111 member_val
= JS_GetPrivate(cx
, obj
);
113 if (type
== JSTYPE_OBJECT
) {
114 *vp
= OBJECT_TO_JSVAL(obj
);
118 JS_ReportError(cx
, "illegal operation on JavaObject prototype object");
128 *vp
= member_val
->field_val
;
131 case JSTYPE_FUNCTION
:
132 *vp
= member_val
->method_val
;
142 * This function exists only to make JavaMember's Call'able. The way the JS
143 * engine is written now, it's never actually called because when a JavaMember
144 * is invoked, it's converted to a JS function via JavaMember_convert().
147 JavaMember_Call(JSContext
*cx
, JSObject
*obj
, uintN argc
, jsval
*argv
, jsval
*rval
)
154 JavaMember_trace(JSTracer
*trc
, JSObject
*obj
)
156 JavaMethodOrFieldValue
*member_val
;
158 member_val
= (JavaMethodOrFieldValue
*)JS_GetPrivate(trc
->context
, obj
);
160 JS_CALL_VALUE_TRACER(trc
, member_val
->method_val
, "method_val");
161 JS_CALL_VALUE_TRACER(trc
, member_val
->field_val
, "field_val");
165 JSClass JavaMember_class
= {
166 "JavaMember", JSCLASS_HAS_PRIVATE
| JSCLASS_MARK_IS_TRACE
,
167 JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
, JS_PropertyStub
,
168 JS_EnumerateStub
, JS_ResolveStub
,
169 JavaMember_convert
, JavaMember_finalize
,
171 NULL
, /* getObjectOps */
172 NULL
, /* checkAccess */
174 NULL
, /* construct */
175 NULL
, /* xdrObject */
176 NULL
, /* hasInstance */
177 JS_CLASS_TRACE(JavaMember_trace
), /* mark/trace */
182 jsj_init_JavaMember(JSContext
*cx
, JSObject
*global_obj
)
184 if (!JS_InitClass(cx
, global_obj
,
185 0, &JavaMember_class
, 0, 0,