-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNPAPI.cpp
More file actions
285 lines (223 loc) · 8.58 KB
/
NPAPI.cpp
File metadata and controls
285 lines (223 loc) · 8.58 KB
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
/*
#include "npruntime.h"
#include "npfunctions.h"
#include "npapi.h"
#include "Rdefines.h"
*/
#include "WebR.h"
bool C_doTest(NPP inst, NPNetscapeFuncs *funcs);
extern "C" {
SEXP R_doTest(SEXP plug);
//success = funcs->invoke(inst, domwin, arrid, NULL, 0, vartmp2);
SEXP R_NPAPI_Invoke(SEXP plug, SEXP Robj, SEXP Rname, SEXP Rargs, SEXP RconvArgsEnum, SEXP RconvArgsFuns, SEXP RconvRet, SEXP RkeepRes );
SEXP R_doRefClassTest();
SEXP R_NP_GetGlobal(SEXP Rplug);
SEXP R_NPAPI_GetProperty(SEXP plug, SEXP Robj, SEXP Rname, SEXP RconvRet);
SEXP R_NPAPI_SetProperty(SEXP plug, SEXP Robj, SEXP Rname, SEXP Rval, SEXP RconvValue);
SEXP R_ConvertRToNP(SEXP Robj, SEXP plug);
SEXP R_ConvertNPToR(SEXP NPObj, SEXP plug);
}
SEXP R_doRefClasWsTest()
{
SEXP ans, klass;
PROTECT(klass = MAKE_CLASS("MyTestRef"));
PROTECT(ans = NEW_OBJECT(klass));
Rf_PrintValue(ans);
UNPROTECT(2);
return ans;
}
SEXP R_NPAPI_Invoke(SEXP plug, SEXP Robj, SEXP Rname, SEXP Rargs, SEXP RconvArgsEnum, SEXP RconvArgsFuns, SEXP RconvRet, SEXP RkeepRes )
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref")));
NPVariant *obj = (NPVariant *) R_ExternalPtrAddr(GET_SLOT( Robj , Rf_install( "ref" ) ) );
if(!NPVARIANT_IS_OBJECT(*obj))
{
//What should we return in this case?
Rf_error("Robj is not an NPVariant containing an NPObject.");
return R_NilValue;
}
//custom conversion functions are applied on R side for return value.
convert_t convRet = (convert_t) INTEGER(RconvRet)[0];
convert_t curConvArg;
int nargs = LENGTH(Rargs);
NPVariant *args = (NPVariant *) funcs->memalloc(nargs*sizeof(NPVariant));
for(int i = 0; i < nargs; i++)
{
curConvArg = (convert_t) INTEGER(RconvArgsEnum)[i];
ConvertRToNP(VECTOR_ELT(Rargs, i), inst, funcs, &(args[i]), curConvArg);
//If we have a custom converter we invoke it with here
if(curConvArg == CONV_CUSTOM)
{
fprintf(stderr, "Custom argument converter detected. Attempting to call JS Conversion function.");fflush(stderr);
funcs->invokeDefault(inst, ((NPVariant *) R_ExternalPtrAddr(GET_SLOT( VECTOR_ELT(RconvArgsFuns, i), Rf_install( "ref" ) ) ) ) -> value.objectValue, &args[i], 1, &args[i]) ;
}
}
NPVariant *ret = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
const char *ccname = CHAR(STRING_ELT(Rname, 0));
bool hasMethod = funcs->hasmethod(inst, obj->value.objectValue, funcs->getstringidentifier(ccname));
if(!hasMethod)
{
char msg[200];
sprintf(msg, "Object has no %s method.", ccname);
Rf_error(msg);
return R_NilValue;
}
bool success = funcs->invoke(inst, obj->value.objectValue, funcs->getstringidentifier(ccname), args, nargs, ret);
if(!success)
{
fprintf(stderr, "\nInvocation of JS method %s failed.", ccname);fflush(stderr);
}
for(int j=0; j<nargs; j++)
{
if (NPVARIANT_IS_OBJECT(args[j]))
funcs->releaseobject(args[j].value.objectValue);
//funcs->releasevariantvalue(&args[j]);
}
funcs->memfree(args);
if(!success)
{
char msg2[200];
sprintf(msg2, "Invoke failed for %s method.", ccname);
Rf_error(msg2);
return R_NilValue;
}
SEXP ans;
//PROTECT(ans = R_NilValue);
PROTECT(ans = NEW_INTEGER(1));
bool canfree = ConvertNPToR(ret, inst, funcs, convRet, &ans);
bool keepRes = LOGICAL(RkeepRes)[0];
if(canfree || !keepRes)
funcs->releasevariantvalue(ret);
UNPROTECT(1);
if(keepRes)
return ans ;
else
return R_NilValue;
}
SEXP R_NPAPI_GetProperty(SEXP plug, SEXP Robj, SEXP Rname, SEXP RconvRet)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref")));
NPVariant *obj = (NPVariant *) R_ExternalPtrAddr(GET_SLOT( Robj , Rf_install( "ref" ) ) );
if(!NPVARIANT_IS_OBJECT(*obj))
{
//What should we return in this case?
Rf_error("Robj is not an NPVariant containing an NPObject.");
return R_NilValue;
}
convert_t convRet = (convert_t) INTEGER(RconvRet)[0];
NPVariant *ret = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
const char *ccname = CHAR(STRING_ELT(Rname, 0));
bool success = funcs->getproperty(inst, obj->value.objectValue, funcs->getstringidentifier(ccname), ret);
if(!success)
{
Rf_error("Invoke failed.");
return R_NilValue;
}
SEXP ans;
PROTECT(ans = R_NilValue);
//ConvertNPToR returns a bool which indicates whether it is safe to release the converted object.
bool canfree = ConvertNPToR(ret, inst, funcs, convRet, &ans);
if(canfree)
funcs->releasevariantvalue(ret);
UNPROTECT(1);
return ans ;
}
SEXP R_NPAPI_SetProperty(SEXP plug, SEXP Robj, SEXP Rname, SEXP Rval, SEXP RconvValue)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref")));
NPVariant *obj = (NPVariant *) R_ExternalPtrAddr(GET_SLOT( Robj , Rf_install( "ref" ) ) );
if(!NPVARIANT_IS_OBJECT(*obj))
{
//What should we return in this case?
Rf_error("Robj is not an NPVariant containing an NPObject.");
return R_NilValue;
}
convert_t convVal = (convert_t) INTEGER(RconvValue)[0];
//NPVariant *val = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
NPVariant val;
ConvertRToNP(Rval, inst, funcs, &val, convVal);
const char *ccname = CHAR(STRING_ELT(Rname, 0));
bool success = funcs->setproperty(inst, obj->value.objectValue, funcs->getstringidentifier(ccname), &val);
if(!success)
{
//funcs->memfree(val);
Rf_error("SetProperty failed.");
return R_NilValue;
}
else {
funcs->releasevariantvalue(&val);
//funcs->memfree(val);
}
return ScalarLogical(success) ;
}
SEXP R_doTest(SEXP plug)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref")));
bool success = C_doTest(inst, funcs);
return ScalarLogical(success);
}
bool C_doTest(NPP inst, NPNetscapeFuncs *funcs)
{
NPObject *domwin = NULL;
NPVariant *vartmp2 = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
//NPVariant vartmp2;
NPError res;
bool success;
res = funcs->getvalue(inst, NPNVWindowNPObject , &domwin);
funcs->retainobject(domwin);
NPIdentifier arrid = funcs->getstringidentifier("Array");
success = funcs->invoke(inst, domwin, arrid, NULL, 0, vartmp2);
fprintf(stderr, "\nIn doTest. success: %d", success); fflush(stderr);
funcs->releaseobject(domwin);
/*
myNPNFuncs->memfree(vartmp3);
myNPNFuncs->memfree(vartmp2);
*/
funcs->releasevariantvalue(vartmp2);
return success;
}
SEXP R_NP_GetGlobal(SEXP Rplug)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( Rplug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(Rplug, Rf_install("funcs")), Rf_install("ref")));
NPObject *domwin = NULL;
NPVariant *toret = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
//NPVariant vartmp2;
NPError res;
bool success;
res = funcs->getvalue(inst, NPNVWindowNPObject , &domwin);
funcs->retainobject(domwin);
OBJECT_TO_NPVARIANT(domwin, *toret);
SEXP ans;
PROTECT(ans = R_NilValue);
ConvertNPToR(toret, inst, funcs, CONV_REF, &ans);
UNPROTECT(1);
return ans;
}
SEXP R_ConvertRToNP(SEXP Robj, SEXP plug)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref") ) ) ;
NPVariant *var = (NPVariant *) funcs->memalloc(sizeof(NPVariant));
SEXP ans;
PROTECT(ans = R_NilValue);
ConvertRToNP(Robj, inst, funcs, var, CONV_COPY);
ConvertNPToR(var, inst, funcs, CONV_REF, &ans);
UNPROTECT(1);
return ans;
}
SEXP R_ConvertNPToR(SEXP Robj, SEXP plug)
{
NPP inst = (NPP) R_ExternalPtrAddr(GET_SLOT( plug , Rf_install( "ref" ) ) );
NPNetscapeFuncs *funcs = (NPNetscapeFuncs *) R_ExternalPtrAddr(GET_SLOT( GET_SLOT(plug, Rf_install("funcs")), Rf_install("ref")));
NPVariant *obj = (NPVariant *) R_ExternalPtrAddr(GET_SLOT( Robj , Rf_install( "ref" ) ) );
SEXP ans;
PROTECT(ans = R_NilValue);
ConvertNPToR(obj, inst, funcs, CONV_COPY, &ans);
UNPROTECT(1);
return ans;
}