-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJSValue.cpp
More file actions
187 lines (162 loc) · 3.79 KB
/
JSValue.cpp
File metadata and controls
187 lines (162 loc) · 3.79 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "stdafx.h"
#include "JSCoreMarshal.h"
#include "JSContext.h"
#include "JSValue.h"
#include "JSObject.h"
JSValue::JSValue(JSContext ^ context, JSValueRef value)
: _context(context), _value(value)
{
// TODO: is this necessary (probably...)?
JSValueProtect(_context->context(), _value);
}
JSValue::~JSValue()
{
// TODO: would probably be nicer to implement IDisposable instead
// erm......
if (_context != nullptr && _context->context() != NULL)
JSValueUnprotect(_context->context(), _value);
}
String ^ JSValue::ToString()
{
JSStringRef jsStr = JSValueToStringCopy(_context->context(), _value, NULL);
String ^ str = JSCoreMarshal::JSStringToString(jsStr);
JSStringRelease(jsStr);
return str;
}
bool JSValue::IsBoolean::get()
{
return JSValueIsBoolean(_context->context(), _value);
}
bool JSValue::IsNull::get()
{
return JSValueIsNull(_context->context(), _value);
}
bool JSValue::IsNumber::get()
{
return JSValueIsNumber(_context->context(), _value);
}
bool JSValue::IsObject::get()
{
return JSValueIsObject(_context->context(), _value);
}
bool JSValue::IsString::get()
{
return JSValueIsString(_context->context(), _value);
}
bool JSValue::IsUndefined::get()
{
return JSValueIsUndefined(_context->context(), _value);
}
String ^ JSValue::ToJSONString()
{
return "";
}
bool JSValue::ToBoolean()
{
return JSValueToBoolean(_context->context(), _value);
}
double JSValue::ToNumber()
{
return JSValueToNumber(_context->context(), _value, NULL);
}
JSObject ^ JSValue::ToObject()
{
return gcnew JSObject(_context, (JSObjectRef)_value);
}
void JSValue::Protect()
{
}
void JSValue::Unprotect()
{
}
#ifdef DOTNET4
bool JSValue::TryConvert(System::Dynamic::ConvertBinder^ binder, [OutAttribute] Object ^% result)
{
if(binder->Type == double::typeid)
{
if(IsNumber)
{
result = ToNumber();
return true;
}
return false;
}
if(binder->Type == bool::typeid)
{
if(IsBoolean)
{
result = ToBoolean();
return true;
}
return false;
}
if(binder->Type == String::typeid)
{
if(IsString)
{
result = ToString();
return true;
}
return false;
}
return false;
}
bool JSValue::TryGetMember(System::Dynamic::GetMemberBinder ^ binder, Object ^% result)
{
if(!IsObject) return false;
JSObject ^ obj = ToObject();
if(!obj->HasProperty(binder->Name)) return false;
JSValue ^ value = obj->GetProperty(binder->Name);
if(value->IsNumber)
{
result = value->ToNumber();
return true;
}
if(value->IsBoolean)
{
result = value->ToBoolean();
return true;
}
if(value->IsString)
{
result = value->ToString();
return true;
}
if(value->IsObject)
{
result = value->ToObject();
return true;
}
return false;
}
bool JSValue::TrySetMember(System::Dynamic::SetMemberBinder ^ binder, Object ^ value)
{
if(!IsObject) return false;
JSObject ^ obj = ToObject();
Type ^ type = value->GetType();
if(type == double::typeid)
{
obj->SetProperty(binder->Name, (double)value);
return true;
}
if(type == bool::typeid)
{
obj->SetProperty(binder->Name, (bool)value);
return true;
}
if(type == String::typeid)
{
obj->SetProperty(binder->Name, (String ^)value);
return true;
}
obj->SetProperty(binder->Name, value);
return true;
}
bool JSValue::TryInvokeMember(System::Dynamic::InvokeMemberBinder ^ binder, array<Object ^> ^ args, Object ^% result)
{
if(!IsObject) return false;
JSObject ^ obj = ToObject();
result = obj->CallFunction(binder->Name, args);
return true;
}
#endif