~marcustomlinson/v8-cpp/add_tests

« back to all changes in this revision

Viewing changes to src/internal/class.h

  • Committer: Marcus Tomlinson
  • Date: 2015-06-22 16:45:48 UTC
  • mfrom: (3.1.16 v8runner)
  • Revision ID: marcus.tomlinson@canonical.com-20150622164548-zqix7e1effj2078u
Merged v8runner

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
        proto_template()->Inherit(base_class->class_template());
97
97
    }
98
98
 
99
 
    v8::Handle<v8::Object> export_object_reference(T* object)
100
 
    {
101
 
        return export_object_(object, false);
102
 
    }
103
 
 
104
99
    v8::Handle<v8::Object> export_object(T* object)
105
100
    {
106
 
        return export_object_(object, true);
 
101
        v8::EscapableHandleScope scope(isolate_);
 
102
 
 
103
        v8::Local<v8::Object> v8_object = class_template()->GetFunction()->NewInstance();
 
104
        v8_object->SetAlignedPointerInInternalField(0, object);
 
105
        v8_object->SetAlignedPointerInInternalField(1, this);
 
106
 
 
107
        MoveablePersistent<v8::Object> v8_object_p(isolate_, v8_object);
 
108
        v8_object_p.SetWeak(object, [](v8::WeakCallbackData<v8::Object, T> const& data)
 
109
                            {
 
110
                                v8::Isolate* isolate = data.GetIsolate();
 
111
                                T* object = data.GetParameter();
 
112
                                instance(isolate).destroy_object(object);
 
113
                            });
 
114
 
 
115
        ClassInfo::add_object(object, std::move(v8_object_p));
 
116
 
 
117
        return scope.Escape(v8_object);
107
118
    }
108
119
 
109
120
    v8::Handle<v8::Object> export_object(v8::FunctionCallbackInfo<v8::Value> const& args)
198
209
        return my_type;
199
210
    }
200
211
 
201
 
    v8::Handle<v8::Object> export_object_(T* object, bool destroy_after)
202
 
    {
203
 
        v8::EscapableHandleScope scope(isolate_);
204
 
 
205
 
        v8::Local<v8::Object> v8_object = class_template()->GetFunction()->NewInstance();
206
 
        v8_object->SetAlignedPointerInInternalField(0, object);
207
 
        v8_object->SetAlignedPointerInInternalField(1, this);
208
 
 
209
 
        MoveablePersistent<v8::Object> v8_object_p(isolate_, v8_object);
210
 
        if (destroy_after)
211
 
        {
212
 
            v8_object_p.SetWeak(object, [](v8::WeakCallbackData<v8::Object, T> const& data)
213
 
                                {
214
 
                                    v8::Isolate* isolate = data.GetIsolate();
215
 
                                    T* object = data.GetParameter();
216
 
                                    instance(isolate).destroy_object(object);
217
 
                                });
218
 
        }
219
 
        else
220
 
        {
221
 
            v8_object_p.SetWeak(object, [](v8::WeakCallbackData<v8::Object, T> const& data)
222
 
                                {
223
 
                                    v8::Isolate* isolate = data.GetIsolate();
224
 
                                    T* object = data.GetParameter();
225
 
                                    instance(isolate).remove_object<T>(isolate, object, nullptr);
226
 
                                });
227
 
        }
228
 
 
229
 
        ClassInfo::add_object(object, std::move(v8_object_p));
230
 
 
231
 
        return scope.Escape(v8_object);
232
 
    }
233
 
 
234
212
    v8::Isolate* isolate_;
235
213
    std::function<T*(v8::FunctionCallbackInfo<v8::Value> const& args)> constructor_;
236
214