FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/r/RSexp.cpp at master · andschar/rstudio · GitHub
andschar
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
r
/
RSexp.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1701 lines (1396 loc) · 42.8 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
r
/
RSexp.cpp
Copy path
File metadata and controls
1701 lines (1396 loc) · 42.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* RSexp.cpp
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
define
R_INTERNAL_FUNCTIONS
#
define
RSTUDIO_DEBUG_LABEL
"
rsexp
"
//
#define RSTUDIO_ENABLE_DEBUG_MACROS
#
include
<
r/RSexp.hpp
>
#
include
<
r/RInternal.hpp
>
#
include
<
core/Algorithm.hpp
>
#
include
<
boost/bind.hpp
>
#
include
<
boost/function.hpp
>
#
include
<
boost/foreach.hpp
>
#
include
<
boost/numeric/conversion/cast.hpp
>
#
include
<
boost/optional.hpp
>
#
include
<
core/Macros.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/DateTime.hpp
>
#
include
<
r/RExec.hpp
>
#
include
<
r/RErrorCategory.hpp
>
//
clean out global definitions of TRUE and FALSE so we can
//
use the Rboolean variations of them
#
undef
TRUE
#
undef
FALSE
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
r
{
using
namespace
exec
;
namespace
sexp
{
using
namespace
core
::r_util
;
namespace
{
struct
LexicalComparator
{
inline
bool
operator
()(
const
char
* lhs,
const
char
* rhs)
const
{
return
strcmp
(lhs, rhs) <
0
;
}
};
//
A simple wrapper set class that is primarily used as a means
//
to re-use R's internal string cache, while providing lexical
//
comparator for efficient lookup.
class
StringSet
:
public
std
::set<
const
char
*, LexicalComparator>
{
public:
bool
contains
(
const
char
* value)
{
return
this
->
find
(value) !=
this
->
end
();
}
};
struct
FunctionSymbolUsage
{
StringSet symbolsUsed;
StringSet symbolsCheckedForMissingness;
};
//
singleton: cache the result of 'examination' of functions
class
FunctionSymbolUsageCache
: boost::noncopyable
{
typedef
std::pair<
SEXP
,
SEXP
> FunctionEnvironmentPair;
public:
bool
contains
(
SEXP
object)
{
return
database_.
count
(
pair
(object));
}
FunctionSymbolUsage&
get
(
SEXP
object)
{
return
database_[
pair
(object)];
}
void
put
(
SEXP
object,
const
FunctionSymbolUsage& usage)
{
database_[
pair
(object)] = usage;
}
private:
static
FunctionEnvironmentPair
pair
(
SEXP
object)
{
return
std::make_pair
(object,
CLOENV
(object));
}
std::map<FunctionEnvironmentPair, FunctionSymbolUsage> database_;
};
FunctionSymbolUsageCache&
functionSymbolUsageCache
()
{
static
FunctionSymbolUsageCache instance;
return
instance;
}
}
//
anonymous namespace
std::string
asString
(
SEXP
object)
{
return
std::string
(
Rf_translateChar
(
Rf_asChar
(object)));
}
std::string
safeAsString
(
SEXP
object,
const
std::string& defValue)
{
if
(object != R_NilValue)
return
asString
(object);
else
return
defValue;
}
int
asInteger
(
SEXP
object)
{
return
Rf_asInteger
(object);
}
double
asReal
(
SEXP
object)
{
return
Rf_asReal
(object);
}
bool
asLogical
(
SEXP
object)
{
return
Rf_asLogical
(object) ?
true
:
false
;
}
bool
fillVectorString
(
SEXP
object, std::vector<std::string>* pVector)
{
if
(
TYPEOF
(object) !=
STRSXP
)
return
false
;
int
n =
Rf_length
(object);
pVector->
reserve
(pVector->
size
() + n);
for
(
int
i =
0
; i < n; i++)
pVector->
push_back
(
std::string
(
CHAR
(
STRING_ELT
(object, i))));
return
true
;
}
bool
fillSetString
(
SEXP
object, std::set<std::string>* pSet)
{
if
(
TYPEOF
(object) !=
STRSXP
)
return
false
;
int
n =
Rf_length
(object);
for
(
int
i =
0
; i < n; i++)
pSet->
insert
(
std::string
(
CHAR
(
STRING_ELT
(object, i))));
return
true
;
}
SEXP
asEnvironment
(std::string name)
{
if
(name ==
"
base
"
)
return
R_BaseEnv;
//
prefix with 'package:' if no prefix specified yet
if
(name.
find
(
"
:
"
) == std::string::npos)
name =
"
package:
"
+ name;
SEXP
envSEXP =
ENCLOS
(R_GlobalEnv);
while
(envSEXP != R_EmptyEnv)
{
SEXP
nameSEXP =
Rf_getAttrib
(envSEXP, R_NameSymbol);
if
(
TYPEOF
(nameSEXP) ==
STRSXP
&&
name ==
CHAR
(
STRING_ELT
(nameSEXP,
0
)))
{
return
envSEXP;
}
envSEXP =
ENCLOS
(envSEXP);
}
LOG_ERROR_MESSAGE
(
"
No environment named '
"
+ name +
"
' on search path
"
);
return
envSEXP;
}
namespace
{
bool
ensureNamespaceLoaded
(
const
std::string& ns)
{
if
(ns.
empty
())
return
false
;
SEXP
nsSEXP =
findNamespace
(ns);
if
(nsSEXP == R_UnboundValue)
{
r::exec::RFunction
requireNamespace
(
"
base:::requireNamespace
"
);
requireNamespace.
addParam
(
"
package
"
, ns);
requireNamespace.
addParam
(
"
quietly
"
,
true
);
Error error = requireNamespace.
call
();
if
(error)
return
false
;
}
return
true
;
}
}
//
anonymous namespace
std::vector<std::string>
getLoadedNamespaces
()
{
std::vector<std::string> result;
r::exec::RFunction
loadedNamespaces
(
"
loadedNamespaces
"
,
"
base
"
);
Error error = loadedNamespaces.
call
(&result);
if
(error)
LOG_ERROR
(error);
return
result;
}
SEXP
asNamespace
(
const
std::string& name)
{
if
(!
ensureNamespaceLoaded
(name))
return
R_EmptyEnv;
return
findNamespace
(name);
}
SEXP
forcePromise
(
SEXP
objectSEXP)
{
//
if this isn't a promise, return it as-is
if
(
TYPEOF
(objectSEXP) !=
PROMSXP
)
return
objectSEXP;
//
if we already have a forced value, return that
SEXP
valueSEXP =
PRVALUE
(objectSEXP);
if
(valueSEXP != R_UnboundValue)
return
valueSEXP;
//
otherwise, evaluate the promise and return that result
r::sexp::Protect protect;
SEXP
resultSEXP;
protect.
add
(resultSEXP = ::
Rf_eval
(
PRCODE
(objectSEXP),
PRENV
(objectSEXP)));
//
update the promise reference
SET_PRVALUE
(objectSEXP, resultSEXP);
//
return the result
return
resultSEXP;
}
SEXP
findNamespace
(
const
std::string& name)
{
if
(name.
empty
())
return
R_UnboundValue;
//
case 4071: namespace look up executes R code that can trip the debugger
DisableDebugScope
disableStepInto
(R_GlobalEnv);
//
R_FindNamespace will throw if it fails to find a particular name.
//
Instead, we manually search the namespace registry.
SEXP
nameSEXP =
Rf_install
(name.
c_str
());
SEXP
ns =
Rf_findVarInFrame
(R_NamespaceRegistry, nameSEXP);
return
ns;
}
void
listEnvironment
(
SEXP
env,
bool
includeAll,
bool
includeLastDotValue,
Protect* pProtect,
std::vector<Variable>* pVariables)
{
//
reset passed vars
pVariables->
clear
();
//
get the list of environment vars (protect locally because we
//
we don't acutally return this list to the caller
SEXP
envVarsSEXP;
Protect
rProtect
(envVarsSEXP =
R_lsInternal
(env, includeAll ?
TRUE
:
FALSE
));
//
get variables
std::vector<std::string> vars;
Error error =
r::sexp::extract
(envVarsSEXP, &vars);
if
(error)
{
LOG_ERROR
(error);
return
;
}
//
add in .Last.value if it exists
if
(!includeAll && includeLastDotValue)
{
SEXP
lastValueSEXP =
Rf_findVar
(
Rf_install
(
"
.Last.value
"
), env);
if
(lastValueSEXP != R_UnboundValue)
vars.
push_back
(
"
.Last.value
"
);
}
//
populate pVariables
BOOST_FOREACH
(
const
std::string& var, vars)
{
SEXP
varSEXP = R_NilValue;
//
Merely calling Rf_findVar on an active binding will fire the binding.
//
Don't try to get the SEXP for the variable in this case; leave the
//
value as nil.
if
(!
isActiveBinding
(var, env))
varSEXP =
Rf_findVar
(
Rf_install
(var.
c_str
()), env);
if
(varSEXP != R_UnboundValue)
//
should never be unbound
{
pProtect->
add
(varSEXP);
pVariables->
push_back
(
std::make_pair
(var, varSEXP));
}
else
{
LOG_WARNING_MESSAGE
(
"
Unexpected R_UnboundValue returned from R_lsInternal
"
);
}
}
}
namespace
{
Error
asPrimitiveEnvironment
(
SEXP
envirSEXP,
SEXP
* pTargetSEXP,
Protect* pProtect)
{
//
fast-case: no need to call back into R
if
(
TYPEOF
(envirSEXP) ==
ENVSXP
)
{
pProtect->
add
(*pTargetSEXP = envirSEXP);
return
Success
();
}
//
for non-S4 objects, we can just return an error (false) early
if
(
TYPEOF
(envirSEXP) !=
S4SXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
//
use R function to convert
Error error =
RFunction
(
"
base:::as.environment
"
)
.
addParam
(envirSEXP)
.
call
(pTargetSEXP, pProtect);
if
(error)
return
error;
//
ensure that we actually succeeded in producing a primitive environment
if
(pTargetSEXP ==
NULL
||
*pTargetSEXP ==
NULL
||
!
isPrimitiveEnvironment
(*pTargetSEXP))
{
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
}
//
we have a primitive environment; all is well
return
Success
();
}
bool
hasActiveBindingImpl
(
const
std::string& name,
SEXP
envirSEXP,
std::set<
SEXP
>* pVisitedObjects)
{
Error error;
Protect protect;
//
ensure we have an environment
if
(!
isEnvironment
(envirSEXP))
return
false
;
//
sanity check that we are working with a primitive environment
//
(required to convert S4 objects that subclass 'environment' into
//
a 'raw' R environment object)
error =
asPrimitiveEnvironment
(envirSEXP, &envirSEXP, &protect);
if
(error)
return
false
;
//
check for active binding
if
(
isActiveBinding
(name, envirSEXP))
return
true
;
//
resolve the object (discover in that frame)
SEXP
nameSEXP =
Rf_install
(name.
c_str
());
SEXP
varSEXP =
Rf_findVarInFrame
(envirSEXP, nameSEXP);
//
check for special values
if
(varSEXP == R_UnboundValue || varSEXP == R_MissingArg)
return
false
;
//
ensure we're working with a primitive R environment
if
(!
isEnvironment
(varSEXP))
return
false
;
error =
asPrimitiveEnvironment
(varSEXP, &varSEXP, &protect);
if
(error)
return
false
;
//
avoid cycles
if
(pVisitedObjects->
count
(varSEXP))
return
false
;
pVisitedObjects->
insert
(varSEXP);
//
list the bindings in this object
SEXP
bindingsSEXP;
protect.
add
(bindingsSEXP =
R_lsInternal
(varSEXP,
TRUE
));
//
iterate over items and search for active bindings
for
(
int
i =
0
, n =
Rf_length
(bindingsSEXP); i < n; ++i)
{
const
char
* binding =
CHAR
(
STRING_ELT
(bindingsSEXP, i));
if
(
hasActiveBindingImpl
(binding, varSEXP, pVisitedObjects))
return
true
;
}
//
no child binding has active binding; return false
return
false
;
}
}
//
end anonymous namespace
bool
hasActiveBinding
(
const
std::string& name,
const
SEXP
envirSEXP)
{
//
avoid cycles when searching recursively
std::set<
SEXP
> visitedObjects;
return
hasActiveBindingImpl
(name, envirSEXP, &visitedObjects);
}
bool
isActiveBinding
(
const
std::string& name,
const
SEXP
env)
{
//
R_BindingIsActive throws error on .Last.value check; avoid that and
//
just assume that it's not an active binding (and hence is okay to eval)
if
(name ==
"
.Last.value
"
)
return
false
;
return
R_BindingIsActive
(
Rf_install
(name.
c_str
()), env);
}
SEXP
functionBody
(
SEXP
functionSEXP)
{
if
(!
Rf_isFunction
(functionSEXP))
return
R_NilValue;
if
(
Rf_isPrimitive
(functionSEXP))
return
R_NilValue;
SEXP
bodySEXP = R_NilValue;
Protect protect;
RFunction
getBody
(
"
base:::body
"
);
getBody.
addParam
(functionSEXP);
Error error = getBody.
call
(&bodySEXP, &protect);
if
(error)
LOG_ERROR
(error);
return
bodySEXP;
}
SEXP
findVar
(
const
std::string &name,
const
SEXP
env)
{
return
Rf_findVar
(
Rf_install
(name.
c_str
()), env);
}
SEXP
findVar
(
const
std::string& name,
const
std::string& ns)
{
if
(name.
empty
())
return
R_UnboundValue;
if
(!ns.
empty
())
if
(!
ensureNamespaceLoaded
(ns))
return
R_UnboundValue;
SEXP
env = ns.
empty
() ? R_GlobalEnv :
findNamespace
(ns);
return
findVar
(name, env);
}
SEXP
findFunction
(
const
std::string& name,
const
std::string& ns)
{
r::sexp::Protect protect;
if
(name.
empty
())
return
R_UnboundValue;
if
(!ns.
empty
())
if
(!
ensureNamespaceLoaded
(ns))
return
R_UnboundValue;
SEXP
env = ns.
empty
() ? R_GlobalEnv :
findNamespace
(ns);
if
(env == R_UnboundValue)
return
R_UnboundValue;
//
We might want to use `Rf_findFun`, but it calls `Rf_error`
//
on failure, which involves printing the error message out
//
to the console. To avoid this,
//
we instead attempt to find the function by manually
//
walking through the environment (and its enclosing environments)
SEXP
nameSEXP =
Rf_install
(name.
c_str
());
//
Search through frames until we find the global environment.
while
(env != R_EmptyEnv)
{
//
If we're searching the global environment, then
//
try using 'Rf_findVar', as this will attempt a search
//
of R's own internal global cache.
if
(env == R_GlobalEnv)
{
SEXP
resultSEXP =
Rf_findVar
(nameSEXP, R_GlobalEnv);
if
(
Rf_isFunction
(resultSEXP))
return
resultSEXP;
else
if
(
TYPEOF
(resultSEXP) ==
PROMSXP
)
{
protect.
add
(resultSEXP =
Rf_eval
(resultSEXP, env));
if
(
Rf_isFunction
(resultSEXP))
return
resultSEXP;
}
}
//
Otherwise, just perform a simple search through
//
the current frame.
SEXP
resultSEXP =
Rf_findVarInFrame
(env, nameSEXP);
if
(resultSEXP != R_UnboundValue)
{
if
(
Rf_isFunction
(resultSEXP))
return
resultSEXP;
else
if
(
TYPEOF
(resultSEXP) ==
PROMSXP
)
{
protect.
add
(resultSEXP =
Rf_eval
(resultSEXP, env));
if
(
Rf_isFunction
(resultSEXP))
return
resultSEXP;
}
}
env =
ENCLOS
(env);
}
return
R_UnboundValue;
}
std::string
typeAsString
(
SEXP
object)
{
return
Rf_type2char
(
TYPEOF
(object));
}
std::string
classOf
(
SEXP
objectSEXP)
{
return
asString
(
Rf_getAttrib
(objectSEXP,
Rf_install
(
"
class
"
)));
}
int
length
(
SEXP
object)
{
return
Rf_length
(object);
}
bool
isLanguage
(
SEXP
object)
{
return
Rf_isLanguage
(object);
}
bool
isList
(
SEXP
object)
{
return
TYPEOF
(object) ==
VECSXP
;
}
bool
isString
(
SEXP
object)
{
return
Rf_isString
(object);
}
bool
isMatrix
(
SEXP
object)
{
return
Rf_isMatrix
(object);
}
bool
isDataFrame
(
SEXP
object)
{
return
Rf_isFrame
(object);
}
bool
isNull
(
SEXP
object)
{
return
Rf_isNull
(object) ==
TRUE
;
}
bool
isPrimitiveEnvironment
(
SEXP
object)
{
return
TYPEOF
(object) ==
ENVSXP
;
}
bool
isEnvironment
(
SEXP
object)
{
//
detect primitive environments (fast path)
if
(
isPrimitiveEnvironment
(object))
return
true
;
//
call back to R to detect objects subclassing environment
if
(
TYPEOF
(object) ==
S4SXP
)
{
bool
result =
false
;
Error error =
RFunction
(
"
base:::is.environment
"
)
.
addParam
(object)
.
call
(&result);
if
(error)
LOG_ERROR
(error);
return
result;
}
return
false
;
}
SEXP
getNames
(
SEXP
sexp)
{
return
Rf_getAttrib
(sexp, R_NamesSymbol);
}
bool
setNames
(
SEXP
sexp,
const
std::vector<std::string>& names)
{
std::
size_t
n = names.
size
();
if
(
static_cast
<std::
size_t
>(
Rf_length
(sexp)) != n)
return
false
;
Rf_setAttrib
(sexp,
R_NamesSymbol,
Rf_allocVector
(
STRSXP
, names.
size
()));
SEXP
namesSEXP =
Rf_getAttrib
(sexp, R_NamesSymbol);
for
(std::
size_t
i =
0
; i < n; ++i)
SET_STRING_ELT
(namesSEXP, i,
Rf_mkChar
(names[i].
c_str
()));
return
true
;
}
Error
getNames
(
SEXP
sexp, std::vector<std::string>* pNames)
{
//
attempt to get the field names
SEXP
namesSEXP =
getNames
(sexp);
if
(namesSEXP == R_NilValue ||
TYPEOF
(namesSEXP) !=
STRSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
else
if
(
Rf_length
(namesSEXP) !=
Rf_length
(sexp))
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
//
copy them into the vector
for
(
int
i=
0
; i<
Rf_length
(namesSEXP); i++)
pNames->
push_back
(
Rf_translateChar
(
STRING_ELT
(namesSEXP, i)) );
return
Success
();
}
SEXP
getAttrib
(
SEXP
object,
SEXP
attrib)
{
return
Rf_getAttrib
(object, attrib);
}
SEXP
getAttrib
(
SEXP
object,
const
std::string& attrib)
{
return
getAttrib
(object,
Rf_install
(attrib.
c_str
()));
}
SEXP
setAttrib
(
SEXP
object,
const
std::string& attrib,
SEXP
val)
{
return
Rf_setAttrib
(object,
Rf_install
(attrib.
c_str
()), val);
}
bool
isExternalPointer
(
SEXP
object)
{
return
TYPEOF
(object) ==
EXTPTRSXP
;
}
bool
isNullExternalPointer
(
SEXP
object)
{
return
isExternalPointer
(object) &&
R_ExternalPtrAddr
(object) ==
NULL
;
}
SEXP
makeWeakRef
(
SEXP
key,
SEXP
val, R_CFinalizer_t fun, Rboolean onexit)
{
return
R_MakeWeakRefC
(key, val, fun, onexit);
}
void
registerFinalizer
(
SEXP
s, R_CFinalizer_t fun)
{
R_RegisterCFinalizer
(s, fun);
}
SEXP
makeExternalPtr
(
void
* ptr, R_CFinalizer_t fun, Protect* pProtect)
{
SEXP
s =
R_MakeExternalPtr
(ptr, R_NilValue, R_NilValue);
if
(pProtect)
pProtect->
add
(s);
registerFinalizer
(s, fun);
return
s;
}
void
*
getExternalPtrAddr
(
SEXP
extptr)
{
return
R_ExternalPtrAddr
(extptr);
}
void
clearExternalPtr
(
SEXP
extptr)
{
R_ClearExternalPtr
(extptr);
}
core::Error
getNamedListSEXP
(
SEXP
listSEXP,
const
std::string& name,
SEXP
* pValueSEXP)
{
int
valueIndex =
indexOfElementNamed
(listSEXP, name);
if
(valueIndex != -
1
)
{
//
get the appropriate value
*pValueSEXP =
VECTOR_ELT
(listSEXP, valueIndex);
return
core::Success
();
}
else
{
//
otherwise an error
core::Error
error
(r::errc::ListElementNotFoundError,
ERROR_LOCATION
);
error.
addProperty
(
"
element
"
, name);
return
error;
}
}
Error
extract
(
SEXP
valueSEXP,
int
* pInt)
{
if
(
TYPEOF
(valueSEXP) !=
INTSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
if
(
Rf_length
(valueSEXP) <
1
)
return
Error
(errc::NoDataAvailableError,
ERROR_LOCATION
);
*pInt =
INTEGER
(valueSEXP)[
0
] ;
return
Success
();
}
Error
extract
(
SEXP
valueSEXP,
bool
* pBool)
{
if
(
TYPEOF
(valueSEXP) !=
LGLSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
if
(
Rf_length
(valueSEXP) <
1
)
return
Error
(errc::NoDataAvailableError,
ERROR_LOCATION
);
*pBool =
LOGICAL
(valueSEXP)[
0
] ==
TRUE
?
true
:
false
;
return
Success
();
}
Error
extract
(
SEXP
valueSEXP,
double
* pDouble)
{
if
(
TYPEOF
(valueSEXP) !=
REALSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
if
(
Rf_length
(valueSEXP) <
1
)
return
Error
(errc::NoDataAvailableError,
ERROR_LOCATION
);
*pDouble =
REAL
(valueSEXP)[
0
];
return
Success
();
}
Error
extract
(
SEXP
valueSEXP, std::vector<
int
>* pVector)
{
if
(
TYPEOF
(valueSEXP) !=
INTSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
pVector->
clear
();
for
(
int
i=
0
; i<
Rf_length
(valueSEXP); i++)
pVector->
push_back
(
INTEGER
(valueSEXP)[i]);
return
Success
();
}
Error
extract
(
SEXP
valueSEXP, std::string* pString,
bool
asUtf8)
{
if
(
TYPEOF
(valueSEXP) !=
STRSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
if
(
Rf_length
(valueSEXP) <
1
)
return
Error
(errc::NoDataAvailableError,
ERROR_LOCATION
);
*pString =
std::string
(asUtf8 ?
Rf_translateCharUTF8
(
STRING_ELT
(valueSEXP,
0
)) :
Rf_translateChar
(
STRING_ELT
(valueSEXP,
0
)));
return
Success
();
}
Error
extract
(
SEXP
valueSEXP, std::vector<std::string>* pVector)
{
if
(
TYPEOF
(valueSEXP) !=
STRSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
pVector->
clear
();
for
(
int
i=
0
; i<
Rf_length
(valueSEXP); i++)
pVector->
push_back
(
Rf_translateChar
(
STRING_ELT
(valueSEXP, i)));
return
Success
();
}
Error
extract
(
SEXP
valueSEXP, std::set<std::string>* pSet)
{
if
(
TYPEOF
(valueSEXP) !=
STRSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
pSet->
clear
();
for
(
int
i=
0
; i<
Rf_length
(valueSEXP); i++)
pSet->
insert
(
Rf_translateChar
(
STRING_ELT
(valueSEXP, i)));
return
Success
();
}
Error
extract
(
SEXP
valueSEXP, std::map< std::string, std::set<std::string> >* pMap)
{
if
(
TYPEOF
(valueSEXP) !=
VECSXP
)
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
if
(
Rf_length
(valueSEXP) ==
0
)
return
Success
();
SEXP
namesSEXP =
r::sexp::getNames
(valueSEXP);
if
(
Rf_isNull
(namesSEXP))
return
Error
(errc::UnexpectedDataTypeError,
ERROR_LOCATION
);
for
(
int
i =
0
; i <
Rf_length
(valueSEXP); ++i)
{
SEXP
el =
VECTOR_ELT
(valueSEXP, i);
std::set<std::string> contents;
for
(
int
j =
0
; j <
Rf_length
(el); ++j)
contents.
insert
(
std::string
(
Rf_translateChar
(
STRING_ELT
(el, j))));
std::string name =
std::string
(
Rf_translateChar
(
STRING_ELT
(namesSEXP, i)));
pMap->
operator
[](name) = contents;
}
return
Success
();
}
SEXP
create
(
SEXP
valueSEXP, Protect* pProtect)
{
pProtect->
add
(valueSEXP);
return
valueSEXP;
}
SEXP
create
(
const
json::Value& value, Protect* pProtect)
{
//
call embedded create function based on type
if
(value.
type
() == json::StringType)
{
return
create
(value.
get_str
(), pProtect);
}
else
if
(value.
type
() == json::IntegerType)
{
return
create
(value.
get_int
(), pProtect);
}
else
if
(value.
type
() == json::RealType)
{
return
create
(value.
get_real
(), pProtect);
}
else
if
(value.
type
() == json::BooleanType)
{
return
create
(value.
get_bool
(), pProtect);
}
else
if
(value.
type
() == json::ArrayType)
{
return
create
(value.
get_array
(), pProtect);
}
else
if
(value.
type
() == json::ObjectType)
{
return
create
(value.
get_obj
(), pProtect);
}
else
if
(value.
is_null
())
{
return
R_NilValue;
}
else
{
return
R_NilValue;
}
}
SEXP
create
(
const
char
* value, Protect* pProtect)
{
return
create
(
std::string
(value), pProtect);
}
SEXP
create
(
const
std::string& value, Protect* pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
STRSXP
,
1
));
SET_STRING_ELT
(valueSEXP,
0
,
Rf_mkChar
(value.
c_str
()));
return
valueSEXP;
}
SEXP
create
(
int
value, Protect* pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
INTSXP
,
1
));
INTEGER
(valueSEXP)[
0
] = value ;
return
valueSEXP;
}
SEXP
create
(
double
value, Protect* pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
REALSXP
,
1
));
REAL
(valueSEXP)[
0
] = value ;
return
valueSEXP;
}
SEXP
create
(
bool
value, Protect* pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
LGLSXP
,
1
));
LOGICAL
(valueSEXP)[
0
] = value ;
return
valueSEXP;
}
SEXP
create
(
const
json::Array& value, Protect* pProtect)
{
//
create the list
SEXP
listSEXP;
pProtect->
add
(listSEXP =
Rf_allocVector
(
VECSXP
, value.
size
()));
//
add each array element to it
for
(json::Array::size_type i=
0
; i<value.
size
(); i++)
{
SEXP
valueSEXP =
create
(value[i], pProtect);
SET_VECTOR_ELT
(listSEXP, i, valueSEXP);
}
return
listSEXP;
}
SEXP
create
(
const
json::Object& value, Protect* pProtect)
{
//
create the list
SEXP
listSEXP ;
pProtect->
add
(listSEXP =
Rf_allocVector
(
VECSXP
, value.
size
()));
//
build list of names
SEXP
namesSEXP ;
pProtect->
add
(namesSEXP =
Rf_allocVector
(
STRSXP
, value.
size
()));
//
add each object field to it
int
index =
0
;
for
(json::Object::const_iterator
it = value.
begin
();
it != value.
end
();
++it)
{
//
set name
SET_STRING_ELT
(namesSEXP, index,
Rf_mkChar
(it->
first
.
c_str
()));
//
set value
SEXP
valueSEXP =
create
(it->
second
, pProtect);
SET_VECTOR_ELT
(listSEXP, index, valueSEXP);
//
increment element index
index++;
}
//
attach names
Rf_setAttrib
(listSEXP, R_NamesSymbol, namesSEXP);
//
return the list
return
listSEXP;
}
SEXP
create
(
const
std::vector<std::string>& value, Protect* pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
STRSXP
, value.
size
()));
int
index =
0
;
for
(std::vector<std::string>::const_iterator
it = value.
begin
(); it != value.
end
(); ++it)
{
SET_STRING_ELT
(valueSEXP, index++,
Rf_mkChar
(it->
c_str
()));
}
return
valueSEXP;
}
SEXP
create
(
const
std::vector<
int
>& value, Protect *pProtect)
{
SEXP
valueSEXP;
pProtect->
add
(valueSEXP =
Rf_allocVector
(
INTSXP
, value.
size
()));
for
(std::
size_t
i =
0
; i < value.
size
(); ++i)
INTEGER
(valueSEXP)[i] = value[i] ;
return
valueSEXP;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL