This repository has been archived by the owner on May 6, 2019. It is now read-only.
/
JSCoreObjectWrapper.cpp
122 lines (104 loc) · 4.04 KB
/
JSCoreObjectWrapper.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
#include "stdafx.h"
#include "JSCoreObjectWrapper.h"
#include "JSCoreMarshal.h"
JSClassDefinition wrapperClass =
{
0, /* int version; */
kJSClassAttributeNone, /* JSClassAttributes attributes; */
"wrapper", /* const char* className; */
NULL, /* JSClassRef parentClass; */
NULL, /* const JSStaticValue* staticValues; */
NULL, /* const JSStaticFunction* staticFunctions; */
NULL, /* JSObjectInitializeCallback initialize; */
NULL, /* JSObjectFinalizeCallback finalize; */
wrapper_HasProperty, /* JSObjectHasPropertyCallback hasProperty; */
wrapper_GetProperty, /* JSObjectGetPropertyCallback getProperty; */
wrapper_SetProperty, /* JSObjectSetPropertyCallback setProperty; */
NULL, /* JSObjectDeletePropertyCallback deleteProperty; */
NULL, /* JSObjectGetPropertyNamesCallback getPropertyNames; */
wrapper_CallAsFunction /* JSObjectCallAsFunctionCallback callAsFunction; */
/* JSObjectCallAsConstructorCallback callAsConstructor; */
/* JSObjectHasInstanceCallback hasInstance; */
/* JSObjectConvertToTypeCallback convertToType; */
};
Object ^ getObjectFromJSObjectRef(JSObjectRef object)
{
void * ptr = JSObjectGetPrivate(object);
GCHandle handle = GCHandle::FromIntPtr(IntPtr(ptr));
return handle.Target;
}
bool wrapper_HasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName)
{
Object ^ obj = getObjectFromJSObjectRef(object);
String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
return obj->GetType()->GetProperty(propName) != nullptr;
}
JSValueRef wrapper_GetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
Object ^ obj = getObjectFromJSObjectRef(object);
String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
PropertyInfo ^ prop = obj->GetType()->GetProperty(propName);
if (prop != nullptr)
{
// for the moment, just return as string
if (prop->CanRead)
{
String ^ val = prop->GetValue(obj, nullptr)->ToString();
JSStringRef jsStr = JSCoreMarshal::StringToJSString(val);
return JSValueMakeString(ctx, jsStr);
}
else
{
// exception?
}
}
return NULL;
}
bool wrapper_SetProperty(JSContextRef ctx, JSObjectRef object,
JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
{
Object ^ obj = getObjectFromJSObjectRef(object);
String ^ propName = JSCoreMarshal::JSStringToString(propertyName);
PropertyInfo ^ prop = obj->GetType()->GetProperty(propName);
if (prop != nullptr)
{
if (prop->CanWrite)
{
Type ^ type = prop->PropertyType;
Object ^ val;
JSValueRef exception = NULL;
if (type == double::typeid)
{
val = JSValueToNumber(ctx, value, &exception);
}
else if (type == bool::typeid)
{
val = JSValueToBoolean(ctx, value);
}
else if (type == String::typeid)
{
JSStringRef temp = JSValueToStringCopy(ctx, value, &exception);
if (!exception)
{
val = JSCoreMarshal::JSStringToString(temp);
JSStringRelease(temp);
}
}
if (!exception)
{
prop->SetValue(obj, val, nullptr);
return true;
}
}
else
{
// exception?
}
}
return false;
}
JSValueRef wrapper_CallAsFunction (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
return NULL;
}