23 ExceptionOr<void> NotImplementedCallback() {
25 "This feature is not implemented.");
35 struct JsIndexerCallback {
36 static void GetIndex(uint32_t index,
37 const v8::PropertyCallbackInfo<v8::Value>& info) {
41 impl::ThrowError<
false>::General(
42 nullptr,
"",
"",
"INTERNAL: Invalid function data.");
45 auto* factory =
reinterpret_cast<BackingObjectFactoryBase*
>(ptr);
46 info.GetReturnValue().Set(factory->GetIndex(info.This(), index));
49 static void SetIndex(uint32_t index, v8::Local<v8::Value> given,
50 const v8::PropertyCallbackInfo<v8::Value>& info) {
54 impl::ThrowError<
false>::General(
55 nullptr,
"",
"",
"INTERNAL: Invalid function data.");
58 auto* factory =
reinterpret_cast<BackingObjectFactoryBase*
>(ptr);
59 factory->SetIndex(info.This(), index, given);
63 #elif defined(USING_JSC) 65 bool TryGetIndex(JSStringRef
name,
size_t* index) {
67 JSValueRef except =
nullptr;
69 JSValueToNumber(cx, JSValueMakeString(cx, name), &except);
70 if (except || isnan(number))
72 *index =
static_cast<size_t>(number);
76 JSValueRef GetProperty(JSContextRef cx, JSObjectRef target, JSStringRef name,
79 auto*
object =
reinterpret_cast<BackingObject*
>(JSObjectGetPrivate(target));
81 if (!TryGetIndex(name, &index))
83 return object->factory()->GetIndex(target, index);
86 bool SetProperty(JSContextRef cx, JSObjectRef target, JSStringRef name,
87 JSValueRef given, JSValueRef* except) {
89 auto*
object =
reinterpret_cast<BackingObject*
>(JSObjectGetPrivate(target));
91 if (!TryGetIndex(name, &index))
93 object->factory()->SetIndex(target, index, given);
102 const std::string& name, NativeCtor ctor,
104 : type_name_(name), base_(base) {
108 const v8::Local<v8::Signature> empty_signature;
109 v8::Local<v8::FunctionTemplate>
type = v8::FunctionTemplate::New(
110 isolate, ctor, v8::Local<v8::Value>(), empty_signature, 0);
116 type->InstanceTemplate()->SetInternalFieldCount(
119 type->Inherit(base->class_definition_);
122 v8::IndexedPropertyGetterCallback getter = &JsIndexerCallback::GetIndex;
123 v8::IndexedPropertySetterCallback setter = &JsIndexerCallback::SetIndex;
124 type->PrototypeTemplate()->SetIndexedPropertyHandler(
125 getter, setter,
nullptr,
nullptr,
nullptr,
126 WrapPointer(static_cast<BackingObjectFactoryBase*>(
this)));
128 v8::MaybeLocal<v8::Function> maybe_ctor =
129 type->GetFunction(isolate->GetCurrentContext());
130 constructor_ = maybe_ctor.ToLocalChecked();
131 class_definition_ =
type;
132 prototype_ = UnsafeJsCast<JsObject>(
GetMemberRaw(constructor_,
"prototype"));
136 #elif defined(USING_JSC) 137 memset(&definition_, 0,
sizeof(definition_));
138 definition_.className = type_name_.c_str();
139 definition_.version = 1;
140 definition_.getProperty = &GetProperty;
141 definition_.setProperty = &SetProperty;
143 definition_.parentClass = base->GetClass();
146 class_definition_ = JSClassCreate(&definition_);
147 constructor_ = JSObjectMakeConstructor(cx, class_definition_, ctor);
148 prototype_ = UnsafeJsCast<JsObject>(
GetMemberRaw(constructor_,
"prototype"));
151 SetMemberRaw(JSContextGetGlobalObject(cx), type_name_, constructor_);
158 if (name == type_name_)
161 return name ==
"BackingObject";
167 LocalVar<JsValue> result;
177 return indexer_->GetIndex(that, index);
181 Handle<JsValue> value) {
183 indexer_->SetIndex(that, index, value);
ReturnVal< JsFunction > CreateStaticFunction(const std::string &target, const std::string &name, Func &&callback)
ReturnVal< JsValue > GetIndex(Handle< JsObject > that, size_t index)
ReturnVal< JsValue > JsUndefined()
void NotImplemented(const std::string &name)
BackingObjectFactoryBase(const std::string &name, NativeCtor ctor, const BackingObjectFactoryBase *base)
static constexpr const size_t kInternalFieldCount
static JsEngine * Instance()
void SetMemberRaw(Handle< JsObject > object, const std::string &name, Handle< JsValue > value)
void SetIndex(Handle< JsObject > that, size_t index, Handle< JsValue > value)
bool DerivedFrom(const std::string &name) const
static JsError DOMException(ExceptionCode code)
JSContextRef GetContext()
virtual ~BackingObjectFactoryBase()
ReturnVal< JsValue > WrapPointer(void *ptr)
ReturnVal< JsString > JsStringFromUtf8(const std::string &str)
ReturnVal< JsValue > WrapInstance(BackingObject *object)
bool InvokeConstructor(Handle< JsFunction > ctor, int argc, LocalVar< JsValue > *argv, LocalVar< JsValue > *result_or_except)
v8::Isolate * GetIsolate()
ReturnVal< JsValue > GetMemberRaw(Handle< JsObject > object, const std::string &name, LocalVar< JsValue > *exception=nullptr)
void * MaybeUnwrapPointer(Handle< JsValue > value)
void SetGenericPropertyRaw(Handle< JsObject > object, const std::string &name, Handle< JsFunction > getter, Handle< JsFunction > setter)