js_class.hpp
4.1 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
////////////////////////////////////////////////////////////////////////////
//
// 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 <map>
#include <string>
#include <vector>
#include "js_types.hpp"
namespace realm {
namespace js {
template<typename T>
struct Arguments {
const typename T::Context ctx;
const size_t count;
const typename T::Value* const value;
typename T::Value operator[](size_t index) const noexcept {
if (index >= count) {
return Value<T>::from_undefined(ctx);
}
return value[index];
}
void validate_maximum(size_t max) const {
if (max < count) {
throw std::invalid_argument(util::format("Invalid arguments: at most %1 expected, but %2 supplied.", max, count));
}
}
void validate_count(size_t expected) const {
if (count != expected) {
throw std::invalid_argument(util::format("Invalid arguments: %1 expected, but %2 supplied.", expected, count));
}
}
void validate_between(size_t min, size_t max) const {
if (count < min || count > max) {
throw std::invalid_argument(util::format("Invalid arguments: expected between %1 and %2, but %3 supplied.", min, max, count));
}
}
};
template<typename T>
using ConstructorType = void(typename T::Context, typename T::Object, Arguments<T> &);
template<typename T>
using ArgumentsMethodType = void(typename T::Context, typename T::Object, Arguments<T> &, ReturnValue<T> &);
template<typename T>
struct PropertyType {
using GetterType = void(typename T::Context, typename T::Object, ReturnValue<T> &);
using SetterType = void(typename T::Context, typename T::Object, typename T::Value);
typename T::PropertyGetterCallback getter;
typename T::PropertySetterCallback setter;
};
template<typename T>
struct IndexPropertyType {
using GetterType = void(typename T::Context, typename T::Object, uint32_t, ReturnValue<T> &);
using SetterType = bool(typename T::Context, typename T::Object, uint32_t, typename T::Value);
typename T::IndexPropertyGetterCallback getter;
typename T::IndexPropertySetterCallback setter;
};
template<typename T>
struct StringPropertyType {
using GetterType = void(typename T::Context, typename T::Object, const String<T> &, ReturnValue<T> &);
using SetterType = bool(typename T::Context, typename T::Object, const String<T> &, typename T::Value);
using EnumeratorType = std::vector<String<T>>(typename T::Context, typename T::Object);
typename T::StringPropertyGetterCallback getter;
typename T::StringPropertySetterCallback setter;
typename T::StringPropertyEnumeratorCallback enumerator;
};
template<typename T>
using MethodMap = std::map<std::string, typename T::FunctionCallback>;
template<typename T>
using PropertyMap = std::map<std::string, PropertyType<T>>;
template<typename T, typename U, typename V = void>
struct ClassDefinition {
using Internal = U;
using Parent = V;
// Every subclass *must* at least have a name.
// std::string const name;
// ClassDefinition specializations should inherit from this class and override what's needed below.
ConstructorType<T>* const constructor = nullptr;
MethodMap<T> const static_methods = {};
PropertyMap<T> const static_properties = {};
MethodMap<T> const methods = {};
PropertyMap<T> const properties = {};
IndexPropertyType<T> const index_accessor = {};
StringPropertyType<T> const string_accessor = {};
};
template<typename T, typename ClassType>
class ObjectWrap;
} // js
} // realm