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() {}
|
|
|
|
|
|
|
|
R push(v8::Handle<v8::Value>& value, const char* name = 0) {
|
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if (value->IsUndefined()) {
|
2009-10-15 23:23:53 -04:00
|
|
|
return dest.pushNull(name);
|
|
|
|
}
|
|
|
|
|
2009-10-16 16:27:39 -04:00
|
|
|
if(value->IsNull()) {
|
|
|
|
return dest.pushNull(name);
|
|
|
|
}
|
|
|
|
|
2009-10-15 23:23:53 -04:00
|
|
|
if(value->IsTrue()) {
|
|
|
|
return dest.pushBool(true, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsFalse()) {
|
|
|
|
return dest.pushBool(false, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return dest.pushString(output, name);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsInt32()) {
|
|
|
|
return dest.pushInt(value->Int32Value(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value->IsNumber()) {
|
|
|
|
return dest.pushDouble(value->NumberValue(), name);
|
|
|
|
}
|
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));
|
|
|
|
return dest.pushObject(object, name);
|
2009-10-22 22:45:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return dest.pushNull(name);
|
2009-10-15 23:23:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|