1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsAutoPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsDependentString.h"
#include "nsIAtom.h"
#include "nsIInterfaceInfoManager.h"
#include "nsServiceManagerUtils.h"
#include "txExpr.h"
#include "txIFunctionEvaluationContext.h"
#include "txIXPathContext.h"
#include "txNodeSetAdaptor.h"
#include "txXPathTreeWalker.h"
#include "xptcall.h"
#include "txXPathObjectAdaptor.h"
#include "mozilla/Attributes.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsIClassInfo.h"
#include "nsIInterfaceInfo.h"
#include "js/RootingAPI.h"
NS_IMPL_ISUPPORTS(txXPathObjectAdaptor, txIXPathObject)
class txFunctionEvaluationContext final : public txIFunctionEvaluationContext
{
public:
txFunctionEvaluationContext(txIEvalContext *aContext, nsISupports *aState);
NS_DECL_ISUPPORTS
NS_DECL_TXIFUNCTIONEVALUATIONCONTEXT
void ClearContext()
{
mContext = nullptr;
}
private:
~txFunctionEvaluationContext() {}
txIEvalContext *mContext;
nsCOMPtr<nsISupports> mState;
};
txFunctionEvaluationContext::txFunctionEvaluationContext(txIEvalContext *aContext,
nsISupports *aState)
: mContext(aContext),
mState(aState)
{
}
NS_IMPL_ISUPPORTS(txFunctionEvaluationContext, txIFunctionEvaluationContext)
NS_IMETHODIMP
txFunctionEvaluationContext::GetPosition(uint32_t *aPosition)
{
NS_ENSURE_TRUE(mContext, NS_ERROR_FAILURE);
*aPosition = mContext->position();
return NS_OK;
}
NS_IMETHODIMP
txFunctionEvaluationContext::GetSize(uint32_t *aSize)
{
NS_ENSURE_TRUE(mContext, NS_ERROR_FAILURE);
*aSize = mContext->size();
return NS_OK;
}
NS_IMETHODIMP
txFunctionEvaluationContext::GetContextNode(nsIDOMNode **aNode)
{
NS_ENSURE_TRUE(mContext, NS_ERROR_FAILURE);
return txXPathNativeNode::getNode(mContext->getContextNode(), aNode);
}
NS_IMETHODIMP
txFunctionEvaluationContext::GetState(nsISupports **aState)
{
NS_IF_ADDREF(*aState = mState);
return NS_OK;
}
enum txArgumentType {
eBOOLEAN = nsXPTType::T_BOOL,
eNUMBER = nsXPTType::T_DOUBLE,
eSTRING = nsXPTType::T_DOMSTRING,
eNODESET,
eCONTEXT,
eOBJECT,
eUNKNOWN
};
class txXPCOMExtensionFunctionCall : public FunctionCall
{
public:
txXPCOMExtensionFunctionCall(nsISupports *aHelper, const nsIID &aIID,
uint16_t aMethodIndex,
#ifdef TX_TO_STRING
nsIAtom *aName,
#endif
nsISupports *aState);
TX_DECL_FUNCTION
private:
txArgumentType GetParamType(const nsXPTParamInfo &aParam,
nsIInterfaceInfo *aInfo);
nsCOMPtr<nsISupports> mHelper;
nsIID mIID;
uint16_t mMethodIndex;
#ifdef TX_TO_STRING
nsCOMPtr<nsIAtom> mName;
#endif
nsCOMPtr<nsISupports> mState;
};
txXPCOMExtensionFunctionCall::txXPCOMExtensionFunctionCall(nsISupports *aHelper,
const nsIID &aIID,
uint16_t aMethodIndex,
#ifdef TX_TO_STRING
nsIAtom *aName,
#endif
nsISupports *aState)
: mHelper(aHelper),
mIID(aIID),
mMethodIndex(aMethodIndex),
#ifdef TX_TO_STRING
mName(aName),
#endif
mState(aState)
{
}
class txInterfacesArrayHolder
{
public:
txInterfacesArrayHolder(nsIID **aArray, uint32_t aCount) : mArray(aArray),
mCount(aCount)
{
}
~txInterfacesArrayHolder()
{
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mArray);
}
private:
nsIID **mArray;
uint32_t mCount;
};
static nsresult
LookupFunction(const char *aContractID, nsIAtom* aName, nsIID &aIID,
uint16_t &aMethodIndex, nsISupports **aHelper)
{
nsresult rv;
nsCOMPtr<nsISupports> helper = do_GetService(aContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(helper, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIInterfaceInfoManager> iim =
do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(iim, NS_ERROR_FAILURE);
nsIID** iidArray = nullptr;
uint32_t iidCount = 0;
rv = classInfo->GetInterfaces(&iidCount, &iidArray);
NS_ENSURE_SUCCESS(rv, rv);
txInterfacesArrayHolder holder(iidArray, iidCount);
// Remove any minus signs and uppercase the following letter (so
// foo-bar becomes fooBar). Note that if there are any names that already
// have uppercase letters they might cause false matches (both fooBar and
// foo-bar matching fooBar).
const char16_t *name = aName->GetUTF16String();
nsAutoCString methodName;
char16_t letter;
bool upperNext = false;
while ((letter = *name)) {
if (letter == '-') {
upperNext = true;
}
else {
MOZ_ASSERT(nsCRT::IsAscii(letter),
"invalid static_cast coming up");
methodName.Append(upperNext ?
nsCRT::ToUpper(static_cast<char>(letter)) :
letter);
upperNext = false;
}
++name;
}
uint32_t i;
for (i = 0; i < iidCount; ++i) {
nsIID *iid = iidArray[i];
nsCOMPtr<nsIInterfaceInfo> info;
rv = iim->GetInfoForIID(iid, getter_AddRefs(info));
NS_ENSURE_SUCCESS(rv, rv);
uint16_t methodIndex;
const nsXPTMethodInfo *methodInfo;
rv = info->GetMethodInfoForName(methodName.get(), &methodIndex,
&methodInfo);
if (NS_SUCCEEDED(rv)) {
// Exclude notxpcom and hidden. Also check that we have at least a
// return value (the xpidl compiler ensures that that return value
// is the last argument).
uint8_t paramCount = methodInfo->GetParamCount();
if (methodInfo->IsNotXPCOM() || methodInfo->IsHidden() ||
paramCount == 0 ||
!methodInfo->GetParam(paramCount - 1).IsRetval()) {
return NS_ERROR_FAILURE;
}
aIID = *iid;
aMethodIndex = methodIndex;
return helper->QueryInterface(aIID, (void**)aHelper);
}
}
return NS_ERROR_XPATH_UNKNOWN_FUNCTION;
}
/* static */
nsresult
TX_ResolveFunctionCallXPCOM(const nsCString &aContractID, int32_t aNamespaceID,
nsIAtom* aName, nsISupports *aState,
FunctionCall **aFunction)
{
nsIID iid;
uint16_t methodIndex = 0;
nsCOMPtr<nsISupports> helper;
nsresult rv = LookupFunction(aContractID.get(), aName, iid, methodIndex,
getter_AddRefs(helper));
NS_ENSURE_SUCCESS(rv, rv);
if (!aFunction) {
return NS_OK;
}
*aFunction = new txXPCOMExtensionFunctionCall(helper, iid, methodIndex,
#ifdef TX_TO_STRING
aName,
#endif
aState);
return NS_OK;
}
txArgumentType
txXPCOMExtensionFunctionCall::GetParamType(const nsXPTParamInfo &aParam,
nsIInterfaceInfo *aInfo)
{
uint8_t tag = aParam.GetType().TagPart();
switch (tag) {
case nsXPTType::T_BOOL:
case nsXPTType::T_DOUBLE:
case nsXPTType::T_DOMSTRING:
{
return txArgumentType(tag);
}
case nsXPTType::T_INTERFACE:
case nsXPTType::T_INTERFACE_IS:
{
nsIID iid;
aInfo->GetIIDForParamNoAlloc(mMethodIndex, &aParam, &iid);
if (iid.Equals(NS_GET_IID(txINodeSet))) {
return eNODESET;
}
if (iid.Equals(NS_GET_IID(txIFunctionEvaluationContext))) {
return eCONTEXT;
}
if (iid.Equals(NS_GET_IID(txIXPathObject))) {
return eOBJECT;
}
return eUNKNOWN;
}
default:
{
// XXX Error!
return eUNKNOWN;
}
}
}
class txParamArrayHolder
{
public:
txParamArrayHolder()
: mCount(0)
{
}
txParamArrayHolder(txParamArrayHolder&& rhs)
: mArray(mozilla::Move(rhs.mArray))
, mCount(rhs.mCount)
{
rhs.mCount = 0;
}
~txParamArrayHolder();
bool Init(uint8_t aCount);
operator nsXPTCVariant*() const
{
return mArray.get();
}
void trace(JSTracer* trc) {
for (uint8_t i = 0; i < mCount; ++i) {
if (mArray[i].type == nsXPTType::T_JSVAL) {
JS::UnsafeTraceRoot(trc, &mArray[i].val.j.asValueRef(), "txParam value");
}
}
}
private:
mozilla::UniquePtr<nsXPTCVariant[]> mArray;
uint8_t mCount;
};
txParamArrayHolder::~txParamArrayHolder()
{
uint8_t i;
for (i = 0; i < mCount; ++i) {
nsXPTCVariant &variant = mArray[i];
if (variant.DoesValNeedCleanup()) {
if (variant.type.TagPart() == nsXPTType::T_DOMSTRING)
delete (nsAString*)variant.val.p;
else {
MOZ_ASSERT(variant.type.TagPart() == nsXPTType::T_INTERFACE ||
variant.type.TagPart() == nsXPTType::T_INTERFACE_IS,
"We only support cleanup of strings and interfaces "
"here, and this looks like neither!");
static_cast<nsISupports*>(variant.val.p)->Release();
}
}
}
}
bool
txParamArrayHolder::Init(uint8_t aCount)
{
mCount = aCount;
mArray = mozilla::MakeUnique<nsXPTCVariant[]>(mCount);
if (!mArray) {
return false;
}
memset(mArray.get(), 0, mCount * sizeof(nsXPTCVariant));
return true;
}
nsresult
txXPCOMExtensionFunctionCall::evaluate(txIEvalContext* aContext,
txAExprResult** aResult)
{
nsCOMPtr<nsIInterfaceInfoManager> iim =
do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(iim, NS_ERROR_FAILURE);
nsCOMPtr<nsIInterfaceInfo> info;
nsresult rv = iim->GetInfoForIID(&mIID, getter_AddRefs(info));
NS_ENSURE_SUCCESS(rv, rv);
const nsXPTMethodInfo *methodInfo;
rv = info->GetMethodInfo(mMethodIndex, &methodInfo);
NS_ENSURE_SUCCESS(rv, rv);
uint8_t paramCount = methodInfo->GetParamCount();
uint8_t inArgs = paramCount - 1;
JS::Rooted<txParamArrayHolder> invokeParams(mozilla::dom::RootingCx());
if (!invokeParams.get().Init(paramCount)) {
return NS_ERROR_OUT_OF_MEMORY;
}
const nsXPTParamInfo ¶mInfo = methodInfo->GetParam(0);
txArgumentType type = GetParamType(paramInfo, info);
if (type == eUNKNOWN) {
return NS_ERROR_FAILURE;
}
txFunctionEvaluationContext *context;
uint32_t paramStart = 0;
if (type == eCONTEXT) {
if (paramInfo.IsOut()) {
// We don't support out values.
return NS_ERROR_FAILURE;
}
// Create context wrapper.
context = new txFunctionEvaluationContext(aContext, mState);
nsXPTCVariant &invokeParam = invokeParams.get()[0];
invokeParam.type = paramInfo.GetType();
invokeParam.SetValNeedsCleanup();
NS_ADDREF((txIFunctionEvaluationContext*&)invokeParam.val.p = context);
// Skip first argument, since it's the context.
paramStart = 1;
}
else {
context = nullptr;
}
// XXX varargs
if (!requireParams(inArgs - paramStart, inArgs - paramStart, aContext)) {
return NS_ERROR_FAILURE;
}
uint32_t i;
for (i = paramStart; i < inArgs; ++i) {
Expr* expr = mParams[i - paramStart];
const nsXPTParamInfo ¶mInfo = methodInfo->GetParam(i);
txArgumentType type = GetParamType(paramInfo, info);
if (type == eUNKNOWN) {
return NS_ERROR_FAILURE;
}
nsXPTCVariant &invokeParam = invokeParams.get()[i];
if (paramInfo.IsOut()) {
// We don't support out values.
return NS_ERROR_FAILURE;
}
invokeParam.type = paramInfo.GetType();
switch (type) {
case eNODESET:
{
RefPtr<txNodeSet> nodes;
rv = evaluateToNodeSet(expr, aContext, getter_AddRefs(nodes));
NS_ENSURE_SUCCESS(rv, rv);
txNodeSetAdaptor *adaptor = new txNodeSetAdaptor(nodes);
if (!adaptor) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsCOMPtr<txINodeSet> nodeSet = adaptor;
rv = adaptor->Init();
NS_ENSURE_SUCCESS(rv, rv);
invokeParam.SetValNeedsCleanup();
nodeSet.swap((txINodeSet*&)invokeParam.val.p);
break;
}
case eBOOLEAN:
{
rv = expr->evaluateToBool(aContext, invokeParam.val.b);
NS_ENSURE_SUCCESS(rv, rv);
break;
}
case eNUMBER:
{
double dbl;
rv = evaluateToNumber(mParams[0], aContext, &dbl);
NS_ENSURE_SUCCESS(rv, rv);
invokeParam.val.d = dbl;
break;
}
case eSTRING:
{
nsString *value = new nsString();
if (!value) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = expr->evaluateToString(aContext, *value);
NS_ENSURE_SUCCESS(rv, rv);
invokeParam.SetValNeedsCleanup();
invokeParam.val.p = value;
break;
}
case eOBJECT:
{
RefPtr<txAExprResult> exprRes;
rv = expr->evaluate(aContext, getter_AddRefs(exprRes));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<txIXPathObject> adaptor =
new txXPathObjectAdaptor(exprRes);
if (!adaptor) {
return NS_ERROR_OUT_OF_MEMORY;
}
invokeParam.SetValNeedsCleanup();
adaptor.swap((txIXPathObject*&)invokeParam.val.p);
break;
}
case eCONTEXT:
case eUNKNOWN:
{
// We only support passing the context as the *first* argument.
return NS_ERROR_FAILURE;
}
}
}
const nsXPTParamInfo &returnInfo = methodInfo->GetParam(inArgs);
txArgumentType returnType = GetParamType(returnInfo, info);
if (returnType == eUNKNOWN) {
return NS_ERROR_FAILURE;
}
nsXPTCVariant &returnParam = invokeParams.get()[inArgs];
returnParam.type = returnInfo.GetType();
if (returnType == eSTRING) {
nsString *value = new nsString();
returnParam.SetValNeedsCleanup();
returnParam.val.p = value;
}
else {
returnParam.SetIndirect();
if (returnType == eNODESET || returnType == eOBJECT) {
returnParam.SetValNeedsCleanup();
}
}
rv = NS_InvokeByIndex(mHelper, mMethodIndex, paramCount, invokeParams.get());
// In case someone is holding on to the txFunctionEvaluationContext which
// could thus stay alive longer than this function.
if (context) {
context->ClearContext();
}
NS_ENSURE_SUCCESS(rv, rv);
switch (returnType) {
case eNODESET:
{
txINodeSet* nodeSet = static_cast<txINodeSet*>(returnParam.val.p);
nsCOMPtr<txIXPathObject> object = do_QueryInterface(nodeSet, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NS_ADDREF(*aResult = object->GetResult());
return NS_OK;
}
case eBOOLEAN:
{
aContext->recycler()->getBoolResult(returnParam.val.b, aResult);
return NS_OK;
}
case eNUMBER:
{
return aContext->recycler()->getNumberResult(returnParam.val.d,
aResult);
}
case eSTRING:
{
nsString *returned = static_cast<nsString*>
(returnParam.val.p);
return aContext->recycler()->getStringResult(*returned, aResult);
}
case eOBJECT:
{
txIXPathObject *object =
static_cast<txIXPathObject*>(returnParam.val.p);
NS_ADDREF(*aResult = object->GetResult());
return NS_OK;
}
default:
{
// Huh?
return NS_ERROR_FAILURE;
}
}
}
Expr::ResultType
txXPCOMExtensionFunctionCall::getReturnType()
{
// It doesn't really matter what we return here, but it might
// be a good idea to try to keep this as unoptimizable as possible
return ANY_RESULT;
}
bool
txXPCOMExtensionFunctionCall::isSensitiveTo(ContextSensitivity aContext)
{
// It doesn't really matter what we return here, but it might
// be a good idea to try to keep this as unoptimizable as possible
return true;
}
#ifdef TX_TO_STRING
nsresult
txXPCOMExtensionFunctionCall::getNameAtom(nsIAtom** aAtom)
{
NS_ADDREF(*aAtom = mName);
return NS_OK;
}
#endif
|