1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

Allow export of V8::C::String to utf8

This commit is contained in:
Charles Lowell 2012-05-03 11:01:10 -07:00
parent 663d4f35ea
commit ba7f5dae8a
3 changed files with 21 additions and 6 deletions

View file

@ -60,6 +60,7 @@ public:
return Ref<T>(new Holder(handle, klass));
}
inline v8::Handle<T> operator->() const { return holder->handle; }
v8::Handle<T> GetHandle() {return holder->handle;}
class Holder {
friend class Ref;
@ -112,8 +113,10 @@ public:
class String: public Ref<v8::String> {
public:
inline String(VALUE value) : Ref<v8::String>(value) {}
virtual operator v8::Handle<v8::Value>();
static VALUE ToRuby(v8::Handle<v8::String> value);
static void Init();
static VALUE Class;
};
class V8 {

View file

@ -1,21 +1,32 @@
#include "rr.h"
namespace rr {
VALUE StringClass;
namespace {
VALUE New(VALUE StringClass, VALUE string) {
v8::HandleScope h;
return String::ToRuby(v8::String::New(RSTRING_PTR(string), (int)RSTRING_LEN(string)));
}
VALUE Utf8Value(VALUE self) {
v8::HandleScope h;
String str(self);
return rb_str_new(*v8::String::Utf8Value(str), str->Utf8Length());
}
}
VALUE String::ToRuby(v8::Handle<v8::String> string) {
return String::create(string, StringClass);
return String::create(string, String::Class);
}
String::operator v8::Handle<v8::Value>() {
return this->GetHandle();
}
VALUE String::Class;
void String::Init() {
rb_gc_register_address(&StringClass);
StringClass = defineClass("String");
RR_DEFINE_SINGLETON_METHOD(StringClass, "New", &New, 1);
rb_gc_register_address(&Class);
Class = defineClass("String");
RR_DEFINE_SINGLETON_METHOD(Class, "New", &New, 1);
RR_DEFINE_METHOD(Class, "Utf8Value", &Utf8Value, 0);
}
}

View file

@ -10,7 +10,8 @@ module V8
source = V8::C::String::New(source.to_s)
filename = V8::C::String::New(filename.to_s)
script = V8::C::Script::New(source, filename)
script.Run()
result = script.Run()
result.kind_of?(V8::C::String) ? result.Utf8Value() : result
ensure
@native.Exit()
end