2009-10-15 23:23:53 -04:00
|
|
|
#ifndef __v8_data_h__
|
|
|
|
#define __v8_data_h__
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "stdint.h"
|
|
|
|
#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-13 11:07:34 -05:00
|
|
|
R push(v8::Handle<v8::Value>& value) {
|
2009-10-15 23:23:53 -04:00
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if (value->IsUndefined()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushNull();
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if(value->IsNull()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushNull();
|
2009-10-16 16:27:39 -04:00
|
|
|
}
|
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
if(value->IsTrue()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushBool(true);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsFalse()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushBool(false);
|
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();
|
|
|
|
char buffer[1024];
|
|
|
|
int strlen = str->Length();
|
2009-10-16 10:55:18 -04:00
|
|
|
std::string output(strlen, 0);
|
2009-10-16 10:32:20 -04:00
|
|
|
for (int total = 0; total < strlen;) {
|
2009-10-16 10:55:18 -04:00
|
|
|
int written = str->WriteAscii(buffer, total, 1024);
|
|
|
|
output.replace(total, written, buffer);
|
2009-10-16 10:32:20 -04:00
|
|
|
total += written;
|
|
|
|
}
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushString(output);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsInt32()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushInt(value->Int32Value());
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsNumber()) {
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushDouble(value->NumberValue());
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
2009-10-22 22:45:41 -04:00
|
|
|
|
|
|
|
if (value->IsObject()) {
|
2009-10-23 00:27:41 -04:00
|
|
|
v8::Local<v8::Object> object(v8::Object::Cast(*value));
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushObject(object);
|
2009-10-22 22:45:41 -04:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:34 -05:00
|
|
|
return dest.pushNull();
|
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.
|
|
|
|
*/
|
|
|
|
class V8HandleDest {
|
|
|
|
|
|
|
|
public:
|
|
|
|
V8HandleDest();
|
|
|
|
~V8HandleDest();
|
|
|
|
|
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-11-15 11:43:16 -05:00
|
|
|
|
|
|
|
// v8:Local<v8:Value> pushFunction(Function) {
|
|
|
|
//
|
|
|
|
// }
|
2009-11-13 23:27:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
#endif
|