1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 #include "jscriptclasses.hxx"
27 
28 //========================================================================
29 // JScriptValue
30 //========================================================================
JScriptValue()31 JScriptValue::JScriptValue(): m_bOutParam(0), m_bInOutParam(0)
32 {
33 }
34 
~JScriptValue()35 JScriptValue::~JScriptValue()
36 {
37 }
38 
39 
40 // JScriptValue, IDispatch --------------------------------------------
GetTypeInfoCount(UINT *)41 STDMETHODIMP JScriptValue::GetTypeInfoCount(UINT* /*pctinfo*/)
42 {
43 	return E_NOTIMPL;
44 }
45 
46 // JScriptValue, IDispatch --------------------------------------------
GetTypeInfo(UINT,LCID,ITypeInfo **)47 STDMETHODIMP JScriptValue::GetTypeInfo( UINT /*iTInfo*/,
48 										   LCID /*lcid*/,
49 										   ITypeInfo** /*ppTInfo*/)
50 {
51 	return E_NOTIMPL;
52 }
53 
54 // JScriptValue, IDispatch --------------------------------------------
GetIDsOfNames(REFIID,LPOLESTR * rgszNames,UINT,LCID,DISPID * rgDispId)55 STDMETHODIMP JScriptValue::GetIDsOfNames( REFIID /*riid*/,
56 											 LPOLESTR *rgszNames,
57 											 UINT /*cNames*/,
58 											 LCID /*lcid*/,
59 											 DISPID *rgDispId)
60 {
61 	if( !rgDispId)
62 		return E_POINTER;
63 
64 
65 	HRESULT ret= S_OK;
66 	CComBSTR name(*rgszNames);
67 	name.ToLower();
68 
69 	if( name == CComBSTR( L"set") )
70 		*rgDispId= 1;
71 	else if( name == CComBSTR( L"get") )
72 		*rgDispId= 2;
73 	else if( name == CComBSTR( L"initoutparam") )
74 		*rgDispId= 3;
75 	else if( name == CComBSTR( L"initinoutparam") )
76 		*rgDispId= 4;
77 	else
78 		ret= DISP_E_UNKNOWNNAME;
79 
80 	return ret;
81 }
82 
83 // JScriptValue, IDispatch --------------------------------------------
Invoke(DISPID dispIdMember,REFIID,LCID,WORD wFlags,DISPPARAMS * pDispParams,VARIANT * pVarResult,EXCEPINFO *,UINT *)84 STDMETHODIMP JScriptValue::Invoke( DISPID dispIdMember,
85 						 REFIID /*riid*/,
86 						 LCID /*lcid*/,
87 						 WORD wFlags,
88 						 DISPPARAMS *pDispParams,
89 						 VARIANT *pVarResult,
90 						 EXCEPINFO* /*pExcepInfo*/,
91 						 UINT* /*puArgErr*/)
92 {
93 	if( pDispParams->cNamedArgs)
94 		return DISP_E_NONAMEDARGS;
95 
96 
97 	HRESULT ret= S_OK;
98 	switch( dispIdMember)
99 	{
100 	case 0: // DISPID_VALUE
101 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
102 		{
103 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
104 				ret= E_FAIL;
105 		}
106 		else
107 			ret= E_POINTER;
108 		break;
109 	case 1:
110 		if( wFlags & DISPATCH_METHOD)
111 			ret= Set( pDispParams->rgvarg[1], pDispParams->rgvarg[0]);
112 		if( FAILED( ret))
113 			ret= DISP_E_EXCEPTION;
114 		break;
115 	case 2:
116 		if( wFlags & DISPATCH_METHOD)
117 			ret= Get( pVarResult);
118 		if( FAILED( ret))
119 			ret= DISP_E_EXCEPTION;
120 		break;
121 	case 3:
122 		if( wFlags & DISPATCH_METHOD)
123 			ret= InitOutParam();
124 		if( FAILED( ret))
125 			ret= DISP_E_EXCEPTION;
126 		break;
127 	case 4:
128 		if( wFlags & DISPATCH_METHOD)
129 			ret= InitInOutParam( pDispParams->rgvarg[1], pDispParams->rgvarg[0]);
130 		if( FAILED( ret))
131 			ret= DISP_E_EXCEPTION;
132 		break;
133 	default:
134 		ret= DISP_E_MEMBERNOTFOUND;
135 		break;
136 	}
137 
138 	return ret;
139 }
140 
141 // JScriptValue, IScriptOutParam-----------------------
Set(VARIANT type,VARIANT value)142 STDMETHODIMP JScriptValue::Set( VARIANT type, VARIANT value)
143 {
144 	Lock();
145 	HRESULT hr= S_OK;
146 	m_varValue.Clear();
147 	hr= VariantCopyInd( &m_varValue, &value);
148 	VARIANT var;
149 	VariantInit( &var);
150 	if( SUCCEEDED( hr= VariantChangeType( &var, &type, 0, VT_BSTR)))
151 		m_bstrType= var.bstrVal;
152 	Unlock();
153 	return hr;
154 }
155 // JScriptValue, IScriptOutParam-----------------------
Get(VARIANT * val)156 STDMETHODIMP JScriptValue::Get( VARIANT *val)
157 {
158 	Lock();
159 	if( !val)
160 		return E_POINTER;
161 	HRESULT hr= VariantCopy( val, &m_varValue);
162 	Unlock();
163 	return hr;
164 }
165 
InitOutParam()166 STDMETHODIMP JScriptValue::InitOutParam()
167 {
168 	Lock();
169 	m_varValue.Clear();
170 	m_bOutParam= true;
171 	m_bInOutParam= false;
172 	Unlock();
173 	return S_OK;
174 }
175 
InitInOutParam(VARIANT type,VARIANT value)176 STDMETHODIMP JScriptValue::InitInOutParam( VARIANT type, VARIANT value)
177 {
178 	Lock();
179 	m_bInOutParam= true;
180 	m_bOutParam= false;
181 	Unlock();
182 	return Set( type, value);
183 }
184 
IsOutParam(VARIANT_BOOL * flag)185 STDMETHODIMP JScriptValue::IsOutParam( VARIANT_BOOL * flag)
186 {
187 	Lock();
188 	if( !flag)
189 		return E_POINTER;
190 	*flag= m_bOutParam ? VARIANT_TRUE : VARIANT_FALSE;
191 	Unlock();
192 	return S_OK;
193 }
194 
IsInOutParam(VARIANT_BOOL * flag)195 STDMETHODIMP JScriptValue::IsInOutParam( VARIANT_BOOL * flag)
196 {
197 	Lock();
198 	if( !flag)
199 		return E_POINTER;
200 	*flag= m_bInOutParam ? VARIANT_TRUE : VARIANT_FALSE;
201 	Unlock();
202 	return S_OK;
203 }
204 
GetValue(BSTR * type,VARIANT * value)205 STDMETHODIMP JScriptValue::GetValue( BSTR* type, VARIANT *value)
206 {
207 	Lock();
208 	if( !type || !value)
209 		return E_POINTER;
210 	HRESULT hr;
211 	if( SUCCEEDED(	hr= m_bstrType.CopyTo( type)))
212 		hr= VariantCopy( value, &m_varValue);
213 	Unlock();
214 	return hr;
215 }
216 
217 //##########################################################################################
218 // JScriptOutValue
219 //##########################################################################################
220 
JScriptOutParam()221 JScriptOutParam::JScriptOutParam()
222 {
223 }
224 
~JScriptOutParam()225 JScriptOutParam::~JScriptOutParam()
226 {
227 }
228 
229 
230 // JScriptOutParam, IDispatch --------------------------------------------
GetTypeInfoCount(UINT *)231 STDMETHODIMP JScriptOutParam::GetTypeInfoCount(UINT* /*pctinfo*/)
232 {
233 	return E_NOTIMPL;
234 }
235 
236 // JScriptOutParam, IDispatch --------------------------------------------
GetTypeInfo(UINT,LCID,ITypeInfo **)237 STDMETHODIMP JScriptOutParam::GetTypeInfo( UINT /*iTInfo*/,
238 										   LCID /*lcid*/,
239 										   ITypeInfo** /*ppTInfo*/)
240 {
241 	return E_NOTIMPL;
242 }
243 
244 // JScriptOutParam, IDispatch --------------------------------------------
GetIDsOfNames(REFIID,LPOLESTR * rgszNames,UINT,LCID,DISPID * rgDispId)245 STDMETHODIMP JScriptOutParam::GetIDsOfNames( REFIID /*riid*/,
246 											 LPOLESTR *rgszNames,
247 											 UINT /*cNames*/,
248 											 LCID /*lcid*/,
249 											 DISPID *rgDispId)
250 {
251 	if( !rgDispId)
252 		return E_POINTER;
253 
254 
255 	HRESULT ret= S_OK;
256 	CComBSTR name(*rgszNames);
257 	name.ToLower();
258 
259 	if( name == CComBSTR( L"0") )
260 		*rgDispId= 1;
261 	else
262 		ret= DISP_E_UNKNOWNNAME;
263 
264 	return ret;
265 }
266 
267 // JScriptOutParam, IDispatch --------------------------------------------
Invoke(DISPID dispIdMember,REFIID,LCID,WORD wFlags,DISPPARAMS * pDispParams,VARIANT * pVarResult,EXCEPINFO *,UINT *)268 STDMETHODIMP JScriptOutParam::Invoke( DISPID dispIdMember,
269 						 REFIID /*riid*/,
270 						 LCID /*lcid*/,
271 						 WORD wFlags,
272 						 DISPPARAMS *pDispParams,
273 						 VARIANT *pVarResult,
274 						 EXCEPINFO* /*pExcepInfo*/,
275 						 UINT* /*puArgErr*/)
276 {
277 	HRESULT ret= S_OK;
278 	switch( dispIdMember)
279 	{
280 	case 0: // DISPID_VALUE
281 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
282 		{
283 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
284 				ret= E_FAIL;
285 		}
286 		else if( wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF)
287 		{
288 			m_varValue.Clear();
289 			if( FAILED( VariantCopyInd( &m_varValue, &pDispParams->rgvarg[0])))
290 				ret= E_FAIL;
291 		}
292 		else
293 			ret= E_POINTER;
294 		break;
295 	case 1: //
296 		if( wFlags & DISPATCH_PROPERTYGET && pVarResult)
297 		{
298 			if( FAILED( VariantCopy( pVarResult, &m_varValue)))
299 				ret= E_FAIL;
300 		}
301 		else if( wFlags & DISPATCH_PROPERTYPUT || wFlags & DISPATCH_PROPERTYPUTREF)
302 		{
303 			m_varValue.Clear();
304 			if( FAILED( VariantCopyInd( &m_varValue, &pDispParams->rgvarg[0])))
305 				ret= E_FAIL;
306 		}
307 		else
308 			ret= E_POINTER;
309 		break;
310 
311 	default:
312 		ret= DISP_E_MEMBERNOTFOUND;
313 		break;
314 	}
315 
316 	return ret;
317 }
318