/
V8.cpp
280 lines (236 loc) · 8.24 KB
/
V8.cpp
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
/*
R bindings to V8. Copyright 2014, Jeroen Ooms.
Notes:
- Rcpp completely ignores character encodings, so need to convert manually.
- Implementation of Typed Arrays taken from node v0.6.21
V8 source parsing:
- http://stackoverflow.com/questions/16613828/how-to-convert-stdstring-to-v8s-localstring
Xptr examples:
- https://github.com/RcppCore/Rcpp/blob/master/inst/unitTests/cpp/XPtr.cpp
- http://romainfrancois.blog.free.fr/index.php?post/2010/01/08/External-pointers-with-Rcpp
*/
#include <v8.h>
#include <Rcpp.h>
#include "v8_typed_array.h"
#include "v8_json.h"
using namespace v8;
/* a linked list keeping track of running contexts */
struct node {
Persistent<Context> context;
node *next;
};
node ctxlist = *(new node);
node *lstail = &ctxlist;
void ctx_finalizer( Persistent<Context>* context ){
if(context){
context->Dispose();
}
}
typedef Rcpp::XPtr<Persistent<Context>, Rcpp::PreserveStorage, ctx_finalizer> ctxptr;
/* Helper fun that compiles JavaScript source code */
Handle<Script> compile_source( std::string src ){
Handle<String> source = String::New(src.c_str());
Handle<Script> script = Script::Compile(source);
return script;
}
/* console.log */
static Handle<Value> ConsoleLog(const Arguments& args) {
for (int i=0; i < args.Length(); i++) {
String::AsciiValue str(args[i]->ToString());
Rprintf("%s", *str);
}
Rprintf("\n");
return v8::Undefined();
}
/* console.warn */
static Handle<Value> ConsoleWarn(const Arguments& args) {
for (int i=0; i < args.Length(); i++) {
String::AsciiValue str(args[i]->ToString());
Rf_warningcall_immediate(R_NilValue, *str);
}
return v8::Undefined();
}
/* console.error */
static Handle<Value> ConsoleError(const Arguments& args) {
if(args.Length()){
return v8::ThrowException(args[0]);
}
return v8::Undefined();
}
static Handle<Value> r_callback(std::string fun, const Arguments& args) {
try {
Rcpp::Function r_call = Rcpp::Environment::namespace_env("V8")[fun];
String::Utf8Value arg0(args[0]);
Rcpp::String fun(*arg0);
Rcpp::CharacterVector out;
if(args[1]->IsUndefined()){
out = r_call(fun);
} else if(args[2]->IsUndefined()) {
String::Utf8Value arg1(json_stringify(args[1]));
Rcpp::String json(*arg1);
out = r_call(fun, json);
} else {
String::Utf8Value arg1(json_stringify(args[1]));
String::Utf8Value arg2(json_stringify(args[2]));
Rcpp::String val(*arg1);
Rcpp::String json(*arg2);
out = r_call(fun, val, json);
}
return json_parse(String::New(std::string(out[0]).c_str()));
} catch( const std::exception& e ) {
return v8::ThrowException(String::New(e.what()));
}
}
/* console.r.call() function */
static Handle<Value> console_r_call(const Arguments& args) {
return r_callback("r_call", args);
}
/* console.r.get() function */
static Handle<Value> console_r_get(const Arguments& args) {
return r_callback("r_get", args);
}
/* console.r.eval() function */
static Handle<Value> console_r_eval(const Arguments& args) {
r_callback("r_eval", args);
return v8::Undefined();
}
/* console.r.eval() function */
static Handle<Value> console_r_assign(const Arguments& args) {
r_callback("r_assign", args);
return v8::Undefined();
}
// [[Rcpp::export]]
ctxptr make_context(bool set_console){
/* setup console.log */
HandleScope handle_scope;
Handle<ObjectTemplate> global = ObjectTemplate::New();
if(set_console){
Handle<ObjectTemplate> console = ObjectTemplate::New();
global->Set(String::NewSymbol("console"), console);
console->Set(String::NewSymbol("log"), FunctionTemplate::New(ConsoleLog));
console->Set(String::NewSymbol("warn"), FunctionTemplate::New(ConsoleWarn));
console->Set(String::NewSymbol("error"), FunctionTemplate::New(ConsoleError));
/* emscripted assumes a print function */
global->Set(String::NewSymbol("print"), FunctionTemplate::New(ConsoleLog));
/* R callback interface */
Handle<ObjectTemplate> console_r = ObjectTemplate::New();
console->Set(String::NewSymbol("r"), console_r);
console_r->Set(String::NewSymbol("call"), FunctionTemplate::New(console_r_call));
console_r->Set(String::NewSymbol("get"), FunctionTemplate::New(console_r_get));
console_r->Set(String::NewSymbol("eval"), FunctionTemplate::New(console_r_eval));
console_r->Set(String::NewSymbol("assign"), FunctionTemplate::New(console_r_assign));
}
/* initialize the context */
lstail->context = Context::New(NULL, global);
ctxptr ptr(&(lstail->context));
lstail->next = new node;
lstail = lstail->next;
return(ptr);
}
// [[Rcpp::export]]
bool context_enable_typed_arrays( Rcpp::XPtr< v8::Persistent<v8::Context> > ctx ){
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
v8_typed_array::AttachBindings((*ctx)->Global());
return true;
}
// [[Rcpp::export]]
std::string version(){
return v8::V8::GetVersion();
}
// [[Rcpp::export]]
std::string context_eval(std::string src, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx){
// Test if context still exists
if(!ctx)
throw std::runtime_error("Context has been disposed.");
// Create a scope
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
// Compile source code
TryCatch trycatch;
Handle<Script> script = compile_source(src);
if(script.IsEmpty()) {
Local<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::invalid_argument(*exception_str);
}
// Run the script to get the result.
Handle<Value> result = script->Run();
if(result.IsEmpty()){
Local<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::runtime_error(*exception_str);
}
// Convert result to UTF8.
String::Utf8Value utf8(result);
return *utf8;
}
// [[Rcpp::export]]
bool context_validate(std::string src, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) {
// Test if context still exists
if(!ctx)
throw std::runtime_error("Context has been disposed.");
// Create scope
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
// Try to compile, catch errors
TryCatch trycatch;
Handle<Script> script = compile_source(src);
return !script.IsEmpty();
}
// [[Rcpp::export]]
bool context_null(Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) {
// Test if context still exists
return(!ctx);
}
/*
Rcpp does not deal well with UTF8 on windows.
Workaround below (hopefully temporary)
*/
// [[Rcpp::export]]
SEXP context_eval_safe(SEXP src, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx){
std::string str(Rf_translateCharUTF8(Rf_asChar(src)));
std::string out = context_eval(str, ctx);
SEXP res = PROTECT(Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(res, 0, Rf_mkCharCE(out.c_str(), CE_UTF8));
UNPROTECT(1);
return res;
}
// [[Rcpp::export]]
bool context_validate_safe(SEXP src, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx){
std::string str(Rf_translateCharUTF8(Rf_asChar(src)));
return context_validate(str, ctx);
}
/*
Method below does not work because null bytes get lost when converting to strings.
Should use ArrayBuffer types instead, e.g. Uint8Array.
*/
// [[Rcpp::export]]
bool context_assign_bin(std::string name, Rcpp::RawVector data, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) {
// Test if context still exists
if(!ctx)
throw std::runtime_error("Context has been disposed.");
// Create scope
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
v8::Handle<v8::Object> global = (*ctx)->Global();
// Currently converts raw vectors to strings. Better would be ArrayBuffer (Uint8Array specifically)
Local<v8::String> mystring = v8::String::New((const char*) RAW(data), data.length());
global->Set(String::NewSymbol(name.c_str()), mystring);
return true;
}
// [[Rcpp::export]]
Rcpp::RawVector context_get_bin(std::string name, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) {
// Test if context still exists
if(!ctx)
throw std::runtime_error("Context has been disposed.");
// Create scope
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
v8::Handle<v8::Object> global = (*ctx)->Global();
//find the string
Local<v8::String> mystring = global->Get(String::NewSymbol(name.c_str()))->ToString();
Rcpp::RawVector res(mystring->Length());
mystring->WriteAscii((char*) res.begin());
return res;
}