2021-09-04 08:06:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-09-07 14:24:16 +00:00
|
|
|
#ifndef ECMASCRIPT_JSNATIVEPOINTER_H
|
|
|
|
#define ECMASCRIPT_JSNATIVEPOINTER_H
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2021-12-16 03:41:16 +00:00
|
|
|
#include "ecmascript/ecma_macros.h"
|
|
|
|
#include "ecmascript/mem/tagged_object.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
|
|
|
using DeleteEntryPoint = void (*)(void *, void *);
|
|
|
|
|
2021-12-16 03:41:16 +00:00
|
|
|
// Used for the requirement of ACE that wants to associated a registered C++ resource with a JSObject.
|
|
|
|
class JSNativePointer : public TaggedObject {
|
2021-09-04 08:06:49 +00:00
|
|
|
public:
|
2022-06-16 15:10:50 +00:00
|
|
|
static JSNativePointer *Cast(TaggedObject *object)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
ASSERT(JSTaggedValue(object).IsJSNativePointer());
|
|
|
|
return reinterpret_cast<JSNativePointer *>(object);
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:24:16 +00:00
|
|
|
inline void ResetExternalPointer(void *externalPointer)
|
|
|
|
{
|
2021-12-16 03:41:16 +00:00
|
|
|
DeleteExternalPointer();
|
2021-09-07 14:24:16 +00:00
|
|
|
SetExternalPointer(externalPointer);
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
inline void Destroy()
|
|
|
|
{
|
2021-12-16 03:41:16 +00:00
|
|
|
DeleteExternalPointer();
|
|
|
|
SetExternalPointer(nullptr);
|
2021-09-07 14:24:16 +00:00
|
|
|
SetDeleter(nullptr);
|
|
|
|
SetData(nullptr);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 03:41:16 +00:00
|
|
|
static constexpr size_t POINTER_OFFSET = TaggedObjectSize();
|
2022-01-25 10:05:12 +00:00
|
|
|
ACCESSORS_NATIVE_FIELD(ExternalPointer, void, POINTER_OFFSET, DELETER_OFFSET);
|
|
|
|
ACCESSORS_PRIMITIVE_FIELD(Deleter, DeleteEntryPoint, DELETER_OFFSET, DATA_OFFSET)
|
2022-09-09 08:58:03 +00:00
|
|
|
ACCESSORS_NATIVE_FIELD(Data, void, DATA_OFFSET, DATA_SIZE_OFFSET);
|
|
|
|
ACCESSORS_PRIMITIVE_FIELD(BindingSize, uint64_t, DATA_SIZE_OFFSET, LAST_OFFSET)
|
|
|
|
|
2022-01-25 10:05:12 +00:00
|
|
|
DEFINE_ALIGN_SIZE(LAST_OFFSET);
|
2021-12-16 03:41:16 +00:00
|
|
|
|
2022-09-09 08:58:03 +00:00
|
|
|
DECL_VISIT_NATIVE_FIELD(POINTER_OFFSET, DATA_SIZE_OFFSET)
|
2022-03-26 09:00:26 +00:00
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
private:
|
2021-12-16 03:41:16 +00:00
|
|
|
inline void DeleteExternalPointer()
|
|
|
|
{
|
|
|
|
void *externalPointer = GetExternalPointer();
|
2022-03-04 07:23:09 +00:00
|
|
|
DeleteEntryPoint deleter = GetDeleter();
|
|
|
|
if (deleter != nullptr) {
|
|
|
|
deleter(externalPointer, GetData());
|
2021-12-16 03:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
};
|
|
|
|
} // namespace panda::ecmascript
|
2021-09-07 14:24:16 +00:00
|
|
|
#endif // ECMASCRIPT_JSNATIVEPOINTER_H
|