|
| 1 | +// -*- mode: c++ -*- |
| 2 | +#ifndef RR_FUNCTION_CALLBACK_H |
| 3 | +#define RR_FUNCTION_CALLBACK_H |
| 4 | + |
| 5 | +namespace rr { |
| 6 | + |
| 7 | + typedef Wrapper<v8::FunctionCallbackInfo<v8::Value>> FunctionCallbackInfoWrapper; |
| 8 | + |
| 9 | + class FunctionCallbackInfo : public FunctionCallbackInfoWrapper { |
| 10 | + public: |
| 11 | + |
| 12 | + inline FunctionCallbackInfo(v8::FunctionCallbackInfo<v8::Value> info, v8::Local<v8::Value> data_) : |
| 13 | + FunctionCallbackInfoWrapper(info), data(data_) {} |
| 14 | + |
| 15 | + inline FunctionCallbackInfo(VALUE self) : FunctionCallbackInfoWrapper(self) {} |
| 16 | + |
| 17 | + inline v8::Local<v8::Value> operator [](int i) { |
| 18 | + return this->container->content[i]; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Package up the callback data for this function so that it can |
| 23 | + * invoke a Ruby callable. |
| 24 | + * |
| 25 | + * Each `v8::Function` can have one `v8::Value` associated with it |
| 26 | + * that is passed to its `v8::FunctionCallback`. To support this |
| 27 | + * same API from ruby, we take the `Value` passed into the |
| 28 | + * Function constructor *and* the callback and store them *both* |
| 29 | + * in a single `v8::Object` which we use for the C++ level |
| 30 | + * callback data. |
| 31 | + */ |
| 32 | + static v8::Local<v8::Value> wrapData(v8::Isolate* isolate, VALUE r_callback, VALUE r_data) { |
| 33 | + v8::Local<v8::Object> holder = v8::Object::New(isolate); |
| 34 | + v8::Local<v8::String> callback_key = v8::String::NewFromUtf8(isolate, "rr::callback"); |
| 35 | + v8::Local<v8::String> data_key = v8::String::NewFromUtf8(isolate, "rr::data"); |
| 36 | + holder->SetHiddenValue(callback_key, External::wrap(isolate, r_callback)); |
| 37 | + holder->SetHiddenValue(data_key, Value(r_data)); |
| 38 | + return holder; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Call the Ruby code associated with this callback. |
| 43 | + * |
| 44 | + * Unpack the Ruby code, and the callback data from the C++ |
| 45 | + * callback data, and then invoke that code. |
| 46 | + * |
| 47 | + * Note: This function implements the `v8::FunctionCallback` API. |
| 48 | + */ |
| 49 | + static void invoke(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 50 | + v8::Isolate* isolate = info.GetIsolate(); |
| 51 | + v8::Local<v8::Object> holder = v8::Local<v8::Object>::Cast<v8::Value>(info.Data()); |
| 52 | + v8::Local<v8::String> data_key = v8::String::NewFromUtf8(isolate, "rr::data"); |
| 53 | + v8::Local<v8::String> callback_key = v8::String::NewFromUtf8(isolate, "rr::callback"); |
| 54 | + v8::Local<v8::Value> data(holder->GetHiddenValue(data_key)); |
| 55 | + |
| 56 | + VALUE code(External::unwrap(holder->GetHiddenValue(callback_key))); |
| 57 | + Unlocker unlock(info.GetIsolate()); |
| 58 | + rb_funcall(code, rb_intern("call"), 1, (VALUE)FunctionCallbackInfo(info, data)); |
| 59 | + } |
| 60 | + |
| 61 | + static VALUE Length(VALUE self) { |
| 62 | + FunctionCallbackInfo info(self); |
| 63 | + Locker lock(info->GetIsolate()); |
| 64 | + return INT2FIX(info->Length()); |
| 65 | + } |
| 66 | + |
| 67 | + static VALUE at(VALUE self, VALUE i) { |
| 68 | + FunctionCallbackInfo info(self); |
| 69 | + Locker lock(info->GetIsolate()); |
| 70 | + return Value::handleToRubyObject(info->GetIsolate(), info[NUM2INT(i)]); |
| 71 | + } |
| 72 | + |
| 73 | + static VALUE Callee(VALUE self) { |
| 74 | + FunctionCallbackInfo info(self); |
| 75 | + Locker lock(info->GetIsolate()); |
| 76 | + return Function(info->GetIsolate(), info->Callee()); |
| 77 | + } |
| 78 | + |
| 79 | + static VALUE This(VALUE self) { |
| 80 | + FunctionCallbackInfo info(self); |
| 81 | + Locker lock(info->GetIsolate()); |
| 82 | + return Object(info->GetIsolate(), info->This()); |
| 83 | + } |
| 84 | + |
| 85 | + static VALUE IsConstructCall(VALUE self) { |
| 86 | + FunctionCallbackInfo info(self); |
| 87 | + Locker lock(info->GetIsolate()); |
| 88 | + return Bool(info->IsConstructCall()); |
| 89 | + } |
| 90 | + |
| 91 | + static VALUE Data(VALUE self) { |
| 92 | + FunctionCallbackInfo info(self); |
| 93 | + Locker lock(info->GetIsolate()); |
| 94 | + return Value::handleToRubyObject(info->GetIsolate(), info.data); |
| 95 | + } |
| 96 | + |
| 97 | + static VALUE GetIsolate(VALUE self) { |
| 98 | + FunctionCallbackInfo info(self); |
| 99 | + return Isolate(info->GetIsolate()); |
| 100 | + } |
| 101 | + |
| 102 | + static VALUE GetReturnValue(VALUE self) { |
| 103 | + FunctionCallbackInfo info(self); |
| 104 | + Locker lock(info->GetIsolate()); |
| 105 | + return ReturnValue(info->GetReturnValue()); |
| 106 | + } |
| 107 | + |
| 108 | + static inline void Init() { |
| 109 | + ClassBuilder("FunctionCallbackInfo"). |
| 110 | + defineMethod("Length", &Length). |
| 111 | + defineMethod("[]", &at). |
| 112 | + defineMethod("Callee", &Callee). |
| 113 | + defineMethod("This", &This). |
| 114 | + defineMethod("IsConstructCall", &IsConstructCall). |
| 115 | + defineMethod("Data", &Data). |
| 116 | + defineMethod("GetIsolate", &GetIsolate). |
| 117 | + defineMethod("GetReturnValue", &GetReturnValue). |
| 118 | + store(&Class); |
| 119 | + } |
| 120 | + |
| 121 | + v8::Local<v8::Value> data; |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +#endif /* RR_FUNCTION_CALLBACK_H */ |
0 commit comments