-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebRFunction.cpp
More file actions
429 lines (368 loc) · 13.4 KB
/
WebRFunction.cpp
File metadata and controls
429 lines (368 loc) · 13.4 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
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
#include "WebR.h"
RFunction::RFunction (NPP instance)
{
//this->m_getVersion_id = NPN_GetStringIdentifier("getVersion");
// this->m_getVersion_id = myNPNFuncs->getstringidentifier("getVersion");
// fprintf(stderr, "\nCreating RFunction object. Instance:%lx", (unsigned long int) instance);fflush(stderr);
this->instance = instance;
this->object = NULL;
this->converter = NULL;
fprintf(stderr, "\nLeaving RFunction()\n");fflush(stderr);
}
void RFunction::Deallocate()
{
if(this->object)
{
R_ReleaseObject(this->object);
this->object=NULL;
}
}
void RFunction::Invalidate()
{
}
bool RFunction::HasMethod(NPIdentifier name)
{
int ret = 0;
if(name == this->funcs->getstringidentifier("toString"))
ret = 1;
if(name == this->funcs->getstringidentifier("valueOf"))
ret = 1;
if(name == this->funcs->getstringidentifier("handleEvent"))
ret = 1;
if(name == this->funcs->getstringidentifier("call"))
ret = 1;
return (bool) ret;
}
bool RFunction::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
fprintf(stderr, "\nLooking for method: %s on RFunction object.", this->funcs->utf8fromidentifier(name));fflush(stderr);
if (name == this->funcs->getstringidentifier("toString"))
{
fprintf(stderr, "\nIn tostring method of an RFunction\n");fflush(stderr);
//From NPN_ReleaseVariantValue docs: NPN_ReleaseVariantValue() will call NPN_ReleaseObject() on NPVariants of type NPVARIANTTYPE_OBJECT, and NPN_FreeMem() on NPVariants of type NPVARIANTTYPE_STRING.
NPUTF8 *strdat = (NPUTF8*) this->funcs->memalloc(21+1);
//strdat = (NPUTF8*)"[Internal R Object]";
memcpy(strdat, "[Internal R Function]", 21+1);
NPString str ={ strdat, 21};
result->type = NPVariantType_String;
result->value.stringValue = str;
return true;
}
if (name == this->funcs->getstringidentifier("valueOf"))
{
fprintf(stderr, "\nIn valueOf method of an RFunction\n");fflush(stderr);
NPUTF8 *strdat = (NPUTF8*) this->funcs->memalloc(21+1);
//strdat = (NPUTF8*)"[Internal R Object]";
memcpy(strdat, "[Internal R Function]", 21+1);
NPString str ={ strdat, 21};
result->type = NPVariantType_String;
result->value.stringValue = str;
return true;
}
if (name == this->funcs->getstringidentifier("handleEvent"))
{
fprintf(stderr, "\nIn handleEvent method of an RFunction\n");fflush(stderr);
return this->InvokeDefault(args, argCount, result);
}
if (name == this->funcs->getstringidentifier("call"))
{
fprintf(stderr, "\nIn call method of an RFunction\n");fflush(stderr);
return this->InvokeDefault(args, argCount, result);
}
return false;
}
bool RFunction::InvokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result)
{
if(argCount && args[0].type == NPVariantType_Object && this->funcs->hasproperty(this->instance, args[0].value.objectValue, this->funcs->getstringidentifier("namedArgsForR")))
{
return doNamedCall(this->instance, this->object, args, argCount, result, this->funcs);
}
SEXP Rargs[argCount];
convert_t convRet = CONV_DEFAULT;
NPVariant convRetVariant;
int numprot = 0;
bool canfree;
uint32_t j=0;
bool wasConvRet;
bool retained[argCount];
//argCountR is the number of arguments to be actually passed to the R function. Does not include, e.g. convertRet specification args
int argCountR = argCount;
//i is position in JS args, j is position in converted R args
for(uint32_t i=0; i< argCount; i++)
{
wasConvRet = false;
//If the argument is not an "emptyArg" object, indicating, eg, foo(a, , c), convert as normal
if(args[i].type == NPVariantType_Object && this->funcs->hasproperty(this->instance, args[i].value.objectValue, this->funcs->getstringidentifier("_convertRet")))
{
wasConvRet = true;
funcs->getproperty(this->instance, args[i].value.objectValue, this->funcs->getstringidentifier("behavior"), &convRetVariant);
if(convRetVariant.type==NPVariantType_Int32)
convRet = (convert_t) convRetVariant.value.intValue;
else if ( convRetVariant.type==NPVariantType_Double)
convRet = (convert_t) convRetVariant.value.doubleValue;
else
convRet = CONV_CUSTOM;
} else if(args[i].type == NPVariantType_Object && this->funcs->hasproperty(this->instance, args[i].value.objectValue, this->funcs->getstringidentifier("emptyRArg")))
{
//If it is the special emptyRArg object, it represents a missing argument, eg rnorm(2, , 5)
PROTECT(Rargs[j] = R_NilValue);
Rargs[j] = R_MissingArg;
numprot++;
}
else
{
PROTECT(Rargs[j] = R_NilValue);
//We need to retain this because we are calling R which can initiate another conversion before we return and I think some things (eg events in raphZoom) are getting improperly freed during the inner conversion cycle
if(NPVARIANT_IS_OBJECT(args[i]))
{
this->funcs->retainobject(args[i].value.objectValue);
retained[ i ] = true;
} else {
retained[ i ] = false;
}
ConvertNPToR((NPVariant *) &(args[i]), this->instance, this->funcs, CONV_DEFAULT, &Rargs[j]);
numprot++;
}
//If the argument was a convertRet specification it doesn't count as a "real argument", we decrease the argument count and do not increment i
if(wasConvRet)
argCountR--;
else
j++;
}
SEXP ans;
SEXP call;
int error = 0;
int addProt = 0;
SEXP ptr;
//argCountR is the number of arguments after we remove any convertRet specifiers
PROTECT(ptr = call = allocVector(LANGSXP, argCountR + 1));
SETCAR(ptr, (SEXP) this->object );
for(uint32_t i=0; i < argCountR; i++)
{
ptr = CDR( ptr );
SETCAR(ptr, Rargs[i]);
}
Rf_PrintValue(call);
PROTECT(ans = R_tryEval( call, R_GlobalEnv, &error));
//PROTECT(ans = rQueue.requestRCall( call, R_GlobalEnv, &error, this->instance));
addProt = 2;
if(!error)
{
//ConvertRToNP(ans, this->instance, this->funcs, result, CONV_DEFAULT);
ConvertRToNP(ans, this->instance, this->funcs, result, convRet);
if(convRet == CONV_CUSTOM)
this->funcs->invokeDefault(this->instance, convRetVariant.value.objectValue, result, 1, result);
}
//If it's an error, just throw an error for the browser.
else
{
// ThrowRError(this, this->funcs);
ConvertRToNP(R_NilValue, this->instance, this->funcs, result, CONV_DEFAULT);
}
UNPROTECT(numprot + addProt);
//There is a bug in chrome where if an NPObject method call returns false NPN_SetException doesn't work. I'm going to experiment with always returning true...
//return !error;
//Unretain objects now that we are done calling R
for(int k = 0; k < argCount; k++)
{
if(retained[k])
this->funcs->releasevariantvalue((NPVariant *) &args[k]);
}
return true;
}
/*
bool RFunction::InvokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result)
{
if(argCount && args[0].type == NPVariantType_Object && this->funcs->hasproperty(this->instance, args[0].value.objectValue, this->funcs->getstringidentifier("namedArrayForR")))
{
return doNamedCall(this->instance, this->object, args, argCount, result, this->funcs);
}
SEXP Rargs[argCount];
for(uint32_t i=0; i<argCount; i++)
{
PROTECT(Rargs[i] = R_NilValue);
//when calling R functions directly we DO want arguments to be converted.
ConvertNPToR((NPVariant *) &(args[i]), this->instance, this->funcs, CONV_DEFAULT, &Rargs[i]);
}
SEXP ans;
SEXP call;
int error = 0;
int addProt = 0;
SEXP ptr;
PROTECT(ptr = call = allocVector(LANGSXP, argCount + 1));
SETCAR(ptr, (SEXP) this->object );
for(uint32_t i=0; i < argCount; i++)
{
ptr = CDR( ptr );
SETCAR(ptr, Rargs[i]);
}
fprintf(stderr, "\nDirect API call: ");fflush(stderr);
Rf_PrintValue(call);
//PROTECT(ans = R_tryEval( call, R_GlobalEnv, &error));
PROTECT(ans = R_tryEval( call, R_GlobalEnv, &error));
addProt = 2;
if(!error)
ConvertRToNP(ans, this->instance, this->funcs, result, CONV_DEFAULT);
//If it's an error, just throw an error for the browser.
//else
// ConvertRToNP(R_NilValue, this->instance, this->funcs, result, false);
UNPROTECT(argCount + addProt);
return !error;
}
*/
bool RFunction::HasProperty(NPIdentifier name)
{
fprintf(stderr, "\nLooking for property: %s on RFunction object.", this->funcs->utf8fromidentifier(name));fflush(stderr);
bool ret = false;
if (name == this->funcs->getstringidentifier("handleEvent"))
ret = true;
else if(name == this->funcs->getstringidentifier("isRObject"))
ret = true;
return ret;
}
bool RFunction::GetProperty(NPIdentifier name, NPVariant *result)
{
if (name == this->funcs->getstringidentifier("handleEvent"))
return this->InvokeDefault(NULL, 0, result);
else if(name == this->funcs->getstringidentifier("isRObject"))
{
BOOLEAN_TO_NPVARIANT(true, *result);
return true;
}
return false;
}
bool RFunction::SetProperty(NPIdentifier name, const NPVariant *value)
{
//Emulate object[[name]]<-value
return false;
}
bool RFunction::RemoveProperty(NPIdentifier name)
{
return false;
}
bool RFunction::Enumerate(NPIdentifier **identifier, uint32_t *count)
{
fprintf(stderr, "\nIn RFunction::Enumerate");fflush(stderr);
return false;
}
bool RFunction::Construct(const NPVariant *args, uint32_t argCount, NPVariant *result)
{
return true;
}
NPObject *RFunction::Allocate(NPP npp, NPClass *aClass)
{
fprintf(stderr, "\nIn RFunction::Allocate\n");fflush(stderr);
NPObject *pObj = (NPObject *)new RFunction(npp);
return pObj;
fprintf(stderr, "\nLeaving RFunction::Allocate\n");fflush(stderr);
}
void RFunction::Detatch (void)
{
m_Instance = NULL;
}
void RFunction::_Deallocate(NPObject *npobj)
{
RFunction *pObj = ((RFunction *) npobj);
// Call the virtual destructor.
pObj->Deallocate ();
delete pObj;
}
void RFunction::_Invalidate(NPObject *npobj)
{
((RFunction*)npobj)->Invalidate();
}
bool RFunction::_HasMethod(NPObject *npobj, NPIdentifier name)
{
return ((RFunction*)npobj)->HasMethod (name);
}
bool RFunction::_Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
return ((RFunction*)npobj)->Invoke (name, args, argCount, result);
}
bool RFunction::_InvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
return ((RFunction*)npobj)->InvokeDefault (args, argCount, result);
}
bool RFunction::_HasProperty(NPObject * npobj, NPIdentifier name)
{
return ((RFunction*)npobj)->HasProperty (name);
}
bool RFunction::_GetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
return ((RFunction*)npobj)->GetProperty (name, result);
}
bool RFunction::_SetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
{
return ((RFunction*)npobj)->SetProperty (name, value);
}
bool RFunction::_RemoveProperty(NPObject *npobj, NPIdentifier name)
{
return ((RFunction*)npobj)->RemoveProperty (name);
}
bool RFunction::_Enumerate(NPObject *npobj, NPIdentifier **identifier, uint32_t *count)
{
return ((RFunction*)npobj)->Enumerate (identifier, count);
}
bool RFunction::_Construct(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
return ((RFunction*)npobj)->Construct (args, argCount, result);
}
NPClass RFunction::_npclass = {
NP_CLASS_STRUCT_VERSION,
RFunction::Allocate,
RFunction::_Deallocate,
RFunction::_Invalidate,
RFunction::_HasMethod,
RFunction::_Invoke,
RFunction::_InvokeDefault,
RFunction::_HasProperty,
RFunction::_GetProperty,
RFunction::_SetProperty,
RFunction::_RemoveProperty,
RFunction::_Enumerate,
RFunction::_Construct
};
bool doNamedCall(NPP inst, SEXP fun, const NPVariant *argsIn, uint32_t count, NPVariant *_res, NPNetscapeFuncs *funcs)
{
fprintf(stderr, "\nAttempting to create R call with named arguments\n");fflush(stderr);
uint32_t idcount = 0;
NPIdentifier *ids;
NPObject *obj = argsIn[0].value.objectValue;
bool success = funcs->enumerate(inst, obj, &ids, &idcount);
SEXP call, ans, ptr, tmp;
NPVariant curprop;
PROTECT(ptr = call = allocVector(LANGSXP, 1 + idcount - 1));
SETCAR(ptr, fun);
PROTECT(tmp = R_NilValue);
for(int i =0; i < idcount; i++)
{
if(ids[i] != funcs->getstringidentifier("namedArrayForR"))
{
fprintf(stderr, "\nAccessing property %s\n", funcs->utf8fromidentifier(ids[i]));fflush(stderr);
ptr = CDR(ptr);
funcs->getproperty(inst, obj, ids[i], &curprop);
ConvertNPToR(&curprop, inst, funcs, CONV_DEFAULT, &tmp);
SETCAR(ptr, tmp);
SET_TAG(ptr, Rf_install((const char *) funcs->utf8fromidentifier(ids[i])));
}
}
/*
if(count > 1)
{
for(int j = 1; j < count; j++)
{
ptr = CDR(ptr);
ConvertNPToR((NPVariant *) &argsIn[j], inst, myNPNFuncs, true, &tmp);
SETCAR(ptr, tmp);
}
}
*/
fprintf(stderr, "\nFull call:\n");fflush(stderr);
Rf_PrintValue(call);
int err = 0;
PROTECT(ans = R_tryEval(call, R_GlobalEnv, &err));
ConvertRToNP(ans, inst, funcs, _res, CONV_DEFAULT);
// funcs->memfree(*ids);
funcs->memfree(ids);
return !err;
}