#include "Object.hpp"
#ifdef SCRIPT_ENGINE_V8
#include "Utils.hpp"
#include "Class.hpp"
#include "ScriptEngine.hpp"
namespace se {
std::unordered_map __nativePtrToObjectMap;
namespace {
v8::Isolate* __isolate = nullptr;
}
Object::Object()
: _cls(nullptr)
, _isRooted(false)
, _isKeepRootedUntilDie(false)
, _hasPrivateData(false)
, _finalizeCb(nullptr)
{
}
Object::~Object()
{
if (_isRooted)
{
_obj.unref();
}
}
/*static*/
void Object::nativeObjectFinalizeHook(void* nativeObj)
{
if (nativeObj == nullptr)
return;
auto iter = __nativePtrToObjectMap.find(nativeObj);
if (iter != __nativePtrToObjectMap.end())
{
Object* obj = iter->second;
if (obj->_finalizeCb != nullptr)
{
obj->_finalizeCb(nativeObj);
}
else
{
assert(obj->_getClass() != nullptr);
if (obj->_getClass()->_finalizeFunc != nullptr)
obj->_getClass()->_finalizeFunc(nativeObj);
}
obj->release();
__nativePtrToObjectMap.erase(iter);
}
else
{
assert(false);
}
}
/* static */
void Object::setIsolate(v8::Isolate* isolate)
{
__isolate = isolate;
}
Object* Object::createPlainObject(bool rooted)
{
v8::Local jsobj = v8::Object::New(__isolate);
Object* obj = _createJSObject(nullptr, jsobj, rooted);
return obj;
}
// Object* Object::createObject(const char* clsName, bool rooted)
// {
// Class* cls = nullptr;
// auto jsobj = Class::_createJSObject(clsName, &cls);
// Object* obj = _createJSObject(cls, jsobj, rooted);
//
// return obj;
// }
Object* Object::getObjectWithPtr(void* ptr)
{
Object* obj = nullptr;
auto iter = __nativePtrToObjectMap.find(ptr);
if (iter != __nativePtrToObjectMap.end())
{
obj = iter->second;
obj->addRef();
}
return obj;
}
// Object* Object::getOrCreateObjectWithPtr(void* ptr, const char* clsName, bool rooted)
// {
// Object* obj = getObjectWithPtr(ptr);
// if (obj == nullptr)
// {
// obj = createObject(clsName, rooted);
// obj->setPrivateData(ptr);
// }
// return obj;
// }
Object* Object::_createJSObject(Class* cls, v8::Local obj, bool rooted)
{
Object* ret = new Object();
if (!ret->init(cls, obj, rooted))
{
delete ret;
ret = nullptr;
}
return ret;
}
Object* Object::createObjectWithClass(Class* cls, bool rooted)
{
v8::Local jsobj = Class::_createJSObjectWithClass(cls);
Object* obj = Object::_createJSObject(cls, jsobj, rooted);
return obj;
}
Object* Object::createArrayObject(size_t length, bool rooted)
{
v8::Local jsobj = v8::Array::New(__isolate, (int)length);
Object* obj = Object::_createJSObject(nullptr, jsobj, rooted);
return obj;
}
Object* Object::createArrayBufferObject(void* data, size_t byteLength, bool rooted)
{
v8::Local jsobj = v8::ArrayBuffer::New(__isolate, byteLength);
memcpy(jsobj->GetContents().Data(), data, byteLength);
Object* obj = Object::_createJSObject(nullptr, jsobj, rooted);
return obj;
}
Object* Object::createUint8TypedArray(uint8_t* data, size_t byteLength, bool rooted)
{
v8::Local jsobj = v8::ArrayBuffer::New(__isolate, byteLength);
memcpy(jsobj->GetContents().Data(), data, byteLength);
v8::Local arr = v8::Uint8Array::New(jsobj, 0, byteLength);
Object* obj = Object::_createJSObject(nullptr, arr, rooted);
return obj;
}
Object* Object::createJSONObject(const std::string& jsonStr, bool rooted)
{
v8::Local context = __isolate->GetCurrentContext();
Value strVal(jsonStr);
v8::Local jsStr;
internal::seToJsValue(__isolate, strVal, &jsStr);
v8::Local v8Str = v8::Local::Cast(jsStr);
v8::MaybeLocal ret = v8::JSON::Parse(context, v8Str);
if (ret.IsEmpty())
return nullptr;
v8::Local jsobj = v8::Local::Cast(ret.ToLocalChecked());
return Object::_createJSObject(nullptr, jsobj, rooted);
}
bool Object::init(Class* cls, v8::Local obj, bool rooted)
{
_cls = cls;
_isRooted = rooted;
_obj.init(obj);
_obj.setFinalizeCallback(nativeObjectFinalizeHook);
if (_isRooted)
{
_obj.ref();
}
return true;
}
bool Object::getProperty(const char *name, Value *data)
{
assert(data != nullptr);
v8::HandleScope handle_scope(__isolate);
if (_obj.persistent().IsEmpty())
{
return false;
}
v8::MaybeLocal nameValue = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
if (nameValue.IsEmpty())
return false;
v8::Local nameValToLocal = nameValue.ToLocalChecked();
v8::Local context = __isolate->GetCurrentContext();
v8::Maybe maybeExist = _obj.handle(__isolate)->Has(context, nameValToLocal);
if (maybeExist.IsNothing())
return false;
if (!maybeExist.FromJust())
return false;
v8::MaybeLocal result = _obj.handle(__isolate)->Get(context, nameValToLocal);
if (result.IsEmpty())
return false;
internal::jsToSeValue(__isolate, result.ToLocalChecked(), data);
return true;
}
void Object::setProperty(const char *name, const Value& data)
{
v8::MaybeLocal nameValue = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
if (nameValue.IsEmpty())
return;
v8::Local value;
internal::seToJsValue(__isolate, data, &value);
v8::Maybe ret = _obj.handle(__isolate)->Set(__isolate->GetCurrentContext(), nameValue.ToLocalChecked(), value);
if (ret.IsNothing())
{
LOGD("ERROR: %s, Set return nothing ...\n", __FUNCTION__);
}
}
bool Object::defineProperty(const char *name, v8::AccessorNameGetterCallback getter, v8::AccessorNameSetterCallback setter)
{
v8::MaybeLocal nameValue = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
if (nameValue.IsEmpty())
return false;
v8::Local nameValChecked = nameValue.ToLocalChecked();
v8::Local jsName = v8::Local::Cast(nameValChecked);
v8::Maybe ret = _obj.handle(__isolate)->SetAccessor(__isolate->GetCurrentContext(), jsName, getter, setter);
return ret.IsJust() && ret.FromJust();
}
bool Object::isFunction() const
{
return const_cast