rr.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "rr.h"
  2. #include "v8_context.h"
  3. #include "v8_handle.h"
  4. #include "v8_value.h"
  5. #include "v8_object.h"
  6. #include "v8_function.h"
  7. #include "v8_array.h"
  8. #include "v8_string.h"
  9. #include "v8_date.h"
  10. #include "v8_message.h"
  11. #include "v8_external.h"
  12. #include "v8_exception.h"
  13. using namespace v8;
  14. VALUE rr_define_class(const char *name, VALUE superclass) {
  15. VALUE V8 = rb_define_module("V8");
  16. VALUE V8_C = rb_define_module_under(V8, "C");
  17. VALUE klass = rb_define_class_under(V8_C, name, superclass);
  18. rb_funcall(klass, rb_intern("private_class_method"), 1, rb_str_new2("new"));
  19. return klass;
  20. }
  21. VALUE rr_define_module(const char *name) {
  22. VALUE V8 = rb_define_module("V8");
  23. VALUE V8_C = rb_define_module_under(V8, "C");
  24. return rb_define_module_under(V8_C, name);
  25. }
  26. VALUE rr_define_const(const char *name, VALUE value) {
  27. VALUE V8 = rb_define_module("V8");
  28. VALUE V8_C = rb_define_module_under(V8, "C");
  29. rb_define_const(V8_C, name, value);
  30. return value;
  31. }
  32. VALUE rr_const_get(const char *name) {
  33. VALUE V8 = rb_define_module("V8");
  34. VALUE V8_C = rb_define_module_under(V8, "C");
  35. return rb_const_get(V8_C, rb_intern(name));
  36. }
  37. VALUE rr_define_finalizer(VALUE object, void* finalizer, VALUE data) {
  38. VALUE finalizer_proc = rb_proc_new((VALUE (*)(...))finalizer, data);
  39. rb_iv_set(finalizer_proc, "data", data);
  40. VALUE ospace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
  41. rb_funcall(ospace, rb_intern("define_finalizer"), 2, object, finalizer_proc);
  42. }
  43. VALUE rr_v82rb(Handle<Value> value) {
  44. if (value.IsEmpty()) {
  45. return rr_v8_value_empty();
  46. }
  47. if (value->IsUndefined() || value->IsNull()) {
  48. return Qnil;
  49. }
  50. if (value->IsExternal()) {
  51. return rr_reflect_v8_external(value);
  52. }
  53. if (value->IsUint32()) {
  54. return UINT2NUM(value->Uint32Value());
  55. }
  56. if (value->IsInt32()) {
  57. return INT2FIX(value->Int32Value());
  58. }
  59. if (value->IsBoolean()) {
  60. return value->BooleanValue() ? Qtrue : Qfalse;
  61. }
  62. if (value->IsNumber()) {
  63. return rb_float_new(value->NumberValue());
  64. }
  65. if (value->IsString()) {
  66. return rr_reflect_v8_string(value);
  67. }
  68. if (value->IsFunction()) {
  69. return rr_reflect_v8_function(value);
  70. }
  71. if (value->IsArray()) {
  72. return rr_reflect_v8_array(value);
  73. }
  74. if (value->IsDate()) {
  75. return rr_reflect_v8_date(value);
  76. }
  77. if (value->IsObject()) {
  78. return rr_reflect_v8_object(value);
  79. }
  80. return rr_wrap_v8_value(value);
  81. }
  82. VALUE rr_v82rb(Handle<Message> value) {
  83. return rr_reflect_v8_message(value);
  84. }
  85. VALUE rr_v82rb(Handle<StackTrace> value) {
  86. return rr_reflect_v8_stacktrace(value);
  87. }
  88. VALUE rr_v82rb(Handle<StackFrame> value) {
  89. return rr_reflect_v8_stackframe(value);
  90. }
  91. VALUE rr_v82rb(Handle<Boolean> value) {
  92. return rr_v82rb((Handle<Value>)value);
  93. }
  94. VALUE rr_v82rb(Handle<Number> value) {
  95. return rr_v82rb((Handle<Value>)value);
  96. }
  97. VALUE rr_v82rb(Handle<String> value) {
  98. return rr_v82rb((Handle<Value>)value);
  99. }
  100. VALUE rr_v82rb(Handle<Object> value) {
  101. return rr_v82rb((Handle<Value>)value);
  102. }
  103. VALUE rr_v82rb(Handle<Array> value) {
  104. return rr_v82rb((Handle<Value>)value);
  105. }
  106. VALUE rr_v82rb(v8::Handle<v8::Function> value) {
  107. return rr_v82rb((Handle<Value>)value);
  108. }
  109. VALUE rr_v82rb(Handle<Integer> value) {
  110. return rr_v82rb((Handle<Value>)value);
  111. }
  112. VALUE rr_v82rb(Handle<Uint32> value) {
  113. return rr_v82rb((Handle<Value>)value);
  114. }
  115. VALUE rr_v82rb(Handle<Int32> value) {
  116. return rr_v82rb((Handle<Value>)value);
  117. }
  118. VALUE rr_v82rb(bool value) {
  119. return value ? Qtrue : Qfalse;
  120. }
  121. VALUE rr_v82rb(double value) {
  122. return rb_float_new(value);
  123. }
  124. VALUE rr_v82rb(int64_t value) {
  125. return LONG2NUM(value);
  126. }
  127. VALUE rr_v82rb(uint32_t value) {
  128. return UINT2NUM(value);
  129. }
  130. VALUE rr_v82rb(int32_t value) {
  131. return INT2FIX(value);
  132. }
  133. Handle<Value> rr_rb2v8(VALUE value) {
  134. switch (TYPE(value)) {
  135. case T_FIXNUM:
  136. // TODO: use this conversion if value will fit in 32 bits.
  137. // return Integer::New(FIX2LONG(value));
  138. case T_FLOAT:
  139. return Number::New(NUM2DBL(value));
  140. case T_STRING:
  141. return String::New(RSTRING_PTR(value), RSTRING_LEN(value));
  142. case T_NIL:
  143. return Null();
  144. case T_TRUE:
  145. return True();
  146. case T_FALSE:
  147. return False();
  148. case T_DATA:
  149. return rr_v8_handle<Value>(value);
  150. case T_OBJECT:
  151. case T_CLASS:
  152. case T_ICLASS:
  153. case T_MODULE:
  154. case T_REGEXP:
  155. case T_MATCH:
  156. case T_ARRAY:
  157. case T_HASH:
  158. case T_STRUCT:
  159. case T_BIGNUM:
  160. case T_FILE:
  161. case T_SYMBOL:
  162. // case T_BLKTAG: (not in 1.9)
  163. case T_UNDEF:
  164. // case T_VARMAP: (not in 1.9)
  165. // case T_SCOPE: (not in 1.9)
  166. case T_NODE:
  167. default:
  168. rb_warn("unknown conversion to V8 for: %s", RSTRING_PTR(rb_inspect(value)));
  169. return String::New("Undefined Conversion");
  170. }
  171. return Undefined();
  172. }
  173. // VALUE rr_v82rb(v8::ScriptData *data) {
  174. // return rr_thunk(rr_wrap_script_data(data));
  175. // }