2009-12-13 14:44:28 -05:00
|
|
|
#ifndef __convert_v8_h__
|
|
|
|
#define __convert_v8_h__
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-12-13 14:44:28 -05:00
|
|
|
#include <v8.h>
|
|
|
|
#include <stdint.h>
|
2009-10-15 23:23:53 -04:00
|
|
|
#include <stdio.h>
|
2009-10-16 10:32:20 -04:00
|
|
|
#include <string>
|
2009-10-15 23:23:53 -04:00
|
|
|
|
|
|
|
template<class T, class R> class V8HandleSource {
|
|
|
|
|
|
|
|
T dest;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
V8HandleSource() {}
|
|
|
|
~V8HandleSource() {}
|
|
|
|
|
2009-12-25 23:47:42 -05:00
|
|
|
bool operator() (v8::Handle<v8::Value>& value, R& result) {
|
|
|
|
|
|
|
|
// a bit klunky, but alternative is to evaluate what type the
|
|
|
|
// object is twice, which is unappealing
|
2009-12-18 02:48:06 -05:00
|
|
|
|
|
|
|
if (value.IsEmpty()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushNull();
|
|
|
|
return true;
|
2009-12-18 02:48:06 -05:00
|
|
|
}
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if (value->IsUndefined()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushNull();
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if(value->IsNull()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushNull();
|
|
|
|
return true;
|
2009-10-16 16:27:39 -04:00
|
|
|
}
|
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
if(value->IsTrue()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushBool(true);
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsFalse()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushBool(false);
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsString()) {
|
2009-10-16 10:32:20 -04:00
|
|
|
v8::Local<v8::String> str = value->ToString();
|
2009-12-25 23:47:42 -05:00
|
|
|
result = convertString(str);
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsInt32()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushInt(value->Int32Value());
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsNumber()) {
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushDouble(value->NumberValue());
|
|
|
|
return true;
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
2009-10-22 22:45:41 -04:00
|
|
|
|
2009-12-25 23:47:42 -05:00
|
|
|
result = dest.pushNull();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
R convertString(v8::Local<v8::String>& str) {
|
|
|
|
char buffer[1024];
|
|
|
|
int strlen = str->Length();
|
|
|
|
std::string output(strlen, 0);
|
|
|
|
for (int total = 0; total < strlen;) {
|
|
|
|
int written = str->WriteAscii(buffer, total, 1024);
|
|
|
|
output.replace(total, written, buffer);
|
|
|
|
total += written;
|
2009-10-22 22:45:41 -04:00
|
|
|
}
|
2009-12-25 23:47:42 -05:00
|
|
|
return dest.pushString(output);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
2009-12-25 23:47:42 -05:00
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
};
|
|
|
|
|
2009-11-13 23:27:48 -05:00
|
|
|
/**
|
|
|
|
* StringDest is a data type conversion destination that converts
|
|
|
|
* any argument into a string. It should have all methods listed
|
|
|
|
* in data_conversion.txt so it can be used as a template argument
|
|
|
|
* for a conversion source class.
|
|
|
|
*/
|
2009-12-13 14:44:28 -05:00
|
|
|
class V8LocalDest {
|
2009-11-13 23:27:48 -05:00
|
|
|
|
|
|
|
public:
|
2009-12-13 14:44:28 -05:00
|
|
|
V8LocalDest();
|
|
|
|
~V8LocalDest();
|
2009-11-13 23:27:48 -05:00
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushString(const std::string& value) {
|
2009-11-15 11:43:16 -05:00
|
|
|
return v8::Local<v8::Value>::New(v8::String::New(value.c_str()));
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushInt(int64_t value) {
|
2009-11-15 11:43:16 -05:00
|
|
|
return v8::Local<v8::Value>::New(v8::Integer::New(value));
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushDouble(double value) {
|
2009-11-15 11:43:16 -05:00
|
|
|
return v8::Local<v8::Value>::New(v8::Number::New(value));
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushBool(bool value) {
|
2009-11-15 11:43:16 -05:00
|
|
|
return v8::Local<v8::Value>::New(v8::Boolean::New(value));
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushNull() {
|
|
|
|
return v8::Local<v8::Value>::New(v8::Null());
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
v8::Local<v8::Value> pushUndefined() {
|
|
|
|
return v8::Local<v8::Value>::New(v8::Undefined());
|
2009-11-13 23:27:48 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
#endif
|