24
24
#include <drizzled/function/str/strfunc.h>
27
#define JS_ENGINE "v8"
28
29
using namespace std;
29
30
using namespace drizzled;
33
// TODO: Can I just declare functions like this? Do I need to use a namespace? Naming convention?
34
v8::Handle<v8::Value> V8Version(const v8::Arguments& args);
35
v8::Handle<v8::Value> JsEngine(const v8::Arguments& args);
32
36
const char* ToCString(const v8::String::Utf8Value& value);
33
37
void ReportException(v8::TryCatch* try_catch);
34
v8::Handle<v8::Value> V8Version(const v8::Arguments& args);
37
40
// TODO: So this is a function that returns strings?
122
125
* Implements js_eval(), evaluate JavaScript code
124
* @note: I only compiled this with -O0 but should work with default O2 also.
127
* @note I only compiled this with -O0 but should work with default O2 also.
126
* @todo: Accepts more than one parameter, but doesn't use them.
127
* @todo: How does it work if I want to return integers and doubles?
128
* @todo: v8 exceptions are printed to the log. Should be converted to
129
* @todo Accepts more than one parameter, but doesn't use them.
130
* @todo How does it work if I want to return integers and doubles?
131
* @todo v8 exceptions are printed to the log. Should be converted to
129
132
* drizzle warning/error and returned with result.
130
* @todo: Probably the v8 stuff will be moved to it's own function in the future.
131
* @todo: Documentation for drizzle manual in .rst format
133
* @todo Probably the v8 stuff will be moved to it's own function in the future.
134
* @todo Documentation for drizzle manual in .rst format
135
* @todo When available, use v8::Isolate instead of v8::Locker for multithreading
133
137
* @param res Pointer to the drizzled::String object that will contain the result
134
138
* @return a drizzled::String containing the value returned by executed JavaScript code (value of last executed statement)
143
147
String *source_str=NULL;
144
148
source_str = args[0]->val_str(source_str);
150
// TODO Some of this (FunctionTemplate and other bindings) should probably be
151
// moved to initialize, but then it must be allocated on the heap.
153
// Need to use Locker in multi-threaded app. v8 is unlocked by the destructor
154
// when locker goes out of scope.
155
// TODO: Newer versions of v8 provide an Isolate class where you can get a
156
// separate instance of v8 (ie one per thread). v8 2.5.9.9 in Ubuntu 11.04 does
146
159
// Pass code and arguments into v8...
147
// TODO Some of this should probably be moved to initialize, but then
148
// it must be allocated on the heap.
149
160
v8::HandleScope handle_scope;
150
// Create a template for the global object.
151
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
152
// Bind the 'version' function (TODO: just an example but this is how we bind arguments too...)
153
global->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Version));
161
// Create a template for the global object and populate a drizzle object.
162
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
163
// Drizzle will contain API's to drizzle variables, functions and tables
164
v8::Handle<v8::ObjectTemplate> drizzle = v8::ObjectTemplate::New();
165
v8::Handle<v8::ObjectTemplate> js = v8::ObjectTemplate::New();
166
// Bind the 'version' function
167
global->Set(v8::String::New("drizzle"), drizzle);
168
drizzle->Set(v8::String::New("js"), js);
169
js->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Version));
170
js->Set(v8::String::New("engine"), v8::FunctionTemplate::New(JsEngine));
171
// TODO: Now bind the arguments into argv[]
155
173
// Create a v8 string containing the JavaScript source code.
156
// Convert from drizzled::String to char[] string to v8::String.
174
// Convert from drizzled::String to char* string to v8::String.
157
175
v8::Handle<v8::String> source = v8::String::New(source_str->c_str());
165
183
v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
184
//v8::Handle<v8::Context> context = v8::Context::New(NULL, global);
166
185
if (context.IsEmpty()) {
167
186
// TODO: how do I set warning/error in the drizzle result?
168
187
printf("Error in js_eval() while creating JavaScript context in v8.\n");
187
206
// TODO: how do I set warning/error in the drizzle result?
188
207
// Print errors that happened during execution.
189
208
ReportException(&try_catch);
209
// Dispose of Persistent objects before returning. (Is it needed?)
192
214
assert(!try_catch.HasCaught());
193
215
if (result->IsUndefined()) {
194
216
printf("js_eval() got Undefined result back from v8.\n");
217
// Dispose of Persistent objects before returning. (Is it needed?)