jsc_object.hpp
5.2 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
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include "jsc_types.hpp"
namespace realm {
namespace js {
template<>
inline JSValueRef jsc::Object::get_property(JSContextRef ctx, const JSObjectRef &object, const jsc::String &key) {
JSValueRef exception = nullptr;
JSValueRef value = JSObjectGetProperty(ctx, object, key, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return value;
}
template<>
inline JSValueRef jsc::Object::get_property(JSContextRef ctx, const JSObjectRef &object, StringData key) {
return get_property(ctx, object, jsc::String(key));
}
template<>
inline JSValueRef jsc::Object::get_property(JSContextRef ctx, const JSObjectRef &object, uint32_t index) {
JSValueRef exception = nullptr;
JSValueRef value = JSObjectGetPropertyAtIndex(ctx, object, index, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return value;
}
template<>
inline void jsc::Object::set_property(JSContextRef ctx, const JSObjectRef &object, const jsc::String &key, const JSValueRef &value, PropertyAttributes attributes) {
JSValueRef exception = nullptr;
JSObjectSetProperty(ctx, object, key, value, attributes << 1, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
}
template<>
inline void jsc::Object::set_property(JSContextRef ctx, const JSObjectRef &object, uint32_t index, const JSValueRef &value) {
JSValueRef exception = nullptr;
JSObjectSetPropertyAtIndex(ctx, object, index, value, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
}
template<>
inline std::vector<jsc::String> jsc::Object::get_property_names(JSContextRef ctx, const JSObjectRef &object) {
JSPropertyNameArrayRef property_names = JSObjectCopyPropertyNames(ctx, object);
size_t property_count = JSPropertyNameArrayGetCount(property_names);
std::vector<jsc::String> names;
names.reserve(property_count);
for (size_t i = 0; i < property_count; i++) {
names.push_back(JSPropertyNameArrayGetNameAtIndex(property_names, i));
}
JSPropertyNameArrayRelease(property_names);
return names;
}
template<>
inline JSValueRef jsc::Object::get_prototype(JSContextRef ctx, const JSObjectRef &object) {
return JSObjectGetPrototype(ctx, object);
}
template<>
inline void jsc::Object::set_prototype(JSContextRef ctx, const JSObjectRef &object, const JSValueRef &prototype) {
JSObjectSetPrototype(ctx, object, prototype);
}
template<>
inline JSObjectRef jsc::Object::create_empty(JSContextRef ctx) {
return JSObjectMake(ctx, nullptr, nullptr);
}
template<>
inline JSObjectRef jsc::Object::create_array(JSContextRef ctx, uint32_t length, const JSValueRef values[]) {
JSValueRef exception = nullptr;
JSObjectRef array = JSObjectMakeArray(ctx, length, values, &exception);
if (exception) {
throw jsc::Exception(ctx, exception);
}
return array;
}
template<>
inline JSObjectRef jsc::Object::create_date(JSContextRef ctx, double time) {
JSValueRef number = jsc::Value::from_number(ctx, time);
return JSObjectMakeDate(ctx, 1, &number, nullptr);
}
template<>
template<typename ClassType>
inline JSObjectRef jsc::Object::create_instance(JSContextRef ctx, typename ClassType::Internal* internal) {
return jsc::ObjectWrap<ClassType>::create_instance(ctx, internal);
}
template<>
template<typename ClassType>
inline bool jsc::Object::is_instance(JSContextRef ctx, const JSObjectRef &object) {
return jsc::ObjectWrap<ClassType>::has_instance(ctx, object);
}
template<>
template<typename ClassType>
inline typename ClassType::Internal* jsc::Object::get_internal(const JSObjectRef &object) {
return *static_cast<jsc::ObjectWrap<ClassType> *>(JSObjectGetPrivate(object));
}
template<>
template<typename ClassType>
inline void jsc::Object::set_internal(const JSObjectRef &object, typename ClassType::Internal* ptr) {
auto wrap = static_cast<jsc::ObjectWrap<ClassType> *>(JSObjectGetPrivate(object));
*wrap = ptr;
}
template<>
inline void jsc::Object::set_global(JSContextRef ctx, const jsc::String &key, const JSValueRef &value) {
JSObjectRef global_object = JSContextGetGlobalObject(ctx);
jsc::Object::set_property(ctx, global_object, key, value, js::ReadOnly | js::DontEnum | js::DontDelete);
}
template<>
inline JSValueRef jsc::Object::get_global(JSContextRef ctx, const jsc::String &key) {
JSObjectRef global_object = JSContextGetGlobalObject(ctx);
return jsc::Object::get_property(ctx, global_object, key);
}
} // js
} // realm