-
Notifications
You must be signed in to change notification settings - Fork 2
/
V8Event.cpp
135 lines (105 loc) · 4.67 KB
/
V8Event.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Created by hyperandroid on 13/02/2016.
//
#include "V8Event.h"
#include "Event.h"
#include "Configuration.h"
const int HC_GARBAGE_COLLECTED_CLASS_ID = 16;
namespace V8EventInternal {
void preventDefault( const FunctionCallbackInfo<Value>& info ) {
}
void TypeGetter(const FunctionCallbackInfo<Value> &info) {
Event* ev = Config::ToImpl<Event>(info.Holder());
if ( ev!= nullptr ) {
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), ev->Type()));
} else {
info.GetReturnValue().Set(Null(info.GetIsolate()));
}
}
void TimeStampGetter(const FunctionCallbackInfo<Value> &info) {
Event* ev = Config::ToImpl<Event>(info.Holder());
if ( ev!=nullptr ) {
info.GetReturnValue().Set((double) ev->TimeStamp());
} else {
info.GetReturnValue().Set(Null(info.GetIsolate()));
}
}
void TargetGetter(const FunctionCallbackInfo<Value> &info) {
Event* ev = Config::ToImpl<Event>(info.Holder());
if ( ev!=nullptr ) {
if ( ev->target==nullptr ) {
info.GetReturnValue().Set(Null(info.GetIsolate()));
} else {
info.GetReturnValue().Set(ev->target->GetWrapper(info.GetIsolate()));
}
} else {
info.GetReturnValue().Set(Null(info.GetIsolate()));
}
}
void CurrentTargetGetter(const FunctionCallbackInfo<Value> &info) {
Event* ev = Config::ToImpl<Event>(info.Holder());
if ( ev!=nullptr ) {
if ( ev->currentTarget==nullptr ) {
info.GetReturnValue().Set(Null(info.GetIsolate()));
} else {
info.GetReturnValue().Set(ev->currentTarget->GetWrapper(info.GetIsolate()));
}
} else {
info.GetReturnValue().Set(Null(info.GetIsolate()));
}
}
}
const WrapperTypeInfo V8Event::wrapperTypeInfo = {
V8Event::InterfaceTemplate,
"Event",
nullptr,
2,
HC_GARBAGE_COLLECTED_CLASS_ID
};
const WrapperTypeInfo& Event::wrapperTypeInfo_ = V8Event::wrapperTypeInfo;
static Config::AccessorConfiguration props[] = {
{"type", V8EventInternal::TypeGetter, nullptr, v8::DontDelete, Config::kOnPrototype},
{"timeStamp", V8EventInternal::TimeStampGetter, nullptr, v8::DontDelete, Config::kOnPrototype},
{"target", V8EventInternal::TargetGetter, nullptr, v8::DontDelete, Config::kOnPrototype},
{"currentTarget", V8EventInternal::CurrentTargetGetter, nullptr, v8::DontDelete, Config::kOnPrototype},
};
static Config::MethodConfiguration methods[] = {
{"preventDefault", V8EventInternal::preventDefault, v8::DontDelete, Config::kOnPrototype, 0}
};
void V8Event::constructorCallback(const FunctionCallbackInfo<Value> &ci) {
if ( !ci.IsConstructCall() ) {
Config::Throw(ci.GetIsolate(), "Must be constructor");
return;
}
// a better approach would be to have PerIsolate data, so that we can query for current
// constructor mode.
if ( Config::Status::CurrentConstructorMode == Config::ConstructorMode::kWrapExistingObject ) {
ci.GetReturnValue().Set( ci.Holder() );
return;
}
if ( ci.kArgsLength<1 || !ci[0]->IsString()) {
Config::Throw(ci.GetIsolate(), "V8Event needs a type string argument.");
return;
}
String::Utf8Value utf(ci[0]);
Event* event = new Event(*utf);
v8::Local<v8::Object> wrapper = ci.Holder();
event->AssociateWithWrapper( ci.GetIsolate(), &V8Event::wrapperTypeInfo, wrapper);
ci.GetReturnValue().Set(wrapper);
}
Local<FunctionTemplate> V8Event::InterfaceTemplate(Isolate *isolate) {
return Config::InterfaceTemplate(isolate, wrapperTypeInfo, V8Event::InstallInterfaceTemplate);
}
void V8Event::InstallInterfaceTemplate( Isolate* isolate, Local<FunctionTemplate> interface_template ) {
Config::InitializeInterfaceTemplate(isolate, interface_template, wrapperTypeInfo );
// function template
interface_template->SetCallHandler(V8Event::constructorCallback);
interface_template->SetLength(1);
v8::Local<v8::Signature> signature = v8::Signature::New(isolate, interface_template);
Local<ObjectTemplate> prototype_t = interface_template->PrototypeTemplate();
Local<ObjectTemplate> instance_t = interface_template->InstanceTemplate();
Config::InstallAccessors(isolate, instance_t, prototype_t, interface_template, signature, props,
ARRAY_LENGTH(props));
Config::InstallMethods(isolate, instance_t, prototype_t, interface_template, signature, methods,
ARRAY_LENGTH(methods));
}