1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/ext/v8/v8_string.cpp
2011-04-13 08:45:07 -05:00

52 lines
1.6 KiB
C++

#include "v8.h"
#include "v8_handle.h"
#include "v8_value.h"
#include "v8_string.h"
using namespace v8;
namespace {
VALUE StringClass;
Persistent<String>& unwrap(VALUE value) {
return rr_v8_handle<String>(value);
}
VALUE New(VALUE string_class, VALUE data) {
HandleScope handles;
VALUE str = rb_funcall(data, rb_intern("to_s"), 0);
return rr_v8_handle_new(string_class, String::New(RSTRING_PTR(str), RSTRING_LEN(str)));
}
VALUE NewSymbol(VALUE string_class, VALUE data) {
HandleScope scope;
VALUE str = rb_funcall(data, rb_intern("to_s"), 0);
return rr_v8_handle_new(string_class, String::NewSymbol(RSTRING_PTR(str), RSTRING_LEN(str)));
}
VALUE Utf8Value(VALUE self) {
HandleScope handles;
return rb_str_new2(*String::Utf8Value(unwrap(self)));
}
VALUE Utf16Value(VALUE self) {
//How are UTF16 strings represented in ruby 1.8, 1.9
return Qnil;
}
VALUE AsciiValue(VALUE self) {
HandleScope handles;
return rb_str_new2(*String::AsciiValue(unwrap(self)));
}
}
VALUE rr_reflect_v8_string(Handle<Value> value) {
HandleScope handles;
Local<String> string = String::Cast(*value);
return rr_v8_handle_new(StringClass, string);
}
void rr_init_string() {
StringClass = rr_define_class("String", rr_v8_value_class());
rr_define_singleton_method(StringClass, "New", New, 1);
rr_define_singleton_method(StringClass, "NewSymbol", NewSymbol, 1);
rr_define_method(StringClass, "Utf8Value", Utf8Value, 0);
rr_define_method(StringClass, "Utf16Value", Utf16Value, 0);
rr_define_method(StringClass, "AsciiValue", AsciiValue, 0);
}